aasmcpu.pas 59 KB

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