aot_emit_parametric.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 =
  21. aot_value_stack_pop(&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. /* is_32: i32, f32, ref.func, ref.extern, v128 */
  40. if (is_32
  41. && !(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  42. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  43. || type == VALUE_TYPE_V128)) {
  44. aot_set_last_error("invalid WASM stack data type.");
  45. return false;
  46. }
  47. /* !is_32: i64, f64 */
  48. if (!is_32 && !(type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)) {
  49. aot_set_last_error("invalid WASM stack data type.");
  50. return false;
  51. }
  52. return true;
  53. }
  54. bool
  55. aot_compile_op_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  56. bool is_drop_32)
  57. {
  58. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, NULL, is_drop_32, NULL))
  59. return false;
  60. return true;
  61. }
  62. bool
  63. aot_compile_op_select(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  64. bool is_select_32)
  65. {
  66. LLVMValueRef val1, val2, cond, selected;
  67. uint8 val1_type, val2_type;
  68. POP_COND(cond);
  69. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, &val2, is_select_32,
  70. &val2_type)
  71. || !pop_value_from_wasm_stack(comp_ctx, func_ctx, &val1, is_select_32,
  72. &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 =
  79. LLVMBuildSelect(comp_ctx->builder, cond, val1, val2, "select"))) {
  80. aot_set_last_error("llvm build select failed.");
  81. return false;
  82. }
  83. PUSH(selected, val1_type);
  84. return true;
  85. fail:
  86. return false;
  87. }