aasmcpu.pas 115 KB

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