test_invoke_native.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_runtime.h"
  6. static void
  7. test_native_args1(WASMModuleInstance *module_inst, int arg0, uint64_t arg1,
  8. float arg2, double arg3, int arg4, int64_t arg5, int64_t arg6,
  9. int arg7, double arg8, float arg9, int arg10, double arg11,
  10. float arg12, int64_t arg13, uint64_t arg14, float arg15,
  11. double arg16, int64_t arg17, uint64_t arg18, float arg19)
  12. {
  13. printf("##test_native_args1 result:\n");
  14. printf("arg0: 0x%X, arg1: 0x%X%08X, arg2: %f, arg3: %f\n", arg0,
  15. (int32)(arg1 >> 32), (int32)arg1, arg2, arg3);
  16. printf("arg4: 0x%X, arg5: 0x%X%08X, arg6: 0x%X%08X, arg7: 0x%X\n", arg4,
  17. (int32)(arg5 >> 32), (int32)arg5, (int32)(arg6 >> 32), (int32)arg6,
  18. arg7);
  19. printf("arg8: %f, arg9: %f, arg10: 0x%X, arg11: %f\n", arg8, arg9, arg10,
  20. arg11);
  21. printf("arg12: %f, arg13: 0x%X%08X, arg14: 0x%X%08X, arg15: %f\n", arg12,
  22. (int32)(arg13 >> 32), (int32)arg13, (int32)(arg14 >> 32),
  23. (int32)arg14, arg15);
  24. printf("arg16: %f, arg17: 0x%X%08X, arg18: 0x%X%08X, arg19: %f\n", arg16,
  25. (int32)(arg17 >> 32), (int32)arg17, (int32)(arg18 >> 32),
  26. (int32)arg18, arg19);
  27. }
  28. static void
  29. test_native_args2(WASMModuleInstance *module_inst, uint64_t arg1, float arg2,
  30. double arg3, int arg4, int64_t arg5, int64_t arg6, int arg7,
  31. double arg8, float arg9, int arg10, double arg11, float arg12,
  32. int64_t arg13, uint64_t arg14, float arg15, double arg16,
  33. int64_t arg17, uint64_t arg18, float arg19)
  34. {
  35. printf("##test_native_args2 result:\n");
  36. printf("arg1: 0x%X%08X, arg2: %f, arg3: %f\n", (int32)(arg1 >> 32),
  37. (int32)arg1, arg2, arg3);
  38. printf("arg4: 0x%X, arg5: 0x%X%08X, arg6: 0x%X%08X, arg7: 0x%X\n", arg4,
  39. (int32)(arg5 >> 32), (int32)arg5, (int32)(arg6 >> 32), (int32)arg6,
  40. arg7);
  41. printf("arg8: %f, arg9: %f, arg10: 0x%X, arg11: %f\n", arg8, arg9, arg10,
  42. arg11);
  43. printf("arg12: %f, arg13: 0x%X%08X, arg14: 0x%X%08X, arg15: %f\n", arg12,
  44. (int32)(arg13 >> 32), (int32)arg13, (int32)(arg14 >> 32),
  45. (int32)arg14, arg15);
  46. printf("arg16: %f, arg17: 0x%X%08X, arg18: 0x%X%08X, arg19: %f\n", arg16,
  47. (int32)(arg17 >> 32), (int32)arg17, (int32)(arg18 >> 32),
  48. (int32)arg18, arg19);
  49. }
  50. static int32
  51. test_return_i32(WASMModuleInstance *module_inst)
  52. {
  53. return 0x12345678;
  54. }
  55. static int64
  56. test_return_i64(WASMModuleInstance *module_inst)
  57. {
  58. return 0x12345678ABCDEFFFll;
  59. }
  60. static float32
  61. test_return_f32(WASMModuleInstance *module_inst)
  62. {
  63. return 1234.5678f;
  64. }
  65. static float64
  66. test_return_f64(WASMModuleInstance *module_inst)
  67. {
  68. return 87654321.12345678;
  69. }
  70. #define STORE_I64(addr, value) \
  71. do { \
  72. union { \
  73. int64 val; \
  74. uint32 parts[2]; \
  75. } u; \
  76. u.val = (int64)(value); \
  77. (addr)[0] = u.parts[0]; \
  78. (addr)[1] = u.parts[1]; \
  79. } while (0)
  80. #define STORE_F64(addr, value) \
  81. do { \
  82. union { \
  83. float64 val; \
  84. uint32 parts[2]; \
  85. } u; \
  86. u.val = (value); \
  87. (addr)[0] = u.parts[0]; \
  88. (addr)[1] = u.parts[1]; \
  89. } while (0)
  90. #define I32 VALUE_TYPE_I32
  91. #define I64 VALUE_TYPE_I64
  92. #define F32 VALUE_TYPE_F32
  93. #define F64 VALUE_TYPE_F64
  94. typedef struct WASMTypeTest {
  95. uint16 param_count;
  96. /* only one result is supported currently */
  97. uint16 result_count;
  98. uint16 param_cell_num;
  99. uint16 ret_cell_num;
  100. /* types of params and results */
  101. uint8 types[128];
  102. } WASMTypeTest;
  103. void
  104. test_invoke_native()
  105. {
  106. uint32 argv[128], *p = argv;
  107. WASMTypeTest func_type1 = { 20, 0, 0, 0, { I32, I64, F32, F64, I32,
  108. I64, I64, I32, F64, F32,
  109. I32, F64, F32, I64, I64,
  110. F32, F64, I64, I64, F32 } };
  111. WASMTypeTest func_type2 = { 19,
  112. 0,
  113. 0,
  114. 0,
  115. { I64, F32, F64, I32, I64, I64, I32, F64, F32,
  116. I32, F64, F32, I64, I64, F32, F64, I64, I64,
  117. F32 } };
  118. WASMTypeTest func_type_i32 = { 0, 1, 0, 0, { I32 } };
  119. WASMTypeTest func_type_i64 = { 0, 1, 0, 0, { I64 } };
  120. WASMTypeTest func_type_f32 = { 0, 1, 0, 0, { F32 } };
  121. WASMTypeTest func_type_f64 = { 0, 1, 0, 0, { F64 } };
  122. WASMModuleInstance module_inst = { 0 };
  123. WASMExecEnv exec_env = { 0 };
  124. module_inst.module_type = Wasm_Module_Bytecode;
  125. exec_env.module_inst = (WASMModuleInstanceCommon *)&module_inst;
  126. *p++ = 0x12345678;
  127. STORE_I64(p, 0xFFFFFFFF87654321ll);
  128. p += 2;
  129. *(float32 *)p++ = 1234.5678f;
  130. STORE_F64(p, 567890.1234);
  131. p += 2;
  132. *p++ = 0x11111111;
  133. STORE_I64(p, 0xAAAAAAAABBBBBBBBll);
  134. p += 2;
  135. STORE_I64(p, 0x7788888899ll);
  136. p += 2;
  137. *p++ = 0x3456;
  138. STORE_F64(p, 8888.7777);
  139. p += 2;
  140. *(float32 *)p++ = 7777.8888f;
  141. *p++ = 0x66666;
  142. STORE_F64(p, 999999.88888);
  143. p += 2;
  144. *(float32 *)p++ = 555555.22f;
  145. STORE_I64(p, 0xBBBBBAAAAAAAAll);
  146. p += 2;
  147. STORE_I64(p, 0x3333AAAABBBBll);
  148. p += 2;
  149. *(float32 *)p++ = 88.77f;
  150. STORE_F64(p, 9999.01234);
  151. p += 2;
  152. STORE_I64(p, 0x1111122222222ll);
  153. p += 2;
  154. STORE_I64(p, 0x444455555555ll);
  155. p += 2;
  156. *(float32 *)p++ = 77.88f;
  157. wasm_runtime_invoke_native(&exec_env, test_native_args1,
  158. (WASMType *)&func_type1, NULL, NULL, argv,
  159. p - argv, argv);
  160. printf("\n");
  161. wasm_runtime_invoke_native(&exec_env, test_native_args2,
  162. (WASMType *)&func_type2, NULL, NULL, argv + 1,
  163. p - argv - 1, argv);
  164. printf("\n");
  165. wasm_runtime_invoke_native(&exec_env, test_return_i32,
  166. (WASMType *)&func_type_i32, NULL, NULL, NULL, 0,
  167. argv);
  168. printf("test_return_i32: 0x%X\n\n", argv[0]);
  169. wasm_runtime_invoke_native(&exec_env, test_return_i64,
  170. (WASMType *)&func_type_i64, NULL, NULL, NULL, 0,
  171. argv);
  172. printf("test_return_i64: 0x%X%08X\n\n", (int32)((*(int64 *)argv) >> 32),
  173. (int32)(*(int64 *)argv));
  174. wasm_runtime_invoke_native(&exec_env, test_return_f32,
  175. (WASMType *)&func_type_f32, NULL, NULL, NULL, 0,
  176. argv);
  177. printf("test_return_f32: %f\n\n", *(float32 *)argv);
  178. wasm_runtime_invoke_native(&exec_env, test_return_f64,
  179. (WASMType *)&func_type_f64, NULL, NULL, NULL, 0,
  180. argv);
  181. printf("test_return_f64: %f\n\n", *(float64 *)argv);
  182. }