From snazz, 4 Weeks ago, written in C.
  1. #ifndef WASM_H_GENERATED_
  2. #define WASM_H_GENERATED_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6.  
  7. #include <stdint.h>
  8.  
  9. #include "wasm-rt.h"
  10.  
  11. #ifndef WASM_RT_MODULE_PREFIX
  12. #define WASM_RT_MODULE_PREFIX
  13. #endif
  14.  
  15. #define WASM_RT_PASTE_(x, y) x ## y
  16. #define WASM_RT_PASTE(x, y) WASM_RT_PASTE_(x, y)
  17. #define WASM_RT_ADD_PREFIX(x) WASM_RT_PASTE(WASM_RT_MODULE_PREFIX, x)
  18.  
  19. /* TODO(binji): only use stdint.h types in header */
  20. typedef uint8_t u8;
  21. typedef int8_t s8;
  22. typedef uint16_t u16;
  23. typedef int16_t s16;
  24. typedef uint32_t u32;
  25. typedef int32_t s32;
  26. typedef uint64_t u64;
  27. typedef int64_t s64;
  28. typedef float f32;
  29. typedef double f64;
  30.  
  31. extern void WASM_RT_ADD_PREFIX(init)(void);
  32.  
  33. /* import: 'env' '__linear_memory' */
  34. extern wasm_rt_memory_t (*Z_envZ___linear_memory);
  35. /* import: 'env' '__indirect_function_table' */
  36. extern wasm_rt_table_t (*Z_envZ___indirect_function_table);
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40.  
  41. #endif  /* WASM_H_GENERATED_ */
  42. #include <math.h>
  43. #include <string.h>
  44.  
  45. #include "wasm.h"
  46. #define UNLIKELY(x) __builtin_expect(!!(x), 0)
  47. #define LIKELY(x) __builtin_expect(!!(x), 1)
  48.  
  49. #define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0)
  50.  
  51. #define FUNC_PROLOGUE                                            
  52.   if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH)
  53.     TRAP(EXHAUSTION)
  54.  
  55. #define FUNC_EPILOGUE --wasm_rt_call_stack_depth
  56.  
  57. #define UNREACHABLE TRAP(UNREACHABLE)
  58.  
  59. #define CALL_INDIRECT(table, t, ft, x, ...)          
  60.   (LIKELY((x) < table.size && table.data[x].func &&  
  61.           table.data[x].func_type == func_types[ft])
  62.        ? ((t)table.data[x].func)(__VA_ARGS__)        
  63.        : TRAP(CALL_INDIRECT))
  64.  
  65. #define MEMCHECK(mem, a, t)  
  66.   if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB)
  67.  
  68. #define DEFINE_LOAD(name, t1, t2, t3)              
  69.   static inline t3 name(wasm_rt_memory_t* mem, u64 addr) {  
  70.     MEMCHECK(mem, addr, t1);                      
  71.     t1 result;                                    
  72.     memcpy(&result, &mem->data[addr], sizeof(t1));
  73.     return (t3)(t2)result;                        
  74.   }
  75.  
  76. #define DEFINE_STORE(name, t1, t2)                          
  77.   static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) {
  78.     MEMCHECK(mem, addr, t1);                                
  79.     t1 wrapped = (t1)value;                                  
  80.     memcpy(&mem->data[addr], &wrapped, sizeof(t1));          
  81.   }
  82.  
  83. DEFINE_LOAD(i32_load, u32, u32, u32);
  84. DEFINE_LOAD(i64_load, u64, u64, u64);
  85. DEFINE_LOAD(f32_load, f32, f32, f32);
  86. DEFINE_LOAD(f64_load, f64, f64, f64);
  87. DEFINE_LOAD(i32_load8_s, s8, s32, u32);
  88. DEFINE_LOAD(i64_load8_s, s8, s64, u64);
  89. DEFINE_LOAD(i32_load8_u, u8, u32, u32);
  90. DEFINE_LOAD(i64_load8_u, u8, u64, u64);
  91. DEFINE_LOAD(i32_load16_s, s16, s32, u32);
  92. DEFINE_LOAD(i64_load16_s, s16, s64, u64);
  93. DEFINE_LOAD(i32_load16_u, u16, u32, u32);
  94. DEFINE_LOAD(i64_load16_u, u16, u64, u64);
  95. DEFINE_LOAD(i64_load32_s, s32, s64, u64);
  96. DEFINE_LOAD(i64_load32_u, u32, u64, u64);
  97. DEFINE_STORE(i32_store, u32, u32);
  98. DEFINE_STORE(i64_store, u64, u64);
  99. DEFINE_STORE(f32_store, f32, f32);
  100. DEFINE_STORE(f64_store, f64, f64);
  101. DEFINE_STORE(i32_store8, u8, u32);
  102. DEFINE_STORE(i32_store16, u16, u32);
  103. DEFINE_STORE(i64_store8, u8, u64);
  104. DEFINE_STORE(i64_store16, u16, u64);
  105. DEFINE_STORE(i64_store32, u32, u64);
  106.  
  107. #define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32)
  108. #define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64)
  109. #define I32_CTZ(x) ((x) ? __builtin_ctz(x) : 32)
  110. #define I64_CTZ(x) ((x) ? __builtin_ctzll(x) : 64)
  111. #define I32_POPCNT(x) (__builtin_popcount(x))
  112. #define I64_POPCNT(x) (__builtin_popcountll(x))
  113.  
  114. #define DIV_S(ut, min, x, y)                                
  115.    ((UNLIKELY((y) == 0)) ?                TRAP(DIV_BY_ZERO)  
  116.   : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW)
  117.   : (ut)((x) / (y)))
  118.  
  119. #define REM_S(ut, min, x, y)                                
  120.    ((UNLIKELY((y) == 0)) ?                TRAP(DIV_BY_ZERO)
  121.   : (UNLIKELY((x) == min && (y) == -1)) ? 0                
  122.   : (ut)((x) % (y)))
  123.  
  124. #define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y)
  125. #define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y)
  126. #define I32_REM_S(x, y) REM_S(u32, INT32_MIN, (s32)x, (s32)y)
  127. #define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y)
  128.  
  129. #define DIVREM_U(op, x, y)
  130.   ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x) op (y)))
  131.  
  132. #define DIV_U(x, y) DIVREM_U(/, x, y)
  133. #define REM_U(x, y) DIVREM_U(%, x, y)
  134.  
  135. #define ROTL(x, y, mask)
  136.   (((x) << ((y) & (mask))) | ((x) >> (((mask) - (y) + 1) & (mask))))
  137. #define ROTR(x, y, mask)
  138.   (((x) >> ((y) & (mask))) | ((x) << (((mask) - (y) + 1) & (mask))))
  139.  
  140. #define I32_ROTL(x, y) ROTL(x, y, 31)
  141. #define I64_ROTL(x, y) ROTL(x, y, 63)
  142. #define I32_ROTR(x, y) ROTR(x, y, 31)
  143. #define I64_ROTR(x, y) ROTR(x, y, 63)
  144.  
  145. #define FMIN(x, y)                                          
  146.    ((UNLIKELY((x) != (x))) ? NAN                            
  147.   : (UNLIKELY((y) != (y))) ? NAN                            
  148.   : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y)
  149.   : (x < y) ? x : y)
  150.  
  151. #define FMAX(x, y)                                          
  152.    ((UNLIKELY((x) != (x))) ? NAN                            
  153.   : (UNLIKELY((y) != (y))) ? NAN                            
  154.   : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x)
  155.   : (x > y) ? x : y)
  156.  
  157. #define TRUNC_S(ut, st, ft, min, max, maxop, x)                            
  158.    ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION)                      
  159.   : (UNLIKELY((x) < (ft)(min) || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW)
  160.   : (ut)(st)(x))
  161.  
  162. #define I32_TRUNC_S_F32(x) TRUNC_S(u32, s32, f32, INT32_MIN, INT32_MAX, >=, x)
  163. #define I64_TRUNC_S_F32(x) TRUNC_S(u64, s64, f32, INT64_MIN, INT64_MAX, >=, x)
  164. #define I32_TRUNC_S_F64(x) TRUNC_S(u32, s32, f64, INT32_MIN, INT32_MAX, >,  x)
  165. #define I64_TRUNC_S_F64(x) TRUNC_S(u64, s64, f64, INT64_MIN, INT64_MAX, >=, x)
  166.  
  167. #define TRUNC_U(ut, ft, max, maxop, x)                                    
  168.    ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION)                    
  169.   : (UNLIKELY((x) <= (ft)-1 || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW)
  170.   : (ut)(x))
  171.  
  172. #define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, UINT32_MAX, >=, x)
  173. #define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, UINT64_MAX, >=, x)
  174. #define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, UINT32_MAX, >,  x)
  175. #define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, UINT64_MAX, >=, x)
  176.  
  177. #define DEFINE_REINTERPRET(name, t1, t2)  
  178.   static inline t2 name(t1 x) {          
  179.     t2 result;                            
  180.     memcpy(&result, &x, sizeof(result));  
  181.     return result;                        
  182.   }
  183.  
  184. DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32)
  185. DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32)
  186. DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64)
  187. DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64)
  188.  
  189.  
  190. static u32 func_types[1];
  191.  
  192. static void init_func_types(void) {
  193.   func_types[0] = wasm_rt_register_func_type(2, 1, WASM_RT_I32, WASM_RT_I32, WASM_RT_F32);
  194. }
  195.  
  196. static f32 f0(u32, u32);
  197.  
  198. static void init_globals(void) {
  199. }
  200.  
  201. static f32 f0(u32 p0, u32 p1) {
  202.   FUNC_PROLOGUE;
  203.   u32 i0, i1, i2;
  204.   f32 f0_0, f1, f2;
  205.   i0 = p0;
  206.   f0_0 = f32_load(Z_envZ___linear_memory, (u64)(i0));
  207.   i1 = p1;
  208.   f1 = f32_load(Z_envZ___linear_memory, (u64)(i1));
  209.   f0_0 *= f1;
  210.   i1 = p0;
  211.   f1 = f32_load(Z_envZ___linear_memory, (u64)(i1 + 4));
  212.   i2 = p1;
  213.   f2 = f32_load(Z_envZ___linear_memory, (u64)(i2 + 4));
  214.   f1 *= f2;
  215.   f0_0 += f1;
  216.   i1 = p0;
  217.   f1 = f32_load(Z_envZ___linear_memory, (u64)(i1 + 8));
  218.   i2 = p1;
  219.   f2 = f32_load(Z_envZ___linear_memory, (u64)(i2 + 8));
  220.   f1 *= f2;
  221.   f0_0 += f1;
  222.   FUNC_EPILOGUE;
  223.   return f0_0;
  224. }
  225.  
  226.  
  227. static void init_memory(void) {
  228. }
  229.  
  230. static void init_table(void) {
  231.   uint32_t offset;
  232. }
  233.  
  234. static void init_exports(void) {
  235. }
  236.  
  237. void WASM_RT_ADD_PREFIX(init)(void) {
  238.   init_func_types();
  239.   init_globals();
  240.   init_memory();
  241.   init_table();
  242.   init_exports();
  243. }
captcha