aasmcpu.pas 88 KB

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