cpubase.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. {
  2. Copyright (c) 2016-2017 by Karoly Balogh
  3. Contains the base types for the WebAssembly
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This Unit contains the base types for the Java Virtual Machine
  18. }
  19. unit cpubase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. aasmbase,cpuinfo,cgbase;
  25. {*****************************************************************************
  26. Assembler Opcodes
  27. *****************************************************************************}
  28. type
  29. TAsmOp=(A_None,
  30. // control flow
  31. a_block, a_loop, a_br, a_br_if, a_br_table, a_if, a_else, a_end_block,
  32. a_end_loop, a_end_if, a_end_function, a_return, a_unreachable,
  33. // basic
  34. a_nop, a_drop, a_i32_const, a_i64_const, a_f32_const, a_f64_const,
  35. a_local_get, a_local_set, a_local_tee, a_global_get, a_global_set,
  36. a_select, a_call, a_call_indirect,
  37. // integer
  38. a_i32_add, a_i64_add, a_i32_sub, a_i64_sub, a_i32_mul, a_i64_mul,
  39. a_i32_div_s, a_i64_div_s, a_i32_div_u, a_i64_div_u, a_i32_rem_s, a_i64_rem_s,
  40. a_i32_rem_u, a_i64_rem_u, a_i32_and, a_i64_and, a_i32_or, a_i64_or,
  41. a_i32_xor, a_i64_xor, a_i32_shl, a_i64_shl, a_i32_shr_s, a_i64_shr_s,
  42. a_i32_shr_u, a_i64_shr_u, a_i32_rotl, a_i64_rotl, a_i32_rotr, a_i64_rotr,
  43. a_i32_clz, a_i64_clz, a_i32_ctz, a_i64_ctz, a_i32_popcnt, a_i64_popcnt,
  44. a_i32_eqz, a_i64_eqz,
  45. // floating point
  46. a_f32_add, a_f64_add, a_f32_sub, a_f64_sub, a_f32_mul, a_f64_mul,
  47. a_f32_div, a_f64_div, a_f32_sqrt, a_f64_sqrt, a_f32_min, a_f64_min,
  48. a_f32_max, a_f64_max, a_f32_ceil, a_f64_ceil, a_f32_floor, a_f64_floor,
  49. a_f32_trunc, a_f64_trunc, a_f32_nearest, a_f64_nearest, a_f32_abs, a_f64_abs,
  50. a_f32_neg, a_f64_neg, a_f32_copysign, a_f64_copysign,
  51. // integer compare
  52. a_i32_eq, a_i64_eq, a_i32_ne, a_i64_ne, a_i32_lt_s, a_i64_lt_s,
  53. a_i32_lt_u, a_i64_lt_u, a_i32_le_s, a_i64_le_s, a_i32_le_u, a_i64_le_u,
  54. a_i32_gt_s, a_i64_gt_s, a_i32_gt_u, a_i64_gt_u, a_i32_ge_s, a_i64_ge_s,
  55. a_i32_ge_u, a_i64_ge_u,
  56. // floating point compare
  57. a_f32_eq, a_f64_eq, a_f32_ne, a_f64_ne, a_f32_lt, a_f64_lt,
  58. a_f32_le, a_f64_le, a_f32_gt, a_f64_gt, a_f32_ge, a_f64_ge,
  59. // conversion
  60. a_i32_wrap_i64, a_i64_extend_i32_s, a_i64_extend_i32_u,
  61. a_i32_extend8_s,a_i32_extend16_s,a_i64_extend8_s,a_i64_extend16_s,a_i64_extend32_s,
  62. a_i32_trunc_f32_s, a_i32_trunc_f64_s, a_i64_trunc_f32_s, a_i64_trunc_f64_s,
  63. a_i32_trunc_f32_u, a_i32_trunc_f64_u, a_i64_trunc_f32_u, a_i64_trunc_f64_u,
  64. a_f32_demote_f64, a_f64_promote_f32,
  65. a_f32_convert_i32_s, a_f32_convert_i64_s,a_f64_convert_i32_s,a_f64_convert_i64_s,
  66. a_f32_convert_i32_u, a_f32_convert_i64_u,a_f64_convert_i32_u,a_f64_convert_i64_u,
  67. a_i32_reinterpret_f32, a_i64_reinterpret_f64, a_f32_reinterpret_i32, a_f64_reinterpret_i64,
  68. // load/store
  69. a_i32_load, a_i64_load, a_f32_load, a_f64_load,
  70. a_i32_store, a_i64_store, a_f32_store, a_f64_store,
  71. a_i32_load8_s, a_i32_load16_s, a_i64_load8_s, a_i64_load16_s, a_i64_load32_s,
  72. a_i32_load8_u, a_i32_load16_u, a_i64_load8_u, a_i64_load16_u, a_i64_load32_u,
  73. a_i32_store8, a_i32_store16, a_i64_store8, a_i64_store16, a_i64_store32,
  74. // additional memory
  75. a_memory_grow, a_memory_size,
  76. // bulk memory operations
  77. a_memory_copy, a_memory_fill, a_memory_init, a_data_drop,
  78. // reference instructions
  79. a_ref_null, a_ref_is_null, a_ref_func,
  80. // table instructions
  81. a_table_get, a_table_set, a_table_size, a_table_grow, a_table_fill, a_table_copy, a_table_init, a_elem_drop,
  82. // exceptions
  83. a_try,a_catch,a_catch_all,a_delegate,a_throw,a_rethrow,a_end_try,
  84. // atomic memory accesses - load/store
  85. a_i32_atomic_load8_u, a_i32_atomic_load16_u, a_i32_atomic_load,
  86. a_i64_atomic_load8_u, a_i64_atomic_load16_u, a_i64_atomic_load32_u,
  87. a_i64_atomic_load, a_i32_atomic_store8, a_i32_atomic_store16,
  88. a_i32_atomic_store, a_i64_atomic_store8, a_i64_atomic_store16,
  89. a_i64_atomic_store32, a_i64_atomic_store,
  90. // atomic memory accesses - read-modify-write
  91. a_i32_atomic_rmw8_add_u, a_i32_atomic_rmw16_add_u, a_i32_atomic_rmw_add,
  92. a_i64_atomic_rmw8_add_u, a_i64_atomic_rmw16_add_u, a_i64_atomic_rmw32_add_u,
  93. a_i64_atomic_rmw_add, a_i32_atomic_rmw8_sub_u, a_i32_atomic_rmw16_sub_u,
  94. a_i32_atomic_rmw_sub, a_i64_atomic_rmw8_sub_u, a_i64_atomic_rmw16_sub_u,
  95. a_i64_atomic_rmw32_sub_u, a_i64_atomic_rmw_sub, a_i32_atomic_rmw8_and_u,
  96. a_i32_atomic_rmw16_and_u, a_i32_atomic_rmw_and, a_i64_atomic_rmw8_and_u,
  97. a_i64_atomic_rmw16_and_u, a_i64_atomic_rmw32_and_u, a_i64_atomic_rmw_and,
  98. a_i32_atomic_rmw8_or_u, a_i32_atomic_rmw16_or_u, a_i32_atomic_rmw_or,
  99. a_i64_atomic_rmw8_or_u, a_i64_atomic_rmw16_or_u, a_i64_atomic_rmw32_or_u,
  100. a_i64_atomic_rmw_or, a_i32_atomic_rmw8_xor_u, a_i32_atomic_rmw16_xor_u,
  101. a_i32_atomic_rmw_xor, a_i64_atomic_rmw8_xor_u, a_i64_atomic_rmw16_xor_u,
  102. a_i64_atomic_rmw32_xor_u, a_i64_atomic_rmw_xor, a_i32_atomic_rmw8_xchg_u,
  103. a_i32_atomic_rmw16_xchg_u, a_i32_atomic_rmw_xchg, a_i64_atomic_rmw8_xchg_u,
  104. a_i64_atomic_rmw16_xchg_u, a_i64_atomic_rmw32_xchg_u, a_i64_atomic_rmw_xchg,
  105. // atomic memory accesses - compare exchange
  106. a_i32_atomic_rmw8_cmpxchg_u, a_i32_atomic_rmw16_cmpxchg_u, a_i32_atomic_rmw_cmpxchg,
  107. a_i64_atomic_rmw8_cmpxchg_u, a_i64_atomic_rmw16_cmpxchg_u, a_i64_atomic_rmw32_cmpxchg_u,
  108. a_i64_atomic_rmw_cmpxchg,
  109. // atomic memory accesses - wait and notify operators
  110. a_memory_atomic_wait32, a_memory_atomic_wait64, a_memory_atomic_notify, a_atomic_fence
  111. );
  112. TWasmBasicType = (
  113. { number types }
  114. wbt_i32, wbt_i64, wbt_f32, wbt_f64,
  115. { reference types }
  116. wbt_funcref, wbt_externref,
  117. { vector types }
  118. wbt_v128
  119. );
  120. TWasmResultType = array of TWasmBasicType;
  121. { TWasmFuncType }
  122. PWasmFuncType = ^TWasmFuncType;
  123. TWasmFuncType = class
  124. params: TWasmResultType;
  125. results: TWasmResultType;
  126. constructor Create(aparams, aresults: TWasmResultType);
  127. constructor Create(afunctype: TWasmFuncType);
  128. procedure add_param(param: TWasmBasicType);
  129. procedure add_result(res: TWasmBasicType);
  130. function Equals(Obj: TObject): boolean; override;
  131. end;
  132. {# This should define the array of instructions as string }
  133. op2strtable=array[tasmop] of string[31];
  134. Const
  135. {# First value of opcode enumeration }
  136. firstop = low(tasmop);
  137. {# Last value of opcode enumeration }
  138. lastop = high(tasmop);
  139. AsmOp_Store = [
  140. a_i32_store, a_i32_store16, a_i32_store8
  141. ,a_i64_store, a_i64_store16, a_i64_store8, a_i64_store32
  142. ,a_f32_store, a_f64_store
  143. ];
  144. AsmOp_Load = [
  145. a_i32_load,
  146. a_i32_load8_s, a_i32_load8_u,
  147. a_i32_load16_s, a_i32_load16_u,
  148. a_i64_load,
  149. a_i64_load8_s, a_i64_load8_u,
  150. a_i64_load16_s, a_i64_load16_u,
  151. a_i64_load32_s, a_i64_load32_u,
  152. a_f32_load, a_f64_load
  153. ];
  154. AsmOp_LoadStore = AsmOp_Load + AsmOp_Store;
  155. {*****************************************************************************
  156. Registers
  157. *****************************************************************************}
  158. type
  159. { Number of registers used for indexing in tables }
  160. tregisterindex=0..{$i rwasmnor.inc}-1; // no registers in wasm
  161. totherregisterset = set of tregisterindex;
  162. const
  163. { Available Superregisters }
  164. // there's no registers in wasm
  165. {$i rwasmsup.inc}
  166. { No Subregisters }
  167. R_SUBWHOLE = R_SUBNONE;
  168. { Available Registers }
  169. // there's no registers in wasm
  170. {$i rwasmcon.inc}
  171. { aliases }
  172. { used as base register in references for parameters passed to
  173. subroutines: these are passed on the evaluation stack, but this way we
  174. can use the offset field to indicate the order, which is used by ncal
  175. to sort the parameters }
  176. NR_EVAL_STACK_BASE = NR_R0;
  177. RS_EVAL_STACK_BASE = RS_R0;
  178. { used as base register in references to indicate that it's a local }
  179. NR_LOCAL_STACK_POINTER_REG = NR_R1;
  180. RS_LOCAL_STACK_POINTER_REG = RS_R1;
  181. { fake register, representing the local frame pointer. Used for accessing
  182. address-taken local variables on the linear stack: (localframeptr+offset). }
  183. NR_LOCAL_FRAME_POINTER_REG = NR_R3;
  184. RS_LOCAL_FRAME_POINTER_REG = RS_R3;
  185. maxvarregs = 1;
  186. maxfpuvarregs = 1;
  187. { Integer Super registers first and last }
  188. first_int_imreg = 4;
  189. { Float Super register first and last }
  190. first_fpu_imreg = 4;
  191. { MM Super register first and last }
  192. first_mm_imreg = 4;
  193. regnumber_table : array[tregisterindex] of tregister = (
  194. {$i rwasmnum.inc}
  195. );
  196. EVALSTACKLOCS = [LOC_REGISTER,LOC_CREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER,
  197. LOC_MMREGISTER,LOC_CMMREGISTER,LOC_SUBSETREG,LOC_CSUBSETREG];
  198. {*****************************************************************************
  199. Conditions
  200. *****************************************************************************}
  201. type
  202. // not used by wasm target
  203. TAsmCond=(C_None);
  204. {*****************************************************************************
  205. Constants
  206. *****************************************************************************}
  207. const
  208. max_operands = 2;
  209. {*****************************************************************************
  210. Default generic sizes
  211. *****************************************************************************}
  212. {$ifdef cpu64bitaddr}
  213. {# Defines the default address size for a processor,
  214. -- fake for JVM, only influences default width of
  215. arithmetic calculations }
  216. OS_ADDR = OS_64;
  217. {# the natural int size for a processor,
  218. has to match osuinttype/ossinttype as initialized in psystem }
  219. OS_INT = OS_64;
  220. OS_SINT = OS_S64;
  221. {$else}
  222. {# Defines the default address size for a processor,
  223. -- fake for wasm, only influences default width of
  224. arithmetic calculations }
  225. OS_ADDR = OS_32;
  226. {# the natural int size for a processor,
  227. has to match osuinttype/ossinttype as initialized in psystem }
  228. OS_INT = OS_32;
  229. OS_SINT = OS_S32;
  230. {$endif}
  231. {# the maximum float size for a processor, }
  232. OS_FLOAT = OS_F64;
  233. {# the size of a vector register for a processor }
  234. OS_VECTOR = OS_M128;
  235. {*****************************************************************************
  236. Generic Register names
  237. *****************************************************************************}
  238. { dummies, not used for Wasm }
  239. {# Stack pointer register }
  240. { used as base register in references to indicate that it's a local }
  241. NR_STACK_POINTER_REG = NR_R1;
  242. RS_STACK_POINTER_REG = RS_R1;
  243. {# Frame pointer register }
  244. NR_FRAME_POINTER_REG = NR_LOCAL_FRAME_POINTER_REG;
  245. RS_FRAME_POINTER_REG = RS_LOCAL_FRAME_POINTER_REG;
  246. { WebAssembly results are returned on the evaluation stack, not via a register }
  247. { Results are returned in this register (32-bit values) }
  248. NR_FUNCTION_RETURN_REG = NR_NO;
  249. RS_FUNCTION_RETURN_REG = RS_NO;
  250. { Low part of 64bit return value }
  251. NR_FUNCTION_RETURN64_LOW_REG = NR_NO;
  252. RS_FUNCTION_RETURN64_LOW_REG = RS_NO;
  253. { High part of 64bit return value }
  254. NR_FUNCTION_RETURN64_HIGH_REG = NR_NO;
  255. RS_FUNCTION_RETURN64_HIGH_REG = RS_NO;
  256. { The value returned from a function is available in this register }
  257. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  258. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  259. { The lowh part of 64bit value returned from a function }
  260. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  261. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  262. { The high part of 64bit value returned from a function }
  263. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  264. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  265. NR_FPU_RESULT_REG = NR_NO;
  266. NR_MM_RESULT_REG = NR_NO;
  267. { No default flags }
  268. NR_DEFAULTFLAGS = NR_NO;
  269. RS_DEFAULTFLAGS = RS_NO;
  270. {*****************************************************************************
  271. GCC /ABI linking information
  272. *****************************************************************************}
  273. { dummies, not used for Wasm }
  274. {# Required parameter alignment when calling a routine
  275. }
  276. std_param_align = 1;
  277. {*****************************************************************************
  278. CPU Dependent Constants
  279. *****************************************************************************}
  280. maxfpuregs = 0;
  281. { Global variable, that acts as the stack pointer in linear memory
  282. (also called the "linear stack"). This stack is used for address-taken
  283. local variables. This separate stack is needed, because the WASM
  284. implementation's runtime call stack (which includes return addresses and
  285. function parameters) is not visible in linear memory. }
  286. STACK_POINTER_SYM = '__stack_pointer';
  287. { The exception tag symbol, used for FPC exceptions }
  288. FPC_EXCEPTION_TAG_SYM = '__FPC_exception';
  289. {*****************************************************************************
  290. Helpers
  291. *****************************************************************************}
  292. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  293. function reg_cgsize(const reg: tregister) : tcgsize;
  294. function std_regnum_search(const s:string):Tregister;
  295. function std_regname(r:Tregister):string;
  296. function findreg_by_number(r:Tregister):tregisterindex;
  297. function dwarf_reg(r:tregister):byte;
  298. function dwarf_reg_no_error(r:tregister):shortint;
  299. function eh_return_data_regno(nr: longint): longint;
  300. { since we don't use tasmconds, don't call this routine
  301. (it will internalerror). We need it anyway to get aoptobj
  302. to compile (but it won't execute it).
  303. }
  304. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  305. function natural_alignment_for_load_store(op: TAsmOp): shortint;
  306. function encode_wasm_basic_type(wbt: TWasmBasicType): Byte;
  307. implementation
  308. uses
  309. verbose,
  310. rgbase;
  311. {*****************************************************************************
  312. Helpers
  313. *****************************************************************************}
  314. const
  315. std_regname_table : array[tregisterindex] of string[15] = (
  316. {$i rwasmstd.inc}
  317. );
  318. regnumber_index : array[tregisterindex] of tregisterindex = (
  319. {$i rwasmrni.inc}
  320. );
  321. std_regname_index : array[tregisterindex] of tregisterindex = (
  322. {$i rwasmsri.inc}
  323. );
  324. function reg_cgsize(const reg: tregister): tcgsize;
  325. begin
  326. result:=OS_NO;
  327. end;
  328. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  329. begin
  330. cgsize2subreg:=R_SUBNONE;
  331. end;
  332. function std_regnum_search(const s:string):Tregister;
  333. begin
  334. result:=NR_NO;
  335. end;
  336. function findreg_by_number(r:Tregister):tregisterindex;
  337. begin
  338. result:=findreg_by_number_table(r,regnumber_index);
  339. end;
  340. function std_regname(r:Tregister):string;
  341. var
  342. p : tregisterindex;
  343. begin
  344. p:=findreg_by_number_table(r,regnumber_index);
  345. if p<>0 then
  346. result:=std_regname_table[p]
  347. else
  348. result:=generic_regname(r);
  349. end;
  350. function dwarf_reg(r:tregister):byte;
  351. begin
  352. result:=0;
  353. internalerror(200603251);
  354. end;
  355. function dwarf_reg_no_error(r:tregister):shortint;
  356. begin
  357. result:=-1;
  358. end;
  359. function eh_return_data_regno(nr: longint): longint;
  360. begin
  361. result:=-1;
  362. end;
  363. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  364. begin
  365. result:=C_None;
  366. internalerror(2015082701);
  367. end;
  368. function natural_alignment_for_load_store(op: TAsmOp): shortint;
  369. begin
  370. case op of
  371. a_i32_load8_s,
  372. a_i32_load8_u,
  373. a_i64_load8_s,
  374. a_i64_load8_u,
  375. a_i32_store8,
  376. a_i64_store8,
  377. a_i32_atomic_load8_u,
  378. a_i64_atomic_load8_u,
  379. a_i32_atomic_store8,
  380. a_i64_atomic_store8,
  381. a_i32_atomic_rmw8_add_u,
  382. a_i64_atomic_rmw8_add_u,
  383. a_i32_atomic_rmw8_sub_u,
  384. a_i64_atomic_rmw8_sub_u,
  385. a_i32_atomic_rmw8_and_u,
  386. a_i64_atomic_rmw8_and_u,
  387. a_i32_atomic_rmw8_or_u,
  388. a_i64_atomic_rmw8_or_u,
  389. a_i32_atomic_rmw8_xor_u,
  390. a_i64_atomic_rmw8_xor_u,
  391. a_i32_atomic_rmw8_xchg_u,
  392. a_i64_atomic_rmw8_xchg_u,
  393. a_i32_atomic_rmw8_cmpxchg_u,
  394. a_i64_atomic_rmw8_cmpxchg_u:
  395. result:=0;
  396. a_i32_load16_s,
  397. a_i32_load16_u,
  398. a_i64_load16_s,
  399. a_i64_load16_u,
  400. a_i32_store16,
  401. a_i64_store16,
  402. a_i32_atomic_load16_u,
  403. a_i64_atomic_load16_u,
  404. a_i32_atomic_store16,
  405. a_i64_atomic_store16,
  406. a_i32_atomic_rmw16_add_u,
  407. a_i64_atomic_rmw16_add_u,
  408. a_i32_atomic_rmw16_sub_u,
  409. a_i64_atomic_rmw16_sub_u,
  410. a_i32_atomic_rmw16_and_u,
  411. a_i64_atomic_rmw16_and_u,
  412. a_i32_atomic_rmw16_or_u,
  413. a_i64_atomic_rmw16_or_u,
  414. a_i32_atomic_rmw16_xor_u,
  415. a_i64_atomic_rmw16_xor_u,
  416. a_i32_atomic_rmw16_xchg_u,
  417. a_i64_atomic_rmw16_xchg_u,
  418. a_i32_atomic_rmw16_cmpxchg_u,
  419. a_i64_atomic_rmw16_cmpxchg_u:
  420. result:=1;
  421. a_i32_load,
  422. a_f32_load,
  423. a_i64_load32_s,
  424. a_i64_load32_u,
  425. a_i32_store,
  426. a_f32_store,
  427. a_i64_store32,
  428. a_memory_atomic_notify,
  429. a_memory_atomic_wait32,
  430. a_i32_atomic_load,
  431. a_i64_atomic_load32_u,
  432. a_i32_atomic_store,
  433. a_i64_atomic_store32,
  434. a_i32_atomic_rmw_add,
  435. a_i64_atomic_rmw32_add_u,
  436. a_i32_atomic_rmw_sub,
  437. a_i64_atomic_rmw32_sub_u,
  438. a_i32_atomic_rmw_and,
  439. a_i64_atomic_rmw32_and_u,
  440. a_i32_atomic_rmw_or,
  441. a_i64_atomic_rmw32_or_u,
  442. a_i32_atomic_rmw_xor,
  443. a_i64_atomic_rmw32_xor_u,
  444. a_i32_atomic_rmw_xchg,
  445. a_i64_atomic_rmw32_xchg_u,
  446. a_i32_atomic_rmw_cmpxchg,
  447. a_i64_atomic_rmw32_cmpxchg_u:
  448. result:=2;
  449. a_i64_load,
  450. a_f64_load,
  451. a_i64_store,
  452. a_f64_store,
  453. a_memory_atomic_wait64,
  454. a_i64_atomic_load,
  455. a_i64_atomic_store,
  456. a_i64_atomic_rmw_add,
  457. a_i64_atomic_rmw_sub,
  458. a_i64_atomic_rmw_and,
  459. a_i64_atomic_rmw_or,
  460. a_i64_atomic_rmw_xor,
  461. a_i64_atomic_rmw_xchg,
  462. a_i64_atomic_rmw_cmpxchg:
  463. result:=3;
  464. else
  465. internalerror(2021092614);
  466. end;
  467. end;
  468. function encode_wasm_basic_type(wbt: TWasmBasicType): Byte;
  469. begin
  470. case wbt of
  471. wbt_i32:
  472. result:=$7F;
  473. wbt_i64:
  474. result:=$7E;
  475. wbt_f32:
  476. result:=$7D;
  477. wbt_f64:
  478. result:=$7C;
  479. wbt_funcref:
  480. result:=$70;
  481. wbt_externref:
  482. result:=$6F;
  483. wbt_v128:
  484. result:=$7B;
  485. end;
  486. end;
  487. {*****************************************************************************
  488. TWasmFuncType
  489. *****************************************************************************}
  490. constructor TWasmFuncType.Create(aparams, aresults: TWasmResultType);
  491. begin
  492. inherited Create;
  493. params:=aparams;
  494. results:=aresults;
  495. end;
  496. constructor TWasmFuncType.Create(afunctype: TWasmFuncType);
  497. begin
  498. inherited Create;
  499. params:=afunctype.params;
  500. results:=afunctype.results;
  501. end;
  502. procedure TWasmFuncType.add_param(param: TWasmBasicType);
  503. begin
  504. SetLength(params,Length(params)+1);
  505. params[High(params)]:=param;
  506. end;
  507. procedure TWasmFuncType.add_result(res: TWasmBasicType);
  508. begin
  509. SetLength(results,Length(results)+1);
  510. results[High(results)]:=res;
  511. end;
  512. function TWasmFuncType.Equals(Obj: TObject): boolean;
  513. var
  514. O: TWasmFuncType;
  515. begin
  516. if Obj=Self then
  517. exit(true)
  518. else if (Obj<>nil) and (Obj is TWasmFuncType) then
  519. begin
  520. O:=TWasmFuncType(Obj);
  521. if (Length(params)<>Length(O.params)) or (Length(results)<>Length(O.results)) then
  522. exit(false);
  523. if (Length(params)>0) and (CompareByte(params[0],O.params[0],Length(params)*SizeOf(params[0]))<>0) then
  524. exit(false);
  525. if (Length(results)>0) and (CompareByte(results[0],O.results[0],Length(results)*SizeOf(results[0]))<>0) then
  526. exit(false);
  527. Result:=true;
  528. end
  529. else
  530. Result:=inherited Equals(Obj);
  531. end;
  532. end.