aasmcpu.pas 109 KB

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