aasmcpu.pas 94 KB

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