aasmcpu.pas 113 KB

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