aasmcpu.pas 88 KB

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