From 35489718f397a9771505a6d9a6e87da08c336a3f Mon Sep 17 00:00:00 2001 From: nickysn Date: Sat, 2 Jan 2021 12:21:30 +0000 Subject: [PATCH] * fixed call_indirect for the llvm-mc asm writer git-svn-id: branches/wasm@47965 - --- compiler/aggas.pas | 73 +++++++++++++++++++----------------- compiler/wasm32/aasmcpu.pas | 8 ++-- compiler/wasm32/agllvmmc.pas | 18 +-------- compiler/wasm32/agwat.pas | 31 +++++++-------- compiler/wasm32/hlcgcpu.pas | 2 +- 5 files changed, 61 insertions(+), 71 deletions(-) diff --git a/compiler/aggas.pas b/compiler/aggas.pas index 11b5bb97eb..60e8590344 100644 --- a/compiler/aggas.pas +++ b/compiler/aggas.pas @@ -32,7 +32,10 @@ interface uses globtype,globals, - aasmbase,aasmtai,aasmdata,aasmcfi, + cpubase,aasmbase,aasmtai,aasmdata,aasmcfi, +{$ifdef wasm} + aasmcpu, +{$endif wasm} assemble; type @@ -66,6 +69,9 @@ interface procedure WriteTree(p:TAsmList);override; procedure WriteAsmList;override; destructor destroy; override; +{$ifdef WASM} + procedure WriteFuncType(functype: TWasmFuncType); +{$endif WASM} private setcount: longint; procedure WriteDecodedSleb128(a: int64); @@ -118,10 +124,7 @@ implementation {$ifdef m68k} cpuinfo,aasmcpu, {$endif m68k} -{$ifdef wasm} - aasmcpu, -{$endif wasm} - cpubase,objcasm; + objcasm; const line_length = 70; @@ -720,6 +723,37 @@ implementation end; +{$ifdef WASM} + procedure TGNUAssembler.WriteFuncType(functype: TWasmFuncType); + var + wasm_basic_typ: TWasmBasicType; + first: boolean; + begin + writer.AsmWrite('('); + first:=true; + for wasm_basic_typ in functype.params do + begin + if first then + first:=false + else + writer.AsmWrite(','); + writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]); + end; + writer.AsmWrite(') -> ('); + first:=true; + for wasm_basic_typ in functype.results do + begin + if first then + first:=false + else + writer.AsmWrite(','); + writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]); + end; + writer.AsmWrite(')'); + end; +{$endif WASM} + + procedure TGNUAssembler.WriteTree(p:TAsmList); function needsObject(hp : tai_symbol) : boolean; @@ -808,35 +842,6 @@ implementation end; {$ifdef WASM} - procedure WriteFuncType(functype: TWasmFuncType); - var - wasm_basic_typ: TWasmBasicType; - first: boolean; - begin - writer.AsmWrite('('); - first:=true; - for wasm_basic_typ in functype.params do - begin - if first then - first:=false - else - writer.AsmWrite(','); - writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]); - end; - writer.AsmWrite(') -> ('); - first:=true; - for wasm_basic_typ in functype.results do - begin - if first then - first:=false - else - writer.AsmWrite(','); - writer.AsmWrite(gas_wasm_basic_type_str[wasm_basic_typ]); - end; - writer.AsmWrite(')'); - end; - - procedure WriteFuncTypeDirective(hp:tai_functype); begin writer.AsmWrite(#9'.functype'#9); diff --git a/compiler/wasm32/aasmcpu.pas b/compiler/wasm32/aasmcpu.pas index 0ebc135199..bd80a41118 100644 --- a/compiler/wasm32/aasmcpu.pas +++ b/compiler/wasm32/aasmcpu.pas @@ -44,7 +44,7 @@ uses { taicpu } taicpu = class(tai_cpu_abstract_sym) - typecode : string; // used for call_indirect + functype : TWasmFuncType; // used for call_indirect constructor op_none(op : tasmop); constructor op_reg(op : tasmop;_op1 : tregister); @@ -57,7 +57,7 @@ uses constructor op_single(op : tasmop;_op1 : single); constructor op_double(op : tasmop;_op1 : double); - constructor op_callindirect(const atypecode: string); + constructor op_callindirect(afunctype: TWasmFuncType); //constructor op_string(op : tasmop;_op1len : aint;_op1 : pchar); //constructor op_wstring(op : tasmop;_op1 : pcompilerwidestring); @@ -251,9 +251,9 @@ implementation loaddouble(0,_op1); end; - constructor taicpu.op_callindirect(const atypecode: string); + constructor taicpu.op_callindirect(afunctype: TWasmFuncType); begin - typecode := atypecode; + functype := afunctype; op_none(a_call_indirect); end; diff --git a/compiler/wasm32/agllvmmc.pas b/compiler/wasm32/agllvmmc.pas index a11b055251..543ccb0612 100644 --- a/compiler/wasm32/agllvmmc.pas +++ b/compiler/wasm32/agllvmmc.pas @@ -297,7 +297,6 @@ implementation var cpu : taicpu; i : integer; - isprm : boolean; writer: TExternalAssemblerOutputFile; begin writer:=owner.writer; @@ -306,23 +305,8 @@ implementation writer.AsmWrite(gas_op2str[cpu.opcode]); if (cpu.opcode = a_call_indirect) then begin - // special wat2wasm syntax "call_indirect (type x)" writer.AsmWrite(#9); - isprm := true; - for i:=1 to length(cpu.typecode) do - if cpu.typecode[i]=':' then - isprm:=false - else begin - if isprm then writer.AsmWrite('(param ') - else writer.AsmWrite('(result '); - case cpu.typecode[i] of - 'i': writer.AsmWrite('i32'); - 'I': writer.AsmWrite('i64'); - 'f': writer.AsmWrite('f32'); - 'F': writer.AsmWrite('f64'); - end; - writer.AsmWrite(')'); - end; + owner.WriteFuncType(cpu.functype); writer.AsmLn; exit; end; diff --git a/compiler/wasm32/agwat.pas b/compiler/wasm32/agwat.pas index 6ff098ce3b..6acafbbe70 100644 --- a/compiler/wasm32/agwat.pas +++ b/compiler/wasm32/agwat.pas @@ -247,21 +247,22 @@ implementation if (cpu.opcode = a_call_indirect) then begin // special wat2wasm syntax "call_indirect (type x)" writer.AsmWrite(#9); - isprm := true; - for i:=1 to length(cpu.typecode) do - if cpu.typecode[i]=':' then - isprm:=false - else begin - if isprm then writer.AsmWrite('(param ') - else writer.AsmWrite('(result '); - case cpu.typecode[i] of - 'i': writer.AsmWrite('i32'); - 'I': writer.AsmWrite('i64'); - 'f': writer.AsmWrite('f32'); - 'F': writer.AsmWrite('f64'); - end; - writer.AsmWrite(')'); - end; + // todo: fix + //isprm := true; + //for i:=1 to length(cpu.typecode) do + // if cpu.typecode[i]=':' then + // isprm:=false + // else begin + // if isprm then writer.AsmWrite('(param ') + // else writer.AsmWrite('(result '); + // case cpu.typecode[i] of + // 'i': writer.AsmWrite('i32'); + // 'I': writer.AsmWrite('i64'); + // 'f': writer.AsmWrite('f32'); + // 'F': writer.AsmWrite('f64'); + // end; + // writer.AsmWrite(')'); + // end; writer.AsmLn; exit; end; diff --git a/compiler/wasm32/hlcgcpu.pas b/compiler/wasm32/hlcgcpu.pas index 543f491cde..c2cb11d85f 100644 --- a/compiler/wasm32/hlcgcpu.pas +++ b/compiler/wasm32/hlcgcpu.pas @@ -386,7 +386,7 @@ implementation function thlcgwasm.a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara; begin a_load_reg_stack(list, ptrsinttype, reg); - current_asmdata.CurrAsmList.Concat(taicpu.op_callindirect( WasmGetTypeCode(pd)) ); + current_asmdata.CurrAsmList.Concat(taicpu.op_callindirect(tcpuprocdef(pd).create_functype)); result:=hlcg.get_call_result_cgpara(pd, nil); end;