aasmcpu.pas 106 KB

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