aasmcpu.pas 91 KB

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