aot_emit_const.c 4.2 KB

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