aasmcpu.pas 108 KB

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