aasmcpu.pas 86 KB

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