cpubase.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 WebAssembly
  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_funcref, a_ref_null_externref, 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. // saturating truncation instructions
  83. a_i32_trunc_sat_f32_s,
  84. a_i32_trunc_sat_f32_u,
  85. a_i32_trunc_sat_f64_s,
  86. a_i32_trunc_sat_f64_u,
  87. a_i64_trunc_sat_f32_s,
  88. a_i64_trunc_sat_f32_u,
  89. a_i64_trunc_sat_f64_s,
  90. a_i64_trunc_sat_f64_u,
  91. // exceptions (legacy)
  92. a_legacy_try,a_legacy_catch,a_legacy_catch_all,a_legacy_delegate,a_legacy_throw,a_legacy_rethrow,a_end_legacy_try,
  93. // exceptions (with exnref)
  94. a_try_table,a_catch,a_catch_ref,a_catch_all,a_catch_all_ref,a_throw,a_throw_ref,a_end_try_table,
  95. // atomic memory accesses - load/store
  96. a_i32_atomic_load8_u, a_i32_atomic_load16_u, a_i32_atomic_load,
  97. a_i64_atomic_load8_u, a_i64_atomic_load16_u, a_i64_atomic_load32_u,
  98. a_i64_atomic_load, a_i32_atomic_store8, a_i32_atomic_store16,
  99. a_i32_atomic_store, a_i64_atomic_store8, a_i64_atomic_store16,
  100. a_i64_atomic_store32, a_i64_atomic_store,
  101. // atomic memory accesses - read-modify-write
  102. a_i32_atomic_rmw8_add_u, a_i32_atomic_rmw16_add_u, a_i32_atomic_rmw_add,
  103. a_i64_atomic_rmw8_add_u, a_i64_atomic_rmw16_add_u, a_i64_atomic_rmw32_add_u,
  104. a_i64_atomic_rmw_add, a_i32_atomic_rmw8_sub_u, a_i32_atomic_rmw16_sub_u,
  105. a_i32_atomic_rmw_sub, a_i64_atomic_rmw8_sub_u, a_i64_atomic_rmw16_sub_u,
  106. a_i64_atomic_rmw32_sub_u, a_i64_atomic_rmw_sub, a_i32_atomic_rmw8_and_u,
  107. a_i32_atomic_rmw16_and_u, a_i32_atomic_rmw_and, a_i64_atomic_rmw8_and_u,
  108. a_i64_atomic_rmw16_and_u, a_i64_atomic_rmw32_and_u, a_i64_atomic_rmw_and,
  109. a_i32_atomic_rmw8_or_u, a_i32_atomic_rmw16_or_u, a_i32_atomic_rmw_or,
  110. a_i64_atomic_rmw8_or_u, a_i64_atomic_rmw16_or_u, a_i64_atomic_rmw32_or_u,
  111. a_i64_atomic_rmw_or, a_i32_atomic_rmw8_xor_u, a_i32_atomic_rmw16_xor_u,
  112. a_i32_atomic_rmw_xor, a_i64_atomic_rmw8_xor_u, a_i64_atomic_rmw16_xor_u,
  113. a_i64_atomic_rmw32_xor_u, a_i64_atomic_rmw_xor, a_i32_atomic_rmw8_xchg_u,
  114. a_i32_atomic_rmw16_xchg_u, a_i32_atomic_rmw_xchg, a_i64_atomic_rmw8_xchg_u,
  115. a_i64_atomic_rmw16_xchg_u, a_i64_atomic_rmw32_xchg_u, a_i64_atomic_rmw_xchg,
  116. // atomic memory accesses - compare exchange
  117. a_i32_atomic_rmw8_cmpxchg_u, a_i32_atomic_rmw16_cmpxchg_u, a_i32_atomic_rmw_cmpxchg,
  118. a_i64_atomic_rmw8_cmpxchg_u, a_i64_atomic_rmw16_cmpxchg_u, a_i64_atomic_rmw32_cmpxchg_u,
  119. a_i64_atomic_rmw_cmpxchg,
  120. // atomic memory accesses - wait and notify operators
  121. a_memory_atomic_wait32, a_memory_atomic_wait64, a_memory_atomic_notify, a_atomic_fence
  122. );
  123. TWasmBasicType = (
  124. wbt_Unknown,
  125. { number types }
  126. wbt_i32, wbt_i64, wbt_f32, wbt_f64,
  127. { reference types }
  128. wbt_funcref, wbt_externref,
  129. { vector types }
  130. wbt_v128
  131. );
  132. TWasmResultType = array of TWasmBasicType;
  133. TWasmLocalsDynArray = array of TWasmBasicType;
  134. TWasmMemoryFlag = (
  135. wmfHasMaximumBound,
  136. wmfShared,
  137. wmfMemory64,
  138. wmfCustomPageSize
  139. );
  140. TWasmMemoryFlags = set of TWasmMemoryFlag;
  141. TWasmMemoryType = record
  142. Flags: TWasmMemoryFlags;
  143. MinPages, MaxPages: UInt64;
  144. PageSize: UInt32;
  145. end;
  146. { TWasmFuncType }
  147. PWasmFuncType = ^TWasmFuncType;
  148. TWasmFuncType = class
  149. params: TWasmResultType;
  150. results: TWasmResultType;
  151. constructor Create(aparams, aresults: TWasmResultType);
  152. constructor Create(afunctype: TWasmFuncType);
  153. procedure add_param(param: TWasmBasicType);
  154. procedure add_result(res: TWasmBasicType);
  155. function Equals(Obj: TObject): boolean; override;
  156. function ToString: ansistring; override;
  157. end;
  158. {# This should define the array of instructions as string }
  159. op2strtable=array[tasmop] of string[31];
  160. Const
  161. WasmNumberTypes = [wbt_i32, wbt_i64, wbt_f32, wbt_f64];
  162. WasmReferenceTypes = [wbt_funcref, wbt_externref];
  163. WasmVectorTypes = [wbt_v128];
  164. wasm_basic_type_str : array [TWasmBasicType] of string = ('unknown','i32','i64','f32','f64','funcref','externref','v128');
  165. {# First value of opcode enumeration }
  166. firstop = low(tasmop);
  167. {# Last value of opcode enumeration }
  168. lastop = high(tasmop);
  169. AsmOp_Store = [
  170. a_i32_store, a_i32_store16, a_i32_store8
  171. ,a_i64_store, a_i64_store16, a_i64_store8, a_i64_store32
  172. ,a_f32_store, a_f64_store
  173. ];
  174. AsmOp_Load = [
  175. a_i32_load,
  176. a_i32_load8_s, a_i32_load8_u,
  177. a_i32_load16_s, a_i32_load16_u,
  178. a_i64_load,
  179. a_i64_load8_s, a_i64_load8_u,
  180. a_i64_load16_s, a_i64_load16_u,
  181. a_i64_load32_s, a_i64_load32_u,
  182. a_f32_load, a_f64_load
  183. ];
  184. AsmOp_LoadStore = AsmOp_Load + AsmOp_Store;
  185. {*****************************************************************************
  186. Registers
  187. *****************************************************************************}
  188. type
  189. { Number of registers used for indexing in tables }
  190. tregisterindex=0..{$i rwasmnor.inc}-1; // no registers in wasm
  191. totherregisterset = set of tregisterindex;
  192. const
  193. { Available Superregisters }
  194. // there's no registers in wasm
  195. {$i rwasmsup.inc}
  196. { No Subregisters }
  197. R_SUBWHOLE = R_SUBNONE;
  198. { Available Registers }
  199. // there's no registers in wasm
  200. {$i rwasmcon.inc}
  201. { aliases }
  202. { used as base register in references for parameters passed to
  203. subroutines: these are passed on the evaluation stack, but this way we
  204. can use the offset field to indicate the order, which is used by ncal
  205. to sort the parameters }
  206. NR_EVAL_STACK_BASE = NR_R0;
  207. RS_EVAL_STACK_BASE = RS_R0;
  208. { used as base register in references to indicate that it's a local }
  209. NR_LOCAL_STACK_POINTER_REG = NR_R1;
  210. RS_LOCAL_STACK_POINTER_REG = RS_R1;
  211. { fake register, representing the local frame pointer. Used for accessing
  212. address-taken local variables on the linear stack: (localframeptr+offset). }
  213. NR_LOCAL_FRAME_POINTER_REG = NR_R3;
  214. RS_LOCAL_FRAME_POINTER_REG = RS_R3;
  215. maxvarregs = 1;
  216. maxfpuvarregs = 1;
  217. { Integer Super registers first and last }
  218. first_int_imreg = 4;
  219. { Float Super register first and last }
  220. first_fpu_imreg = 4;
  221. { MM Super register first and last }
  222. first_mm_imreg = 4;
  223. { funcref Super register first and last }
  224. first_funcref_imreg = 4;
  225. { externref Super register first and last }
  226. first_externref_imreg = 4;
  227. regnumber_table : array[tregisterindex] of tregister = (
  228. {$i rwasmnum.inc}
  229. );
  230. EVALSTACKLOCS = [LOC_REGISTER,LOC_CREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER,
  231. LOC_MMREGISTER,LOC_CMMREGISTER,LOC_SUBSETREG,LOC_CSUBSETREG];
  232. {*****************************************************************************
  233. Conditions
  234. *****************************************************************************}
  235. type
  236. // not used by wasm target
  237. TAsmCond=(C_None);
  238. {*****************************************************************************
  239. Constants
  240. *****************************************************************************}
  241. const
  242. max_operands = 2;
  243. {*****************************************************************************
  244. Default generic sizes
  245. *****************************************************************************}
  246. {$ifdef cpu64bitaddr}
  247. {# Defines the default address size for a processor,
  248. -- fake for JVM, only influences default width of
  249. arithmetic calculations }
  250. OS_ADDR = OS_64;
  251. {# the natural int size for a processor,
  252. has to match osuinttype/ossinttype as initialized in psystem }
  253. OS_INT = OS_64;
  254. OS_SINT = OS_S64;
  255. {$else}
  256. {# Defines the default address size for a processor,
  257. -- fake for wasm, only influences default width of
  258. arithmetic calculations }
  259. OS_ADDR = OS_32;
  260. {# the natural int size for a processor,
  261. has to match osuinttype/ossinttype as initialized in psystem }
  262. OS_INT = OS_32;
  263. OS_SINT = OS_S32;
  264. {$endif}
  265. {# the maximum float size for a processor, }
  266. OS_FLOAT = OS_F64;
  267. {# the size of a vector register for a processor }
  268. OS_VECTOR = OS_M128;
  269. {*****************************************************************************
  270. Generic Register names
  271. *****************************************************************************}
  272. { dummies, not used for Wasm }
  273. {# Stack pointer register }
  274. { used as base register in references to indicate that it's a local }
  275. NR_STACK_POINTER_REG = NR_R1;
  276. RS_STACK_POINTER_REG = RS_R1;
  277. {# Frame pointer register }
  278. NR_FRAME_POINTER_REG = NR_LOCAL_FRAME_POINTER_REG;
  279. RS_FRAME_POINTER_REG = RS_LOCAL_FRAME_POINTER_REG;
  280. { WebAssembly results are returned on the evaluation stack, not via a register }
  281. { Results are returned in this register (32-bit values) }
  282. NR_FUNCTION_RETURN_REG = NR_NO;
  283. RS_FUNCTION_RETURN_REG = RS_NO;
  284. { Low part of 64bit return value }
  285. NR_FUNCTION_RETURN64_LOW_REG = NR_NO;
  286. RS_FUNCTION_RETURN64_LOW_REG = RS_NO;
  287. { High part of 64bit return value }
  288. NR_FUNCTION_RETURN64_HIGH_REG = NR_NO;
  289. RS_FUNCTION_RETURN64_HIGH_REG = RS_NO;
  290. { The value returned from a function is available in this register }
  291. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  292. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  293. { The lowh part of 64bit value returned from a function }
  294. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  295. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  296. { The high part of 64bit value returned from a function }
  297. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  298. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  299. NR_FPU_RESULT_REG = NR_NO;
  300. NR_MM_RESULT_REG = NR_NO;
  301. { No default flags }
  302. NR_DEFAULTFLAGS = NR_NO;
  303. RS_DEFAULTFLAGS = RS_NO;
  304. {*****************************************************************************
  305. GCC /ABI linking information
  306. *****************************************************************************}
  307. { dummies, not used for Wasm }
  308. {# Required parameter alignment when calling a routine
  309. }
  310. std_param_align = 1;
  311. {*****************************************************************************
  312. CPU Dependent Constants
  313. *****************************************************************************}
  314. maxfpuregs = 0;
  315. { Global variable, that acts as the stack pointer in linear memory
  316. (also called the "linear stack"). This stack is used for address-taken
  317. local variables. This separate stack is needed, because the WASM
  318. implementation's runtime call stack (which includes return addresses and
  319. function parameters) is not visible in linear memory. }
  320. STACK_POINTER_SYM = '__stack_pointer';
  321. { The exception tag symbol, used for FPC exceptions }
  322. FPC_EXCEPTION_TAG_SYM = '__FPC_exception';
  323. { Immutable global variable, created by the linker, when multithreading
  324. support is enabled. Contains the total size (plus padding) of all the
  325. threadvars, used by the program. }
  326. TLS_SIZE_SYM = '__tls_size';
  327. { Immutable global variable, created by the linker, when multithreading
  328. support is enabled. Contains the alignment requirement for the thread
  329. local block. }
  330. TLS_ALIGN_SYM = '__tls_align';
  331. { Mutable global variable, created by the linker, when multithreading
  332. support is enabled. }
  333. TLS_BASE_SYM = '__tls_base';
  334. {*****************************************************************************
  335. Helpers
  336. *****************************************************************************}
  337. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  338. function reg_cgsize(const reg: tregister) : tcgsize;
  339. function std_regnum_search(const s:string):Tregister;
  340. function std_regname(r:Tregister):string;
  341. function findreg_by_number(r:Tregister):tregisterindex;
  342. function dwarf_reg(r:tregister):byte;
  343. function dwarf_reg_no_error(r:tregister):shortint;
  344. function eh_return_data_regno(nr: longint): longint;
  345. { since we don't use tasmconds, don't call this routine
  346. (it will internalerror). We need it anyway to get aoptobj
  347. to compile (but it won't execute it).
  348. }
  349. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  350. function natural_alignment_for_load_store(op: TAsmOp): shortint;
  351. function encode_wasm_basic_type(wbt: TWasmBasicType): Byte;
  352. function decode_wasm_basic_type(b: Byte; out wbt: TWasmBasicType): Boolean;
  353. function is_atomic_op(op: TAsmOp): boolean;
  354. implementation
  355. uses
  356. verbose,
  357. rgbase;
  358. {*****************************************************************************
  359. Helpers
  360. *****************************************************************************}
  361. const
  362. std_regname_table : array[tregisterindex] of string[15] = (
  363. {$i rwasmstd.inc}
  364. );
  365. regnumber_index : array[tregisterindex] of tregisterindex = (
  366. {$i rwasmrni.inc}
  367. );
  368. std_regname_index : array[tregisterindex] of tregisterindex = (
  369. {$i rwasmsri.inc}
  370. );
  371. function reg_cgsize(const reg: tregister): tcgsize;
  372. begin
  373. result:=OS_NO;
  374. end;
  375. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  376. begin
  377. cgsize2subreg:=R_SUBNONE;
  378. end;
  379. function std_regnum_search(const s:string):Tregister;
  380. begin
  381. result:=NR_NO;
  382. end;
  383. function findreg_by_number(r:Tregister):tregisterindex;
  384. begin
  385. result:=findreg_by_number_table(r,regnumber_index);
  386. end;
  387. function std_regname(r:Tregister):string;
  388. var
  389. p : tregisterindex;
  390. begin
  391. p:=findreg_by_number_table(r,regnumber_index);
  392. if p<>0 then
  393. result:=std_regname_table[p]
  394. else
  395. result:=generic_regname(r);
  396. end;
  397. function dwarf_reg(r:tregister):byte;
  398. begin
  399. result:=0;
  400. internalerror(200603251);
  401. end;
  402. function dwarf_reg_no_error(r:tregister):shortint;
  403. begin
  404. result:=-1;
  405. end;
  406. function eh_return_data_regno(nr: longint): longint;
  407. begin
  408. result:=-1;
  409. end;
  410. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  411. begin
  412. result:=C_None;
  413. internalerror(2015082701);
  414. end;
  415. function natural_alignment_for_load_store(op: TAsmOp): shortint;
  416. begin
  417. case op of
  418. a_i32_load8_s,
  419. a_i32_load8_u,
  420. a_i64_load8_s,
  421. a_i64_load8_u,
  422. a_i32_store8,
  423. a_i64_store8,
  424. a_i32_atomic_load8_u,
  425. a_i64_atomic_load8_u,
  426. a_i32_atomic_store8,
  427. a_i64_atomic_store8,
  428. a_i32_atomic_rmw8_add_u,
  429. a_i64_atomic_rmw8_add_u,
  430. a_i32_atomic_rmw8_sub_u,
  431. a_i64_atomic_rmw8_sub_u,
  432. a_i32_atomic_rmw8_and_u,
  433. a_i64_atomic_rmw8_and_u,
  434. a_i32_atomic_rmw8_or_u,
  435. a_i64_atomic_rmw8_or_u,
  436. a_i32_atomic_rmw8_xor_u,
  437. a_i64_atomic_rmw8_xor_u,
  438. a_i32_atomic_rmw8_xchg_u,
  439. a_i64_atomic_rmw8_xchg_u,
  440. a_i32_atomic_rmw8_cmpxchg_u,
  441. a_i64_atomic_rmw8_cmpxchg_u:
  442. result:=0;
  443. a_i32_load16_s,
  444. a_i32_load16_u,
  445. a_i64_load16_s,
  446. a_i64_load16_u,
  447. a_i32_store16,
  448. a_i64_store16,
  449. a_i32_atomic_load16_u,
  450. a_i64_atomic_load16_u,
  451. a_i32_atomic_store16,
  452. a_i64_atomic_store16,
  453. a_i32_atomic_rmw16_add_u,
  454. a_i64_atomic_rmw16_add_u,
  455. a_i32_atomic_rmw16_sub_u,
  456. a_i64_atomic_rmw16_sub_u,
  457. a_i32_atomic_rmw16_and_u,
  458. a_i64_atomic_rmw16_and_u,
  459. a_i32_atomic_rmw16_or_u,
  460. a_i64_atomic_rmw16_or_u,
  461. a_i32_atomic_rmw16_xor_u,
  462. a_i64_atomic_rmw16_xor_u,
  463. a_i32_atomic_rmw16_xchg_u,
  464. a_i64_atomic_rmw16_xchg_u,
  465. a_i32_atomic_rmw16_cmpxchg_u,
  466. a_i64_atomic_rmw16_cmpxchg_u:
  467. result:=1;
  468. a_i32_load,
  469. a_f32_load,
  470. a_i64_load32_s,
  471. a_i64_load32_u,
  472. a_i32_store,
  473. a_f32_store,
  474. a_i64_store32,
  475. a_memory_atomic_notify,
  476. a_memory_atomic_wait32,
  477. a_i32_atomic_load,
  478. a_i64_atomic_load32_u,
  479. a_i32_atomic_store,
  480. a_i64_atomic_store32,
  481. a_i32_atomic_rmw_add,
  482. a_i64_atomic_rmw32_add_u,
  483. a_i32_atomic_rmw_sub,
  484. a_i64_atomic_rmw32_sub_u,
  485. a_i32_atomic_rmw_and,
  486. a_i64_atomic_rmw32_and_u,
  487. a_i32_atomic_rmw_or,
  488. a_i64_atomic_rmw32_or_u,
  489. a_i32_atomic_rmw_xor,
  490. a_i64_atomic_rmw32_xor_u,
  491. a_i32_atomic_rmw_xchg,
  492. a_i64_atomic_rmw32_xchg_u,
  493. a_i32_atomic_rmw_cmpxchg,
  494. a_i64_atomic_rmw32_cmpxchg_u:
  495. result:=2;
  496. a_i64_load,
  497. a_f64_load,
  498. a_i64_store,
  499. a_f64_store,
  500. a_memory_atomic_wait64,
  501. a_i64_atomic_load,
  502. a_i64_atomic_store,
  503. a_i64_atomic_rmw_add,
  504. a_i64_atomic_rmw_sub,
  505. a_i64_atomic_rmw_and,
  506. a_i64_atomic_rmw_or,
  507. a_i64_atomic_rmw_xor,
  508. a_i64_atomic_rmw_xchg,
  509. a_i64_atomic_rmw_cmpxchg:
  510. result:=3;
  511. else
  512. internalerror(2021092614);
  513. end;
  514. end;
  515. function encode_wasm_basic_type(wbt: TWasmBasicType): Byte;
  516. begin
  517. case wbt of
  518. wbt_unknown:
  519. internalerror(2024011702);
  520. wbt_i32:
  521. result:=$7F;
  522. wbt_i64:
  523. result:=$7E;
  524. wbt_f32:
  525. result:=$7D;
  526. wbt_f64:
  527. result:=$7C;
  528. wbt_funcref:
  529. result:=$70;
  530. wbt_externref:
  531. result:=$6F;
  532. wbt_v128:
  533. result:=$7B;
  534. end;
  535. end;
  536. function decode_wasm_basic_type(b: Byte; out wbt: TWasmBasicType): Boolean;
  537. begin
  538. result:=true;
  539. case b of
  540. $7F:
  541. wbt:=wbt_i32;
  542. $7E:
  543. wbt:=wbt_i64;
  544. $7D:
  545. wbt:=wbt_f32;
  546. $7C:
  547. wbt:=wbt_f64;
  548. $7B:
  549. wbt:=wbt_v128;
  550. $70:
  551. wbt:=wbt_funcref;
  552. $6F:
  553. wbt:=wbt_externref;
  554. else
  555. begin
  556. result:=false;
  557. wbt:=default(TWasmBasicType);
  558. end;
  559. end;
  560. end;
  561. function is_atomic_op(op: TAsmOp): boolean;
  562. begin
  563. result:=(op>=a_i32_atomic_load8_u) and (op<=a_atomic_fence);
  564. end;
  565. {*****************************************************************************
  566. TWasmFuncType
  567. *****************************************************************************}
  568. constructor TWasmFuncType.Create(aparams, aresults: TWasmResultType);
  569. begin
  570. inherited Create;
  571. params:=aparams;
  572. results:=aresults;
  573. end;
  574. constructor TWasmFuncType.Create(afunctype: TWasmFuncType);
  575. begin
  576. inherited Create;
  577. params:=afunctype.params;
  578. results:=afunctype.results;
  579. end;
  580. procedure TWasmFuncType.add_param(param: TWasmBasicType);
  581. var
  582. len : integer;
  583. begin
  584. len:=Length(params);
  585. SetLength(params,len+1);
  586. params[len]:=param;
  587. end;
  588. procedure TWasmFuncType.add_result(res: TWasmBasicType);
  589. begin
  590. SetLength(results,Length(results)+1);
  591. results[High(results)]:=res;
  592. end;
  593. function TWasmFuncType.Equals(Obj: TObject): boolean;
  594. var
  595. O: TWasmFuncType;
  596. begin
  597. if Obj=Self then
  598. exit(true)
  599. else if (Obj<>nil) and (Obj is TWasmFuncType) then
  600. begin
  601. O:=TWasmFuncType(Obj);
  602. if (Length(params)<>Length(O.params)) or (Length(results)<>Length(O.results)) then
  603. exit(false);
  604. if (Length(params)>0) and (CompareByte(params[0],O.params[0],Length(params)*SizeOf(params[0]))<>0) then
  605. exit(false);
  606. if (Length(results)>0) and (CompareByte(results[0],O.results[0],Length(results)*SizeOf(results[0]))<>0) then
  607. exit(false);
  608. Result:=true;
  609. end
  610. else
  611. Result:=inherited Equals(Obj);
  612. end;
  613. function TWasmFuncType.ToString: ansistring;
  614. var
  615. i: Integer;
  616. begin
  617. Result:='(';
  618. for i:=0 to high(params) do
  619. begin
  620. if i<>0 then
  621. Result:=Result+', ';
  622. Result:=Result+wasm_basic_type_str[params[i]];
  623. end;
  624. Result:=Result+') -> (';
  625. for i:=0 to high(results) do
  626. begin
  627. if i<>0 then
  628. Result:=Result+', ';
  629. Result:=Result+wasm_basic_type_str[results[i]];
  630. end;
  631. Result:=Result+')';
  632. end;
  633. end.