aot_emit_const.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 (comp_ctx->is_indirect_mode
  63. && aot_intrinsic_check_capability(comp_ctx, "f32.const")) {
  64. WASMValue wasm_value;
  65. memcpy(&wasm_value.f32, &f32_const, sizeof(float32));
  66. value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
  67. &wasm_value, VALUE_TYPE_F32);
  68. if (!value) {
  69. return false;
  70. }
  71. PUSH_F32(value);
  72. }
  73. else if (!isnan(f32_const)) {
  74. value = F32_CONST(f32_const);
  75. CHECK_LLVM_CONST(value);
  76. PUSH_F32(value);
  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 (comp_ctx->is_indirect_mode
  113. && aot_intrinsic_check_capability(comp_ctx, "f64.const")) {
  114. WASMValue wasm_value;
  115. memcpy(&wasm_value.f64, &f64_const, sizeof(float64));
  116. value = aot_load_const_from_table(comp_ctx, func_ctx->native_symbol,
  117. &wasm_value, VALUE_TYPE_F64);
  118. if (!value) {
  119. return false;
  120. }
  121. PUSH_F64(value);
  122. }
  123. else if (!isnan(f64_const)) {
  124. value = F64_CONST(f64_const);
  125. CHECK_LLVM_CONST(value);
  126. PUSH_F64(value);
  127. }
  128. else {
  129. int64 i64_const;
  130. memcpy(&i64_const, &f64_const, sizeof(int64));
  131. if (!(alloca =
  132. LLVMBuildAlloca(comp_ctx->builder, I64_TYPE, "i64_ptr"))) {
  133. aot_set_last_error("llvm build alloca failed.");
  134. return false;
  135. }
  136. value = I64_CONST((uint64)i64_const);
  137. CHECK_LLVM_CONST(value);
  138. if (!LLVMBuildStore(comp_ctx->builder, value, alloca)) {
  139. aot_set_last_error("llvm build store failed.");
  140. return false;
  141. }
  142. if (!(alloca = LLVMBuildBitCast(comp_ctx->builder, alloca, F64_PTR_TYPE,
  143. "f64_ptr"))) {
  144. aot_set_last_error("llvm build bitcast failed.");
  145. return false;
  146. }
  147. if (!(value =
  148. LLVMBuildLoad2(comp_ctx->builder, F64_TYPE, alloca, ""))) {
  149. aot_set_last_error("llvm build load failed.");
  150. return false;
  151. }
  152. PUSH_F64(value);
  153. }
  154. return true;
  155. fail:
  156. return false;
  157. }