aot_emit_const.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_const.h"
  6. #include "../aot/aot_intrinsic.h"
  7. bool
  8. aot_compile_op_i32_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  9. int32 i32_const)
  10. {
  11. LLVMValueRef value;
  12. if (comp_ctx->is_indirect_mode
  13. && aot_intrinsic_check_capability(comp_ctx, "i32.const")) {
  14. WASMValue wasm_value;
  15. wasm_value.i32 = i32_const;
  16. value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
  17. &wasm_value, VALUE_TYPE_I32);
  18. if (!value) {
  19. return false;
  20. }
  21. }
  22. else {
  23. value = I32_CONST((uint32)i32_const);
  24. CHECK_LLVM_CONST(value);
  25. }
  26. PUSH_I32(value);
  27. return true;
  28. fail:
  29. return false;
  30. }
  31. bool
  32. aot_compile_op_i64_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  33. int64 i64_const)
  34. {
  35. LLVMValueRef value;
  36. if (comp_ctx->is_indirect_mode
  37. && aot_intrinsic_check_capability(comp_ctx, "i64.const")) {
  38. WASMValue wasm_value;
  39. wasm_value.i64 = i64_const;
  40. value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
  41. &wasm_value, VALUE_TYPE_I64);
  42. if (!value) {
  43. return false;
  44. }
  45. }
  46. else {
  47. value = I64_CONST((uint64)i64_const);
  48. CHECK_LLVM_CONST(value);
  49. }
  50. PUSH_I64(value);
  51. return true;
  52. fail:
  53. return false;
  54. }
  55. bool
  56. aot_compile_op_f32_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  57. float32 f32_const)
  58. {
  59. LLVMValueRef alloca, value;
  60. if (!isnan(f32_const)) {
  61. if (comp_ctx->is_indirect_mode
  62. && aot_intrinsic_check_capability(comp_ctx, "f32.const")) {
  63. WASMValue wasm_value;
  64. memcpy(&wasm_value.f32, &f32_const, sizeof(float32));
  65. value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
  66. &wasm_value, VALUE_TYPE_F32);
  67. if (!value) {
  68. return false;
  69. }
  70. PUSH_F32(value);
  71. }
  72. else {
  73. value = F32_CONST(f32_const);
  74. CHECK_LLVM_CONST(value);
  75. PUSH_F32(value);
  76. }
  77. }
  78. else {
  79. int32 i32_const;
  80. memcpy(&i32_const, &f32_const, sizeof(int32));
  81. if (!(alloca =
  82. LLVMBuildAlloca(comp_ctx->builder, I32_TYPE, "i32_ptr"))) {
  83. aot_set_last_error("llvm build alloca failed.");
  84. return false;
  85. }
  86. if (!LLVMBuildStore(comp_ctx->builder, I32_CONST((uint32)i32_const),
  87. alloca)) {
  88. aot_set_last_error("llvm build store failed.");
  89. return false;
  90. }
  91. if (!(alloca = LLVMBuildBitCast(comp_ctx->builder, alloca, F32_PTR_TYPE,
  92. "f32_ptr"))) {
  93. aot_set_last_error("llvm build bitcast failed.");
  94. return false;
  95. }
  96. if (!(value =
  97. LLVMBuildLoad2(comp_ctx->builder, F32_TYPE, alloca, ""))) {
  98. aot_set_last_error("llvm build load failed.");
  99. return false;
  100. }
  101. PUSH_F32(value);
  102. }
  103. return true;
  104. fail:
  105. return false;
  106. }
  107. bool
  108. aot_compile_op_f64_const(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  109. float64 f64_const)
  110. {
  111. LLVMValueRef alloca, value;
  112. if (!isnan(f64_const)) {
  113. if (comp_ctx->is_indirect_mode
  114. && aot_intrinsic_check_capability(comp_ctx, "f64.const")) {
  115. WASMValue wasm_value;
  116. memcpy(&wasm_value.f64, &f64_const, sizeof(float64));
  117. value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
  118. &wasm_value, VALUE_TYPE_F64);
  119. if (!value) {
  120. return false;
  121. }
  122. PUSH_F64(value);
  123. }
  124. else {
  125. value = F64_CONST(f64_const);
  126. CHECK_LLVM_CONST(value);
  127. PUSH_F64(value);
  128. }
  129. }
  130. else {
  131. int64 i64_const;
  132. memcpy(&i64_const, &f64_const, sizeof(int64));
  133. if (!(alloca =
  134. LLVMBuildAlloca(comp_ctx->builder, I64_TYPE, "i64_ptr"))) {
  135. aot_set_last_error("llvm build alloca failed.");
  136. return false;
  137. }
  138. value = I64_CONST((uint64)i64_const);
  139. CHECK_LLVM_CONST(value);
  140. if (!LLVMBuildStore(comp_ctx->builder, value, alloca)) {
  141. aot_set_last_error("llvm build store failed.");
  142. return false;
  143. }
  144. if (!(alloca = LLVMBuildBitCast(comp_ctx->builder, alloca, F64_PTR_TYPE,
  145. "f64_ptr"))) {
  146. aot_set_last_error("llvm build bitcast failed.");
  147. return false;
  148. }
  149. if (!(value =
  150. LLVMBuildLoad2(comp_ctx->builder, F64_TYPE, alloca, ""))) {
  151. aot_set_last_error("llvm build load failed.");
  152. return false;
  153. }
  154. PUSH_F64(value);
  155. }
  156. return true;
  157. fail:
  158. return false;
  159. }