aasmcpu.pas 89 KB

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