aasmcpu.pas 96 KB

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