aasmcpu.pas 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. {
  2. Copyright (c) 2019 by Free Pascal and Lazarus foundation
  3. Contains the assembler object 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. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,verbose,
  23. aasmbase,aasmtai,aasmdata,aasmsym,
  24. cgbase,cgutils,cpubase,cpuinfo,ogbase,
  25. widestr;
  26. { fake, there are no "mov reg,reg" instructions here }
  27. const
  28. { "mov reg,reg" source operand number }
  29. O_MOV_SOURCE = 0;
  30. { "mov reg,reg" source operand number }
  31. O_MOV_DEST = 0;
  32. type
  33. { taicpu }
  34. taicpu = class(tai_cpu_abstract_sym)
  35. constructor op_none(op : tasmop);
  36. constructor op_reg(op : tasmop;_op1 : tregister);
  37. constructor op_const(op : tasmop;_op1 : aint);
  38. constructor op_ref(op : tasmop;const _op1 : treference);
  39. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  40. constructor op_sym_const(op : tasmop;_op1 : tasmsymbol;_op2 : aint);
  41. constructor op_single(op : tasmop;_op1 : single);
  42. constructor op_double(op : tasmop;_op1 : double);
  43. constructor op_functype(op : tasmop; _op1: TWasmFuncType);
  44. procedure loadfunctype(opidx:longint;ft:TWasmFuncType);
  45. procedure loadsingle(opidx:longint;f:single);
  46. procedure loaddouble(opidx:longint;d:double);
  47. { register allocation }
  48. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  49. { register spilling code }
  50. function spilling_get_operation_type(opnr: longint): topertype;override;
  51. function Pass1(objdata:TObjData):longint;override;
  52. procedure Pass2(objdata:TObjData);override;
  53. end;
  54. tai_align = class(tai_align_abstract)
  55. { nothing to add }
  56. end;
  57. TImpExpType= (
  58. ie_Func, // functions
  59. ie_Table, // tables (arrays of methods)
  60. ie_Memory, // memory reference
  61. ie_Global // global variables
  62. );
  63. { tai_export_name }
  64. tai_export_name = class(tai)
  65. extname : ansistring; // external name
  66. intname : ansistring; // internal name
  67. symstype: TImpExpType;
  68. constructor create(const aextname, aintname: ansistring; asymtype: timpexptype);
  69. end;
  70. // local variable declaration
  71. { tai_local }
  72. tai_local = class(tai)
  73. bastyp: TWasmBasicType;
  74. name : string;
  75. first: boolean;
  76. last: boolean;
  77. constructor create(abasictype: TWasmBasicType; const aname: string = '');
  78. end;
  79. { tai_globaltype }
  80. tai_globaltype = class(tai)
  81. globalname: string;
  82. gtype: TWasmBasicType;
  83. immutable: boolean;
  84. constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean);
  85. end;
  86. { tai_functype }
  87. tai_functype = class(tai)
  88. funcname: string;
  89. functype: TWasmFuncType;
  90. constructor create(const afuncname: string; afunctype: TWasmFuncType);
  91. destructor destroy;override;
  92. end;
  93. { tai_tagtype }
  94. tai_tagtype = class(tai)
  95. tagname: string;
  96. params: TWasmResultType;
  97. constructor create(const atagname: string; aparams: TWasmResultType);
  98. end;
  99. { tai_import_module }
  100. tai_import_module = class(tai)
  101. symname: string;
  102. importmodule: string;
  103. constructor create(const asymname, aimportmodule: string);
  104. end;
  105. { tai_import_name }
  106. tai_import_name = class(tai)
  107. symname: string;
  108. importname: string;
  109. constructor create(const asymname, aimportname: string);
  110. end;
  111. procedure InitAsm;
  112. procedure DoneAsm;
  113. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  114. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  115. implementation
  116. uses
  117. ogwasm;
  118. { tai_globaltype }
  119. constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
  120. begin
  121. inherited Create;
  122. typ:=ait_globaltype;
  123. globalname:=aglobalname;
  124. gtype:=atype;
  125. immutable:=aimmutable;
  126. end;
  127. { tai_import_name }
  128. constructor tai_import_name.create(const asymname, aimportname: string);
  129. begin
  130. inherited Create;
  131. typ:=ait_import_name;
  132. symname:=asymname;
  133. importname:=aimportname;
  134. end;
  135. { tai_import_module }
  136. constructor tai_import_module.create(const asymname, aimportmodule: string);
  137. begin
  138. inherited Create;
  139. typ:=ait_import_module;
  140. symname:=asymname;
  141. importmodule:=aimportmodule;
  142. end;
  143. { tai_functype }
  144. constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType);
  145. begin
  146. inherited Create;
  147. typ:=ait_functype;
  148. funcname:=afuncname;
  149. functype:=afunctype;
  150. end;
  151. destructor tai_functype.destroy;
  152. begin
  153. functype.free;
  154. inherited;
  155. end;
  156. { tai_tagtype }
  157. constructor tai_tagtype.create(const atagname: string; aparams: TWasmResultType);
  158. begin
  159. typ:=ait_tagtype;
  160. tagname:=atagname;
  161. params:=aparams;
  162. end;
  163. { tai_local }
  164. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  165. begin
  166. inherited Create;
  167. bastyp := abasictype;
  168. typ := ait_local;
  169. name := aname;
  170. end;
  171. { timpexp_ai }
  172. constructor tai_export_name.create(const aextname, aintname: ansistring;
  173. asymtype: timpexptype);
  174. begin
  175. inherited create;
  176. typ := ait_export_name;
  177. extname := aextname;
  178. intname := aintname;
  179. symstype:= asymtype;
  180. end;
  181. {*****************************************************************************
  182. taicpu Constructors
  183. *****************************************************************************}
  184. constructor taicpu.op_none(op : tasmop);
  185. begin
  186. inherited create(op);
  187. end;
  188. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  189. begin
  190. inherited create(op);
  191. ops:=1;
  192. {$ifdef EXTDEBUG}
  193. if getregtype(_op1)=R_INVALIDREGISTER then
  194. InternalError(2021011901);
  195. {$endif EXTDEBUG}
  196. loadreg(0,_op1);
  197. end;
  198. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  199. begin
  200. inherited create(op);
  201. ops:=1;
  202. loadref(0,_op1);
  203. if op in [a_local_get,a_local_set,a_local_tee] then
  204. begin
  205. if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
  206. internalerror(2021010201);
  207. end
  208. else
  209. begin
  210. if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
  211. internalerror(2021010202);
  212. end;
  213. end;
  214. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  215. begin
  216. inherited create(op);
  217. ops:=1;
  218. loadconst(0,_op1);
  219. end;
  220. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  221. begin
  222. inherited create(op);
  223. ops:=1;
  224. loadsymbol(0,_op1,0);
  225. end;
  226. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  227. begin
  228. inherited create(op);
  229. ops:=2;
  230. loadsymbol(0,_op1,0);
  231. loadconst(1,_op2);
  232. end;
  233. constructor taicpu.op_single(op: tasmop; _op1: single);
  234. begin
  235. inherited create(op);
  236. ops:=1;
  237. loadsingle(0,_op1);
  238. end;
  239. constructor taicpu.op_double(op: tasmop; _op1: double);
  240. begin
  241. inherited create(op);
  242. ops:=1;
  243. loaddouble(0,_op1);
  244. end;
  245. constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
  246. begin
  247. inherited create(op);
  248. ops:=1;
  249. loadfunctype(0,_op1);
  250. end;
  251. procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
  252. begin
  253. allocate_oper(opidx+1);
  254. with oper[opidx]^ do
  255. begin
  256. if typ<>top_functype then
  257. clearop(opidx);
  258. functype:=ft;
  259. typ:=top_functype;
  260. end;
  261. end;
  262. procedure taicpu.loadsingle(opidx:longint;f:single);
  263. begin
  264. allocate_oper(opidx+1);
  265. with oper[opidx]^ do
  266. begin
  267. if typ<>top_single then
  268. clearop(opidx);
  269. sval:=f;
  270. typ:=top_single;
  271. end;
  272. end;
  273. procedure taicpu.loaddouble(opidx: longint; d: double);
  274. begin
  275. allocate_oper(opidx+1);
  276. with oper[opidx]^ do
  277. begin
  278. if typ<>top_double then
  279. clearop(opidx);
  280. dval:=d;
  281. typ:=top_double;
  282. end;
  283. end;
  284. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  285. begin
  286. result:=false;
  287. end;
  288. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  289. begin
  290. if opcode in AsmOp_Store then
  291. result:=operand_write
  292. else
  293. result:=operand_read;
  294. end;
  295. function taicpu.Pass1(objdata: TObjData): longint;
  296. function SlebSize(v: tcgint): longint;
  297. begin
  298. result:=0;
  299. repeat
  300. v:=SarInt64(v,7);
  301. Inc(result);
  302. until ((v=0) and ((byte(v) and 64)=0)) or ((v=-1) and ((byte(v) and 64)<>0));
  303. end;
  304. function UlebSize(v: tcgint): longint;
  305. begin
  306. result:=0;
  307. repeat
  308. v:=v shr 7;
  309. Inc(result);
  310. until v=0;
  311. end;
  312. begin
  313. result:=0;
  314. case opcode of
  315. a_unreachable,
  316. a_nop,
  317. a_return,
  318. a_drop,
  319. a_i32_eqz,
  320. a_i32_eq,
  321. a_i32_ne,
  322. a_i32_lt_s,
  323. a_i32_lt_u,
  324. a_i32_gt_s,
  325. a_i32_gt_u,
  326. a_i32_le_s,
  327. a_i32_le_u,
  328. a_i32_ge_s,
  329. a_i32_ge_u,
  330. a_i64_eqz,
  331. a_i64_eq,
  332. a_i64_ne,
  333. a_i64_lt_s,
  334. a_i64_lt_u,
  335. a_i64_gt_s,
  336. a_i64_gt_u,
  337. a_i64_le_s,
  338. a_i64_le_u,
  339. a_i64_ge_s,
  340. a_i64_ge_u,
  341. a_f32_eq,
  342. a_f32_ne,
  343. a_f32_lt,
  344. a_f32_gt,
  345. a_f32_le,
  346. a_f32_ge,
  347. a_f64_eq,
  348. a_f64_ne,
  349. a_f64_lt,
  350. a_f64_gt,
  351. a_f64_le,
  352. a_f64_ge,
  353. a_i32_clz,
  354. a_i32_ctz,
  355. a_i32_popcnt,
  356. a_i32_add,
  357. a_i32_sub,
  358. a_i32_mul,
  359. a_i32_div_s,
  360. a_i32_div_u,
  361. a_i32_rem_s,
  362. a_i32_rem_u,
  363. a_i32_and,
  364. a_i32_or,
  365. a_i32_xor,
  366. a_i32_shl,
  367. a_i32_shr_s,
  368. a_i32_shr_u,
  369. a_i32_rotl,
  370. a_i32_rotr,
  371. a_i64_clz,
  372. a_i64_ctz,
  373. a_i64_popcnt,
  374. a_i64_add,
  375. a_i64_sub,
  376. a_i64_mul,
  377. a_i64_div_s,
  378. a_i64_div_u,
  379. a_i64_rem_s,
  380. a_i64_rem_u,
  381. a_i64_and,
  382. a_i64_or,
  383. a_i64_xor,
  384. a_i64_shl,
  385. a_i64_shr_s,
  386. a_i64_shr_u,
  387. a_i64_rotl,
  388. a_i64_rotr,
  389. a_f32_abs,
  390. a_f32_neg,
  391. a_f32_ceil,
  392. a_f32_floor,
  393. a_f32_trunc,
  394. a_f32_nearest,
  395. a_f32_sqrt,
  396. a_f32_add,
  397. a_f32_sub,
  398. a_f32_mul,
  399. a_f32_div,
  400. a_f32_min,
  401. a_f32_max,
  402. a_f32_copysign,
  403. a_f64_abs,
  404. a_f64_neg,
  405. a_f64_ceil,
  406. a_f64_floor,
  407. a_f64_trunc,
  408. a_f64_nearest,
  409. a_f64_sqrt,
  410. a_f64_add,
  411. a_f64_sub,
  412. a_f64_mul,
  413. a_f64_div,
  414. a_f64_min,
  415. a_f64_max,
  416. a_f64_copysign,
  417. a_i32_wrap_i64,
  418. a_i32_trunc_f32_s,
  419. a_i32_trunc_f32_u,
  420. a_i32_trunc_f64_s,
  421. a_i32_trunc_f64_u,
  422. a_i64_extend_i32_s,
  423. a_i64_extend_i32_u,
  424. a_i64_trunc_f32_s,
  425. a_i64_trunc_f32_u,
  426. a_i64_trunc_f64_s,
  427. a_i64_trunc_f64_u,
  428. a_f32_convert_i32_s,
  429. a_f32_convert_i32_u,
  430. a_f32_convert_i64_s,
  431. a_f32_convert_i64_u,
  432. a_f32_demote_f64,
  433. a_f64_convert_i32_s,
  434. a_f64_convert_i32_u,
  435. a_f64_convert_i64_s,
  436. a_f64_convert_i64_u,
  437. a_f64_promote_f32,
  438. a_i32_reinterpret_f32,
  439. a_i64_reinterpret_f64,
  440. a_f32_reinterpret_i32,
  441. a_f64_reinterpret_i64,
  442. a_i32_extend8_s,
  443. a_i32_extend16_s,
  444. a_i64_extend8_s,
  445. a_i64_extend16_s,
  446. a_i64_extend32_s,
  447. a_else,
  448. a_end_block,
  449. a_end_if,
  450. a_end_loop,
  451. a_end_try,
  452. a_catch_all:
  453. result:=1;
  454. a_i32_trunc_sat_f32_s,
  455. a_i32_trunc_sat_f32_u,
  456. a_i32_trunc_sat_f64_s,
  457. a_i32_trunc_sat_f64_u,
  458. a_i64_trunc_sat_f32_s,
  459. a_i64_trunc_sat_f32_u,
  460. a_i64_trunc_sat_f64_s,
  461. a_i64_trunc_sat_f64_u,
  462. a_memory_size,
  463. a_memory_grow:
  464. result:=2;
  465. a_memory_copy:
  466. result:=4;
  467. a_memory_fill,
  468. a_atomic_fence:
  469. result:=3;
  470. a_i32_const:
  471. begin
  472. if ops<>1 then
  473. internalerror(2021092001);
  474. with oper[0]^ do
  475. case typ of
  476. top_ref:
  477. begin
  478. if assigned(ref^.symbol) then
  479. result:=6
  480. else
  481. begin
  482. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  483. internalerror(2021092018);
  484. result:=1+SlebSize(longint(ref^.offset));
  485. end;
  486. end;
  487. top_const:
  488. result:=1+SlebSize(longint(val));
  489. else
  490. internalerror(2021092615);
  491. end;
  492. end;
  493. a_i64_const:
  494. begin
  495. if ops<>1 then
  496. internalerror(2021092001);
  497. with oper[0]^ do
  498. case typ of
  499. top_ref:
  500. begin
  501. if assigned(ref^.symbol) then
  502. result:=6
  503. else
  504. begin
  505. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  506. internalerror(2021092018);
  507. result:=1+SlebSize(int64(ref^.offset));
  508. end;
  509. end;
  510. top_const:
  511. result:=1+SlebSize(int64(val));
  512. else
  513. internalerror(2021092615);
  514. end;
  515. end;
  516. a_f32_const:
  517. result:=5;
  518. a_f64_const:
  519. result:=9;
  520. a_local_get,
  521. a_local_set,
  522. a_local_tee:
  523. begin
  524. if ops<>1 then
  525. internalerror(2021092001);
  526. with oper[0]^ do
  527. case typ of
  528. top_ref:
  529. begin
  530. if assigned(ref^.symbol) then
  531. internalerror(2021092005);
  532. if ref^.base<>NR_STACK_POINTER_REG then
  533. internalerror(2021092006);
  534. if ref^.index<>NR_NO then
  535. internalerror(2021092007);
  536. result:=1+UlebSize(ref^.offset);
  537. end;
  538. else
  539. internalerror(2021092008);
  540. end;
  541. end;
  542. a_global_get,
  543. a_global_set:
  544. begin
  545. if ops<>1 then
  546. internalerror(2021092010);
  547. with oper[0]^ do
  548. case typ of
  549. top_ref:
  550. begin
  551. if not assigned(ref^.symbol) then
  552. internalerror(2021092012);
  553. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  554. internalerror(2021092013);
  555. result:=6;
  556. end;
  557. else
  558. internalerror(2021092011);
  559. end;
  560. end;
  561. a_end_function:
  562. result:=0;
  563. a_block,
  564. a_loop,
  565. a_if,
  566. a_try:
  567. begin
  568. if ops=0 then
  569. result:=2
  570. else
  571. begin
  572. if ops<>1 then
  573. internalerror(2021092015);
  574. with oper[0]^ do
  575. case typ of
  576. top_functype:
  577. begin
  578. if (length(functype.params)=0) and (length(functype.results)<=1) then
  579. result:=2
  580. else
  581. { more complex blocktypes are not yet implemented }
  582. internalerror(2021092621);
  583. end;
  584. else
  585. internalerror(2021092620);
  586. end;
  587. end;
  588. end;
  589. a_i32_load,
  590. a_i64_load,
  591. a_f32_load,
  592. a_f64_load,
  593. a_i32_load8_s,
  594. a_i32_load8_u,
  595. a_i32_load16_s,
  596. a_i32_load16_u,
  597. a_i64_load8_s,
  598. a_i64_load8_u,
  599. a_i64_load16_s,
  600. a_i64_load16_u,
  601. a_i64_load32_s,
  602. a_i64_load32_u,
  603. a_i32_store,
  604. a_i64_store,
  605. a_f32_store,
  606. a_f64_store,
  607. a_i32_store8,
  608. a_i32_store16,
  609. a_i64_store8,
  610. a_i64_store16,
  611. a_i64_store32:
  612. begin
  613. if ops<>1 then
  614. internalerror(2021092016);
  615. with oper[0]^ do
  616. case typ of
  617. top_ref:
  618. begin
  619. if assigned(ref^.symbol) then
  620. begin
  621. Result:=1+
  622. UlebSize(natural_alignment_for_load_store(opcode))+
  623. 5; { relocation, fixed size = 5 bytes }
  624. end
  625. else
  626. begin
  627. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  628. internalerror(2021092018);
  629. Result:=1+
  630. UlebSize(natural_alignment_for_load_store(opcode))+
  631. UlebSize(ref^.offset);
  632. end;
  633. end;
  634. top_const:
  635. begin
  636. Result:=1+
  637. UlebSize(natural_alignment_for_load_store(opcode))+
  638. UlebSize(val);
  639. end;
  640. else
  641. internalerror(2021092017);
  642. end;
  643. end;
  644. a_memory_atomic_notify,
  645. a_memory_atomic_wait32,
  646. a_memory_atomic_wait64,
  647. a_i32_atomic_load,
  648. a_i64_atomic_load,
  649. a_i32_atomic_load8_u,
  650. a_i32_atomic_load16_u,
  651. a_i64_atomic_load8_u,
  652. a_i64_atomic_load16_u,
  653. a_i64_atomic_load32_u,
  654. a_i32_atomic_store,
  655. a_i64_atomic_store,
  656. a_i32_atomic_store8,
  657. a_i32_atomic_store16,
  658. a_i64_atomic_store8,
  659. a_i64_atomic_store16,
  660. a_i64_atomic_store32,
  661. a_i32_atomic_rmw_add,
  662. a_i64_atomic_rmw_add,
  663. a_i32_atomic_rmw8_add_u,
  664. a_i32_atomic_rmw16_add_u,
  665. a_i64_atomic_rmw8_add_u,
  666. a_i64_atomic_rmw16_add_u,
  667. a_i64_atomic_rmw32_add_u,
  668. a_i32_atomic_rmw_sub,
  669. a_i64_atomic_rmw_sub,
  670. a_i32_atomic_rmw8_sub_u,
  671. a_i32_atomic_rmw16_sub_u,
  672. a_i64_atomic_rmw8_sub_u,
  673. a_i64_atomic_rmw16_sub_u,
  674. a_i64_atomic_rmw32_sub_u,
  675. a_i32_atomic_rmw_and,
  676. a_i64_atomic_rmw_and,
  677. a_i32_atomic_rmw8_and_u,
  678. a_i32_atomic_rmw16_and_u,
  679. a_i64_atomic_rmw8_and_u,
  680. a_i64_atomic_rmw16_and_u,
  681. a_i64_atomic_rmw32_and_u,
  682. a_i32_atomic_rmw_or,
  683. a_i64_atomic_rmw_or,
  684. a_i32_atomic_rmw8_or_u,
  685. a_i32_atomic_rmw16_or_u,
  686. a_i64_atomic_rmw8_or_u,
  687. a_i64_atomic_rmw16_or_u,
  688. a_i64_atomic_rmw32_or_u,
  689. a_i32_atomic_rmw_xor,
  690. a_i64_atomic_rmw_xor,
  691. a_i32_atomic_rmw8_xor_u,
  692. a_i32_atomic_rmw16_xor_u,
  693. a_i64_atomic_rmw8_xor_u,
  694. a_i64_atomic_rmw16_xor_u,
  695. a_i64_atomic_rmw32_xor_u,
  696. a_i32_atomic_rmw_xchg,
  697. a_i64_atomic_rmw_xchg,
  698. a_i32_atomic_rmw8_xchg_u,
  699. a_i32_atomic_rmw16_xchg_u,
  700. a_i64_atomic_rmw8_xchg_u,
  701. a_i64_atomic_rmw16_xchg_u,
  702. a_i64_atomic_rmw32_xchg_u,
  703. a_i32_atomic_rmw_cmpxchg,
  704. a_i64_atomic_rmw_cmpxchg,
  705. a_i32_atomic_rmw8_cmpxchg_u,
  706. a_i32_atomic_rmw16_cmpxchg_u,
  707. a_i64_atomic_rmw8_cmpxchg_u,
  708. a_i64_atomic_rmw16_cmpxchg_u,
  709. a_i64_atomic_rmw32_cmpxchg_u:
  710. begin
  711. if ops<>1 then
  712. internalerror(2021092016);
  713. with oper[0]^ do
  714. case typ of
  715. top_ref:
  716. begin
  717. if assigned(ref^.symbol) then
  718. begin
  719. Result:=2+
  720. UlebSize(natural_alignment_for_load_store(opcode))+
  721. 5; { relocation, fixed size = 5 bytes }
  722. end
  723. else
  724. begin
  725. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  726. internalerror(2021092018);
  727. Result:=2+
  728. UlebSize(natural_alignment_for_load_store(opcode))+
  729. UlebSize(ref^.offset);
  730. end;
  731. end;
  732. top_const:
  733. begin
  734. Result:=2+
  735. UlebSize(natural_alignment_for_load_store(opcode))+
  736. UlebSize(val);
  737. end;
  738. else
  739. internalerror(2021092017);
  740. end;
  741. end;
  742. a_call:
  743. begin
  744. if ops<>1 then
  745. internalerror(2021092021);
  746. with oper[0]^ do
  747. case typ of
  748. top_ref:
  749. begin
  750. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  751. internalerror(2021092023);
  752. result:=6;
  753. end;
  754. else
  755. internalerror(2021092022);
  756. end;
  757. end;
  758. a_call_indirect:
  759. begin
  760. if ops<>1 then
  761. internalerror(2021092610);
  762. with oper[0]^ do
  763. case typ of
  764. top_functype:
  765. begin
  766. TWasmObjData(objdata).AddFuncType(functype);
  767. result:=6+
  768. UlebSize(0);
  769. end;
  770. else
  771. internalerror(2021092611);
  772. end;
  773. end;
  774. a_br,
  775. a_br_if,
  776. a_rethrow,
  777. a_delegate:
  778. begin
  779. if ops<>1 then
  780. internalerror(2021092610);
  781. with oper[0]^ do
  782. case typ of
  783. top_const:
  784. result:=1+
  785. UlebSize(val);
  786. else
  787. internalerror(2021092625);
  788. end;
  789. end;
  790. a_catch,
  791. a_throw:
  792. begin
  793. if ops<>1 then
  794. internalerror(2021092709);
  795. with oper[0]^ do
  796. case typ of
  797. top_ref:
  798. begin
  799. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  800. internalerror(2021092711);
  801. result:=6;
  802. end;
  803. else
  804. internalerror(2021092710);
  805. end;
  806. end;
  807. a_memory_init:
  808. begin
  809. if ops<>1 then
  810. internalerror(2022052802);
  811. with oper[0]^ do
  812. case typ of
  813. top_const:
  814. result:=3+UlebSize(val);
  815. else
  816. internalerror(2022052803);
  817. end;
  818. end;
  819. a_data_drop:
  820. begin
  821. if ops<>1 then
  822. internalerror(2022052804);
  823. with oper[0]^ do
  824. case typ of
  825. top_const:
  826. result:=2+UlebSize(val);
  827. else
  828. internalerror(2022052805);
  829. end;
  830. end;
  831. else
  832. internalerror(2021092623);
  833. end;
  834. end;
  835. procedure taicpu.Pass2(objdata: TObjData);
  836. procedure WriteByte(b: byte);
  837. begin
  838. objdata.writebytes(b,1);
  839. end;
  840. {$ifdef FPC_LITTLE_ENDIAN}
  841. procedure WriteSingle(s: single);
  842. begin
  843. objdata.writebytes(s,4);
  844. end;
  845. procedure WriteDouble(d: double);
  846. begin
  847. objdata.writebytes(d,8);
  848. end;
  849. {$else FPC_LITTLE_ENDIAN}
  850. procedure WriteSingle(s: single);
  851. var
  852. l: longword;
  853. begin
  854. Move(s,l,4);
  855. l:=SwapEndian(l);
  856. objdata.writebytes(l,4);
  857. end;
  858. procedure WriteDouble(d: double);
  859. var
  860. q: qword;
  861. begin
  862. Move(d,q,8);
  863. q:=SwapEndian(q);
  864. objdata.writebytes(q,8);
  865. end;
  866. {$endif FPC_LITTLE_ENDIAN}
  867. procedure WriteSleb(v: tcgint);
  868. var
  869. b: byte;
  870. Done: Boolean=false;
  871. begin
  872. repeat
  873. b:=byte(v) and 127;
  874. v:=SarInt64(v,7);
  875. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  876. Done:=true
  877. else
  878. b:=b or 128;
  879. objdata.writebytes(b,1);
  880. until Done;
  881. end;
  882. procedure WriteUleb(v: tcgint);
  883. var
  884. b: byte;
  885. begin
  886. repeat
  887. b:=byte(v) and 127;
  888. v:=v shr 7;
  889. if v<>0 then
  890. b:=b or 128;
  891. objdata.writebytes(b,1);
  892. until v=0;
  893. end;
  894. begin
  895. case opcode of
  896. a_unreachable:
  897. WriteByte($00);
  898. a_nop:
  899. WriteByte($01);
  900. a_return:
  901. WriteByte($0F);
  902. a_drop:
  903. WriteByte($1A);
  904. a_memory_size:
  905. begin
  906. WriteByte($3F);
  907. WriteByte($00);
  908. end;
  909. a_memory_grow:
  910. begin
  911. WriteByte($40);
  912. WriteByte($00);
  913. end;
  914. a_memory_copy:
  915. begin
  916. WriteByte($FC);
  917. WriteUleb(10);
  918. WriteByte($00);
  919. WriteByte($00);
  920. end;
  921. a_memory_fill:
  922. begin
  923. WriteByte($FC);
  924. WriteUleb(11);
  925. WriteByte($00);
  926. end;
  927. a_atomic_fence:
  928. begin
  929. WriteByte($FE);
  930. WriteByte($03);
  931. WriteByte($00);
  932. end;
  933. a_i32_eqz:
  934. WriteByte($45);
  935. a_i32_eq:
  936. WriteByte($46);
  937. a_i32_ne:
  938. WriteByte($47);
  939. a_i32_lt_s:
  940. WriteByte($48);
  941. a_i32_lt_u:
  942. WriteByte($49);
  943. a_i32_gt_s:
  944. WriteByte($4A);
  945. a_i32_gt_u:
  946. WriteByte($4B);
  947. a_i32_le_s:
  948. WriteByte($4C);
  949. a_i32_le_u:
  950. WriteByte($4D);
  951. a_i32_ge_s:
  952. WriteByte($4E);
  953. a_i32_ge_u:
  954. WriteByte($4F);
  955. a_i64_eqz:
  956. WriteByte($50);
  957. a_i64_eq:
  958. WriteByte($51);
  959. a_i64_ne:
  960. WriteByte($52);
  961. a_i64_lt_s:
  962. WriteByte($53);
  963. a_i64_lt_u:
  964. WriteByte($54);
  965. a_i64_gt_s:
  966. WriteByte($55);
  967. a_i64_gt_u:
  968. WriteByte($56);
  969. a_i64_le_s:
  970. WriteByte($57);
  971. a_i64_le_u:
  972. WriteByte($58);
  973. a_i64_ge_s:
  974. WriteByte($59);
  975. a_i64_ge_u:
  976. WriteByte($5A);
  977. a_f32_eq:
  978. WriteByte($5B);
  979. a_f32_ne:
  980. WriteByte($5C);
  981. a_f32_lt:
  982. WriteByte($5D);
  983. a_f32_gt:
  984. WriteByte($5E);
  985. a_f32_le:
  986. WriteByte($5F);
  987. a_f32_ge:
  988. WriteByte($60);
  989. a_f64_eq:
  990. WriteByte($61);
  991. a_f64_ne:
  992. WriteByte($62);
  993. a_f64_lt:
  994. WriteByte($63);
  995. a_f64_gt:
  996. WriteByte($64);
  997. a_f64_le:
  998. WriteByte($65);
  999. a_f64_ge:
  1000. WriteByte($66);
  1001. a_i32_clz:
  1002. WriteByte($67);
  1003. a_i32_ctz:
  1004. WriteByte($68);
  1005. a_i32_popcnt:
  1006. WriteByte($69);
  1007. a_i32_add:
  1008. WriteByte($6A);
  1009. a_i32_sub:
  1010. WriteByte($6B);
  1011. a_i32_mul:
  1012. WriteByte($6C);
  1013. a_i32_div_s:
  1014. WriteByte($6D);
  1015. a_i32_div_u:
  1016. WriteByte($6E);
  1017. a_i32_rem_s:
  1018. WriteByte($6F);
  1019. a_i32_rem_u:
  1020. WriteByte($70);
  1021. a_i32_and:
  1022. WriteByte($71);
  1023. a_i32_or:
  1024. WriteByte($72);
  1025. a_i32_xor:
  1026. WriteByte($73);
  1027. a_i32_shl:
  1028. WriteByte($74);
  1029. a_i32_shr_s:
  1030. WriteByte($75);
  1031. a_i32_shr_u:
  1032. WriteByte($76);
  1033. a_i32_rotl:
  1034. WriteByte($77);
  1035. a_i32_rotr:
  1036. WriteByte($78);
  1037. a_i64_clz:
  1038. WriteByte($79);
  1039. a_i64_ctz:
  1040. WriteByte($7A);
  1041. a_i64_popcnt:
  1042. WriteByte($7B);
  1043. a_i64_add:
  1044. WriteByte($7C);
  1045. a_i64_sub:
  1046. WriteByte($7D);
  1047. a_i64_mul:
  1048. WriteByte($7E);
  1049. a_i64_div_s:
  1050. WriteByte($7F);
  1051. a_i64_div_u:
  1052. WriteByte($80);
  1053. a_i64_rem_s:
  1054. WriteByte($81);
  1055. a_i64_rem_u:
  1056. WriteByte($82);
  1057. a_i64_and:
  1058. WriteByte($83);
  1059. a_i64_or:
  1060. WriteByte($84);
  1061. a_i64_xor:
  1062. WriteByte($85);
  1063. a_i64_shl:
  1064. WriteByte($86);
  1065. a_i64_shr_s:
  1066. WriteByte($87);
  1067. a_i64_shr_u:
  1068. WriteByte($88);
  1069. a_i64_rotl:
  1070. WriteByte($89);
  1071. a_i64_rotr:
  1072. WriteByte($8A);
  1073. a_f32_abs:
  1074. WriteByte($8B);
  1075. a_f32_neg:
  1076. WriteByte($8C);
  1077. a_f32_ceil:
  1078. WriteByte($8D);
  1079. a_f32_floor:
  1080. WriteByte($8E);
  1081. a_f32_trunc:
  1082. WriteByte($8F);
  1083. a_f32_nearest:
  1084. WriteByte($90);
  1085. a_f32_sqrt:
  1086. WriteByte($91);
  1087. a_f32_add:
  1088. WriteByte($92);
  1089. a_f32_sub:
  1090. WriteByte($93);
  1091. a_f32_mul:
  1092. WriteByte($94);
  1093. a_f32_div:
  1094. WriteByte($95);
  1095. a_f32_min:
  1096. WriteByte($96);
  1097. a_f32_max:
  1098. WriteByte($97);
  1099. a_f32_copysign:
  1100. WriteByte($98);
  1101. a_f64_abs:
  1102. WriteByte($99);
  1103. a_f64_neg:
  1104. WriteByte($9A);
  1105. a_f64_ceil:
  1106. WriteByte($9B);
  1107. a_f64_floor:
  1108. WriteByte($9C);
  1109. a_f64_trunc:
  1110. WriteByte($9D);
  1111. a_f64_nearest:
  1112. WriteByte($9E);
  1113. a_f64_sqrt:
  1114. WriteByte($9F);
  1115. a_f64_add:
  1116. WriteByte($A0);
  1117. a_f64_sub:
  1118. WriteByte($A1);
  1119. a_f64_mul:
  1120. WriteByte($A2);
  1121. a_f64_div:
  1122. WriteByte($A3);
  1123. a_f64_min:
  1124. WriteByte($A4);
  1125. a_f64_max:
  1126. WriteByte($A5);
  1127. a_f64_copysign:
  1128. WriteByte($A6);
  1129. a_i32_wrap_i64:
  1130. WriteByte($A7);
  1131. a_i32_trunc_f32_s:
  1132. WriteByte($A8);
  1133. a_i32_trunc_f32_u:
  1134. WriteByte($A9);
  1135. a_i32_trunc_f64_s:
  1136. WriteByte($AA);
  1137. a_i32_trunc_f64_u:
  1138. WriteByte($AB);
  1139. a_i64_extend_i32_s:
  1140. WriteByte($AC);
  1141. a_i64_extend_i32_u:
  1142. WriteByte($AD);
  1143. a_i64_trunc_f32_s:
  1144. WriteByte($AE);
  1145. a_i64_trunc_f32_u:
  1146. WriteByte($AF);
  1147. a_i64_trunc_f64_s:
  1148. WriteByte($B0);
  1149. a_i64_trunc_f64_u:
  1150. WriteByte($B1);
  1151. a_f32_convert_i32_s:
  1152. WriteByte($B2);
  1153. a_f32_convert_i32_u:
  1154. WriteByte($B3);
  1155. a_f32_convert_i64_s:
  1156. WriteByte($B4);
  1157. a_f32_convert_i64_u:
  1158. WriteByte($B5);
  1159. a_f32_demote_f64:
  1160. WriteByte($B6);
  1161. a_f64_convert_i32_s:
  1162. WriteByte($B7);
  1163. a_f64_convert_i32_u:
  1164. WriteByte($B8);
  1165. a_f64_convert_i64_s:
  1166. WriteByte($B9);
  1167. a_f64_convert_i64_u:
  1168. WriteByte($BA);
  1169. a_f64_promote_f32:
  1170. WriteByte($BB);
  1171. a_i32_reinterpret_f32:
  1172. WriteByte($BC);
  1173. a_i64_reinterpret_f64:
  1174. WriteByte($BD);
  1175. a_f32_reinterpret_i32:
  1176. WriteByte($BE);
  1177. a_f64_reinterpret_i64:
  1178. WriteByte($BF);
  1179. a_i32_extend8_s:
  1180. WriteByte($C0);
  1181. a_i32_extend16_s:
  1182. WriteByte($C1);
  1183. a_i64_extend8_s:
  1184. WriteByte($C2);
  1185. a_i64_extend16_s:
  1186. WriteByte($C3);
  1187. a_i64_extend32_s:
  1188. WriteByte($C4);
  1189. a_end_block,
  1190. a_end_if,
  1191. a_end_loop,
  1192. a_end_try:
  1193. WriteByte($0B);
  1194. a_catch_all:
  1195. WriteByte($19);
  1196. a_i32_const:
  1197. begin
  1198. WriteByte($41);
  1199. if ops<>1 then
  1200. internalerror(2021092001);
  1201. with oper[0]^ do
  1202. case typ of
  1203. top_ref:
  1204. begin
  1205. if assigned(ref^.symbol) then
  1206. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  1207. else
  1208. begin
  1209. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1210. internalerror(2021092018);
  1211. WriteSleb(longint(ref^.offset));
  1212. end;
  1213. end;
  1214. top_const:
  1215. WriteSleb(longint(val));
  1216. else
  1217. internalerror(2021092615);
  1218. end;
  1219. end;
  1220. a_i64_const:
  1221. begin
  1222. WriteByte($42);
  1223. if ops<>1 then
  1224. internalerror(2021092001);
  1225. with oper[0]^ do
  1226. case typ of
  1227. top_ref:
  1228. begin
  1229. if assigned(ref^.symbol) then
  1230. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  1231. else
  1232. begin
  1233. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1234. internalerror(2021092018);
  1235. WriteSleb(int64(ref^.offset));
  1236. end;
  1237. end;
  1238. top_const:
  1239. WriteSleb(int64(val));
  1240. else
  1241. internalerror(2021092615);
  1242. end;
  1243. end;
  1244. a_f32_const:
  1245. begin
  1246. if ops<>1 then
  1247. internalerror(2021092619);
  1248. WriteByte($43);
  1249. with oper[0]^ do
  1250. case typ of
  1251. top_single:
  1252. WriteSingle(sval);
  1253. else
  1254. internalerror(2021092618);
  1255. end;
  1256. end;
  1257. a_f64_const:
  1258. begin
  1259. if ops<>1 then
  1260. internalerror(2021092616);
  1261. WriteByte($44);
  1262. with oper[0]^ do
  1263. case typ of
  1264. top_double:
  1265. WriteDouble(dval);
  1266. else
  1267. internalerror(2021092617);
  1268. end;
  1269. end;
  1270. a_local_get,
  1271. a_local_set,
  1272. a_local_tee:
  1273. begin
  1274. case opcode of
  1275. a_local_get:
  1276. WriteByte($20);
  1277. a_local_set:
  1278. WriteByte($21);
  1279. a_local_tee:
  1280. WriteByte($22);
  1281. else
  1282. internalerror(2021092003);
  1283. end;
  1284. if ops<>1 then
  1285. internalerror(2021092004);
  1286. with oper[0]^ do
  1287. case typ of
  1288. top_ref:
  1289. begin
  1290. if assigned(ref^.symbol) then
  1291. internalerror(2021092005);
  1292. if ref^.base<>NR_STACK_POINTER_REG then
  1293. internalerror(2021092006);
  1294. if ref^.index<>NR_NO then
  1295. internalerror(2021092007);
  1296. WriteUleb(ref^.offset);
  1297. end;
  1298. else
  1299. internalerror(2021092008);
  1300. end;
  1301. end;
  1302. a_global_get,
  1303. a_global_set:
  1304. begin
  1305. case opcode of
  1306. a_global_get:
  1307. WriteByte($23);
  1308. a_global_set:
  1309. WriteByte($24);
  1310. else
  1311. internalerror(2021092009);
  1312. end;
  1313. if ops<>1 then
  1314. internalerror(2021092010);
  1315. with oper[0]^ do
  1316. case typ of
  1317. top_ref:
  1318. begin
  1319. if not assigned(ref^.symbol) then
  1320. internalerror(2021092012);
  1321. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1322. internalerror(2021092013);
  1323. objdata.writeReloc(0,5,TWasmObjData(ObjData).globalref(ref^.symbol),RELOC_GLOBAL_INDEX_LEB);
  1324. end;
  1325. else
  1326. internalerror(2021092011);
  1327. end;
  1328. end;
  1329. a_end_function:
  1330. ;
  1331. a_block,
  1332. a_loop,
  1333. a_if,
  1334. a_try:
  1335. begin
  1336. case opcode of
  1337. a_block:
  1338. WriteByte($02);
  1339. a_loop:
  1340. WriteByte($03);
  1341. a_if:
  1342. WriteByte($04);
  1343. a_try:
  1344. WriteByte($06);
  1345. else
  1346. internalerror(2021092626);
  1347. end;
  1348. if ops=0 then
  1349. WriteByte($40)
  1350. else
  1351. begin
  1352. if ops<>1 then
  1353. internalerror(2021092015);
  1354. with oper[0]^ do
  1355. case typ of
  1356. top_functype:
  1357. begin
  1358. if (length(functype.params)=0) and (length(functype.results)<=1) then
  1359. begin
  1360. if length(functype.results)=1 then
  1361. WriteByte(encode_wasm_basic_type(functype.results[0]))
  1362. else
  1363. WriteByte($40);
  1364. end
  1365. else
  1366. { more complex blocktypes are not yet implemented }
  1367. internalerror(2021092621);
  1368. end;
  1369. else
  1370. internalerror(2021092620);
  1371. end;
  1372. end;
  1373. end;
  1374. a_else:
  1375. WriteByte($05);
  1376. a_i32_load,
  1377. a_i64_load,
  1378. a_f32_load,
  1379. a_f64_load,
  1380. a_i32_load8_s,
  1381. a_i32_load8_u,
  1382. a_i32_load16_s,
  1383. a_i32_load16_u,
  1384. a_i64_load8_s,
  1385. a_i64_load8_u,
  1386. a_i64_load16_s,
  1387. a_i64_load16_u,
  1388. a_i64_load32_s,
  1389. a_i64_load32_u,
  1390. a_i32_store,
  1391. a_i64_store,
  1392. a_f32_store,
  1393. a_f64_store,
  1394. a_i32_store8,
  1395. a_i32_store16,
  1396. a_i64_store8,
  1397. a_i64_store16,
  1398. a_i64_store32:
  1399. begin
  1400. case opcode of
  1401. a_i32_load:
  1402. WriteByte($28);
  1403. a_i64_load:
  1404. WriteByte($29);
  1405. a_f32_load:
  1406. WriteByte($2A);
  1407. a_f64_load:
  1408. WriteByte($2B);
  1409. a_i32_load8_s:
  1410. WriteByte($2C);
  1411. a_i32_load8_u:
  1412. WriteByte($2D);
  1413. a_i32_load16_s:
  1414. WriteByte($2E);
  1415. a_i32_load16_u:
  1416. WriteByte($2F);
  1417. a_i64_load8_s:
  1418. WriteByte($30);
  1419. a_i64_load8_u:
  1420. WriteByte($31);
  1421. a_i64_load16_s:
  1422. WriteByte($32);
  1423. a_i64_load16_u:
  1424. WriteByte($33);
  1425. a_i64_load32_s:
  1426. WriteByte($34);
  1427. a_i64_load32_u:
  1428. WriteByte($35);
  1429. a_i32_store:
  1430. WriteByte($36);
  1431. a_i64_store:
  1432. WriteByte($37);
  1433. a_f32_store:
  1434. WriteByte($38);
  1435. a_f64_store:
  1436. WriteByte($39);
  1437. a_i32_store8:
  1438. WriteByte($3A);
  1439. a_i32_store16:
  1440. WriteByte($3B);
  1441. a_i64_store8:
  1442. WriteByte($3C);
  1443. a_i64_store16:
  1444. WriteByte($3D);
  1445. a_i64_store32:
  1446. WriteByte($3E);
  1447. else
  1448. internalerror(2021092019);
  1449. end;
  1450. if ops<>1 then
  1451. internalerror(2021092016);
  1452. with oper[0]^ do
  1453. case typ of
  1454. top_ref:
  1455. begin
  1456. if assigned(ref^.symbol) then
  1457. begin
  1458. WriteUleb(natural_alignment_for_load_store(opcode));
  1459. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  1460. end
  1461. else
  1462. begin
  1463. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1464. internalerror(2021092018);
  1465. WriteUleb(natural_alignment_for_load_store(opcode));
  1466. WriteUleb(ref^.offset);
  1467. end;
  1468. end;
  1469. top_const:
  1470. begin
  1471. WriteUleb(natural_alignment_for_load_store(opcode));
  1472. WriteUleb(val);
  1473. end;
  1474. else
  1475. internalerror(2021092017);
  1476. end;
  1477. end;
  1478. a_memory_atomic_notify,
  1479. a_memory_atomic_wait32,
  1480. a_memory_atomic_wait64,
  1481. a_i32_atomic_load,
  1482. a_i64_atomic_load,
  1483. a_i32_atomic_load8_u,
  1484. a_i32_atomic_load16_u,
  1485. a_i64_atomic_load8_u,
  1486. a_i64_atomic_load16_u,
  1487. a_i64_atomic_load32_u,
  1488. a_i32_atomic_store,
  1489. a_i64_atomic_store,
  1490. a_i32_atomic_store8,
  1491. a_i32_atomic_store16,
  1492. a_i64_atomic_store8,
  1493. a_i64_atomic_store16,
  1494. a_i64_atomic_store32,
  1495. a_i32_atomic_rmw_add,
  1496. a_i64_atomic_rmw_add,
  1497. a_i32_atomic_rmw8_add_u,
  1498. a_i32_atomic_rmw16_add_u,
  1499. a_i64_atomic_rmw8_add_u,
  1500. a_i64_atomic_rmw16_add_u,
  1501. a_i64_atomic_rmw32_add_u,
  1502. a_i32_atomic_rmw_sub,
  1503. a_i64_atomic_rmw_sub,
  1504. a_i32_atomic_rmw8_sub_u,
  1505. a_i32_atomic_rmw16_sub_u,
  1506. a_i64_atomic_rmw8_sub_u,
  1507. a_i64_atomic_rmw16_sub_u,
  1508. a_i64_atomic_rmw32_sub_u,
  1509. a_i32_atomic_rmw_and,
  1510. a_i64_atomic_rmw_and,
  1511. a_i32_atomic_rmw8_and_u,
  1512. a_i32_atomic_rmw16_and_u,
  1513. a_i64_atomic_rmw8_and_u,
  1514. a_i64_atomic_rmw16_and_u,
  1515. a_i64_atomic_rmw32_and_u,
  1516. a_i32_atomic_rmw_or,
  1517. a_i64_atomic_rmw_or,
  1518. a_i32_atomic_rmw8_or_u,
  1519. a_i32_atomic_rmw16_or_u,
  1520. a_i64_atomic_rmw8_or_u,
  1521. a_i64_atomic_rmw16_or_u,
  1522. a_i64_atomic_rmw32_or_u,
  1523. a_i32_atomic_rmw_xor,
  1524. a_i64_atomic_rmw_xor,
  1525. a_i32_atomic_rmw8_xor_u,
  1526. a_i32_atomic_rmw16_xor_u,
  1527. a_i64_atomic_rmw8_xor_u,
  1528. a_i64_atomic_rmw16_xor_u,
  1529. a_i64_atomic_rmw32_xor_u,
  1530. a_i32_atomic_rmw_xchg,
  1531. a_i64_atomic_rmw_xchg,
  1532. a_i32_atomic_rmw8_xchg_u,
  1533. a_i32_atomic_rmw16_xchg_u,
  1534. a_i64_atomic_rmw8_xchg_u,
  1535. a_i64_atomic_rmw16_xchg_u,
  1536. a_i64_atomic_rmw32_xchg_u,
  1537. a_i32_atomic_rmw_cmpxchg,
  1538. a_i64_atomic_rmw_cmpxchg,
  1539. a_i32_atomic_rmw8_cmpxchg_u,
  1540. a_i32_atomic_rmw16_cmpxchg_u,
  1541. a_i64_atomic_rmw8_cmpxchg_u,
  1542. a_i64_atomic_rmw16_cmpxchg_u,
  1543. a_i64_atomic_rmw32_cmpxchg_u:
  1544. begin
  1545. WriteByte($FE);
  1546. case opcode of
  1547. a_memory_atomic_notify:
  1548. WriteByte($00);
  1549. a_memory_atomic_wait32:
  1550. WriteByte($01);
  1551. a_memory_atomic_wait64:
  1552. WriteByte($02);
  1553. a_i32_atomic_load:
  1554. WriteByte($10);
  1555. a_i64_atomic_load:
  1556. WriteByte($11);
  1557. a_i32_atomic_load8_u:
  1558. WriteByte($12);
  1559. a_i32_atomic_load16_u:
  1560. WriteByte($13);
  1561. a_i64_atomic_load8_u:
  1562. WriteByte($14);
  1563. a_i64_atomic_load16_u:
  1564. WriteByte($15);
  1565. a_i64_atomic_load32_u:
  1566. WriteByte($16);
  1567. a_i32_atomic_store:
  1568. WriteByte($17);
  1569. a_i64_atomic_store:
  1570. WriteByte($18);
  1571. a_i32_atomic_store8:
  1572. WriteByte($19);
  1573. a_i32_atomic_store16:
  1574. WriteByte($1A);
  1575. a_i64_atomic_store8:
  1576. WriteByte($1B);
  1577. a_i64_atomic_store16:
  1578. WriteByte($1C);
  1579. a_i64_atomic_store32:
  1580. WriteByte($1D);
  1581. a_i32_atomic_rmw_add:
  1582. WriteByte($1E);
  1583. a_i64_atomic_rmw_add:
  1584. WriteByte($1F);
  1585. a_i32_atomic_rmw8_add_u:
  1586. WriteByte($20);
  1587. a_i32_atomic_rmw16_add_u:
  1588. WriteByte($21);
  1589. a_i64_atomic_rmw8_add_u:
  1590. WriteByte($22);
  1591. a_i64_atomic_rmw16_add_u:
  1592. WriteByte($23);
  1593. a_i64_atomic_rmw32_add_u:
  1594. WriteByte($24);
  1595. a_i32_atomic_rmw_sub:
  1596. WriteByte($25);
  1597. a_i64_atomic_rmw_sub:
  1598. WriteByte($26);
  1599. a_i32_atomic_rmw8_sub_u:
  1600. WriteByte($27);
  1601. a_i32_atomic_rmw16_sub_u:
  1602. WriteByte($28);
  1603. a_i64_atomic_rmw8_sub_u:
  1604. WriteByte($29);
  1605. a_i64_atomic_rmw16_sub_u:
  1606. WriteByte($2A);
  1607. a_i64_atomic_rmw32_sub_u:
  1608. WriteByte($2B);
  1609. a_i32_atomic_rmw_and:
  1610. WriteByte($2C);
  1611. a_i64_atomic_rmw_and:
  1612. WriteByte($2D);
  1613. a_i32_atomic_rmw8_and_u:
  1614. WriteByte($2E);
  1615. a_i32_atomic_rmw16_and_u:
  1616. WriteByte($2F);
  1617. a_i64_atomic_rmw8_and_u:
  1618. WriteByte($30);
  1619. a_i64_atomic_rmw16_and_u:
  1620. WriteByte($31);
  1621. a_i64_atomic_rmw32_and_u:
  1622. WriteByte($32);
  1623. a_i32_atomic_rmw_or:
  1624. WriteByte($33);
  1625. a_i64_atomic_rmw_or:
  1626. WriteByte($34);
  1627. a_i32_atomic_rmw8_or_u:
  1628. WriteByte($35);
  1629. a_i32_atomic_rmw16_or_u:
  1630. WriteByte($36);
  1631. a_i64_atomic_rmw8_or_u:
  1632. WriteByte($37);
  1633. a_i64_atomic_rmw16_or_u:
  1634. WriteByte($38);
  1635. a_i64_atomic_rmw32_or_u:
  1636. WriteByte($39);
  1637. a_i32_atomic_rmw_xor:
  1638. WriteByte($3A);
  1639. a_i64_atomic_rmw_xor:
  1640. WriteByte($3B);
  1641. a_i32_atomic_rmw8_xor_u:
  1642. WriteByte($3C);
  1643. a_i32_atomic_rmw16_xor_u:
  1644. WriteByte($3D);
  1645. a_i64_atomic_rmw8_xor_u:
  1646. WriteByte($3E);
  1647. a_i64_atomic_rmw16_xor_u:
  1648. WriteByte($3F);
  1649. a_i64_atomic_rmw32_xor_u:
  1650. WriteByte($40);
  1651. a_i32_atomic_rmw_xchg:
  1652. WriteByte($41);
  1653. a_i64_atomic_rmw_xchg:
  1654. WriteByte($42);
  1655. a_i32_atomic_rmw8_xchg_u:
  1656. WriteByte($43);
  1657. a_i32_atomic_rmw16_xchg_u:
  1658. WriteByte($44);
  1659. a_i64_atomic_rmw8_xchg_u:
  1660. WriteByte($45);
  1661. a_i64_atomic_rmw16_xchg_u:
  1662. WriteByte($46);
  1663. a_i64_atomic_rmw32_xchg_u:
  1664. WriteByte($47);
  1665. a_i32_atomic_rmw_cmpxchg:
  1666. WriteByte($48);
  1667. a_i64_atomic_rmw_cmpxchg:
  1668. WriteByte($49);
  1669. a_i32_atomic_rmw8_cmpxchg_u:
  1670. WriteByte($4A);
  1671. a_i32_atomic_rmw16_cmpxchg_u:
  1672. WriteByte($4B);
  1673. a_i64_atomic_rmw8_cmpxchg_u:
  1674. WriteByte($4C);
  1675. a_i64_atomic_rmw16_cmpxchg_u:
  1676. WriteByte($4D);
  1677. a_i64_atomic_rmw32_cmpxchg_u:
  1678. WriteByte($4E);
  1679. else
  1680. internalerror(2022052101);
  1681. end;
  1682. if ops<>1 then
  1683. internalerror(2021092016);
  1684. with oper[0]^ do
  1685. case typ of
  1686. top_ref:
  1687. begin
  1688. if assigned(ref^.symbol) then
  1689. begin
  1690. WriteUleb(natural_alignment_for_load_store(opcode));
  1691. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  1692. end
  1693. else
  1694. begin
  1695. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1696. internalerror(2021092018);
  1697. WriteUleb(natural_alignment_for_load_store(opcode));
  1698. WriteUleb(ref^.offset);
  1699. end;
  1700. end;
  1701. top_const:
  1702. begin
  1703. WriteUleb(natural_alignment_for_load_store(opcode));
  1704. WriteUleb(val);
  1705. end;
  1706. else
  1707. internalerror(2021092017);
  1708. end;
  1709. end;
  1710. a_call:
  1711. begin
  1712. if ops<>1 then
  1713. internalerror(2021092021);
  1714. with oper[0]^ do
  1715. case typ of
  1716. top_ref:
  1717. begin
  1718. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1719. internalerror(2021092023);
  1720. WriteByte($10);
  1721. objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
  1722. end;
  1723. else
  1724. internalerror(2021092022);
  1725. end;
  1726. end;
  1727. a_call_indirect:
  1728. begin
  1729. if ops<>1 then
  1730. internalerror(2021092610);
  1731. with oper[0]^ do
  1732. case typ of
  1733. top_functype:
  1734. begin
  1735. WriteByte($11);
  1736. objdata.writeReloc(TWasmObjData(objdata).AddFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
  1737. WriteUleb(0);
  1738. end;
  1739. else
  1740. internalerror(2021092611);
  1741. end;
  1742. end;
  1743. a_br,
  1744. a_br_if,
  1745. a_rethrow,
  1746. a_delegate:
  1747. begin
  1748. case opcode of
  1749. a_br:
  1750. WriteByte($0C);
  1751. a_br_if:
  1752. WriteByte($0D);
  1753. a_rethrow:
  1754. WriteByte($09);
  1755. a_delegate:
  1756. WriteByte($18);
  1757. else
  1758. internalerror(2021092622);
  1759. end;
  1760. if ops<>1 then
  1761. internalerror(2021092610);
  1762. with oper[0]^ do
  1763. case typ of
  1764. top_const:
  1765. WriteUleb(val);
  1766. else
  1767. internalerror(2021092625);
  1768. end;
  1769. end;
  1770. a_catch,
  1771. a_throw:
  1772. begin
  1773. case opcode of
  1774. a_catch:
  1775. WriteByte($07);
  1776. a_throw:
  1777. WriteByte($08);
  1778. else
  1779. internalerror(2021092708);
  1780. end;
  1781. if ops<>1 then
  1782. internalerror(2021092709);
  1783. with oper[0]^ do
  1784. case typ of
  1785. top_ref:
  1786. begin
  1787. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1788. internalerror(2021092711);
  1789. objdata.writeReloc(0,5,TWasmObjData(ObjData).ExceptionTagRef(ref^.symbol),RELOC_TAG_INDEX_LEB);
  1790. end;
  1791. else
  1792. internalerror(2021092710);
  1793. end;
  1794. end;
  1795. a_memory_init:
  1796. begin
  1797. WriteByte($FC);
  1798. WriteByte($08);
  1799. if ops<>1 then
  1800. internalerror(2022052806);
  1801. with oper[0]^ do
  1802. case typ of
  1803. top_const:
  1804. WriteUleb(val);
  1805. else
  1806. internalerror(2022052807);
  1807. end;
  1808. WriteByte($00);
  1809. end;
  1810. a_data_drop:
  1811. begin
  1812. WriteByte($FC);
  1813. WriteByte($09);
  1814. if ops<>1 then
  1815. internalerror(2022052808);
  1816. with oper[0]^ do
  1817. case typ of
  1818. top_const:
  1819. WriteUleb(val);
  1820. else
  1821. internalerror(2022052809);
  1822. end;
  1823. end;
  1824. a_i32_trunc_sat_f32_s:
  1825. begin
  1826. WriteByte($FC);
  1827. WriteByte($00);
  1828. end;
  1829. a_i32_trunc_sat_f32_u:
  1830. begin
  1831. WriteByte($FC);
  1832. WriteByte($01);
  1833. end;
  1834. a_i32_trunc_sat_f64_s:
  1835. begin
  1836. WriteByte($FC);
  1837. WriteByte($02);
  1838. end;
  1839. a_i32_trunc_sat_f64_u:
  1840. begin
  1841. WriteByte($FC);
  1842. WriteByte($03);
  1843. end;
  1844. a_i64_trunc_sat_f32_s:
  1845. begin
  1846. WriteByte($FC);
  1847. WriteByte($04);
  1848. end;
  1849. a_i64_trunc_sat_f32_u:
  1850. begin
  1851. WriteByte($FC);
  1852. WriteByte($05);
  1853. end;
  1854. a_i64_trunc_sat_f64_s:
  1855. begin
  1856. WriteByte($FC);
  1857. WriteByte($06);
  1858. end;
  1859. a_i64_trunc_sat_f64_u:
  1860. begin
  1861. WriteByte($FC);
  1862. WriteByte($07);
  1863. end;
  1864. else
  1865. internalerror(2021092624);
  1866. end;
  1867. end;
  1868. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1869. begin
  1870. internalerror(2010122614);
  1871. result:=nil;
  1872. end;
  1873. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1874. begin
  1875. internalerror(2010122615);
  1876. result:=nil;
  1877. end;
  1878. procedure InitAsm;
  1879. begin
  1880. end;
  1881. procedure DoneAsm;
  1882. begin
  1883. end;
  1884. initialization
  1885. cai_cpu:=taicpu;
  1886. cai_align:=tai_align;
  1887. end.