123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479 |
- {
- 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,
- 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
- { taicpu }
- taicpu = class(tai_cpu_abstract_sym)
- 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;
- 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
- );
- // the actual use is defined by the assembly section used
- { timpexp_ai }
- tai_impexp = class(tai)
- extname : ansistring; // external name
- intname : ansistring; // internal name
- extmodule : ansistring; // external unit name
- symstype: TImpExpType;
- constructor create(const aextname, aintname: ansistring; asymtype: timpexptype); overload;
- constructor create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype); overload;
- 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_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;
- implementation
- uses
- ogwasm;
- { 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_impexp.create(const aextname, aintname: ansistring;
- asymtype: timpexptype);
- begin
- create('', aextname, aintname, asymtype);;
- end;
- constructor tai_impexp.create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype);
- begin
- inherited create;
- typ := ait_importexport;
- extmodule := aextmodule;
- 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:
- result:=1;
- a_memory_size,
- a_memory_grow:
- result:=2;
- 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;
- 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);
- if ref^.symbol.Name<>'__stack_pointer' then
- internalerror(2021092014);
- result:=1+UlebSize(0);
- end;
- else
- internalerror(2021092011);
- end;
- end;
- a_end_function:
- result:=0;
- a_block,
- a_loop,
- a_if:
- 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_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).AddFuncType(functype);
- result:=6+
- UlebSize(0);
- end;
- else
- internalerror(2021092611);
- end;
- end;
- a_br,
- a_br_if:
- 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;
- 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_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($44);
- 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;
- 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);
- if ref^.symbol.Name<>'__stack_pointer' then
- internalerror(2021092014);
- WriteUleb(0);
- end;
- else
- internalerror(2021092011);
- end;
- end;
- a_end_function:
- ;
- a_block,
- a_loop,
- a_if:
- begin
- case opcode of
- a_block:
- WriteByte($02);
- a_loop:
- WriteByte($03);
- a_if:
- WriteByte($04);
- 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_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).AddFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
- WriteUleb(0);
- end;
- else
- internalerror(2021092611);
- end;
- end;
- a_br,
- a_br_if:
- begin
- case opcode of
- a_br:
- WriteByte($0C);
- a_br_if:
- WriteByte($0D);
- 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;
- 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;
- initialization
- cai_cpu:=taicpu;
- cai_align:=tai_align;
- casmdata:=TAsmData;
- end.
|