aot_emit_const.c 5.2 KB

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