aot_emit_const.c 3.2 KB

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