12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047 |
- {
- Copyright (c) 2019 by Free Pascal and Lazarus foundation
- Contains the assembler object for the WebAssembly
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit aasmcpu;
- {$i fpcdefs.inc}
- interface
- uses
- cclasses,
- globtype,globals,verbose,
- aasmbase,aasmtai,aasmdata,aasmsym,
- cgbase,cgutils,cpubase,cpuinfo,ogbase,
- symtype,
- widestr;
- { fake, there are no "mov reg,reg" instructions here }
- const
- { "mov reg,reg" source operand number }
- O_MOV_SOURCE = 0;
- { "mov reg,reg" source operand number }
- O_MOV_DEST = 0;
- type
- { TWasmValueStack }
- TWasmValueStack = class
- private
- FValStack: array of TWasmBasicType;
- function GetItems(AIndex: Integer): TWasmBasicType;
- procedure SetItems(AIndex: Integer; AValue: TWasmBasicType);
- public
- procedure Push(wbt: TWasmBasicType);
- function Pop: TWasmBasicType;
- property Items[AIndex: Integer]: TWasmBasicType read GetItems write SetItems; default;
- end;
- twasmstruc_stack = class;
- TAsmMapFuncResultType = (amfrtNoChange, amfrtNewAi, amfrtNewList, amfrtDeleteAi);
- TAsmMapFuncResult = record
- case typ: TAsmMapFuncResultType of
- amfrtNoChange: ();
- amfrtNewAi: (newai: tai);
- amfrtNewList: (newlist: TAsmList);
- amfrtDeleteAi: ();
- end;
- TAsmMapFunc = function(ai: tai; blockstack: twasmstruc_stack): TAsmMapFuncResult of object;
- TWasmLocalAllocator = function(wbt: TWasmBasicType): Integer of object;
- { taicpu }
- taicpu = class(tai_cpu_abstract_sym)
- is_br_generated_by_goto: boolean;
- constructor op_none(op : tasmop);
- constructor op_reg(op : tasmop;_op1 : tregister);
- constructor op_const(op : tasmop;_op1 : aint);
- constructor op_ref(op : tasmop;const _op1 : treference);
- constructor op_sym(op : tasmop;_op1 : tasmsymbol);
- constructor op_sym_const(op : tasmop;_op1 : tasmsymbol;_op2 : aint);
- constructor op_single(op : tasmop;_op1 : single);
- constructor op_double(op : tasmop;_op1 : double);
- constructor op_functype(op : tasmop; _op1: TWasmFuncType);
- procedure loadfunctype(opidx:longint;ft:TWasmFuncType);
- procedure loadsingle(opidx:longint;f:single);
- procedure loaddouble(opidx:longint;d:double);
- { register allocation }
- function is_same_reg_move(regtype: Tregistertype):boolean; override;
- { register spilling code }
- function spilling_get_operation_type(opnr: longint): topertype;override;
- function Pass1(objdata:TObjData):longint;override;
- procedure Pass2(objdata:TObjData);override;
- end;
- taiwstype = (
- aitws_if,
- aitws_block,
- aitws_loop,
- aitws_try_delegate,
- aitws_try_catch
- );
- { taicpu_wasm_structured_instruction }
- taicpu_wasm_structured_instruction = class(tai)
- protected
- FLabel: TAsmLabel;
- FLabelIsNew: Boolean;
- public
- wstyp: taiwstype;
- constructor Create;
- procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);virtual;abstract;
- procedure ConvertToFlatList(l: TAsmList);virtual;abstract;
- function getlabel: TAsmLabel;
- end;
- { tai_wasmstruc_if }
- tai_wasmstruc_if = class(taicpu_wasm_structured_instruction)
- if_instr: taicpu;
- then_asmlist: TAsmList;
- else_asmlist: TAsmList;
- constructor create_from(a_if_instr: taicpu; srclist: TAsmList);
- destructor Destroy; override;
- function getcopy:TLinkedListItem;override;
- procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
- procedure ConvertToFlatList(l: TAsmList);override;
- procedure ConvertToBrIf(list: TAsmList; local_alloc: TWasmLocalAllocator);
- end;
- { tai_wasmstruc_block }
- tai_wasmstruc_block = class(taicpu_wasm_structured_instruction)
- block_instr: taicpu;
- inner_asmlist: TAsmList;
- constructor create_from(a_block_instr: taicpu; srclist: TAsmList);
- destructor Destroy; override;
- function getcopy:TLinkedListItem;override;
- procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
- procedure ConvertToFlatList(l: TAsmList);override;
- end;
- { tai_wasmstruc_loop }
- tai_wasmstruc_loop = class(taicpu_wasm_structured_instruction)
- loop_instr: taicpu;
- inner_asmlist: TAsmList;
- constructor create_from(a_loop_instr: taicpu; srclist: TAsmList);
- destructor Destroy; override;
- function getcopy:TLinkedListItem;override;
- procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
- procedure ConvertToBr(list: TAsmList);
- procedure ConvertToFlatList(l: TAsmList);override;
- end;
- { tai_wasmstruc_try }
- tai_wasmstruc_try = class(taicpu_wasm_structured_instruction)
- private
- class function create_from(srclist: TAsmList): tai_wasmstruc_try;
- public
- try_asmlist: TAsmList;
- constructor internal_create(a_try_asmlist: TAsmList);
- destructor Destroy; override;
- function getcopy:TLinkedListItem;override;
- procedure ConvertToFlatList(l: TAsmList);override;
- end;
- { tai_wasmstruc_try_delegate }
- tai_wasmstruc_try_delegate = class(tai_wasmstruc_try)
- delegate_instr: taicpu;
- constructor internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
- destructor Destroy; override;
- function getcopy:TLinkedListItem;override;
- procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
- procedure ConvertToFlatList(l: TAsmList);override;
- end;
- { tai_wasmstruc_try_catch }
- tai_wasmstruc_try_catch = class(tai_wasmstruc_try)
- catch_list: array of record
- catch_instr: taicpu;
- asmlist: TAsmList;
- end;
- catch_all_asmlist: TAsmList;
- constructor internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
- destructor Destroy; override;
- function getcopy:TLinkedListItem;override;
- procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
- procedure ConvertToFlatList(l: TAsmList);override;
- end;
- { twasmstruc_stack }
- twasmstruc_stack = class
- private
- FStack: array of taicpu_wasm_structured_instruction;
- function Get(Index: Integer): taicpu_wasm_structured_instruction;
- public
- procedure push(ins: taicpu_wasm_structured_instruction);
- procedure pop;
- property Items [Index: Integer]: taicpu_wasm_structured_instruction read Get;default;
- end;
- tai_align = class(tai_align_abstract)
- { nothing to add }
- end;
- TImpExpType= (
- ie_Func, // functions
- ie_Table, // tables (arrays of methods)
- ie_Memory, // memory reference
- ie_Global // global variables
- );
- { tai_export_name }
- tai_export_name = class(tai)
- extname : ansistring; // external name
- intname : ansistring; // internal name
- symstype: TImpExpType;
- constructor create(const aextname, aintname: ansistring; asymtype: timpexptype);
- end;
- // local variable declaration
- { tai_local }
- tai_local = class(tai)
- bastyp: TWasmBasicType;
- name : string;
- first: boolean;
- last: boolean;
- constructor create(abasictype: TWasmBasicType; const aname: string = '');
- end;
- { tai_globaltype }
- tai_globaltype = class(tai)
- globalname: string;
- gtype: TWasmBasicType;
- immutable: boolean;
- is_external: boolean;
- is_global: boolean;
- sym : tasmsymbol;
- constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean);
- constructor create_local(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
- constructor create_global(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
- end;
- { tai_functype }
- tai_functype = class(tai)
- funcname: string;
- functype: TWasmFuncType;
- constructor create(const afuncname: string; afunctype: TWasmFuncType);
- destructor destroy;override;
- end;
- { tai_tagtype }
- tai_tagtype = class(tai)
- tagname: string;
- params: TWasmResultType;
- constructor create(const atagname: string; aparams: TWasmResultType);
- end;
- { tai_import_module }
- tai_import_module = class(tai)
- symname: string;
- importmodule: string;
- constructor create(const asymname, aimportmodule: string);
- end;
- { tai_import_name }
- tai_import_name = class(tai)
- symname: string;
- importname: string;
- constructor create(const asymname, aimportname: string);
- end;
- procedure InitAsm;
- procedure DoneAsm;
- function spilling_create_load(const ref:treference;r:tregister):Taicpu;
- function spilling_create_store(r:tregister; const ref:treference):Taicpu;
- procedure wasm_convert_to_structured_asmlist(var asmlist: TAsmList);
- procedure wasm_convert_to_flat_asmlist(var asmlist: TAsmList);
- procedure map_structured_asmlist(l: TAsmList; f: TAsmMapFunc);
- implementation
- uses
- ogwasm;
- function wasm_convert_first_item_to_structured(srclist: TAsmList): tai; forward;
- procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack); forward;
- { TWasmValueStack }
- function TWasmValueStack.GetItems(AIndex: Integer): TWasmBasicType;
- begin
- if (AIndex<Low(FValStack)) or (AIndex>High(FValStack)) then
- internalerror(2024011702);
- Result:=FValStack[AIndex];
- end;
- procedure TWasmValueStack.SetItems(AIndex: Integer; AValue: TWasmBasicType);
- begin
- if (AIndex<Low(FValStack)) or (AIndex>High(FValStack)) then
- internalerror(2024011703);
- FValStack[AIndex]:=AValue;
- end;
- procedure TWasmValueStack.Push(wbt: TWasmBasicType);
- begin
- SetLength(FValStack,Length(FValStack)+1);
- FValStack[High(FValStack)]:=wbt;
- end;
- function TWasmValueStack.Pop: TWasmBasicType;
- begin
- if Length(FValStack)=0 then
- internalerror(2024011701);
- Result:=FValStack[High(FValStack)];
- SetLength(FValStack,Length(FValStack)-1);
- end;
- { twasmstruc_stack }
- function twasmstruc_stack.Get(Index: Integer): taicpu_wasm_structured_instruction;
- begin
- {$push}{$r+,q+}
- Result:=FStack[High(FStack)-Index];
- {$pop}
- end;
- procedure twasmstruc_stack.push(ins: taicpu_wasm_structured_instruction);
- begin
- SetLength(FStack,Length(FStack)+1);
- FStack[High(FStack)]:=ins;
- end;
- procedure twasmstruc_stack.pop;
- begin
- SetLength(FStack,Length(FStack)-1);
- end;
- { taicpu_wasm_structured_instruction }
- constructor taicpu_wasm_structured_instruction.Create;
- begin
- inherited;
- typ:=ait_wasm_structured_instruction;
- end;
- function taicpu_wasm_structured_instruction.getlabel: TAsmLabel;
- begin
- if not assigned(FLabel) then
- begin
- current_asmdata.getjumplabel(FLabel);
- FLabelIsNew:=true;
- end;
- result:=FLabel;
- end;
- { tai_wasmstruc_if }
- constructor tai_wasmstruc_if.create_from(a_if_instr: taicpu; srclist: TAsmList);
- var
- p: tai;
- ThenDone, ElsePresent, ElseDone: Boolean;
- begin
- wstyp:=aitws_if;
- inherited Create;
- if assigned(a_if_instr.Previous) or assigned(a_if_instr.Next) then
- internalerror(2023100301);
- if_instr:=a_if_instr;
- then_asmlist:=TAsmList.Create;
- ThenDone:=False;
- ElsePresent:=False;
- repeat
- p:=tai(srclist.First);
- if not assigned(p) then
- internalerror(2023100302);
- 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
- begin
- srclist.Remove(p);
- case taicpu(p).opcode of
- a_else:
- begin
- ThenDone:=True;
- ElsePresent:=True;
- end;
- a_end_if:
- ThenDone:=True;
- else
- internalerror(2023100501);
- end;
- end
- else
- then_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
- until ThenDone;
- if ElsePresent then
- begin
- else_asmlist:=TAsmList.Create;
- ElseDone:=False;
- repeat
- p:=tai(srclist.First);
- if not assigned(p) then
- internalerror(2023100303);
- if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_if) then
- begin
- srclist.Remove(p);
- ElseDone:=True;
- end
- else
- else_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
- until ElseDone;
- end;
- end;
- destructor tai_wasmstruc_if.Destroy;
- begin
- then_asmlist.free;
- else_asmlist.free;
- if_instr.free;
- inherited Destroy;
- end;
- function tai_wasmstruc_if.getcopy: TLinkedListItem;
- var
- p: tai_wasmstruc_if;
- begin
- p:=tai_wasmstruc_if(inherited getcopy);
- if assigned(if_instr) then
- p.if_instr:=taicpu(if_instr.getcopy);
- if assigned(then_asmlist) then
- begin
- p.then_asmlist:=TAsmList.Create;
- p.then_asmlist.concatListcopy(then_asmlist);
- end;
- if assigned(else_asmlist) then
- begin
- p.else_asmlist:=TAsmList.Create;
- p.else_asmlist.concatListcopy(else_asmlist);
- end;
- getcopy:=p;
- end;
- procedure tai_wasmstruc_if.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
- begin
- blockstack.push(self);
- map_structured_asmlist_inner(then_asmlist,f,blockstack);
- map_structured_asmlist_inner(else_asmlist,f,blockstack);
- blockstack.pop;
- end;
- procedure tai_wasmstruc_if.ConvertToFlatList(l: TAsmList);
- begin
- l.Concat(if_instr);
- if_instr:=nil;
- l.concatList(then_asmlist);
- if assigned(else_asmlist) then
- begin
- l.Concat(taicpu.op_none(A_ELSE));
- l.concatList(else_asmlist);
- end;
- l.Concat(taicpu.op_none(a_end_if));
- if FLabelIsNew then
- l.concat(tai_label.create(FLabel));
- end;
- procedure tai_wasmstruc_if.ConvertToBrIf(list: TAsmList; local_alloc: TWasmLocalAllocator);
- var
- res_ft: TWasmFuncType;
- save_if_reg: Integer;
- save_param_reg: array of Integer;
- save_result_reg: array of Integer;
- procedure AllocateLocalsForSavingParamsAndResult;
- var
- i: Integer;
- begin
- if not assigned(res_ft) then
- exit;
- if length(res_ft.params)<>0 then
- begin
- save_if_reg:=local_alloc(wbt_i32);
- SetLength(save_param_reg,length(res_ft.params));
- for i:=low(res_ft.params) to high(res_ft.params) do
- save_param_reg[i]:=local_alloc(res_ft.params[i]);
- end;
- if length(res_ft.results)<>0 then
- begin
- SetLength(save_result_reg,length(res_ft.results));
- for i:=low(res_ft.results) to high(res_ft.results) do
- save_result_reg[i]:=local_alloc(res_ft.results[i]);
- end;
- end;
- procedure SaveParams;
- var
- i: Integer;
- begin
- if (not assigned(res_ft)) or (length(res_ft.params)=0) then
- exit;
- list.concat(taicpu.op_const(a_local_set,save_if_reg));
- for i:=high(res_ft.params) downto low(res_ft.params) do
- list.concat(taicpu.op_const(a_local_set,save_param_reg[i]));
- list.concat(taicpu.op_const(a_local_get,save_if_reg));
- end;
- procedure RestoreParams;
- var
- i: Integer;
- begin
- if (not assigned(res_ft)) or (length(res_ft.params)=0) then
- exit;
- for i:=low(res_ft.params) to high(res_ft.params) do
- list.concat(taicpu.op_const(a_local_get,save_param_reg[i]));
- end;
- procedure SaveResults;
- var
- i: Integer;
- begin
- if (not assigned(res_ft)) or (length(res_ft.results)=0) then
- exit;
- for i:=high(res_ft.results) downto low(res_ft.results) do
- list.concat(taicpu.op_const(a_local_set,save_result_reg[i]));
- end;
- procedure RestoreResults;
- var
- i: Integer;
- begin
- if (not assigned(res_ft)) or (length(res_ft.results)=0) then
- exit;
- for i:=low(res_ft.results) to high(res_ft.results) do
- list.concat(taicpu.op_const(a_local_get,save_result_reg[i]));
- end;
- var
- then_label: TAsmLabel;
- begin
- if if_instr.ops>1 then
- internalerror(2023101701);
- if (if_instr.ops=1) and (if_instr.oper[0]^.typ=top_functype) then
- res_ft:=if_instr.oper[0]^.functype
- else
- res_ft:=nil;
- AllocateLocalsForSavingParamsAndResult;
- current_asmdata.getjumplabel(then_label);
- SaveParams;
- list.concat(taicpu.op_sym(a_br_if,then_label));
- RestoreParams;
- if assigned(else_asmlist) then
- list.concatList(else_asmlist);
- SaveResults;
- list.concat(taicpu.op_sym(a_br, GetLabel));
- list.concat(tai_label.create(then_label));
- RestoreParams;
- list.concatList(then_asmlist);
- SaveResults;
- list.concat(tai_label.create(GetLabel));
- RestoreResults;
- end;
- { tai_wasmstruc_block }
- constructor tai_wasmstruc_block.create_from(a_block_instr: taicpu; srclist: TAsmList);
- var
- Done: Boolean;
- p: tai;
- begin
- wstyp:=aitws_block;
- inherited Create;
- if assigned(a_block_instr.Previous) or assigned(a_block_instr.Next) then
- internalerror(2023100304);
- block_instr:=a_block_instr;
- inner_asmlist:=TAsmList.Create;
- Done:=False;
- repeat
- p:=tai(srclist.First);
- if not assigned(p) then
- internalerror(2023100305);
- if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_block) then
- begin
- srclist.Remove(p);
- Done:=True;
- end
- else
- inner_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
- until Done;
- end;
- destructor tai_wasmstruc_block.Destroy;
- begin
- inner_asmlist.free;
- block_instr.free;
- inherited Destroy;
- end;
- function tai_wasmstruc_block.getcopy: TLinkedListItem;
- var
- p: tai_wasmstruc_block;
- begin
- p:=tai_wasmstruc_block(inherited getcopy);
- if assigned(block_instr) then
- p.block_instr:=taicpu(block_instr.getcopy);
- if assigned(inner_asmlist) then
- begin
- p.inner_asmlist:=TAsmList.Create;
- p.inner_asmlist.concatListcopy(inner_asmlist);
- end;
- getcopy:=p;
- end;
- procedure tai_wasmstruc_block.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
- begin
- blockstack.push(self);
- map_structured_asmlist_inner(inner_asmlist,f,blockstack);
- blockstack.pop;
- end;
- procedure tai_wasmstruc_block.ConvertToFlatList(l: TAsmList);
- begin
- l.Concat(block_instr);
- block_instr:=nil;
- l.concatList(inner_asmlist);
- l.Concat(taicpu.op_none(a_end_block));
- if FLabelIsNew then
- l.concat(tai_label.create(FLabel));
- end;
- { tai_wasmstruc_loop }
- constructor tai_wasmstruc_loop.create_from(a_loop_instr: taicpu; srclist: TAsmList);
- var
- Done: Boolean;
- p: tai;
- begin
- wstyp:=aitws_loop;
- inherited Create;
- if assigned(a_loop_instr.Previous) or assigned(a_loop_instr.Next) then
- internalerror(2023100306);
- loop_instr:=a_loop_instr;
- inner_asmlist:=TAsmList.Create;
- Done:=False;
- repeat
- p:=tai(srclist.First);
- if not assigned(p) then
- internalerror(2023100307);
- if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_loop) then
- begin
- srclist.Remove(p);
- Done:=True;
- end
- else
- inner_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
- until Done;
- end;
- destructor tai_wasmstruc_loop.Destroy;
- begin
- inner_asmlist.free;
- loop_instr.free;
- inherited Destroy;
- end;
- function tai_wasmstruc_loop.getcopy: TLinkedListItem;
- var
- p: tai_wasmstruc_loop;
- begin
- p:=tai_wasmstruc_loop(inherited getcopy);
- if assigned(loop_instr) then
- p.loop_instr:=taicpu(loop_instr.getcopy);
- if assigned(inner_asmlist) then
- begin
- p.inner_asmlist:=TAsmList.Create;
- p.inner_asmlist.concatListcopy(inner_asmlist);
- end;
- getcopy:=p;
- end;
- procedure tai_wasmstruc_loop.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
- begin
- blockstack.push(self);
- map_structured_asmlist_inner(inner_asmlist,f,blockstack);
- blockstack.pop;
- end;
- procedure tai_wasmstruc_loop.ConvertToBr(list: TAsmList);
- begin
- list.concat(tai_label.create(GetLabel));
- list.concatList(inner_asmlist);
- list.concat(taicpu.op_sym(a_br,GetLabel));
- end;
- procedure tai_wasmstruc_loop.ConvertToFlatList(l: TAsmList);
- begin
- l.Concat(loop_instr);
- loop_instr:=nil;
- if FLabelIsNew then
- l.concat(tai_label.create(FLabel));
- l.concatList(inner_asmlist);
- l.Concat(taicpu.op_none(a_end_loop));
- end;
- { tai_wasmstruc_try }
- class function tai_wasmstruc_try.create_from(srclist: TAsmList): tai_wasmstruc_try;
- var
- Done: Boolean;
- p: tai;
- tmp_asmlist: TAsmList;
- begin
- result:=nil;
- tmp_asmlist:=TAsmList.Create;
- Done:=False;
- repeat
- p:=tai(srclist.First);
- if not assigned(p) then
- internalerror(2023100308);
- if (p.typ=ait_instruction) and (taicpu(p).opcode in [a_end_try,a_catch,a_catch_all,a_delegate]) then
- begin
- srclist.Remove(p);
- Done:=True;
- end
- else
- tmp_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
- until Done;
- case taicpu(p).opcode of
- a_end_try,a_catch,a_catch_all:
- result:=tai_wasmstruc_try_catch.internal_create(taicpu(p),tmp_asmlist,srclist);
- a_delegate:
- result:=tai_wasmstruc_try_delegate.internal_create(taicpu(p),tmp_asmlist,srclist);
- else
- internalerror(2023100502);
- end;
- end;
- constructor tai_wasmstruc_try.internal_create(a_try_asmlist: TAsmList);
- begin
- inherited Create;
- try_asmlist:=a_try_asmlist;
- end;
- destructor tai_wasmstruc_try.Destroy;
- begin
- try_asmlist.free;
- inherited Destroy;
- end;
- function tai_wasmstruc_try.getcopy: TLinkedListItem;
- var
- p: tai_wasmstruc_try;
- begin
- p:=tai_wasmstruc_try(inherited getcopy);
- if assigned(try_asmlist) then
- begin
- p.try_asmlist:=TAsmList.Create;
- p.try_asmlist.concatListcopy(try_asmlist);
- end;
- getcopy:=p;
- end;
- procedure tai_wasmstruc_try.ConvertToFlatList(l: TAsmList);
- begin
- l.Concat(taicpu.op_none(A_TRY));
- l.concatList(try_asmlist);
- end;
- { tai_wasmstruc_try_catch }
- constructor tai_wasmstruc_try_catch.internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
- var
- p: tai;
- procedure parse_next_catch_block;
- var
- new_catch_index: Integer;
- al: TAsmList;
- Done: Boolean;
- pp: tai;
- begin
- SetLength(catch_list,Length(catch_list)+1);
- new_catch_index:=High(catch_list);
- catch_list[new_catch_index].catch_instr:=taicpu(p);
- al:=TAsmList.Create;
- catch_list[new_catch_index].asmlist:=al;
- Done:=False;
- repeat
- pp:=tai(srclist.First);
- if (pp.typ=ait_instruction) and (taicpu(pp).opcode in [a_catch,a_catch_all,a_end_try]) then
- Done:=True
- else
- al.Concat(wasm_convert_first_item_to_structured(srclist));
- until Done;
- end;
- procedure parse_catch_all;
- var
- Done: Boolean;
- pp: tai;
- begin
- catch_all_asmlist:=TAsmList.Create;
- Done:=False;
- repeat
- pp:=tai(srclist.First);
- if (pp.typ=ait_instruction) and (taicpu(pp).opcode=a_end_try) then
- begin
- srclist.Remove(pp);
- Done:=True;
- end
- else
- catch_all_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
- until Done;
- end;
- var
- Done: Boolean;
- begin
- wstyp:=aitws_try_catch;
- inherited internal_create(a_try_asmlist);
- if assigned(first_ins.Previous) or assigned(first_ins.Next) then
- internalerror(2023100310);
- Done:=False;
- p:=first_ins;
- repeat
- if p.typ=ait_instruction then
- case taicpu(p).opcode of
- a_catch:
- begin
- parse_next_catch_block;
- p:=tai(srclist.First);
- srclist.Remove(p);
- end;
- a_catch_all:
- begin
- parse_catch_all;
- Done:=True;
- end;
- a_end_try:
- Done:=True;
- else
- internalerror(2023100311);
- end
- else
- internalerror(2023100312);
- until Done;
- end;
- destructor tai_wasmstruc_try_catch.Destroy;
- var
- i: Integer;
- begin
- for i:=low(catch_list) to high(catch_list) do
- begin
- catch_list[i].asmlist.free;
- catch_list[i].catch_instr.free;
- end;
- catch_all_asmlist.free;
- inherited Destroy;
- end;
- function tai_wasmstruc_try_catch.getcopy: TLinkedListItem;
- var
- p: tai_wasmstruc_try_catch;
- i: Integer;
- begin
- p:=tai_wasmstruc_try_catch(inherited getcopy);
- p.catch_list:=Copy(catch_list);
- for i:=0 to length(catch_list)-1 do
- begin
- if assigned(catch_list[i].asmlist) then
- begin
- p.catch_list[i].asmlist:=TAsmList.Create;
- p.catch_list[i].asmlist.concatListcopy(catch_list[i].asmlist);
- end;
- if assigned(catch_list[i].catch_instr) then
- p.catch_list[i].catch_instr:=taicpu(catch_list[i].catch_instr.getcopy);
- end;
- if assigned(catch_all_asmlist) then
- begin
- p.catch_all_asmlist:=TAsmList.Create;
- p.catch_all_asmlist.concatListcopy(catch_all_asmlist);
- end;
- getcopy:=p;
- end;
- procedure tai_wasmstruc_try_catch.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
- var
- i: Integer;
- begin
- blockstack.push(self);
- map_structured_asmlist_inner(try_asmlist,f,blockstack);
- for i:=low(catch_list) to high(catch_list) do
- map_structured_asmlist_inner(catch_list[i].asmlist,f,blockstack);
- map_structured_asmlist_inner(catch_all_asmlist,f,blockstack);
- blockstack.pop;
- end;
- procedure tai_wasmstruc_try_catch.ConvertToFlatList(l: TAsmList);
- var
- i: Integer;
- begin
- inherited ConvertToFlatList(l);
- for i:=low(catch_list) to high(catch_list) do
- begin
- l.Concat(catch_list[i].catch_instr);
- catch_list[i].catch_instr:=nil;
- l.concatList(catch_list[i].asmlist);
- end;
- if assigned(catch_all_asmlist) then
- begin
- l.Concat(taicpu.op_none(a_catch_all));
- l.concatList(catch_all_asmlist);
- end;
- l.Concat(taicpu.op_none(a_end_try));
- if FLabelIsNew then
- l.concat(tai_label.create(FLabel));
- end;
- { tai_wasmstruc_try_delegate }
- constructor tai_wasmstruc_try_delegate.internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
- begin
- wstyp:=aitws_try_delegate;
- inherited internal_create(a_try_asmlist);
- if assigned(first_ins.Previous) or assigned(first_ins.Next) then
- internalerror(2023100309);
- delegate_instr:=first_ins;
- end;
- destructor tai_wasmstruc_try_delegate.Destroy;
- begin
- delegate_instr.free;
- inherited Destroy;
- end;
- function tai_wasmstruc_try_delegate.getcopy: TLinkedListItem;
- var
- p: tai_wasmstruc_try_delegate;
- begin
- p:=tai_wasmstruc_try_delegate(inherited getcopy);
- if assigned(delegate_instr) then
- p.delegate_instr:=taicpu(delegate_instr.getcopy);
- getcopy:=p;
- end;
- procedure tai_wasmstruc_try_delegate.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
- begin
- blockstack.push(self);
- map_structured_asmlist_inner(try_asmlist,f,blockstack);
- blockstack.pop;
- end;
- procedure tai_wasmstruc_try_delegate.ConvertToFlatList(l: TAsmList);
- begin
- inherited ConvertToFlatList(l);
- l.Concat(delegate_instr);
- delegate_instr:=nil;
- if FLabelIsNew then
- l.concat(tai_label.create(FLabel));
- end;
- { tai_globaltype }
- constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
- begin
- inherited Create;
- sym:=current_asmdata.RefAsmSymbol(aglobalname,AT_WASM_GLOBAL);
- typ:=ait_globaltype;
- globalname:=aglobalname;
- gtype:=atype;
- immutable:=aimmutable;
- is_external:=true;
- is_global:=false;
- end;
- constructor tai_globaltype.create_local(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
- begin
- inherited Create;
- sym:=current_asmdata.DefineAsmSymbol(aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def);
- typ:=ait_globaltype;
- globalname:=aglobalname;
- gtype:=atype;
- immutable:=aimmutable;
- is_external:=false;
- is_global:=false;
- end;
- constructor tai_globaltype.create_global(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
- begin
- inherited Create;
- sym:=current_asmdata.DefineAsmSymbol(aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def);
- typ:=ait_globaltype;
- globalname:=aglobalname;
- gtype:=atype;
- immutable:=aimmutable;
- is_external:=false;
- is_global:=true;
- end;
- { tai_import_name }
- constructor tai_import_name.create(const asymname, aimportname: string);
- begin
- inherited Create;
- typ:=ait_import_name;
- symname:=asymname;
- importname:=aimportname;
- end;
- { tai_import_module }
- constructor tai_import_module.create(const asymname, aimportmodule: string);
- begin
- inherited Create;
- typ:=ait_import_module;
- symname:=asymname;
- importmodule:=aimportmodule;
- end;
- { tai_functype }
- constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType);
- begin
- inherited Create;
- typ:=ait_functype;
- funcname:=afuncname;
- functype:=afunctype;
- end;
- destructor tai_functype.destroy;
- begin
- functype.free;
- inherited;
- end;
- { tai_tagtype }
- constructor tai_tagtype.create(const atagname: string; aparams: TWasmResultType);
- begin
- typ:=ait_tagtype;
- tagname:=atagname;
- params:=aparams;
- end;
- { tai_local }
- constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
- begin
- inherited Create;
- bastyp := abasictype;
- typ := ait_local;
- name := aname;
- end;
- { timpexp_ai }
- constructor tai_export_name.create(const aextname, aintname: ansistring;
- asymtype: timpexptype);
- begin
- inherited create;
- typ := ait_export_name;
- extname := aextname;
- intname := aintname;
- symstype:= asymtype;
- end;
- {*****************************************************************************
- taicpu Constructors
- *****************************************************************************}
- constructor taicpu.op_none(op : tasmop);
- begin
- inherited create(op);
- end;
- constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
- begin
- inherited create(op);
- ops:=1;
- {$ifdef EXTDEBUG}
- if getregtype(_op1)=R_INVALIDREGISTER then
- InternalError(2021011901);
- {$endif EXTDEBUG}
- loadreg(0,_op1);
- end;
- constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
- begin
- inherited create(op);
- ops:=1;
- loadref(0,_op1);
- if op in [a_local_get,a_local_set,a_local_tee] then
- begin
- if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
- internalerror(2021010201);
- end
- else
- begin
- if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
- internalerror(2021010202);
- end;
- end;
- constructor taicpu.op_const(op : tasmop;_op1 : aint);
- begin
- inherited create(op);
- ops:=1;
- loadconst(0,_op1);
- end;
- constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
- begin
- inherited create(op);
- ops:=1;
- loadsymbol(0,_op1,0);
- end;
- constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
- begin
- inherited create(op);
- ops:=2;
- loadsymbol(0,_op1,0);
- loadconst(1,_op2);
- end;
- constructor taicpu.op_single(op: tasmop; _op1: single);
- begin
- inherited create(op);
- ops:=1;
- loadsingle(0,_op1);
- end;
- constructor taicpu.op_double(op: tasmop; _op1: double);
- begin
- inherited create(op);
- ops:=1;
- loaddouble(0,_op1);
- end;
- constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
- begin
- inherited create(op);
- ops:=1;
- loadfunctype(0,_op1);
- end;
- procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
- begin
- allocate_oper(opidx+1);
- with oper[opidx]^ do
- begin
- if typ<>top_functype then
- clearop(opidx);
- functype:=ft;
- typ:=top_functype;
- end;
- end;
- procedure taicpu.loadsingle(opidx:longint;f:single);
- begin
- allocate_oper(opidx+1);
- with oper[opidx]^ do
- begin
- if typ<>top_single then
- clearop(opidx);
- sval:=f;
- typ:=top_single;
- end;
- end;
- procedure taicpu.loaddouble(opidx: longint; d: double);
- begin
- allocate_oper(opidx+1);
- with oper[opidx]^ do
- begin
- if typ<>top_double then
- clearop(opidx);
- dval:=d;
- typ:=top_double;
- end;
- end;
- function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
- begin
- result:=false;
- end;
- function taicpu.spilling_get_operation_type(opnr: longint): topertype;
- begin
- if opcode in AsmOp_Store then
- result:=operand_write
- else
- result:=operand_read;
- end;
- function taicpu.Pass1(objdata: TObjData): longint;
- function SlebSize(v: tcgint): longint;
- begin
- result:=0;
- repeat
- v:=SarInt64(v,7);
- Inc(result);
- until ((v=0) and ((byte(v) and 64)=0)) or ((v=-1) and ((byte(v) and 64)<>0));
- end;
- function UlebSize(v: tcgint): longint;
- begin
- result:=0;
- repeat
- v:=v shr 7;
- Inc(result);
- until v=0;
- end;
- begin
- result:=0;
- case opcode of
- a_unreachable,
- a_nop,
- a_return,
- a_drop,
- a_i32_eqz,
- a_i32_eq,
- a_i32_ne,
- a_i32_lt_s,
- a_i32_lt_u,
- a_i32_gt_s,
- a_i32_gt_u,
- a_i32_le_s,
- a_i32_le_u,
- a_i32_ge_s,
- a_i32_ge_u,
- a_i64_eqz,
- a_i64_eq,
- a_i64_ne,
- a_i64_lt_s,
- a_i64_lt_u,
- a_i64_gt_s,
- a_i64_gt_u,
- a_i64_le_s,
- a_i64_le_u,
- a_i64_ge_s,
- a_i64_ge_u,
- a_f32_eq,
- a_f32_ne,
- a_f32_lt,
- a_f32_gt,
- a_f32_le,
- a_f32_ge,
- a_f64_eq,
- a_f64_ne,
- a_f64_lt,
- a_f64_gt,
- a_f64_le,
- a_f64_ge,
- a_i32_clz,
- a_i32_ctz,
- a_i32_popcnt,
- a_i32_add,
- a_i32_sub,
- a_i32_mul,
- a_i32_div_s,
- a_i32_div_u,
- a_i32_rem_s,
- a_i32_rem_u,
- a_i32_and,
- a_i32_or,
- a_i32_xor,
- a_i32_shl,
- a_i32_shr_s,
- a_i32_shr_u,
- a_i32_rotl,
- a_i32_rotr,
- a_i64_clz,
- a_i64_ctz,
- a_i64_popcnt,
- a_i64_add,
- a_i64_sub,
- a_i64_mul,
- a_i64_div_s,
- a_i64_div_u,
- a_i64_rem_s,
- a_i64_rem_u,
- a_i64_and,
- a_i64_or,
- a_i64_xor,
- a_i64_shl,
- a_i64_shr_s,
- a_i64_shr_u,
- a_i64_rotl,
- a_i64_rotr,
- a_f32_abs,
- a_f32_neg,
- a_f32_ceil,
- a_f32_floor,
- a_f32_trunc,
- a_f32_nearest,
- a_f32_sqrt,
- a_f32_add,
- a_f32_sub,
- a_f32_mul,
- a_f32_div,
- a_f32_min,
- a_f32_max,
- a_f32_copysign,
- a_f64_abs,
- a_f64_neg,
- a_f64_ceil,
- a_f64_floor,
- a_f64_trunc,
- a_f64_nearest,
- a_f64_sqrt,
- a_f64_add,
- a_f64_sub,
- a_f64_mul,
- a_f64_div,
- a_f64_min,
- a_f64_max,
- a_f64_copysign,
- a_i32_wrap_i64,
- a_i32_trunc_f32_s,
- a_i32_trunc_f32_u,
- a_i32_trunc_f64_s,
- a_i32_trunc_f64_u,
- a_i64_extend_i32_s,
- a_i64_extend_i32_u,
- a_i64_trunc_f32_s,
- a_i64_trunc_f32_u,
- a_i64_trunc_f64_s,
- a_i64_trunc_f64_u,
- a_f32_convert_i32_s,
- a_f32_convert_i32_u,
- a_f32_convert_i64_s,
- a_f32_convert_i64_u,
- a_f32_demote_f64,
- a_f64_convert_i32_s,
- a_f64_convert_i32_u,
- a_f64_convert_i64_s,
- a_f64_convert_i64_u,
- a_f64_promote_f32,
- a_i32_reinterpret_f32,
- a_i64_reinterpret_f64,
- a_f32_reinterpret_i32,
- a_f64_reinterpret_i64,
- a_i32_extend8_s,
- a_i32_extend16_s,
- a_i64_extend8_s,
- a_i64_extend16_s,
- a_i64_extend32_s,
- a_else,
- a_end_block,
- a_end_if,
- a_end_loop,
- a_end_try,
- a_catch_all,
- a_ref_is_null:
- result:=1;
- a_i32_trunc_sat_f32_s,
- a_i32_trunc_sat_f32_u,
- a_i32_trunc_sat_f64_s,
- a_i32_trunc_sat_f64_u,
- a_i64_trunc_sat_f32_s,
- a_i64_trunc_sat_f32_u,
- a_i64_trunc_sat_f64_s,
- a_i64_trunc_sat_f64_u,
- a_memory_size,
- a_memory_grow,
- a_ref_null_funcref,
- a_ref_null_externref:
- result:=2;
- a_memory_copy:
- result:=4;
- a_memory_fill,
- a_atomic_fence:
- result:=3;
- a_i32_const:
- begin
- if ops<>1 then
- internalerror(2021092001);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- result:=6
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- result:=1+SlebSize(longint(ref^.offset));
- end;
- end;
- top_const:
- result:=1+SlebSize(longint(val));
- else
- internalerror(2021092615);
- end;
- end;
- a_i64_const:
- begin
- if ops<>1 then
- internalerror(2021092001);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- result:=6
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- result:=1+SlebSize(int64(ref^.offset));
- end;
- end;
- top_const:
- result:=1+SlebSize(int64(val));
- else
- internalerror(2021092615);
- end;
- end;
- a_f32_const:
- result:=5;
- a_f64_const:
- result:=9;
- a_local_get,
- a_local_set,
- a_local_tee:
- begin
- if ops<>1 then
- internalerror(2021092001);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- internalerror(2021092005);
- if ref^.base<>NR_STACK_POINTER_REG then
- internalerror(2021092006);
- if ref^.index<>NR_NO then
- internalerror(2021092007);
- result:=1+UlebSize(ref^.offset);
- end;
- top_const:
- result:=1+UlebSize(val);
- else
- internalerror(2021092008);
- end;
- end;
- a_global_get,
- a_global_set:
- begin
- if ops<>1 then
- internalerror(2021092010);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if not assigned(ref^.symbol) then
- internalerror(2021092012);
- if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
- internalerror(2021092013);
- result:=6;
- end;
- else
- internalerror(2021092011);
- end;
- end;
- a_end_function:
- result:=0;
- a_block,
- a_loop,
- a_if,
- a_try:
- begin
- if ops=0 then
- result:=2
- else
- begin
- if ops<>1 then
- internalerror(2021092015);
- with oper[0]^ do
- case typ of
- top_functype:
- begin
- if (length(functype.params)=0) and (length(functype.results)<=1) then
- result:=2
- else
- { more complex blocktypes are not yet implemented }
- internalerror(2021092621);
- end;
- else
- internalerror(2021092620);
- end;
- end;
- end;
- a_i32_load,
- a_i64_load,
- a_f32_load,
- a_f64_load,
- a_i32_load8_s,
- a_i32_load8_u,
- a_i32_load16_s,
- a_i32_load16_u,
- a_i64_load8_s,
- a_i64_load8_u,
- a_i64_load16_s,
- a_i64_load16_u,
- a_i64_load32_s,
- a_i64_load32_u,
- a_i32_store,
- a_i64_store,
- a_f32_store,
- a_f64_store,
- a_i32_store8,
- a_i32_store16,
- a_i64_store8,
- a_i64_store16,
- a_i64_store32:
- begin
- if ops<>1 then
- internalerror(2021092016);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- begin
- Result:=1+
- UlebSize(natural_alignment_for_load_store(opcode))+
- 5; { relocation, fixed size = 5 bytes }
- end
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- Result:=1+
- UlebSize(natural_alignment_for_load_store(opcode))+
- UlebSize(ref^.offset);
- end;
- end;
- top_const:
- begin
- Result:=1+
- UlebSize(natural_alignment_for_load_store(opcode))+
- UlebSize(val);
- end;
- else
- internalerror(2021092017);
- end;
- end;
- a_memory_atomic_notify,
- a_memory_atomic_wait32,
- a_memory_atomic_wait64,
- a_i32_atomic_load,
- a_i64_atomic_load,
- a_i32_atomic_load8_u,
- a_i32_atomic_load16_u,
- a_i64_atomic_load8_u,
- a_i64_atomic_load16_u,
- a_i64_atomic_load32_u,
- a_i32_atomic_store,
- a_i64_atomic_store,
- a_i32_atomic_store8,
- a_i32_atomic_store16,
- a_i64_atomic_store8,
- a_i64_atomic_store16,
- a_i64_atomic_store32,
- a_i32_atomic_rmw_add,
- a_i64_atomic_rmw_add,
- a_i32_atomic_rmw8_add_u,
- a_i32_atomic_rmw16_add_u,
- a_i64_atomic_rmw8_add_u,
- a_i64_atomic_rmw16_add_u,
- a_i64_atomic_rmw32_add_u,
- a_i32_atomic_rmw_sub,
- a_i64_atomic_rmw_sub,
- a_i32_atomic_rmw8_sub_u,
- a_i32_atomic_rmw16_sub_u,
- a_i64_atomic_rmw8_sub_u,
- a_i64_atomic_rmw16_sub_u,
- a_i64_atomic_rmw32_sub_u,
- a_i32_atomic_rmw_and,
- a_i64_atomic_rmw_and,
- a_i32_atomic_rmw8_and_u,
- a_i32_atomic_rmw16_and_u,
- a_i64_atomic_rmw8_and_u,
- a_i64_atomic_rmw16_and_u,
- a_i64_atomic_rmw32_and_u,
- a_i32_atomic_rmw_or,
- a_i64_atomic_rmw_or,
- a_i32_atomic_rmw8_or_u,
- a_i32_atomic_rmw16_or_u,
- a_i64_atomic_rmw8_or_u,
- a_i64_atomic_rmw16_or_u,
- a_i64_atomic_rmw32_or_u,
- a_i32_atomic_rmw_xor,
- a_i64_atomic_rmw_xor,
- a_i32_atomic_rmw8_xor_u,
- a_i32_atomic_rmw16_xor_u,
- a_i64_atomic_rmw8_xor_u,
- a_i64_atomic_rmw16_xor_u,
- a_i64_atomic_rmw32_xor_u,
- a_i32_atomic_rmw_xchg,
- a_i64_atomic_rmw_xchg,
- a_i32_atomic_rmw8_xchg_u,
- a_i32_atomic_rmw16_xchg_u,
- a_i64_atomic_rmw8_xchg_u,
- a_i64_atomic_rmw16_xchg_u,
- a_i64_atomic_rmw32_xchg_u,
- a_i32_atomic_rmw_cmpxchg,
- a_i64_atomic_rmw_cmpxchg,
- a_i32_atomic_rmw8_cmpxchg_u,
- a_i32_atomic_rmw16_cmpxchg_u,
- a_i64_atomic_rmw8_cmpxchg_u,
- a_i64_atomic_rmw16_cmpxchg_u,
- a_i64_atomic_rmw32_cmpxchg_u:
- begin
- if ops<>1 then
- internalerror(2021092016);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- begin
- Result:=2+
- UlebSize(natural_alignment_for_load_store(opcode))+
- 5; { relocation, fixed size = 5 bytes }
- end
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- Result:=2+
- UlebSize(natural_alignment_for_load_store(opcode))+
- UlebSize(ref^.offset);
- end;
- end;
- top_const:
- begin
- Result:=2+
- UlebSize(natural_alignment_for_load_store(opcode))+
- UlebSize(val);
- end;
- else
- internalerror(2021092017);
- end;
- end;
- a_call:
- begin
- if ops<>1 then
- internalerror(2021092021);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
- internalerror(2021092023);
- result:=6;
- end;
- else
- internalerror(2021092022);
- end;
- end;
- a_call_indirect:
- begin
- if ops<>1 then
- internalerror(2021092610);
- with oper[0]^ do
- case typ of
- top_functype:
- begin
- TWasmObjData(objdata).FuncTypes.AddOrGetFuncType(functype);
- result:=6+
- UlebSize(0);
- end;
- else
- internalerror(2021092611);
- end;
- end;
- a_br,
- a_br_if,
- a_rethrow,
- a_delegate:
- begin
- if ops<>1 then
- internalerror(2021092610);
- with oper[0]^ do
- case typ of
- top_const:
- result:=1+
- UlebSize(val);
- else
- internalerror(2021092625);
- end;
- end;
- a_catch,
- a_throw:
- begin
- if ops<>1 then
- internalerror(2021092709);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
- internalerror(2021092711);
- result:=6;
- end;
- else
- internalerror(2021092710);
- end;
- end;
- a_memory_init:
- begin
- if ops<>1 then
- internalerror(2022052802);
- with oper[0]^ do
- case typ of
- top_const:
- result:=3+UlebSize(val);
- else
- internalerror(2022052803);
- end;
- end;
- a_data_drop:
- begin
- if ops<>1 then
- internalerror(2022052804);
- with oper[0]^ do
- case typ of
- top_const:
- result:=2+UlebSize(val);
- else
- internalerror(2022052805);
- end;
- end;
- else
- internalerror(2021092623);
- end;
- end;
- procedure taicpu.Pass2(objdata: TObjData);
- procedure WriteByte(b: byte);
- begin
- objdata.writebytes(b,1);
- end;
- {$ifdef FPC_LITTLE_ENDIAN}
- procedure WriteSingle(s: single);
- begin
- objdata.writebytes(s,4);
- end;
- procedure WriteDouble(d: double);
- begin
- objdata.writebytes(d,8);
- end;
- {$else FPC_LITTLE_ENDIAN}
- procedure WriteSingle(s: single);
- var
- l: longword;
- begin
- Move(s,l,4);
- l:=SwapEndian(l);
- objdata.writebytes(l,4);
- end;
- procedure WriteDouble(d: double);
- var
- q: qword;
- begin
- Move(d,q,8);
- q:=SwapEndian(q);
- objdata.writebytes(q,8);
- end;
- {$endif FPC_LITTLE_ENDIAN}
- procedure WriteSleb(v: tcgint);
- var
- b: byte;
- Done: Boolean=false;
- begin
- repeat
- b:=byte(v) and 127;
- v:=SarInt64(v,7);
- if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
- Done:=true
- else
- b:=b or 128;
- objdata.writebytes(b,1);
- until Done;
- end;
- procedure WriteUleb(v: tcgint);
- var
- b: byte;
- begin
- repeat
- b:=byte(v) and 127;
- v:=v shr 7;
- if v<>0 then
- b:=b or 128;
- objdata.writebytes(b,1);
- until v=0;
- end;
- begin
- case opcode of
- a_unreachable:
- WriteByte($00);
- a_nop:
- WriteByte($01);
- a_return:
- WriteByte($0F);
- a_drop:
- WriteByte($1A);
- a_memory_size:
- begin
- WriteByte($3F);
- WriteByte($00);
- end;
- a_memory_grow:
- begin
- WriteByte($40);
- WriteByte($00);
- end;
- a_memory_copy:
- begin
- WriteByte($FC);
- WriteUleb(10);
- WriteByte($00);
- WriteByte($00);
- end;
- a_memory_fill:
- begin
- WriteByte($FC);
- WriteUleb(11);
- WriteByte($00);
- end;
- a_atomic_fence:
- begin
- WriteByte($FE);
- WriteByte($03);
- WriteByte($00);
- end;
- a_i32_eqz:
- WriteByte($45);
- a_i32_eq:
- WriteByte($46);
- a_i32_ne:
- WriteByte($47);
- a_i32_lt_s:
- WriteByte($48);
- a_i32_lt_u:
- WriteByte($49);
- a_i32_gt_s:
- WriteByte($4A);
- a_i32_gt_u:
- WriteByte($4B);
- a_i32_le_s:
- WriteByte($4C);
- a_i32_le_u:
- WriteByte($4D);
- a_i32_ge_s:
- WriteByte($4E);
- a_i32_ge_u:
- WriteByte($4F);
- a_i64_eqz:
- WriteByte($50);
- a_i64_eq:
- WriteByte($51);
- a_i64_ne:
- WriteByte($52);
- a_i64_lt_s:
- WriteByte($53);
- a_i64_lt_u:
- WriteByte($54);
- a_i64_gt_s:
- WriteByte($55);
- a_i64_gt_u:
- WriteByte($56);
- a_i64_le_s:
- WriteByte($57);
- a_i64_le_u:
- WriteByte($58);
- a_i64_ge_s:
- WriteByte($59);
- a_i64_ge_u:
- WriteByte($5A);
- a_f32_eq:
- WriteByte($5B);
- a_f32_ne:
- WriteByte($5C);
- a_f32_lt:
- WriteByte($5D);
- a_f32_gt:
- WriteByte($5E);
- a_f32_le:
- WriteByte($5F);
- a_f32_ge:
- WriteByte($60);
- a_f64_eq:
- WriteByte($61);
- a_f64_ne:
- WriteByte($62);
- a_f64_lt:
- WriteByte($63);
- a_f64_gt:
- WriteByte($64);
- a_f64_le:
- WriteByte($65);
- a_f64_ge:
- WriteByte($66);
- a_i32_clz:
- WriteByte($67);
- a_i32_ctz:
- WriteByte($68);
- a_i32_popcnt:
- WriteByte($69);
- a_i32_add:
- WriteByte($6A);
- a_i32_sub:
- WriteByte($6B);
- a_i32_mul:
- WriteByte($6C);
- a_i32_div_s:
- WriteByte($6D);
- a_i32_div_u:
- WriteByte($6E);
- a_i32_rem_s:
- WriteByte($6F);
- a_i32_rem_u:
- WriteByte($70);
- a_i32_and:
- WriteByte($71);
- a_i32_or:
- WriteByte($72);
- a_i32_xor:
- WriteByte($73);
- a_i32_shl:
- WriteByte($74);
- a_i32_shr_s:
- WriteByte($75);
- a_i32_shr_u:
- WriteByte($76);
- a_i32_rotl:
- WriteByte($77);
- a_i32_rotr:
- WriteByte($78);
- a_i64_clz:
- WriteByte($79);
- a_i64_ctz:
- WriteByte($7A);
- a_i64_popcnt:
- WriteByte($7B);
- a_i64_add:
- WriteByte($7C);
- a_i64_sub:
- WriteByte($7D);
- a_i64_mul:
- WriteByte($7E);
- a_i64_div_s:
- WriteByte($7F);
- a_i64_div_u:
- WriteByte($80);
- a_i64_rem_s:
- WriteByte($81);
- a_i64_rem_u:
- WriteByte($82);
- a_i64_and:
- WriteByte($83);
- a_i64_or:
- WriteByte($84);
- a_i64_xor:
- WriteByte($85);
- a_i64_shl:
- WriteByte($86);
- a_i64_shr_s:
- WriteByte($87);
- a_i64_shr_u:
- WriteByte($88);
- a_i64_rotl:
- WriteByte($89);
- a_i64_rotr:
- WriteByte($8A);
- a_f32_abs:
- WriteByte($8B);
- a_f32_neg:
- WriteByte($8C);
- a_f32_ceil:
- WriteByte($8D);
- a_f32_floor:
- WriteByte($8E);
- a_f32_trunc:
- WriteByte($8F);
- a_f32_nearest:
- WriteByte($90);
- a_f32_sqrt:
- WriteByte($91);
- a_f32_add:
- WriteByte($92);
- a_f32_sub:
- WriteByte($93);
- a_f32_mul:
- WriteByte($94);
- a_f32_div:
- WriteByte($95);
- a_f32_min:
- WriteByte($96);
- a_f32_max:
- WriteByte($97);
- a_f32_copysign:
- WriteByte($98);
- a_f64_abs:
- WriteByte($99);
- a_f64_neg:
- WriteByte($9A);
- a_f64_ceil:
- WriteByte($9B);
- a_f64_floor:
- WriteByte($9C);
- a_f64_trunc:
- WriteByte($9D);
- a_f64_nearest:
- WriteByte($9E);
- a_f64_sqrt:
- WriteByte($9F);
- a_f64_add:
- WriteByte($A0);
- a_f64_sub:
- WriteByte($A1);
- a_f64_mul:
- WriteByte($A2);
- a_f64_div:
- WriteByte($A3);
- a_f64_min:
- WriteByte($A4);
- a_f64_max:
- WriteByte($A5);
- a_f64_copysign:
- WriteByte($A6);
- a_i32_wrap_i64:
- WriteByte($A7);
- a_i32_trunc_f32_s:
- WriteByte($A8);
- a_i32_trunc_f32_u:
- WriteByte($A9);
- a_i32_trunc_f64_s:
- WriteByte($AA);
- a_i32_trunc_f64_u:
- WriteByte($AB);
- a_i64_extend_i32_s:
- WriteByte($AC);
- a_i64_extend_i32_u:
- WriteByte($AD);
- a_i64_trunc_f32_s:
- WriteByte($AE);
- a_i64_trunc_f32_u:
- WriteByte($AF);
- a_i64_trunc_f64_s:
- WriteByte($B0);
- a_i64_trunc_f64_u:
- WriteByte($B1);
- a_f32_convert_i32_s:
- WriteByte($B2);
- a_f32_convert_i32_u:
- WriteByte($B3);
- a_f32_convert_i64_s:
- WriteByte($B4);
- a_f32_convert_i64_u:
- WriteByte($B5);
- a_f32_demote_f64:
- WriteByte($B6);
- a_f64_convert_i32_s:
- WriteByte($B7);
- a_f64_convert_i32_u:
- WriteByte($B8);
- a_f64_convert_i64_s:
- WriteByte($B9);
- a_f64_convert_i64_u:
- WriteByte($BA);
- a_f64_promote_f32:
- WriteByte($BB);
- a_i32_reinterpret_f32:
- WriteByte($BC);
- a_i64_reinterpret_f64:
- WriteByte($BD);
- a_f32_reinterpret_i32:
- WriteByte($BE);
- a_f64_reinterpret_i64:
- WriteByte($BF);
- a_i32_extend8_s:
- WriteByte($C0);
- a_i32_extend16_s:
- WriteByte($C1);
- a_i64_extend8_s:
- WriteByte($C2);
- a_i64_extend16_s:
- WriteByte($C3);
- a_i64_extend32_s:
- WriteByte($C4);
- a_end_block,
- a_end_if,
- a_end_loop,
- a_end_try:
- WriteByte($0B);
- a_catch_all:
- WriteByte($19);
- a_i32_const:
- begin
- WriteByte($41);
- if ops<>1 then
- internalerror(2021092001);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- WriteSleb(longint(ref^.offset));
- end;
- end;
- top_const:
- WriteSleb(longint(val));
- else
- internalerror(2021092615);
- end;
- end;
- a_i64_const:
- begin
- WriteByte($42);
- if ops<>1 then
- internalerror(2021092001);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- WriteSleb(int64(ref^.offset));
- end;
- end;
- top_const:
- WriteSleb(int64(val));
- else
- internalerror(2021092615);
- end;
- end;
- a_f32_const:
- begin
- if ops<>1 then
- internalerror(2021092619);
- WriteByte($43);
- with oper[0]^ do
- case typ of
- top_single:
- WriteSingle(sval);
- else
- internalerror(2021092618);
- end;
- end;
- a_f64_const:
- begin
- if ops<>1 then
- internalerror(2021092616);
- WriteByte($44);
- with oper[0]^ do
- case typ of
- top_double:
- WriteDouble(dval);
- else
- internalerror(2021092617);
- end;
- end;
- a_local_get,
- a_local_set,
- a_local_tee:
- begin
- case opcode of
- a_local_get:
- WriteByte($20);
- a_local_set:
- WriteByte($21);
- a_local_tee:
- WriteByte($22);
- else
- internalerror(2021092003);
- end;
- if ops<>1 then
- internalerror(2021092004);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- internalerror(2021092005);
- if ref^.base<>NR_STACK_POINTER_REG then
- internalerror(2021092006);
- if ref^.index<>NR_NO then
- internalerror(2021092007);
- WriteUleb(ref^.offset);
- end;
- top_const:
- WriteUleb(val);
- else
- internalerror(2021092008);
- end;
- end;
- a_global_get,
- a_global_set:
- begin
- case opcode of
- a_global_get:
- WriteByte($23);
- a_global_set:
- WriteByte($24);
- else
- internalerror(2021092009);
- end;
- if ops<>1 then
- internalerror(2021092010);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if not assigned(ref^.symbol) then
- internalerror(2021092012);
- if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
- internalerror(2021092013);
- objdata.writeReloc(0,5,TWasmObjData(ObjData).globalref(ref^.symbol),RELOC_GLOBAL_INDEX_LEB);
- end;
- else
- internalerror(2021092011);
- end;
- end;
- a_end_function:
- ;
- a_block,
- a_loop,
- a_if,
- a_try:
- begin
- case opcode of
- a_block:
- WriteByte($02);
- a_loop:
- WriteByte($03);
- a_if:
- WriteByte($04);
- a_try:
- WriteByte($06);
- else
- internalerror(2021092626);
- end;
- if ops=0 then
- WriteByte($40)
- else
- begin
- if ops<>1 then
- internalerror(2021092015);
- with oper[0]^ do
- case typ of
- top_functype:
- begin
- if (length(functype.params)=0) and (length(functype.results)<=1) then
- begin
- if length(functype.results)=1 then
- WriteByte(encode_wasm_basic_type(functype.results[0]))
- else
- WriteByte($40);
- end
- else
- { more complex blocktypes are not yet implemented }
- internalerror(2021092621);
- end;
- else
- internalerror(2021092620);
- end;
- end;
- end;
- a_else:
- WriteByte($05);
- a_i32_load,
- a_i64_load,
- a_f32_load,
- a_f64_load,
- a_i32_load8_s,
- a_i32_load8_u,
- a_i32_load16_s,
- a_i32_load16_u,
- a_i64_load8_s,
- a_i64_load8_u,
- a_i64_load16_s,
- a_i64_load16_u,
- a_i64_load32_s,
- a_i64_load32_u,
- a_i32_store,
- a_i64_store,
- a_f32_store,
- a_f64_store,
- a_i32_store8,
- a_i32_store16,
- a_i64_store8,
- a_i64_store16,
- a_i64_store32:
- begin
- case opcode of
- a_i32_load:
- WriteByte($28);
- a_i64_load:
- WriteByte($29);
- a_f32_load:
- WriteByte($2A);
- a_f64_load:
- WriteByte($2B);
- a_i32_load8_s:
- WriteByte($2C);
- a_i32_load8_u:
- WriteByte($2D);
- a_i32_load16_s:
- WriteByte($2E);
- a_i32_load16_u:
- WriteByte($2F);
- a_i64_load8_s:
- WriteByte($30);
- a_i64_load8_u:
- WriteByte($31);
- a_i64_load16_s:
- WriteByte($32);
- a_i64_load16_u:
- WriteByte($33);
- a_i64_load32_s:
- WriteByte($34);
- a_i64_load32_u:
- WriteByte($35);
- a_i32_store:
- WriteByte($36);
- a_i64_store:
- WriteByte($37);
- a_f32_store:
- WriteByte($38);
- a_f64_store:
- WriteByte($39);
- a_i32_store8:
- WriteByte($3A);
- a_i32_store16:
- WriteByte($3B);
- a_i64_store8:
- WriteByte($3C);
- a_i64_store16:
- WriteByte($3D);
- a_i64_store32:
- WriteByte($3E);
- else
- internalerror(2021092019);
- end;
- if ops<>1 then
- internalerror(2021092016);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- begin
- WriteUleb(natural_alignment_for_load_store(opcode));
- objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
- end
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- WriteUleb(natural_alignment_for_load_store(opcode));
- WriteUleb(ref^.offset);
- end;
- end;
- top_const:
- begin
- WriteUleb(natural_alignment_for_load_store(opcode));
- WriteUleb(val);
- end;
- else
- internalerror(2021092017);
- end;
- end;
- a_memory_atomic_notify,
- a_memory_atomic_wait32,
- a_memory_atomic_wait64,
- a_i32_atomic_load,
- a_i64_atomic_load,
- a_i32_atomic_load8_u,
- a_i32_atomic_load16_u,
- a_i64_atomic_load8_u,
- a_i64_atomic_load16_u,
- a_i64_atomic_load32_u,
- a_i32_atomic_store,
- a_i64_atomic_store,
- a_i32_atomic_store8,
- a_i32_atomic_store16,
- a_i64_atomic_store8,
- a_i64_atomic_store16,
- a_i64_atomic_store32,
- a_i32_atomic_rmw_add,
- a_i64_atomic_rmw_add,
- a_i32_atomic_rmw8_add_u,
- a_i32_atomic_rmw16_add_u,
- a_i64_atomic_rmw8_add_u,
- a_i64_atomic_rmw16_add_u,
- a_i64_atomic_rmw32_add_u,
- a_i32_atomic_rmw_sub,
- a_i64_atomic_rmw_sub,
- a_i32_atomic_rmw8_sub_u,
- a_i32_atomic_rmw16_sub_u,
- a_i64_atomic_rmw8_sub_u,
- a_i64_atomic_rmw16_sub_u,
- a_i64_atomic_rmw32_sub_u,
- a_i32_atomic_rmw_and,
- a_i64_atomic_rmw_and,
- a_i32_atomic_rmw8_and_u,
- a_i32_atomic_rmw16_and_u,
- a_i64_atomic_rmw8_and_u,
- a_i64_atomic_rmw16_and_u,
- a_i64_atomic_rmw32_and_u,
- a_i32_atomic_rmw_or,
- a_i64_atomic_rmw_or,
- a_i32_atomic_rmw8_or_u,
- a_i32_atomic_rmw16_or_u,
- a_i64_atomic_rmw8_or_u,
- a_i64_atomic_rmw16_or_u,
- a_i64_atomic_rmw32_or_u,
- a_i32_atomic_rmw_xor,
- a_i64_atomic_rmw_xor,
- a_i32_atomic_rmw8_xor_u,
- a_i32_atomic_rmw16_xor_u,
- a_i64_atomic_rmw8_xor_u,
- a_i64_atomic_rmw16_xor_u,
- a_i64_atomic_rmw32_xor_u,
- a_i32_atomic_rmw_xchg,
- a_i64_atomic_rmw_xchg,
- a_i32_atomic_rmw8_xchg_u,
- a_i32_atomic_rmw16_xchg_u,
- a_i64_atomic_rmw8_xchg_u,
- a_i64_atomic_rmw16_xchg_u,
- a_i64_atomic_rmw32_xchg_u,
- a_i32_atomic_rmw_cmpxchg,
- a_i64_atomic_rmw_cmpxchg,
- a_i32_atomic_rmw8_cmpxchg_u,
- a_i32_atomic_rmw16_cmpxchg_u,
- a_i64_atomic_rmw8_cmpxchg_u,
- a_i64_atomic_rmw16_cmpxchg_u,
- a_i64_atomic_rmw32_cmpxchg_u:
- begin
- WriteByte($FE);
- case opcode of
- a_memory_atomic_notify:
- WriteByte($00);
- a_memory_atomic_wait32:
- WriteByte($01);
- a_memory_atomic_wait64:
- WriteByte($02);
- a_i32_atomic_load:
- WriteByte($10);
- a_i64_atomic_load:
- WriteByte($11);
- a_i32_atomic_load8_u:
- WriteByte($12);
- a_i32_atomic_load16_u:
- WriteByte($13);
- a_i64_atomic_load8_u:
- WriteByte($14);
- a_i64_atomic_load16_u:
- WriteByte($15);
- a_i64_atomic_load32_u:
- WriteByte($16);
- a_i32_atomic_store:
- WriteByte($17);
- a_i64_atomic_store:
- WriteByte($18);
- a_i32_atomic_store8:
- WriteByte($19);
- a_i32_atomic_store16:
- WriteByte($1A);
- a_i64_atomic_store8:
- WriteByte($1B);
- a_i64_atomic_store16:
- WriteByte($1C);
- a_i64_atomic_store32:
- WriteByte($1D);
- a_i32_atomic_rmw_add:
- WriteByte($1E);
- a_i64_atomic_rmw_add:
- WriteByte($1F);
- a_i32_atomic_rmw8_add_u:
- WriteByte($20);
- a_i32_atomic_rmw16_add_u:
- WriteByte($21);
- a_i64_atomic_rmw8_add_u:
- WriteByte($22);
- a_i64_atomic_rmw16_add_u:
- WriteByte($23);
- a_i64_atomic_rmw32_add_u:
- WriteByte($24);
- a_i32_atomic_rmw_sub:
- WriteByte($25);
- a_i64_atomic_rmw_sub:
- WriteByte($26);
- a_i32_atomic_rmw8_sub_u:
- WriteByte($27);
- a_i32_atomic_rmw16_sub_u:
- WriteByte($28);
- a_i64_atomic_rmw8_sub_u:
- WriteByte($29);
- a_i64_atomic_rmw16_sub_u:
- WriteByte($2A);
- a_i64_atomic_rmw32_sub_u:
- WriteByte($2B);
- a_i32_atomic_rmw_and:
- WriteByte($2C);
- a_i64_atomic_rmw_and:
- WriteByte($2D);
- a_i32_atomic_rmw8_and_u:
- WriteByte($2E);
- a_i32_atomic_rmw16_and_u:
- WriteByte($2F);
- a_i64_atomic_rmw8_and_u:
- WriteByte($30);
- a_i64_atomic_rmw16_and_u:
- WriteByte($31);
- a_i64_atomic_rmw32_and_u:
- WriteByte($32);
- a_i32_atomic_rmw_or:
- WriteByte($33);
- a_i64_atomic_rmw_or:
- WriteByte($34);
- a_i32_atomic_rmw8_or_u:
- WriteByte($35);
- a_i32_atomic_rmw16_or_u:
- WriteByte($36);
- a_i64_atomic_rmw8_or_u:
- WriteByte($37);
- a_i64_atomic_rmw16_or_u:
- WriteByte($38);
- a_i64_atomic_rmw32_or_u:
- WriteByte($39);
- a_i32_atomic_rmw_xor:
- WriteByte($3A);
- a_i64_atomic_rmw_xor:
- WriteByte($3B);
- a_i32_atomic_rmw8_xor_u:
- WriteByte($3C);
- a_i32_atomic_rmw16_xor_u:
- WriteByte($3D);
- a_i64_atomic_rmw8_xor_u:
- WriteByte($3E);
- a_i64_atomic_rmw16_xor_u:
- WriteByte($3F);
- a_i64_atomic_rmw32_xor_u:
- WriteByte($40);
- a_i32_atomic_rmw_xchg:
- WriteByte($41);
- a_i64_atomic_rmw_xchg:
- WriteByte($42);
- a_i32_atomic_rmw8_xchg_u:
- WriteByte($43);
- a_i32_atomic_rmw16_xchg_u:
- WriteByte($44);
- a_i64_atomic_rmw8_xchg_u:
- WriteByte($45);
- a_i64_atomic_rmw16_xchg_u:
- WriteByte($46);
- a_i64_atomic_rmw32_xchg_u:
- WriteByte($47);
- a_i32_atomic_rmw_cmpxchg:
- WriteByte($48);
- a_i64_atomic_rmw_cmpxchg:
- WriteByte($49);
- a_i32_atomic_rmw8_cmpxchg_u:
- WriteByte($4A);
- a_i32_atomic_rmw16_cmpxchg_u:
- WriteByte($4B);
- a_i64_atomic_rmw8_cmpxchg_u:
- WriteByte($4C);
- a_i64_atomic_rmw16_cmpxchg_u:
- WriteByte($4D);
- a_i64_atomic_rmw32_cmpxchg_u:
- WriteByte($4E);
- else
- internalerror(2022052101);
- end;
- if ops<>1 then
- internalerror(2021092016);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if assigned(ref^.symbol) then
- begin
- WriteUleb(natural_alignment_for_load_store(opcode));
- objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
- end
- else
- begin
- if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
- internalerror(2021092018);
- WriteUleb(natural_alignment_for_load_store(opcode));
- WriteUleb(ref^.offset);
- end;
- end;
- top_const:
- begin
- WriteUleb(natural_alignment_for_load_store(opcode));
- WriteUleb(val);
- end;
- else
- internalerror(2021092017);
- end;
- end;
- a_call:
- begin
- if ops<>1 then
- internalerror(2021092021);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
- internalerror(2021092023);
- WriteByte($10);
- objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
- end;
- else
- internalerror(2021092022);
- end;
- end;
- a_call_indirect:
- begin
- if ops<>1 then
- internalerror(2021092610);
- with oper[0]^ do
- case typ of
- top_functype:
- begin
- WriteByte($11);
- objdata.writeReloc(TWasmObjData(objdata).FuncTypes.AddOrGetFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
- WriteUleb(0);
- end;
- else
- internalerror(2021092611);
- end;
- end;
- a_br,
- a_br_if,
- a_rethrow,
- a_delegate:
- begin
- case opcode of
- a_br:
- WriteByte($0C);
- a_br_if:
- WriteByte($0D);
- a_rethrow:
- WriteByte($09);
- a_delegate:
- WriteByte($18);
- else
- internalerror(2021092622);
- end;
- if ops<>1 then
- internalerror(2021092610);
- with oper[0]^ do
- case typ of
- top_const:
- WriteUleb(val);
- else
- internalerror(2021092625);
- end;
- end;
- a_catch,
- a_throw:
- begin
- case opcode of
- a_catch:
- WriteByte($07);
- a_throw:
- WriteByte($08);
- else
- internalerror(2021092708);
- end;
- if ops<>1 then
- internalerror(2021092709);
- with oper[0]^ do
- case typ of
- top_ref:
- begin
- if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
- internalerror(2021092711);
- objdata.writeReloc(0,5,TWasmObjData(ObjData).ExceptionTagRef(ref^.symbol),RELOC_TAG_INDEX_LEB);
- end;
- else
- internalerror(2021092710);
- end;
- end;
- a_memory_init:
- begin
- WriteByte($FC);
- WriteByte($08);
- if ops<>1 then
- internalerror(2022052806);
- with oper[0]^ do
- case typ of
- top_const:
- WriteUleb(val);
- else
- internalerror(2022052807);
- end;
- WriteByte($00);
- end;
- a_data_drop:
- begin
- WriteByte($FC);
- WriteByte($09);
- if ops<>1 then
- internalerror(2022052808);
- with oper[0]^ do
- case typ of
- top_const:
- WriteUleb(val);
- else
- internalerror(2022052809);
- end;
- end;
- a_i32_trunc_sat_f32_s:
- begin
- WriteByte($FC);
- WriteByte($00);
- end;
- a_i32_trunc_sat_f32_u:
- begin
- WriteByte($FC);
- WriteByte($01);
- end;
- a_i32_trunc_sat_f64_s:
- begin
- WriteByte($FC);
- WriteByte($02);
- end;
- a_i32_trunc_sat_f64_u:
- begin
- WriteByte($FC);
- WriteByte($03);
- end;
- a_i64_trunc_sat_f32_s:
- begin
- WriteByte($FC);
- WriteByte($04);
- end;
- a_i64_trunc_sat_f32_u:
- begin
- WriteByte($FC);
- WriteByte($05);
- end;
- a_i64_trunc_sat_f64_s:
- begin
- WriteByte($FC);
- WriteByte($06);
- end;
- a_i64_trunc_sat_f64_u:
- begin
- WriteByte($FC);
- WriteByte($07);
- end;
- a_ref_null_funcref:
- begin
- WriteByte($D0);
- WriteByte($70);
- end;
- a_ref_null_externref:
- begin
- WriteByte($D0);
- WriteByte($6F);
- end;
- a_ref_is_null:
- WriteByte($D1);
- else
- internalerror(2021092624);
- end;
- end;
- function spilling_create_load(const ref:treference;r:tregister):Taicpu;
- begin
- internalerror(2010122614);
- result:=nil;
- end;
- function spilling_create_store(r:tregister; const ref:treference):Taicpu;
- begin
- internalerror(2010122615);
- result:=nil;
- end;
- procedure InitAsm;
- begin
- end;
- procedure DoneAsm;
- begin
- end;
- function wasm_convert_first_item_to_structured(srclist: TAsmList): tai;
- begin
- result:=tai(srclist.First);
- if result<>nil then
- begin
- srclist.Remove(result);
- if result.typ=ait_instruction then
- case taicpu(result).opcode of
- a_if:
- result:=tai_wasmstruc_if.create_from(taicpu(result),srclist);
- a_block:
- result:=tai_wasmstruc_block.create_from(taicpu(result),srclist);
- a_loop:
- result:=tai_wasmstruc_loop.create_from(taicpu(result),srclist);
- a_try:
- result:=tai_wasmstruc_try.create_from(srclist);
- a_else,a_end_if,a_end_block,a_end_loop,a_end_try,a_catch,a_catch_all,a_delegate:
- internalerror(2023100503);
- else
- ;
- end;
- end;
- end;
- procedure wasm_convert_to_structured_asmlist_internal(srclist, destlist: TAsmList);
- begin
- while not srclist.Empty do
- destlist.Concat(wasm_convert_first_item_to_structured(srclist));
- end;
- procedure wasm_convert_to_structured_asmlist(var asmlist: TAsmList);
- var
- tmplist: TAsmList;
- begin
- tmplist:=TAsmList.Create;
- wasm_convert_to_structured_asmlist_internal(asmlist,tmplist);
- asmlist.Free;
- asmlist:=tmplist;
- end;
- procedure wasm_convert_to_flat_asmlist_internal(srclist, destlist: TAsmList);
- var
- p: tai;
- tmplist: TAsmList;
- begin
- tmplist:=TAsmList.Create;
- while not srclist.Empty do
- begin
- p:=tai(srclist.First);
- srclist.Remove(p);
- if p.typ=ait_wasm_structured_instruction then
- begin
- taicpu_wasm_structured_instruction(p).ConvertToFlatList(tmplist);
- srclist.insertList(tmplist);
- end
- else
- destlist.Concat(p);
- end;
- tmplist.free;
- end;
- procedure wasm_convert_to_flat_asmlist(var asmlist: TAsmList);
- var
- tmplist: TAsmList;
- begin
- tmplist:=TAsmList.Create;
- wasm_convert_to_flat_asmlist_internal(asmlist,tmplist);
- asmlist.Free;
- asmlist:=tmplist;
- end;
- procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack);
- var
- p, q: tai;
- mapres: TAsmMapFuncResult;
- begin
- if not assigned(l) then
- exit;
- p:=tai(l.First);
- while p<>nil do
- begin
- if p.typ=ait_wasm_structured_instruction then
- begin
- mapres:=f(p,blockstack);
- case mapres.typ of
- amfrtNoChange:
- begin
- taicpu_wasm_structured_instruction(p).Map(f,blockstack);
- p:=tai(p.next);
- end;
- amfrtNewAi:
- begin
- q:=mapres.newai;
- if q<>p then
- begin
- l.InsertAfter(q,p);
- l.Remove(p);
- p:=q;
- end;
- p:=tai(p.next);
- end;
- amfrtNewList:
- begin
- q:=tai(mapres.newlist.First);
- l.insertListAfter(p,mapres.newlist);
- mapres.newlist.free;
- l.Remove(p);
- p:=q;
- end;
- amfrtDeleteAi:
- begin
- q:=p;
- p:=tai(p.next);
- l.Remove(q);
- end;
- end;
- end
- else
- begin
- mapres:=f(p,blockstack);
- case mapres.typ of
- amfrtNoChange:
- p:=tai(p.next);
- amfrtNewAi:
- begin
- q:=mapres.newai;
- if q<>p then
- begin
- l.InsertAfter(q,p);
- l.Remove(p);
- p:=tai(q.next);
- end
- else
- p:=tai(p.next);
- end;
- amfrtNewList:
- begin
- q:=tai(mapres.newlist.First);
- l.insertListAfter(p,mapres.newlist);
- mapres.newlist.free;
- l.Remove(p);
- p:=q;
- end;
- amfrtDeleteAi:
- begin
- q:=p;
- p:=tai(p.next);
- l.Remove(q);
- end;
- end;
- end;
- end;
- end;
- procedure map_structured_asmlist(l: TAsmList; f: TAsmMapFunc);
- var
- blockstack: twasmstruc_stack;
- begin
- blockstack:=twasmstruc_stack.create;
- map_structured_asmlist_inner(l,f,blockstack);
- blockstack.free;
- end;
- initialization
- cai_cpu:=taicpu;
- cai_align:=tai_align;
- end.
|