aasmcpu.pas 79 KB

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