aasmcpu.pas 75 KB

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