aasmcpu.pas 89 KB

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