aasmcpu.pas 95 KB

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