aot_emit_parametric.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if ((is_32
  41. && (type != VALUE_TYPE_I32 && type != VALUE_TYPE_F32))
  42. || (!is_32
  43. && (type != VALUE_TYPE_I64 && type != VALUE_TYPE_F64))) {
  44. aot_set_last_error("invalid WASM stack data type.");
  45. return false;
  46. }
  47. return true;
  48. }
  49. bool
  50. aot_compile_op_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  51. bool is_drop_32)
  52. {
  53. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, NULL, is_drop_32, NULL))
  54. return false;
  55. return true;
  56. }
  57. bool
  58. aot_compile_op_select(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  59. bool is_select_32)
  60. {
  61. LLVMValueRef val1, val2, cond, selected;
  62. uint8 val1_type, val2_type;
  63. POP_COND(cond);
  64. if (!pop_value_from_wasm_stack(comp_ctx, func_ctx, &val2, is_select_32, &val2_type)
  65. || !pop_value_from_wasm_stack(comp_ctx, func_ctx, &val1, is_select_32, &val1_type))
  66. return false;
  67. if (val1_type != val2_type) {
  68. aot_set_last_error("invalid stack values with different type");
  69. return false;
  70. }
  71. if (!(selected = LLVMBuildSelect(comp_ctx->builder,
  72. cond, val1, val2,
  73. "select"))) {
  74. aot_set_last_error("llvm build select failed.");
  75. return false;
  76. }
  77. PUSH(selected, val1_type);
  78. return true;
  79. fail:
  80. return false;
  81. }