aot_emit_parametric.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_emit_parametric.h"
  6. static bool
  7. pop_value_from_wasm_stack(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  8. LLVMValueRef *p_value,
  9. bool is_32, uint8 *p_type)
  10. {
  11. AOTValue *aot_value;
  12. uint8 type;
  13. if (!func_ctx->block_stack.block_list_end) {
  14. aot_set_last_error("WASM block stack underflow.");
  15. return false;
  16. }
  17. if (!func_ctx->block_stack.block_list_end->value_stack.value_list_end) {
  18. aot_set_last_error("WASM data stack underflow.");
  19. return false;
  20. }
  21. aot_value = aot_value_stack_pop
  22. (&func_ctx->block_stack.block_list_end->value_stack);
  23. type = aot_value->type;
  24. if (aot_value->type == VALUE_TYPE_I1) {
  25. if (!(aot_value->value =
  26. LLVMBuildZExt(comp_ctx->builder, aot_value->value,
  27. I32_TYPE, "val_s_ext"))) {
  28. aot_set_last_error("llvm build sign ext failed.");
  29. return false;
  30. }
  31. type = aot_value->type = VALUE_TYPE_I32;
  32. }
  33. if (p_type != NULL) {
  34. *p_type = aot_value->type;
  35. }
  36. if (p_value != NULL) {
  37. *p_value = aot_value->value;
  38. }
  39. wasm_runtime_free(aot_value);
  40. /* is_32: i32, f32, ref.func, ref.extern, v128 */
  41. if (is_32
  42. && !(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  43. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  44. || type == VALUE_TYPE_V128)) {
  45. aot_set_last_error("invalid WASM stack data type.");
  46. return false;
  47. }
  48. /* !is_32: i64, f64 */
  49. if (!is_32
  50. && !(type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)) {
  51. aot_set_last_error("invalid WASM stack data type.");
  52. return false;
  53. }
  54. return true;
  55. }
  56. bool
  57. aot_compile_op_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  58. bool is_drop_32)
  59. {
  60. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, NULL, is_drop_32, NULL))
  61. return false;
  62. return true;
  63. }
  64. bool
  65. aot_compile_op_select(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  66. bool is_select_32)
  67. {
  68. LLVMValueRef val1, val2, cond, selected;
  69. uint8 val1_type, val2_type;
  70. POP_COND(cond);
  71. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, &val2, is_select_32, &val2_type)
  72. || !pop_value_from_wasm_stack(comp_ctx, func_ctx, &val1, is_select_32, &val1_type))
  73. return false;
  74. if (val1_type != val2_type) {
  75. aot_set_last_error("invalid stack values with different type");
  76. return false;
  77. }
  78. if (!(selected = LLVMBuildSelect(comp_ctx->builder,
  79. cond, val1, val2,
  80. "select"))) {
  81. aot_set_last_error("llvm build select failed.");
  82. return false;
  83. }
  84. PUSH(selected, val1_type);
  85. return true;
  86. fail:
  87. return false;
  88. }