aot_emit_parametric.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, bool is_32, uint8 *p_type)
  9. {
  10. AOTValue *aot_value;
  11. uint8 type;
  12. if (!func_ctx->block_stack.block_list_end) {
  13. aot_set_last_error("WASM block stack underflow.");
  14. return false;
  15. }
  16. if (!func_ctx->block_stack.block_list_end->value_stack.value_list_end) {
  17. aot_set_last_error("WASM data stack underflow.");
  18. return false;
  19. }
  20. aot_value = aot_value_stack_pop(
  21. comp_ctx, &func_ctx->block_stack.block_list_end->value_stack);
  22. type = aot_value->type;
  23. if (aot_value->type == VALUE_TYPE_I1) {
  24. if (!(aot_value->value =
  25. LLVMBuildZExt(comp_ctx->builder, aot_value->value, I32_TYPE,
  26. "val_s_ext"))) {
  27. aot_set_last_error("llvm build sign ext failed.");
  28. return false;
  29. }
  30. type = aot_value->type = VALUE_TYPE_I32;
  31. }
  32. if (p_type != NULL) {
  33. *p_type = aot_value->type;
  34. }
  35. if (p_value != NULL) {
  36. *p_value = aot_value->value;
  37. }
  38. wasm_runtime_free(aot_value);
  39. if (is_32) {
  40. /* is_32: i32, f32, ref.func, ref.extern, v128,
  41. or GC ref types */
  42. if (!(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  43. || type == VALUE_TYPE_V128
  44. || (comp_ctx->enable_ref_types
  45. && (type == VALUE_TYPE_FUNCREF
  46. || type == VALUE_TYPE_EXTERNREF))
  47. #if WASM_ENABLE_GC != 0
  48. || (comp_ctx->enable_gc && type == VALUE_TYPE_GC_REF)
  49. #endif
  50. )) {
  51. aot_set_last_error("invalid WASM stack data type.");
  52. return false;
  53. }
  54. }
  55. else {
  56. /* !is_32: i64, f64, or GC ref types */
  57. if (!(type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  58. #if WASM_ENABLE_GC != 0
  59. || (comp_ctx->enable_gc && type == VALUE_TYPE_GC_REF)
  60. /* may be i32 which denotes funcref/externref */
  61. || (!comp_ctx->enable_gc && type == VALUE_TYPE_I32)
  62. #endif
  63. )) {
  64. aot_set_last_error("invalid WASM stack data type.");
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. bool
  71. aot_compile_op_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  72. bool is_drop_32)
  73. {
  74. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, NULL, is_drop_32, NULL))
  75. return false;
  76. return true;
  77. }
  78. bool
  79. aot_compile_op_select(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  80. bool is_select_32)
  81. {
  82. LLVMValueRef val1, val2, cond, selected;
  83. uint8 val1_type, val2_type;
  84. POP_COND(cond);
  85. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, &val2, is_select_32,
  86. &val2_type)
  87. || !pop_value_from_wasm_stack(comp_ctx, func_ctx, &val1, is_select_32,
  88. &val1_type))
  89. return false;
  90. if (val1_type != val2_type) {
  91. aot_set_last_error("invalid stack values with different type");
  92. return false;
  93. }
  94. if (!(selected =
  95. LLVMBuildSelect(comp_ctx->builder, cond, val1, val2, "select"))) {
  96. aot_set_last_error("llvm build select failed.");
  97. return false;
  98. }
  99. PUSH(selected, val1_type);
  100. return true;
  101. fail:
  102. return false;
  103. }