aasmcpu.pas 97 KB

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