aasmcpu.pas 79 KB

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