cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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,
  78. // exceptions
  79. a_try,a_catch,a_catch_all,a_delegate,a_throw,a_rethrow,a_end_try
  80. );
  81. TWasmBasicType = (wbt_i32, wbt_i64, wbt_f32, wbt_f64);
  82. TWasmResultType = array of TWasmBasicType;
  83. { TWasmFuncType }
  84. PWasmFuncType = ^TWasmFuncType;
  85. TWasmFuncType = class
  86. params: TWasmResultType;
  87. results: TWasmResultType;
  88. constructor Create(aparams, aresults: TWasmResultType);
  89. constructor Create(afunctype: TWasmFuncType);
  90. procedure add_param(param: TWasmBasicType);
  91. procedure add_result(res: TWasmBasicType);
  92. function Equals(Obj: TObject): boolean; override;
  93. end;
  94. {# This should define the array of instructions as string }
  95. op2strtable=array[tasmop] of string[31];
  96. Const
  97. {# First value of opcode enumeration }
  98. firstop = low(tasmop);
  99. {# Last value of opcode enumeration }
  100. lastop = high(tasmop);
  101. AsmOp_Store = [
  102. a_i32_store, a_i32_store16, a_i32_store8
  103. ,a_i64_store, a_i64_store16, a_i64_store8, a_i64_store32
  104. ,a_f32_store, a_f64_store
  105. ];
  106. AsmOp_Load = [
  107. a_i32_load,
  108. a_i32_load8_s, a_i32_load8_u,
  109. a_i32_load16_s, a_i32_load16_u,
  110. a_i64_load,
  111. a_i64_load8_s, a_i64_load8_u,
  112. a_i64_load16_s, a_i64_load16_u,
  113. a_i64_load32_s, a_i64_load32_u,
  114. a_f32_load, a_f64_load
  115. ];
  116. AsmOp_LoadStore = AsmOp_Load + AsmOp_Store;
  117. {*****************************************************************************
  118. Registers
  119. *****************************************************************************}
  120. type
  121. { Number of registers used for indexing in tables }
  122. tregisterindex=0..{$i rwasmnor.inc}-1; // no registers in wasm
  123. totherregisterset = set of tregisterindex;
  124. const
  125. { Available Superregisters }
  126. // there's no registers in wasm
  127. {$i rwasmsup.inc}
  128. { No Subregisters }
  129. R_SUBWHOLE = R_SUBNONE;
  130. { Available Registers }
  131. // there's no registers in wasm
  132. {$i rwasmcon.inc}
  133. { aliases }
  134. { used as base register in references for parameters passed to
  135. subroutines: these are passed on the evaluation stack, but this way we
  136. can use the offset field to indicate the order, which is used by ncal
  137. to sort the parameters }
  138. NR_EVAL_STACK_BASE = NR_R0;
  139. RS_EVAL_STACK_BASE = RS_R0;
  140. { used as base register in references to indicate that it's a local }
  141. NR_LOCAL_STACK_POINTER_REG = NR_R1;
  142. RS_LOCAL_STACK_POINTER_REG = RS_R1;
  143. { fake register, representing the local frame pointer. Used for accessing
  144. address-taken local variables on the linear stack: (localframeptr+offset). }
  145. NR_LOCAL_FRAME_POINTER_REG = NR_R3;
  146. RS_LOCAL_FRAME_POINTER_REG = RS_R3;
  147. maxvarregs = 1;
  148. maxfpuvarregs = 1;
  149. { Integer Super registers first and last }
  150. first_int_imreg = 4;
  151. { Float Super register first and last }
  152. first_fpu_imreg = 4;
  153. { MM Super register first and last }
  154. first_mm_imreg = 4;
  155. regnumber_table : array[tregisterindex] of tregister = (
  156. {$i rwasmnum.inc}
  157. );
  158. EVALSTACKLOCS = [LOC_REGISTER,LOC_CREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER,
  159. LOC_MMREGISTER,LOC_CMMREGISTER,LOC_SUBSETREG,LOC_CSUBSETREG];
  160. {*****************************************************************************
  161. Conditions
  162. *****************************************************************************}
  163. type
  164. // not used by wasm target
  165. TAsmCond=(C_None);
  166. {*****************************************************************************
  167. Constants
  168. *****************************************************************************}
  169. const
  170. max_operands = 2;
  171. {*****************************************************************************
  172. Default generic sizes
  173. *****************************************************************************}
  174. {$ifdef cpu64bitaddr}
  175. {# Defines the default address size for a processor,
  176. -- fake for JVM, only influences default width of
  177. arithmetic calculations }
  178. OS_ADDR = OS_64;
  179. {# the natural int size for a processor,
  180. has to match osuinttype/ossinttype as initialized in psystem }
  181. OS_INT = OS_64;
  182. OS_SINT = OS_S64;
  183. {$else}
  184. {# Defines the default address size for a processor,
  185. -- fake for wasm, only influences default width of
  186. arithmetic calculations }
  187. OS_ADDR = OS_32;
  188. {# the natural int size for a processor,
  189. has to match osuinttype/ossinttype as initialized in psystem }
  190. OS_INT = OS_32;
  191. OS_SINT = OS_S32;
  192. {$endif}
  193. {# the maximum float size for a processor, }
  194. OS_FLOAT = OS_F64;
  195. {# the size of a vector register for a processor }
  196. OS_VECTOR = OS_M128;
  197. {*****************************************************************************
  198. Generic Register names
  199. *****************************************************************************}
  200. { dummies, not used for Wasm }
  201. {# Stack pointer register }
  202. { used as base register in references to indicate that it's a local }
  203. NR_STACK_POINTER_REG = NR_R1;
  204. RS_STACK_POINTER_REG = RS_R1;
  205. {# Frame pointer register }
  206. NR_FRAME_POINTER_REG = NR_LOCAL_FRAME_POINTER_REG;
  207. RS_FRAME_POINTER_REG = RS_LOCAL_FRAME_POINTER_REG;
  208. { WebAssembly results are returned on the evaluation stack, not via a register }
  209. { Results are returned in this register (32-bit values) }
  210. NR_FUNCTION_RETURN_REG = NR_NO;
  211. RS_FUNCTION_RETURN_REG = RS_NO;
  212. { Low part of 64bit return value }
  213. NR_FUNCTION_RETURN64_LOW_REG = NR_NO;
  214. RS_FUNCTION_RETURN64_LOW_REG = RS_NO;
  215. { High part of 64bit return value }
  216. NR_FUNCTION_RETURN64_HIGH_REG = NR_NO;
  217. RS_FUNCTION_RETURN64_HIGH_REG = RS_NO;
  218. { The value returned from a function is available in this register }
  219. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  220. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  221. { The lowh part of 64bit value returned from a function }
  222. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  223. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  224. { The high part of 64bit value returned from a function }
  225. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  226. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  227. NR_FPU_RESULT_REG = NR_NO;
  228. NR_MM_RESULT_REG = NR_NO;
  229. {*****************************************************************************
  230. GCC /ABI linking information
  231. *****************************************************************************}
  232. { dummies, not used for Wasm }
  233. {# Required parameter alignment when calling a routine
  234. }
  235. std_param_align = 1;
  236. {*****************************************************************************
  237. CPU Dependent Constants
  238. *****************************************************************************}
  239. maxfpuregs = 0;
  240. { Global variable, that acts as the stack pointer in linear memory
  241. (also called the "linear stack"). This stack is used for address-taken
  242. local variables. This separate stack is needed, because the WASM
  243. implementation's runtime call stack (which includes return addresses and
  244. function parameters) is not visible in linear memory. }
  245. STACK_POINTER_SYM = '__stack_pointer';
  246. { The exception tag symbol, used for FPC exceptions }
  247. FPC_EXCEPTION_TAG_SYM = '__FPC_exception';
  248. {*****************************************************************************
  249. Helpers
  250. *****************************************************************************}
  251. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  252. function reg_cgsize(const reg: tregister) : tcgsize;
  253. function std_regnum_search(const s:string):Tregister;
  254. function std_regname(r:Tregister):string;
  255. function findreg_by_number(r:Tregister):tregisterindex;
  256. function dwarf_reg(r:tregister):byte;
  257. function dwarf_reg_no_error(r:tregister):shortint;
  258. function eh_return_data_regno(nr: longint): longint;
  259. { since we don't use tasmconds, don't call this routine
  260. (it will internalerror). We need it anyway to get aoptobj
  261. to compile (but it won't execute it).
  262. }
  263. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  264. function natural_alignment_for_load_store(op: TAsmOp): shortint;
  265. function encode_wasm_basic_type(wbt: TWasmBasicType): Byte;
  266. implementation
  267. uses
  268. verbose,
  269. rgbase;
  270. {*****************************************************************************
  271. Helpers
  272. *****************************************************************************}
  273. const
  274. std_regname_table : array[tregisterindex] of string[15] = (
  275. {$i rwasmstd.inc}
  276. );
  277. regnumber_index : array[tregisterindex] of tregisterindex = (
  278. {$i rwasmrni.inc}
  279. );
  280. std_regname_index : array[tregisterindex] of tregisterindex = (
  281. {$i rwasmsri.inc}
  282. );
  283. function reg_cgsize(const reg: tregister): tcgsize;
  284. begin
  285. result:=OS_NO;
  286. end;
  287. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  288. begin
  289. cgsize2subreg:=R_SUBNONE;
  290. end;
  291. function std_regnum_search(const s:string):Tregister;
  292. begin
  293. result:=NR_NO;
  294. end;
  295. function findreg_by_number(r:Tregister):tregisterindex;
  296. begin
  297. result:=findreg_by_number_table(r,regnumber_index);
  298. end;
  299. function std_regname(r:Tregister):string;
  300. var
  301. p : tregisterindex;
  302. begin
  303. p:=findreg_by_number_table(r,regnumber_index);
  304. if p<>0 then
  305. result:=std_regname_table[p]
  306. else
  307. result:=generic_regname(r);
  308. end;
  309. function dwarf_reg(r:tregister):byte;
  310. begin
  311. result:=0;
  312. internalerror(200603251);
  313. end;
  314. function dwarf_reg_no_error(r:tregister):shortint;
  315. begin
  316. result:=-1;
  317. end;
  318. function eh_return_data_regno(nr: longint): longint;
  319. begin
  320. result:=-1;
  321. end;
  322. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  323. begin
  324. result:=C_None;
  325. internalerror(2015082701);
  326. end;
  327. function natural_alignment_for_load_store(op: TAsmOp): shortint;
  328. begin
  329. case op of
  330. a_i32_load8_s,
  331. a_i32_load8_u,
  332. a_i64_load8_s,
  333. a_i64_load8_u,
  334. a_i32_store8,
  335. a_i64_store8:
  336. result:=0;
  337. a_i32_load16_s,
  338. a_i32_load16_u,
  339. a_i64_load16_s,
  340. a_i64_load16_u,
  341. a_i32_store16,
  342. a_i64_store16:
  343. result:=1;
  344. a_i32_load,
  345. a_f32_load,
  346. a_i64_load32_s,
  347. a_i64_load32_u,
  348. a_i32_store,
  349. a_f32_store,
  350. a_i64_store32:
  351. result:=2;
  352. a_i64_load,
  353. a_f64_load,
  354. a_i64_store,
  355. a_f64_store:
  356. result:=3;
  357. else
  358. internalerror(2021092614);
  359. end;
  360. end;
  361. function encode_wasm_basic_type(wbt: TWasmBasicType): Byte;
  362. begin
  363. case wbt of
  364. wbt_i32:
  365. result:=$7F;
  366. wbt_i64:
  367. result:=$7E;
  368. wbt_f32:
  369. result:=$7D;
  370. wbt_f64:
  371. result:=$7C;
  372. end;
  373. end;
  374. {*****************************************************************************
  375. TWasmFuncType
  376. *****************************************************************************}
  377. constructor TWasmFuncType.Create(aparams, aresults: TWasmResultType);
  378. begin
  379. inherited Create;
  380. params:=aparams;
  381. results:=aresults;
  382. end;
  383. constructor TWasmFuncType.Create(afunctype: TWasmFuncType);
  384. begin
  385. inherited Create;
  386. params:=afunctype.params;
  387. results:=afunctype.results;
  388. end;
  389. procedure TWasmFuncType.add_param(param: TWasmBasicType);
  390. begin
  391. SetLength(params,Length(params)+1);
  392. params[High(params)]:=param;
  393. end;
  394. procedure TWasmFuncType.add_result(res: TWasmBasicType);
  395. begin
  396. SetLength(results,Length(results)+1);
  397. results[High(results)]:=res;
  398. end;
  399. function TWasmFuncType.Equals(Obj: TObject): boolean;
  400. var
  401. O: TWasmFuncType;
  402. begin
  403. if Obj=Self then
  404. exit(true)
  405. else if (Obj<>nil) and (Obj is TWasmFuncType) then
  406. begin
  407. O:=TWasmFuncType(Obj);
  408. if (Length(params)<>Length(O.params)) or (Length(results)<>Length(O.results)) then
  409. exit(false);
  410. if (Length(params)>0) and (CompareByte(params[0],O.params[0],Length(params)*SizeOf(params[0]))<>0) then
  411. exit(false);
  412. if (Length(results)>0) and (CompareByte(results[0],O.results[0],Length(results)*SizeOf(results[0]))<>0) then
  413. exit(false);
  414. Result:=true;
  415. end
  416. else
  417. Result:=inherited Equals(Obj);
  418. end;
  419. end.