cpubase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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,
  32. 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_get_local, a_set_local, a_tee_local, a_get_global, a_set_global,
  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_s_i32, a_i64_extend_u_i32,
  61. a_i32_trunc_s_f32, a_i32_trunc_s_f64, a_i64_trunc_s_f32, a_i64_trunc_s_f64,
  62. a_i32_trunc_u_f32, a_i32_trunc_u_f64, a_i64_trunc_u_f32, a_i64_trunc_u_f64,
  63. a_f32_demote_f64, a_f64_promote_f32,
  64. a_f32_convert_s_i32, a_f32_convert_s_i64,a_f64_convert_s_i32,a_f64_convert_s_i64,
  65. a_f32_convert_u_i32, a_f32_convert_u_i64,a_f64_convert_u_i32,a_f64_convert_u_i64,
  66. a_i32_reinterpret_f32, a_i64_reinterpret_f64, a_f32_reinterpret_i32, a_f64_reinterpret_f64,
  67. // load/store
  68. a_i32_load, a_i64_load, a_f32_load, a_f64_load,
  69. a_i32_store, a_i64_store, a_f32_store, a_f64_store,
  70. a_i32_load8_s, a_i32_load16_s, a_i64_load8_s, a_i64_load16_s, a_i64_load32_s,
  71. a_i32_load8_u, a_i32_load16_u, a_i64_load8_u, a_i64_load16_u, a_i64_load32_u,
  72. a_i32_store8, a_i32_store16, a_i64_store8, a_i64_store16, a_i64_store32,
  73. // additional memory
  74. a_grow_memory, a_current_memory
  75. );
  76. {# This should define the array of instructions as string }
  77. op2strtable=array[tasmop] of string[8];
  78. Const
  79. {# First value of opcode enumeration }
  80. firstop = low(tasmop);
  81. {# Last value of opcode enumeration }
  82. lastop = high(tasmop);
  83. AsmOp_Store = [
  84. a_i32_store, a_i32_store16, a_i32_store8
  85. ,a_i64_store, a_i64_store16, a_i64_store8, a_i64_store32
  86. ,a_f32_store, a_f64_store
  87. ];
  88. AsmOp_Load = [
  89. a_i32_load,
  90. a_i32_load8_s, a_i32_load8_u,
  91. a_i32_load16_s, a_i32_load16_u,
  92. a_i64_load,
  93. a_i64_load8_s, a_i64_load8_u,
  94. a_i64_load16_s, a_i64_load16_u,
  95. a_i64_load32_s, a_i64_load32_u,
  96. a_f32_load, a_f64_load
  97. ];
  98. AsmOp_LoadStore = AsmOp_Load + AsmOp_Store;
  99. {*****************************************************************************
  100. Registers
  101. *****************************************************************************}
  102. type
  103. { Number of registers used for indexing in tables }
  104. tregisterindex=0..{$i rwasmnor.inc}-1; // no registers in wasm
  105. totherregisterset = set of tregisterindex;
  106. const
  107. { Available Superregisters }
  108. // there's no registers in wasm
  109. {$i rwasmsup.inc}
  110. { No Subregisters }
  111. R_SUBWHOLE = R_SUBNONE;
  112. { Available Registers }
  113. // there's no registers in wasm
  114. {$i rwasmcon.inc}
  115. { aliases }
  116. { used as base register in references for parameters passed to
  117. subroutines: these are passed on the evaluation stack, but this way we
  118. can use the offset field to indicate the order, which is used by ncal
  119. to sort the parameters }
  120. NR_EVAL_STACK_BASE = NR_R0;
  121. maxvarregs = 1;
  122. maxfpuvarregs = 1;
  123. { Integer Super registers first and last }
  124. first_int_imreg = 2;
  125. { Float Super register first and last }
  126. first_fpu_imreg = 2;
  127. { MM Super register first and last }
  128. first_mm_imreg = 2;
  129. regnumber_table : array[tregisterindex] of tregister = (
  130. {$i rwasmnum.inc}
  131. );
  132. EVALSTACKLOCS = [LOC_REGISTER,LOC_CREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER,
  133. LOC_MMREGISTER,LOC_CMMREGISTER,LOC_SUBSETREG,LOC_CSUBSETREG];
  134. {*****************************************************************************
  135. References
  136. *****************************************************************************}
  137. type
  138. { array reference types }
  139. tarrayreftype = (art_none,art_indexreg,art_indexref,art_indexconst);
  140. {*****************************************************************************
  141. Conditions
  142. *****************************************************************************}
  143. type
  144. // not used by wasm target
  145. TAsmCond=(C_None);
  146. {*****************************************************************************
  147. Constants
  148. *****************************************************************************}
  149. const
  150. max_operands = 2;
  151. {*****************************************************************************
  152. Default generic sizes
  153. *****************************************************************************}
  154. {$ifdef cpu64bitaddr}
  155. {# Defines the default address size for a processor,
  156. -- fake for JVM, only influences default width of
  157. arithmetic calculations }
  158. OS_ADDR = OS_64;
  159. {# the natural int size for a processor,
  160. has to match osuinttype/ossinttype as initialized in psystem }
  161. OS_INT = OS_64;
  162. OS_SINT = OS_S64;
  163. {$else}
  164. {# Defines the default address size for a processor,
  165. -- fake for wasm, only influences default width of
  166. arithmetic calculations }
  167. OS_ADDR = OS_32;
  168. {# the natural int size for a processor,
  169. has to match osuinttype/ossinttype as initialized in psystem }
  170. OS_INT = OS_32;
  171. OS_SINT = OS_S32;
  172. {$endif}
  173. {# the maximum float size for a processor, }
  174. OS_FLOAT = OS_F64;
  175. {# the size of a vector register for a processor }
  176. OS_VECTOR = OS_M128;
  177. {*****************************************************************************
  178. Generic Register names
  179. *****************************************************************************}
  180. { dummies, not used for Wasm }
  181. {# Stack pointer register }
  182. { used as base register in references to indicate that it's a local }
  183. NR_STACK_POINTER_REG = NR_R1;
  184. RS_STACK_POINTER_REG = RS_R1;
  185. {# Frame pointer register }
  186. NR_FRAME_POINTER_REG = NR_STACK_POINTER_REG;
  187. RS_FRAME_POINTER_REG = RS_STACK_POINTER_REG;
  188. { WebAssembly results are returned on the evaluation stack, not via a register }
  189. { Results are returned in this register (32-bit values) }
  190. NR_FUNCTION_RETURN_REG = NR_NO;
  191. RS_FUNCTION_RETURN_REG = RS_NO;
  192. { Low part of 64bit return value }
  193. NR_FUNCTION_RETURN64_LOW_REG = NR_NO;
  194. RS_FUNCTION_RETURN64_LOW_REG = RS_NO;
  195. { High part of 64bit return value }
  196. NR_FUNCTION_RETURN64_HIGH_REG = NR_NO;
  197. RS_FUNCTION_RETURN64_HIGH_REG = RS_NO;
  198. { The value returned from a function is available in this register }
  199. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  200. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  201. { The lowh part of 64bit value returned from a function }
  202. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  203. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  204. { The high part of 64bit value returned from a function }
  205. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  206. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  207. NR_FPU_RESULT_REG = NR_NO;
  208. NR_MM_RESULT_REG = NR_NO;
  209. {*****************************************************************************
  210. GCC /ABI linking information
  211. *****************************************************************************}
  212. { dummies, not used for Wasm }
  213. {# Required parameter alignment when calling a routine
  214. }
  215. std_param_align = 1;
  216. {*****************************************************************************
  217. CPU Dependent Constants
  218. *****************************************************************************}
  219. maxfpuregs = 0;
  220. {*****************************************************************************
  221. Helpers
  222. *****************************************************************************}
  223. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  224. function reg_cgsize(const reg: tregister) : tcgsize;
  225. function std_regnum_search(const s:string):Tregister;
  226. function std_regname(r:Tregister):string;
  227. function findreg_by_number(r:Tregister):tregisterindex;
  228. function eh_return_data_regno(nr: longint): longint;
  229. { since we don't use tasmconds, don't call this routine
  230. (it will internalerror). We need it anyway to get aoptobj
  231. to compile (but it won't execute it).
  232. }
  233. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  234. implementation
  235. uses
  236. verbose,
  237. rgbase;
  238. {*****************************************************************************
  239. Helpers
  240. *****************************************************************************}
  241. const
  242. std_regname_table : array[tregisterindex] of string[15] = (
  243. {$i rwasmstd.inc}
  244. );
  245. regnumber_index : array[tregisterindex] of tregisterindex = (
  246. {$i rwasmrni.inc}
  247. );
  248. std_regname_index : array[tregisterindex] of tregisterindex = (
  249. {$i rwasmsri.inc}
  250. );
  251. function reg_cgsize(const reg: tregister): tcgsize;
  252. begin
  253. result:=OS_NO;
  254. end;
  255. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  256. begin
  257. cgsize2subreg:=R_SUBNONE;
  258. end;
  259. function std_regnum_search(const s:string):Tregister;
  260. begin
  261. result:=NR_NO;
  262. end;
  263. function findreg_by_number(r:Tregister):tregisterindex;
  264. begin
  265. result:=findreg_by_number_table(r,regnumber_index);
  266. end;
  267. function std_regname(r:Tregister):string;
  268. var
  269. p : tregisterindex;
  270. begin
  271. p:=findreg_by_number_table(r,regnumber_index);
  272. if p<>0 then
  273. result:=std_regname_table[p]
  274. else
  275. result:=generic_regname(r);
  276. end;
  277. function eh_return_data_regno(nr: longint): longint;
  278. begin
  279. result:=-1;
  280. end;
  281. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  282. begin
  283. result:=C_None;
  284. internalerror(2015082701);
  285. end;
  286. end.