aasmcpu.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  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. a_ref_is_null:
  485. result:=1;
  486. a_i32_trunc_sat_f32_s,
  487. a_i32_trunc_sat_f32_u,
  488. a_i32_trunc_sat_f64_s,
  489. a_i32_trunc_sat_f64_u,
  490. a_i64_trunc_sat_f32_s,
  491. a_i64_trunc_sat_f32_u,
  492. a_i64_trunc_sat_f64_s,
  493. a_i64_trunc_sat_f64_u,
  494. a_memory_size,
  495. a_memory_grow,
  496. a_ref_null_funcref,
  497. a_ref_null_externref:
  498. result:=2;
  499. a_memory_copy:
  500. result:=4;
  501. a_memory_fill,
  502. a_atomic_fence:
  503. result:=3;
  504. a_i32_const:
  505. begin
  506. if ops<>1 then
  507. internalerror(2021092001);
  508. with oper[0]^ do
  509. case typ of
  510. top_ref:
  511. begin
  512. if assigned(ref^.symbol) then
  513. result:=6
  514. else
  515. begin
  516. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  517. internalerror(2021092018);
  518. result:=1+SlebSize(longint(ref^.offset));
  519. end;
  520. end;
  521. top_const:
  522. result:=1+SlebSize(longint(val));
  523. else
  524. internalerror(2021092615);
  525. end;
  526. end;
  527. a_i64_const:
  528. begin
  529. if ops<>1 then
  530. internalerror(2021092001);
  531. with oper[0]^ do
  532. case typ of
  533. top_ref:
  534. begin
  535. if assigned(ref^.symbol) then
  536. result:=6
  537. else
  538. begin
  539. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  540. internalerror(2021092018);
  541. result:=1+SlebSize(int64(ref^.offset));
  542. end;
  543. end;
  544. top_const:
  545. result:=1+SlebSize(int64(val));
  546. else
  547. internalerror(2021092615);
  548. end;
  549. end;
  550. a_f32_const:
  551. result:=5;
  552. a_f64_const:
  553. result:=9;
  554. a_local_get,
  555. a_local_set,
  556. a_local_tee:
  557. begin
  558. if ops<>1 then
  559. internalerror(2021092001);
  560. with oper[0]^ do
  561. case typ of
  562. top_ref:
  563. begin
  564. if assigned(ref^.symbol) then
  565. internalerror(2021092005);
  566. if ref^.base<>NR_STACK_POINTER_REG then
  567. internalerror(2021092006);
  568. if ref^.index<>NR_NO then
  569. internalerror(2021092007);
  570. result:=1+UlebSize(ref^.offset);
  571. end;
  572. else
  573. internalerror(2021092008);
  574. end;
  575. end;
  576. a_global_get,
  577. a_global_set:
  578. begin
  579. if ops<>1 then
  580. internalerror(2021092010);
  581. with oper[0]^ do
  582. case typ of
  583. top_ref:
  584. begin
  585. if not assigned(ref^.symbol) then
  586. internalerror(2021092012);
  587. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  588. internalerror(2021092013);
  589. result:=6;
  590. end;
  591. else
  592. internalerror(2021092011);
  593. end;
  594. end;
  595. a_end_function:
  596. result:=0;
  597. a_block,
  598. a_loop,
  599. a_if,
  600. a_try:
  601. begin
  602. if ops=0 then
  603. result:=2
  604. else
  605. begin
  606. if ops<>1 then
  607. internalerror(2021092015);
  608. with oper[0]^ do
  609. case typ of
  610. top_functype:
  611. begin
  612. if (length(functype.params)=0) and (length(functype.results)<=1) then
  613. result:=2
  614. else
  615. { more complex blocktypes are not yet implemented }
  616. internalerror(2021092621);
  617. end;
  618. else
  619. internalerror(2021092620);
  620. end;
  621. end;
  622. end;
  623. a_i32_load,
  624. a_i64_load,
  625. a_f32_load,
  626. a_f64_load,
  627. a_i32_load8_s,
  628. a_i32_load8_u,
  629. a_i32_load16_s,
  630. a_i32_load16_u,
  631. a_i64_load8_s,
  632. a_i64_load8_u,
  633. a_i64_load16_s,
  634. a_i64_load16_u,
  635. a_i64_load32_s,
  636. a_i64_load32_u,
  637. a_i32_store,
  638. a_i64_store,
  639. a_f32_store,
  640. a_f64_store,
  641. a_i32_store8,
  642. a_i32_store16,
  643. a_i64_store8,
  644. a_i64_store16,
  645. a_i64_store32:
  646. begin
  647. if ops<>1 then
  648. internalerror(2021092016);
  649. with oper[0]^ do
  650. case typ of
  651. top_ref:
  652. begin
  653. if assigned(ref^.symbol) then
  654. begin
  655. Result:=1+
  656. UlebSize(natural_alignment_for_load_store(opcode))+
  657. 5; { relocation, fixed size = 5 bytes }
  658. end
  659. else
  660. begin
  661. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  662. internalerror(2021092018);
  663. Result:=1+
  664. UlebSize(natural_alignment_for_load_store(opcode))+
  665. UlebSize(ref^.offset);
  666. end;
  667. end;
  668. top_const:
  669. begin
  670. Result:=1+
  671. UlebSize(natural_alignment_for_load_store(opcode))+
  672. UlebSize(val);
  673. end;
  674. else
  675. internalerror(2021092017);
  676. end;
  677. end;
  678. a_memory_atomic_notify,
  679. a_memory_atomic_wait32,
  680. a_memory_atomic_wait64,
  681. a_i32_atomic_load,
  682. a_i64_atomic_load,
  683. a_i32_atomic_load8_u,
  684. a_i32_atomic_load16_u,
  685. a_i64_atomic_load8_u,
  686. a_i64_atomic_load16_u,
  687. a_i64_atomic_load32_u,
  688. a_i32_atomic_store,
  689. a_i64_atomic_store,
  690. a_i32_atomic_store8,
  691. a_i32_atomic_store16,
  692. a_i64_atomic_store8,
  693. a_i64_atomic_store16,
  694. a_i64_atomic_store32,
  695. a_i32_atomic_rmw_add,
  696. a_i64_atomic_rmw_add,
  697. a_i32_atomic_rmw8_add_u,
  698. a_i32_atomic_rmw16_add_u,
  699. a_i64_atomic_rmw8_add_u,
  700. a_i64_atomic_rmw16_add_u,
  701. a_i64_atomic_rmw32_add_u,
  702. a_i32_atomic_rmw_sub,
  703. a_i64_atomic_rmw_sub,
  704. a_i32_atomic_rmw8_sub_u,
  705. a_i32_atomic_rmw16_sub_u,
  706. a_i64_atomic_rmw8_sub_u,
  707. a_i64_atomic_rmw16_sub_u,
  708. a_i64_atomic_rmw32_sub_u,
  709. a_i32_atomic_rmw_and,
  710. a_i64_atomic_rmw_and,
  711. a_i32_atomic_rmw8_and_u,
  712. a_i32_atomic_rmw16_and_u,
  713. a_i64_atomic_rmw8_and_u,
  714. a_i64_atomic_rmw16_and_u,
  715. a_i64_atomic_rmw32_and_u,
  716. a_i32_atomic_rmw_or,
  717. a_i64_atomic_rmw_or,
  718. a_i32_atomic_rmw8_or_u,
  719. a_i32_atomic_rmw16_or_u,
  720. a_i64_atomic_rmw8_or_u,
  721. a_i64_atomic_rmw16_or_u,
  722. a_i64_atomic_rmw32_or_u,
  723. a_i32_atomic_rmw_xor,
  724. a_i64_atomic_rmw_xor,
  725. a_i32_atomic_rmw8_xor_u,
  726. a_i32_atomic_rmw16_xor_u,
  727. a_i64_atomic_rmw8_xor_u,
  728. a_i64_atomic_rmw16_xor_u,
  729. a_i64_atomic_rmw32_xor_u,
  730. a_i32_atomic_rmw_xchg,
  731. a_i64_atomic_rmw_xchg,
  732. a_i32_atomic_rmw8_xchg_u,
  733. a_i32_atomic_rmw16_xchg_u,
  734. a_i64_atomic_rmw8_xchg_u,
  735. a_i64_atomic_rmw16_xchg_u,
  736. a_i64_atomic_rmw32_xchg_u,
  737. a_i32_atomic_rmw_cmpxchg,
  738. a_i64_atomic_rmw_cmpxchg,
  739. a_i32_atomic_rmw8_cmpxchg_u,
  740. a_i32_atomic_rmw16_cmpxchg_u,
  741. a_i64_atomic_rmw8_cmpxchg_u,
  742. a_i64_atomic_rmw16_cmpxchg_u,
  743. a_i64_atomic_rmw32_cmpxchg_u:
  744. begin
  745. if ops<>1 then
  746. internalerror(2021092016);
  747. with oper[0]^ do
  748. case typ of
  749. top_ref:
  750. begin
  751. if assigned(ref^.symbol) then
  752. begin
  753. Result:=2+
  754. UlebSize(natural_alignment_for_load_store(opcode))+
  755. 5; { relocation, fixed size = 5 bytes }
  756. end
  757. else
  758. begin
  759. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  760. internalerror(2021092018);
  761. Result:=2+
  762. UlebSize(natural_alignment_for_load_store(opcode))+
  763. UlebSize(ref^.offset);
  764. end;
  765. end;
  766. top_const:
  767. begin
  768. Result:=2+
  769. UlebSize(natural_alignment_for_load_store(opcode))+
  770. UlebSize(val);
  771. end;
  772. else
  773. internalerror(2021092017);
  774. end;
  775. end;
  776. a_call:
  777. begin
  778. if ops<>1 then
  779. internalerror(2021092021);
  780. with oper[0]^ do
  781. case typ of
  782. top_ref:
  783. begin
  784. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  785. internalerror(2021092023);
  786. result:=6;
  787. end;
  788. else
  789. internalerror(2021092022);
  790. end;
  791. end;
  792. a_call_indirect:
  793. begin
  794. if ops<>1 then
  795. internalerror(2021092610);
  796. with oper[0]^ do
  797. case typ of
  798. top_functype:
  799. begin
  800. TWasmObjData(objdata).AddFuncType(functype);
  801. result:=6+
  802. UlebSize(0);
  803. end;
  804. else
  805. internalerror(2021092611);
  806. end;
  807. end;
  808. a_br,
  809. a_br_if,
  810. a_rethrow,
  811. a_delegate:
  812. begin
  813. if ops<>1 then
  814. internalerror(2021092610);
  815. with oper[0]^ do
  816. case typ of
  817. top_const:
  818. result:=1+
  819. UlebSize(val);
  820. else
  821. internalerror(2021092625);
  822. end;
  823. end;
  824. a_catch,
  825. a_throw:
  826. begin
  827. if ops<>1 then
  828. internalerror(2021092709);
  829. with oper[0]^ do
  830. case typ of
  831. top_ref:
  832. begin
  833. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  834. internalerror(2021092711);
  835. result:=6;
  836. end;
  837. else
  838. internalerror(2021092710);
  839. end;
  840. end;
  841. a_memory_init:
  842. begin
  843. if ops<>1 then
  844. internalerror(2022052802);
  845. with oper[0]^ do
  846. case typ of
  847. top_const:
  848. result:=3+UlebSize(val);
  849. else
  850. internalerror(2022052803);
  851. end;
  852. end;
  853. a_data_drop:
  854. begin
  855. if ops<>1 then
  856. internalerror(2022052804);
  857. with oper[0]^ do
  858. case typ of
  859. top_const:
  860. result:=2+UlebSize(val);
  861. else
  862. internalerror(2022052805);
  863. end;
  864. end;
  865. else
  866. internalerror(2021092623);
  867. end;
  868. end;
  869. procedure taicpu.Pass2(objdata: TObjData);
  870. procedure WriteByte(b: byte);
  871. begin
  872. objdata.writebytes(b,1);
  873. end;
  874. {$ifdef FPC_LITTLE_ENDIAN}
  875. procedure WriteSingle(s: single);
  876. begin
  877. objdata.writebytes(s,4);
  878. end;
  879. procedure WriteDouble(d: double);
  880. begin
  881. objdata.writebytes(d,8);
  882. end;
  883. {$else FPC_LITTLE_ENDIAN}
  884. procedure WriteSingle(s: single);
  885. var
  886. l: longword;
  887. begin
  888. Move(s,l,4);
  889. l:=SwapEndian(l);
  890. objdata.writebytes(l,4);
  891. end;
  892. procedure WriteDouble(d: double);
  893. var
  894. q: qword;
  895. begin
  896. Move(d,q,8);
  897. q:=SwapEndian(q);
  898. objdata.writebytes(q,8);
  899. end;
  900. {$endif FPC_LITTLE_ENDIAN}
  901. procedure WriteSleb(v: tcgint);
  902. var
  903. b: byte;
  904. Done: Boolean=false;
  905. begin
  906. repeat
  907. b:=byte(v) and 127;
  908. v:=SarInt64(v,7);
  909. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  910. Done:=true
  911. else
  912. b:=b or 128;
  913. objdata.writebytes(b,1);
  914. until Done;
  915. end;
  916. procedure WriteUleb(v: tcgint);
  917. var
  918. b: byte;
  919. begin
  920. repeat
  921. b:=byte(v) and 127;
  922. v:=v shr 7;
  923. if v<>0 then
  924. b:=b or 128;
  925. objdata.writebytes(b,1);
  926. until v=0;
  927. end;
  928. begin
  929. case opcode of
  930. a_unreachable:
  931. WriteByte($00);
  932. a_nop:
  933. WriteByte($01);
  934. a_return:
  935. WriteByte($0F);
  936. a_drop:
  937. WriteByte($1A);
  938. a_memory_size:
  939. begin
  940. WriteByte($3F);
  941. WriteByte($00);
  942. end;
  943. a_memory_grow:
  944. begin
  945. WriteByte($40);
  946. WriteByte($00);
  947. end;
  948. a_memory_copy:
  949. begin
  950. WriteByte($FC);
  951. WriteUleb(10);
  952. WriteByte($00);
  953. WriteByte($00);
  954. end;
  955. a_memory_fill:
  956. begin
  957. WriteByte($FC);
  958. WriteUleb(11);
  959. WriteByte($00);
  960. end;
  961. a_atomic_fence:
  962. begin
  963. WriteByte($FE);
  964. WriteByte($03);
  965. WriteByte($00);
  966. end;
  967. a_i32_eqz:
  968. WriteByte($45);
  969. a_i32_eq:
  970. WriteByte($46);
  971. a_i32_ne:
  972. WriteByte($47);
  973. a_i32_lt_s:
  974. WriteByte($48);
  975. a_i32_lt_u:
  976. WriteByte($49);
  977. a_i32_gt_s:
  978. WriteByte($4A);
  979. a_i32_gt_u:
  980. WriteByte($4B);
  981. a_i32_le_s:
  982. WriteByte($4C);
  983. a_i32_le_u:
  984. WriteByte($4D);
  985. a_i32_ge_s:
  986. WriteByte($4E);
  987. a_i32_ge_u:
  988. WriteByte($4F);
  989. a_i64_eqz:
  990. WriteByte($50);
  991. a_i64_eq:
  992. WriteByte($51);
  993. a_i64_ne:
  994. WriteByte($52);
  995. a_i64_lt_s:
  996. WriteByte($53);
  997. a_i64_lt_u:
  998. WriteByte($54);
  999. a_i64_gt_s:
  1000. WriteByte($55);
  1001. a_i64_gt_u:
  1002. WriteByte($56);
  1003. a_i64_le_s:
  1004. WriteByte($57);
  1005. a_i64_le_u:
  1006. WriteByte($58);
  1007. a_i64_ge_s:
  1008. WriteByte($59);
  1009. a_i64_ge_u:
  1010. WriteByte($5A);
  1011. a_f32_eq:
  1012. WriteByte($5B);
  1013. a_f32_ne:
  1014. WriteByte($5C);
  1015. a_f32_lt:
  1016. WriteByte($5D);
  1017. a_f32_gt:
  1018. WriteByte($5E);
  1019. a_f32_le:
  1020. WriteByte($5F);
  1021. a_f32_ge:
  1022. WriteByte($60);
  1023. a_f64_eq:
  1024. WriteByte($61);
  1025. a_f64_ne:
  1026. WriteByte($62);
  1027. a_f64_lt:
  1028. WriteByte($63);
  1029. a_f64_gt:
  1030. WriteByte($64);
  1031. a_f64_le:
  1032. WriteByte($65);
  1033. a_f64_ge:
  1034. WriteByte($66);
  1035. a_i32_clz:
  1036. WriteByte($67);
  1037. a_i32_ctz:
  1038. WriteByte($68);
  1039. a_i32_popcnt:
  1040. WriteByte($69);
  1041. a_i32_add:
  1042. WriteByte($6A);
  1043. a_i32_sub:
  1044. WriteByte($6B);
  1045. a_i32_mul:
  1046. WriteByte($6C);
  1047. a_i32_div_s:
  1048. WriteByte($6D);
  1049. a_i32_div_u:
  1050. WriteByte($6E);
  1051. a_i32_rem_s:
  1052. WriteByte($6F);
  1053. a_i32_rem_u:
  1054. WriteByte($70);
  1055. a_i32_and:
  1056. WriteByte($71);
  1057. a_i32_or:
  1058. WriteByte($72);
  1059. a_i32_xor:
  1060. WriteByte($73);
  1061. a_i32_shl:
  1062. WriteByte($74);
  1063. a_i32_shr_s:
  1064. WriteByte($75);
  1065. a_i32_shr_u:
  1066. WriteByte($76);
  1067. a_i32_rotl:
  1068. WriteByte($77);
  1069. a_i32_rotr:
  1070. WriteByte($78);
  1071. a_i64_clz:
  1072. WriteByte($79);
  1073. a_i64_ctz:
  1074. WriteByte($7A);
  1075. a_i64_popcnt:
  1076. WriteByte($7B);
  1077. a_i64_add:
  1078. WriteByte($7C);
  1079. a_i64_sub:
  1080. WriteByte($7D);
  1081. a_i64_mul:
  1082. WriteByte($7E);
  1083. a_i64_div_s:
  1084. WriteByte($7F);
  1085. a_i64_div_u:
  1086. WriteByte($80);
  1087. a_i64_rem_s:
  1088. WriteByte($81);
  1089. a_i64_rem_u:
  1090. WriteByte($82);
  1091. a_i64_and:
  1092. WriteByte($83);
  1093. a_i64_or:
  1094. WriteByte($84);
  1095. a_i64_xor:
  1096. WriteByte($85);
  1097. a_i64_shl:
  1098. WriteByte($86);
  1099. a_i64_shr_s:
  1100. WriteByte($87);
  1101. a_i64_shr_u:
  1102. WriteByte($88);
  1103. a_i64_rotl:
  1104. WriteByte($89);
  1105. a_i64_rotr:
  1106. WriteByte($8A);
  1107. a_f32_abs:
  1108. WriteByte($8B);
  1109. a_f32_neg:
  1110. WriteByte($8C);
  1111. a_f32_ceil:
  1112. WriteByte($8D);
  1113. a_f32_floor:
  1114. WriteByte($8E);
  1115. a_f32_trunc:
  1116. WriteByte($8F);
  1117. a_f32_nearest:
  1118. WriteByte($90);
  1119. a_f32_sqrt:
  1120. WriteByte($91);
  1121. a_f32_add:
  1122. WriteByte($92);
  1123. a_f32_sub:
  1124. WriteByte($93);
  1125. a_f32_mul:
  1126. WriteByte($94);
  1127. a_f32_div:
  1128. WriteByte($95);
  1129. a_f32_min:
  1130. WriteByte($96);
  1131. a_f32_max:
  1132. WriteByte($97);
  1133. a_f32_copysign:
  1134. WriteByte($98);
  1135. a_f64_abs:
  1136. WriteByte($99);
  1137. a_f64_neg:
  1138. WriteByte($9A);
  1139. a_f64_ceil:
  1140. WriteByte($9B);
  1141. a_f64_floor:
  1142. WriteByte($9C);
  1143. a_f64_trunc:
  1144. WriteByte($9D);
  1145. a_f64_nearest:
  1146. WriteByte($9E);
  1147. a_f64_sqrt:
  1148. WriteByte($9F);
  1149. a_f64_add:
  1150. WriteByte($A0);
  1151. a_f64_sub:
  1152. WriteByte($A1);
  1153. a_f64_mul:
  1154. WriteByte($A2);
  1155. a_f64_div:
  1156. WriteByte($A3);
  1157. a_f64_min:
  1158. WriteByte($A4);
  1159. a_f64_max:
  1160. WriteByte($A5);
  1161. a_f64_copysign:
  1162. WriteByte($A6);
  1163. a_i32_wrap_i64:
  1164. WriteByte($A7);
  1165. a_i32_trunc_f32_s:
  1166. WriteByte($A8);
  1167. a_i32_trunc_f32_u:
  1168. WriteByte($A9);
  1169. a_i32_trunc_f64_s:
  1170. WriteByte($AA);
  1171. a_i32_trunc_f64_u:
  1172. WriteByte($AB);
  1173. a_i64_extend_i32_s:
  1174. WriteByte($AC);
  1175. a_i64_extend_i32_u:
  1176. WriteByte($AD);
  1177. a_i64_trunc_f32_s:
  1178. WriteByte($AE);
  1179. a_i64_trunc_f32_u:
  1180. WriteByte($AF);
  1181. a_i64_trunc_f64_s:
  1182. WriteByte($B0);
  1183. a_i64_trunc_f64_u:
  1184. WriteByte($B1);
  1185. a_f32_convert_i32_s:
  1186. WriteByte($B2);
  1187. a_f32_convert_i32_u:
  1188. WriteByte($B3);
  1189. a_f32_convert_i64_s:
  1190. WriteByte($B4);
  1191. a_f32_convert_i64_u:
  1192. WriteByte($B5);
  1193. a_f32_demote_f64:
  1194. WriteByte($B6);
  1195. a_f64_convert_i32_s:
  1196. WriteByte($B7);
  1197. a_f64_convert_i32_u:
  1198. WriteByte($B8);
  1199. a_f64_convert_i64_s:
  1200. WriteByte($B9);
  1201. a_f64_convert_i64_u:
  1202. WriteByte($BA);
  1203. a_f64_promote_f32:
  1204. WriteByte($BB);
  1205. a_i32_reinterpret_f32:
  1206. WriteByte($BC);
  1207. a_i64_reinterpret_f64:
  1208. WriteByte($BD);
  1209. a_f32_reinterpret_i32:
  1210. WriteByte($BE);
  1211. a_f64_reinterpret_i64:
  1212. WriteByte($BF);
  1213. a_i32_extend8_s:
  1214. WriteByte($C0);
  1215. a_i32_extend16_s:
  1216. WriteByte($C1);
  1217. a_i64_extend8_s:
  1218. WriteByte($C2);
  1219. a_i64_extend16_s:
  1220. WriteByte($C3);
  1221. a_i64_extend32_s:
  1222. WriteByte($C4);
  1223. a_end_block,
  1224. a_end_if,
  1225. a_end_loop,
  1226. a_end_try:
  1227. WriteByte($0B);
  1228. a_catch_all:
  1229. WriteByte($19);
  1230. a_i32_const:
  1231. begin
  1232. WriteByte($41);
  1233. if ops<>1 then
  1234. internalerror(2021092001);
  1235. with oper[0]^ do
  1236. case typ of
  1237. top_ref:
  1238. begin
  1239. if assigned(ref^.symbol) then
  1240. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  1241. else
  1242. begin
  1243. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1244. internalerror(2021092018);
  1245. WriteSleb(longint(ref^.offset));
  1246. end;
  1247. end;
  1248. top_const:
  1249. WriteSleb(longint(val));
  1250. else
  1251. internalerror(2021092615);
  1252. end;
  1253. end;
  1254. a_i64_const:
  1255. begin
  1256. WriteByte($42);
  1257. if ops<>1 then
  1258. internalerror(2021092001);
  1259. with oper[0]^ do
  1260. case typ of
  1261. top_ref:
  1262. begin
  1263. if assigned(ref^.symbol) then
  1264. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  1265. else
  1266. begin
  1267. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1268. internalerror(2021092018);
  1269. WriteSleb(int64(ref^.offset));
  1270. end;
  1271. end;
  1272. top_const:
  1273. WriteSleb(int64(val));
  1274. else
  1275. internalerror(2021092615);
  1276. end;
  1277. end;
  1278. a_f32_const:
  1279. begin
  1280. if ops<>1 then
  1281. internalerror(2021092619);
  1282. WriteByte($43);
  1283. with oper[0]^ do
  1284. case typ of
  1285. top_single:
  1286. WriteSingle(sval);
  1287. else
  1288. internalerror(2021092618);
  1289. end;
  1290. end;
  1291. a_f64_const:
  1292. begin
  1293. if ops<>1 then
  1294. internalerror(2021092616);
  1295. WriteByte($44);
  1296. with oper[0]^ do
  1297. case typ of
  1298. top_double:
  1299. WriteDouble(dval);
  1300. else
  1301. internalerror(2021092617);
  1302. end;
  1303. end;
  1304. a_local_get,
  1305. a_local_set,
  1306. a_local_tee:
  1307. begin
  1308. case opcode of
  1309. a_local_get:
  1310. WriteByte($20);
  1311. a_local_set:
  1312. WriteByte($21);
  1313. a_local_tee:
  1314. WriteByte($22);
  1315. else
  1316. internalerror(2021092003);
  1317. end;
  1318. if ops<>1 then
  1319. internalerror(2021092004);
  1320. with oper[0]^ do
  1321. case typ of
  1322. top_ref:
  1323. begin
  1324. if assigned(ref^.symbol) then
  1325. internalerror(2021092005);
  1326. if ref^.base<>NR_STACK_POINTER_REG then
  1327. internalerror(2021092006);
  1328. if ref^.index<>NR_NO then
  1329. internalerror(2021092007);
  1330. WriteUleb(ref^.offset);
  1331. end;
  1332. else
  1333. internalerror(2021092008);
  1334. end;
  1335. end;
  1336. a_global_get,
  1337. a_global_set:
  1338. begin
  1339. case opcode of
  1340. a_global_get:
  1341. WriteByte($23);
  1342. a_global_set:
  1343. WriteByte($24);
  1344. else
  1345. internalerror(2021092009);
  1346. end;
  1347. if ops<>1 then
  1348. internalerror(2021092010);
  1349. with oper[0]^ do
  1350. case typ of
  1351. top_ref:
  1352. begin
  1353. if not assigned(ref^.symbol) then
  1354. internalerror(2021092012);
  1355. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1356. internalerror(2021092013);
  1357. objdata.writeReloc(0,5,TWasmObjData(ObjData).globalref(ref^.symbol),RELOC_GLOBAL_INDEX_LEB);
  1358. end;
  1359. else
  1360. internalerror(2021092011);
  1361. end;
  1362. end;
  1363. a_end_function:
  1364. ;
  1365. a_block,
  1366. a_loop,
  1367. a_if,
  1368. a_try:
  1369. begin
  1370. case opcode of
  1371. a_block:
  1372. WriteByte($02);
  1373. a_loop:
  1374. WriteByte($03);
  1375. a_if:
  1376. WriteByte($04);
  1377. a_try:
  1378. WriteByte($06);
  1379. else
  1380. internalerror(2021092626);
  1381. end;
  1382. if ops=0 then
  1383. WriteByte($40)
  1384. else
  1385. begin
  1386. if ops<>1 then
  1387. internalerror(2021092015);
  1388. with oper[0]^ do
  1389. case typ of
  1390. top_functype:
  1391. begin
  1392. if (length(functype.params)=0) and (length(functype.results)<=1) then
  1393. begin
  1394. if length(functype.results)=1 then
  1395. WriteByte(encode_wasm_basic_type(functype.results[0]))
  1396. else
  1397. WriteByte($40);
  1398. end
  1399. else
  1400. { more complex blocktypes are not yet implemented }
  1401. internalerror(2021092621);
  1402. end;
  1403. else
  1404. internalerror(2021092620);
  1405. end;
  1406. end;
  1407. end;
  1408. a_else:
  1409. WriteByte($05);
  1410. a_i32_load,
  1411. a_i64_load,
  1412. a_f32_load,
  1413. a_f64_load,
  1414. a_i32_load8_s,
  1415. a_i32_load8_u,
  1416. a_i32_load16_s,
  1417. a_i32_load16_u,
  1418. a_i64_load8_s,
  1419. a_i64_load8_u,
  1420. a_i64_load16_s,
  1421. a_i64_load16_u,
  1422. a_i64_load32_s,
  1423. a_i64_load32_u,
  1424. a_i32_store,
  1425. a_i64_store,
  1426. a_f32_store,
  1427. a_f64_store,
  1428. a_i32_store8,
  1429. a_i32_store16,
  1430. a_i64_store8,
  1431. a_i64_store16,
  1432. a_i64_store32:
  1433. begin
  1434. case opcode of
  1435. a_i32_load:
  1436. WriteByte($28);
  1437. a_i64_load:
  1438. WriteByte($29);
  1439. a_f32_load:
  1440. WriteByte($2A);
  1441. a_f64_load:
  1442. WriteByte($2B);
  1443. a_i32_load8_s:
  1444. WriteByte($2C);
  1445. a_i32_load8_u:
  1446. WriteByte($2D);
  1447. a_i32_load16_s:
  1448. WriteByte($2E);
  1449. a_i32_load16_u:
  1450. WriteByte($2F);
  1451. a_i64_load8_s:
  1452. WriteByte($30);
  1453. a_i64_load8_u:
  1454. WriteByte($31);
  1455. a_i64_load16_s:
  1456. WriteByte($32);
  1457. a_i64_load16_u:
  1458. WriteByte($33);
  1459. a_i64_load32_s:
  1460. WriteByte($34);
  1461. a_i64_load32_u:
  1462. WriteByte($35);
  1463. a_i32_store:
  1464. WriteByte($36);
  1465. a_i64_store:
  1466. WriteByte($37);
  1467. a_f32_store:
  1468. WriteByte($38);
  1469. a_f64_store:
  1470. WriteByte($39);
  1471. a_i32_store8:
  1472. WriteByte($3A);
  1473. a_i32_store16:
  1474. WriteByte($3B);
  1475. a_i64_store8:
  1476. WriteByte($3C);
  1477. a_i64_store16:
  1478. WriteByte($3D);
  1479. a_i64_store32:
  1480. WriteByte($3E);
  1481. else
  1482. internalerror(2021092019);
  1483. end;
  1484. if ops<>1 then
  1485. internalerror(2021092016);
  1486. with oper[0]^ do
  1487. case typ of
  1488. top_ref:
  1489. begin
  1490. if assigned(ref^.symbol) then
  1491. begin
  1492. WriteUleb(natural_alignment_for_load_store(opcode));
  1493. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  1494. end
  1495. else
  1496. begin
  1497. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1498. internalerror(2021092018);
  1499. WriteUleb(natural_alignment_for_load_store(opcode));
  1500. WriteUleb(ref^.offset);
  1501. end;
  1502. end;
  1503. top_const:
  1504. begin
  1505. WriteUleb(natural_alignment_for_load_store(opcode));
  1506. WriteUleb(val);
  1507. end;
  1508. else
  1509. internalerror(2021092017);
  1510. end;
  1511. end;
  1512. a_memory_atomic_notify,
  1513. a_memory_atomic_wait32,
  1514. a_memory_atomic_wait64,
  1515. a_i32_atomic_load,
  1516. a_i64_atomic_load,
  1517. a_i32_atomic_load8_u,
  1518. a_i32_atomic_load16_u,
  1519. a_i64_atomic_load8_u,
  1520. a_i64_atomic_load16_u,
  1521. a_i64_atomic_load32_u,
  1522. a_i32_atomic_store,
  1523. a_i64_atomic_store,
  1524. a_i32_atomic_store8,
  1525. a_i32_atomic_store16,
  1526. a_i64_atomic_store8,
  1527. a_i64_atomic_store16,
  1528. a_i64_atomic_store32,
  1529. a_i32_atomic_rmw_add,
  1530. a_i64_atomic_rmw_add,
  1531. a_i32_atomic_rmw8_add_u,
  1532. a_i32_atomic_rmw16_add_u,
  1533. a_i64_atomic_rmw8_add_u,
  1534. a_i64_atomic_rmw16_add_u,
  1535. a_i64_atomic_rmw32_add_u,
  1536. a_i32_atomic_rmw_sub,
  1537. a_i64_atomic_rmw_sub,
  1538. a_i32_atomic_rmw8_sub_u,
  1539. a_i32_atomic_rmw16_sub_u,
  1540. a_i64_atomic_rmw8_sub_u,
  1541. a_i64_atomic_rmw16_sub_u,
  1542. a_i64_atomic_rmw32_sub_u,
  1543. a_i32_atomic_rmw_and,
  1544. a_i64_atomic_rmw_and,
  1545. a_i32_atomic_rmw8_and_u,
  1546. a_i32_atomic_rmw16_and_u,
  1547. a_i64_atomic_rmw8_and_u,
  1548. a_i64_atomic_rmw16_and_u,
  1549. a_i64_atomic_rmw32_and_u,
  1550. a_i32_atomic_rmw_or,
  1551. a_i64_atomic_rmw_or,
  1552. a_i32_atomic_rmw8_or_u,
  1553. a_i32_atomic_rmw16_or_u,
  1554. a_i64_atomic_rmw8_or_u,
  1555. a_i64_atomic_rmw16_or_u,
  1556. a_i64_atomic_rmw32_or_u,
  1557. a_i32_atomic_rmw_xor,
  1558. a_i64_atomic_rmw_xor,
  1559. a_i32_atomic_rmw8_xor_u,
  1560. a_i32_atomic_rmw16_xor_u,
  1561. a_i64_atomic_rmw8_xor_u,
  1562. a_i64_atomic_rmw16_xor_u,
  1563. a_i64_atomic_rmw32_xor_u,
  1564. a_i32_atomic_rmw_xchg,
  1565. a_i64_atomic_rmw_xchg,
  1566. a_i32_atomic_rmw8_xchg_u,
  1567. a_i32_atomic_rmw16_xchg_u,
  1568. a_i64_atomic_rmw8_xchg_u,
  1569. a_i64_atomic_rmw16_xchg_u,
  1570. a_i64_atomic_rmw32_xchg_u,
  1571. a_i32_atomic_rmw_cmpxchg,
  1572. a_i64_atomic_rmw_cmpxchg,
  1573. a_i32_atomic_rmw8_cmpxchg_u,
  1574. a_i32_atomic_rmw16_cmpxchg_u,
  1575. a_i64_atomic_rmw8_cmpxchg_u,
  1576. a_i64_atomic_rmw16_cmpxchg_u,
  1577. a_i64_atomic_rmw32_cmpxchg_u:
  1578. begin
  1579. WriteByte($FE);
  1580. case opcode of
  1581. a_memory_atomic_notify:
  1582. WriteByte($00);
  1583. a_memory_atomic_wait32:
  1584. WriteByte($01);
  1585. a_memory_atomic_wait64:
  1586. WriteByte($02);
  1587. a_i32_atomic_load:
  1588. WriteByte($10);
  1589. a_i64_atomic_load:
  1590. WriteByte($11);
  1591. a_i32_atomic_load8_u:
  1592. WriteByte($12);
  1593. a_i32_atomic_load16_u:
  1594. WriteByte($13);
  1595. a_i64_atomic_load8_u:
  1596. WriteByte($14);
  1597. a_i64_atomic_load16_u:
  1598. WriteByte($15);
  1599. a_i64_atomic_load32_u:
  1600. WriteByte($16);
  1601. a_i32_atomic_store:
  1602. WriteByte($17);
  1603. a_i64_atomic_store:
  1604. WriteByte($18);
  1605. a_i32_atomic_store8:
  1606. WriteByte($19);
  1607. a_i32_atomic_store16:
  1608. WriteByte($1A);
  1609. a_i64_atomic_store8:
  1610. WriteByte($1B);
  1611. a_i64_atomic_store16:
  1612. WriteByte($1C);
  1613. a_i64_atomic_store32:
  1614. WriteByte($1D);
  1615. a_i32_atomic_rmw_add:
  1616. WriteByte($1E);
  1617. a_i64_atomic_rmw_add:
  1618. WriteByte($1F);
  1619. a_i32_atomic_rmw8_add_u:
  1620. WriteByte($20);
  1621. a_i32_atomic_rmw16_add_u:
  1622. WriteByte($21);
  1623. a_i64_atomic_rmw8_add_u:
  1624. WriteByte($22);
  1625. a_i64_atomic_rmw16_add_u:
  1626. WriteByte($23);
  1627. a_i64_atomic_rmw32_add_u:
  1628. WriteByte($24);
  1629. a_i32_atomic_rmw_sub:
  1630. WriteByte($25);
  1631. a_i64_atomic_rmw_sub:
  1632. WriteByte($26);
  1633. a_i32_atomic_rmw8_sub_u:
  1634. WriteByte($27);
  1635. a_i32_atomic_rmw16_sub_u:
  1636. WriteByte($28);
  1637. a_i64_atomic_rmw8_sub_u:
  1638. WriteByte($29);
  1639. a_i64_atomic_rmw16_sub_u:
  1640. WriteByte($2A);
  1641. a_i64_atomic_rmw32_sub_u:
  1642. WriteByte($2B);
  1643. a_i32_atomic_rmw_and:
  1644. WriteByte($2C);
  1645. a_i64_atomic_rmw_and:
  1646. WriteByte($2D);
  1647. a_i32_atomic_rmw8_and_u:
  1648. WriteByte($2E);
  1649. a_i32_atomic_rmw16_and_u:
  1650. WriteByte($2F);
  1651. a_i64_atomic_rmw8_and_u:
  1652. WriteByte($30);
  1653. a_i64_atomic_rmw16_and_u:
  1654. WriteByte($31);
  1655. a_i64_atomic_rmw32_and_u:
  1656. WriteByte($32);
  1657. a_i32_atomic_rmw_or:
  1658. WriteByte($33);
  1659. a_i64_atomic_rmw_or:
  1660. WriteByte($34);
  1661. a_i32_atomic_rmw8_or_u:
  1662. WriteByte($35);
  1663. a_i32_atomic_rmw16_or_u:
  1664. WriteByte($36);
  1665. a_i64_atomic_rmw8_or_u:
  1666. WriteByte($37);
  1667. a_i64_atomic_rmw16_or_u:
  1668. WriteByte($38);
  1669. a_i64_atomic_rmw32_or_u:
  1670. WriteByte($39);
  1671. a_i32_atomic_rmw_xor:
  1672. WriteByte($3A);
  1673. a_i64_atomic_rmw_xor:
  1674. WriteByte($3B);
  1675. a_i32_atomic_rmw8_xor_u:
  1676. WriteByte($3C);
  1677. a_i32_atomic_rmw16_xor_u:
  1678. WriteByte($3D);
  1679. a_i64_atomic_rmw8_xor_u:
  1680. WriteByte($3E);
  1681. a_i64_atomic_rmw16_xor_u:
  1682. WriteByte($3F);
  1683. a_i64_atomic_rmw32_xor_u:
  1684. WriteByte($40);
  1685. a_i32_atomic_rmw_xchg:
  1686. WriteByte($41);
  1687. a_i64_atomic_rmw_xchg:
  1688. WriteByte($42);
  1689. a_i32_atomic_rmw8_xchg_u:
  1690. WriteByte($43);
  1691. a_i32_atomic_rmw16_xchg_u:
  1692. WriteByte($44);
  1693. a_i64_atomic_rmw8_xchg_u:
  1694. WriteByte($45);
  1695. a_i64_atomic_rmw16_xchg_u:
  1696. WriteByte($46);
  1697. a_i64_atomic_rmw32_xchg_u:
  1698. WriteByte($47);
  1699. a_i32_atomic_rmw_cmpxchg:
  1700. WriteByte($48);
  1701. a_i64_atomic_rmw_cmpxchg:
  1702. WriteByte($49);
  1703. a_i32_atomic_rmw8_cmpxchg_u:
  1704. WriteByte($4A);
  1705. a_i32_atomic_rmw16_cmpxchg_u:
  1706. WriteByte($4B);
  1707. a_i64_atomic_rmw8_cmpxchg_u:
  1708. WriteByte($4C);
  1709. a_i64_atomic_rmw16_cmpxchg_u:
  1710. WriteByte($4D);
  1711. a_i64_atomic_rmw32_cmpxchg_u:
  1712. WriteByte($4E);
  1713. else
  1714. internalerror(2022052101);
  1715. end;
  1716. if ops<>1 then
  1717. internalerror(2021092016);
  1718. with oper[0]^ do
  1719. case typ of
  1720. top_ref:
  1721. begin
  1722. if assigned(ref^.symbol) then
  1723. begin
  1724. WriteUleb(natural_alignment_for_load_store(opcode));
  1725. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  1726. end
  1727. else
  1728. begin
  1729. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1730. internalerror(2021092018);
  1731. WriteUleb(natural_alignment_for_load_store(opcode));
  1732. WriteUleb(ref^.offset);
  1733. end;
  1734. end;
  1735. top_const:
  1736. begin
  1737. WriteUleb(natural_alignment_for_load_store(opcode));
  1738. WriteUleb(val);
  1739. end;
  1740. else
  1741. internalerror(2021092017);
  1742. end;
  1743. end;
  1744. a_call:
  1745. begin
  1746. if ops<>1 then
  1747. internalerror(2021092021);
  1748. with oper[0]^ do
  1749. case typ of
  1750. top_ref:
  1751. begin
  1752. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1753. internalerror(2021092023);
  1754. WriteByte($10);
  1755. objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
  1756. end;
  1757. else
  1758. internalerror(2021092022);
  1759. end;
  1760. end;
  1761. a_call_indirect:
  1762. begin
  1763. if ops<>1 then
  1764. internalerror(2021092610);
  1765. with oper[0]^ do
  1766. case typ of
  1767. top_functype:
  1768. begin
  1769. WriteByte($11);
  1770. objdata.writeReloc(TWasmObjData(objdata).AddFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
  1771. WriteUleb(0);
  1772. end;
  1773. else
  1774. internalerror(2021092611);
  1775. end;
  1776. end;
  1777. a_br,
  1778. a_br_if,
  1779. a_rethrow,
  1780. a_delegate:
  1781. begin
  1782. case opcode of
  1783. a_br:
  1784. WriteByte($0C);
  1785. a_br_if:
  1786. WriteByte($0D);
  1787. a_rethrow:
  1788. WriteByte($09);
  1789. a_delegate:
  1790. WriteByte($18);
  1791. else
  1792. internalerror(2021092622);
  1793. end;
  1794. if ops<>1 then
  1795. internalerror(2021092610);
  1796. with oper[0]^ do
  1797. case typ of
  1798. top_const:
  1799. WriteUleb(val);
  1800. else
  1801. internalerror(2021092625);
  1802. end;
  1803. end;
  1804. a_catch,
  1805. a_throw:
  1806. begin
  1807. case opcode of
  1808. a_catch:
  1809. WriteByte($07);
  1810. a_throw:
  1811. WriteByte($08);
  1812. else
  1813. internalerror(2021092708);
  1814. end;
  1815. if ops<>1 then
  1816. internalerror(2021092709);
  1817. with oper[0]^ do
  1818. case typ of
  1819. top_ref:
  1820. begin
  1821. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1822. internalerror(2021092711);
  1823. objdata.writeReloc(0,5,TWasmObjData(ObjData).ExceptionTagRef(ref^.symbol),RELOC_TAG_INDEX_LEB);
  1824. end;
  1825. else
  1826. internalerror(2021092710);
  1827. end;
  1828. end;
  1829. a_memory_init:
  1830. begin
  1831. WriteByte($FC);
  1832. WriteByte($08);
  1833. if ops<>1 then
  1834. internalerror(2022052806);
  1835. with oper[0]^ do
  1836. case typ of
  1837. top_const:
  1838. WriteUleb(val);
  1839. else
  1840. internalerror(2022052807);
  1841. end;
  1842. WriteByte($00);
  1843. end;
  1844. a_data_drop:
  1845. begin
  1846. WriteByte($FC);
  1847. WriteByte($09);
  1848. if ops<>1 then
  1849. internalerror(2022052808);
  1850. with oper[0]^ do
  1851. case typ of
  1852. top_const:
  1853. WriteUleb(val);
  1854. else
  1855. internalerror(2022052809);
  1856. end;
  1857. end;
  1858. a_i32_trunc_sat_f32_s:
  1859. begin
  1860. WriteByte($FC);
  1861. WriteByte($00);
  1862. end;
  1863. a_i32_trunc_sat_f32_u:
  1864. begin
  1865. WriteByte($FC);
  1866. WriteByte($01);
  1867. end;
  1868. a_i32_trunc_sat_f64_s:
  1869. begin
  1870. WriteByte($FC);
  1871. WriteByte($02);
  1872. end;
  1873. a_i32_trunc_sat_f64_u:
  1874. begin
  1875. WriteByte($FC);
  1876. WriteByte($03);
  1877. end;
  1878. a_i64_trunc_sat_f32_s:
  1879. begin
  1880. WriteByte($FC);
  1881. WriteByte($04);
  1882. end;
  1883. a_i64_trunc_sat_f32_u:
  1884. begin
  1885. WriteByte($FC);
  1886. WriteByte($05);
  1887. end;
  1888. a_i64_trunc_sat_f64_s:
  1889. begin
  1890. WriteByte($FC);
  1891. WriteByte($06);
  1892. end;
  1893. a_i64_trunc_sat_f64_u:
  1894. begin
  1895. WriteByte($FC);
  1896. WriteByte($07);
  1897. end;
  1898. a_ref_null_funcref:
  1899. begin
  1900. WriteByte($D0);
  1901. WriteByte($70);
  1902. end;
  1903. a_ref_null_externref:
  1904. begin
  1905. WriteByte($D0);
  1906. WriteByte($6F);
  1907. end;
  1908. a_ref_is_null:
  1909. WriteByte($D1);
  1910. else
  1911. internalerror(2021092624);
  1912. end;
  1913. end;
  1914. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  1915. begin
  1916. internalerror(2010122614);
  1917. result:=nil;
  1918. end;
  1919. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  1920. begin
  1921. internalerror(2010122615);
  1922. result:=nil;
  1923. end;
  1924. procedure InitAsm;
  1925. begin
  1926. end;
  1927. procedure DoneAsm;
  1928. begin
  1929. end;
  1930. initialization
  1931. cai_cpu:=taicpu;
  1932. cai_align:=tai_align;
  1933. end.