aasmcpu.pas 77 KB

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