aasmcpu.pas 75 KB

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