LLVM: support for attaching metadata to instructions

This commit is contained in:
Jonas Maebe 2022-04-09 14:50:27 +02:00
parent 4293d4455c
commit a8cb061a50
3 changed files with 64 additions and 7 deletions

View File

@ -27,7 +27,7 @@ interface
uses
globtype,verbose,cclasses,
aasmbase,aasmtai,aasmdata,aasmsym,
aasmbase,aasmtai,aasmdata,aasmsym,aasmcnst,
cpubase,cgbase,cgutils,
symtype,symdef,symsym,
llvmbase;
@ -36,8 +36,10 @@ interface
{ taillvm }
taillvm = class(tai_cpu_abstract_sym)
llvmopcode: tllvmop;
metadata: tai;
constructor create_llvm(op: tllvmop);
destructor Destroy; override;
{ e.g. unreachable }
constructor op_none(op : tllvmop);
@ -143,6 +145,8 @@ interface
procedure loadasmlist(opidx: longint; _asmlist: tasmlist);
procedure loadcallingconvention(opidx: longint; calloption: tproccalloption);
procedure addinsmetadata(insmeta: tai);
procedure landingpad_add_clause(op: tllvmop; def: tdef; kind: TAsmSymbol);
{ register spilling code }
@ -193,10 +197,13 @@ interface
alignment: shortint;
flags: taillvmdeclflags;
secname: TSymStr;
metadata: tai;
constructor createdecl(_namesym: tasmsymbol; _def: tdef; _initdata: tasmlist; _sec: tasmsectiontype; _alignment: shortint);
constructor createdef(_namesym: tasmsymbol; _def: tdef; _initdata: tasmlist; _sec: tasmsectiontype; _alignment: shortint);
constructor createtls(_namesym: tasmsymbol; _def: tdef; _alignment: shortint);
procedure setsecname(const name: TSymStr);
procedure addinsmetadata(insmeta: tai);
destructor destroy; override;
end;
@ -223,7 +230,7 @@ implementation
uses
cutils, strings,
symconst,
aasmcnst,aasmcpu;
aasmcpu;
{ taillvmprocdecl }
@ -262,9 +269,23 @@ uses
secname:=name;
end;
procedure taillvmdecl.addinsmetadata(insmeta: tai);
begin
insmeta.next:=metadata;
metadata:=insmeta;
end;
destructor taillvmdecl.destroy;
var
hp: tai;
begin
while assigned(metadata) do
begin
hp:=tai(metadata.next);
metadata.free;
metadata:=hp;
end;
initdata.free;
inherited destroy;
end;
@ -310,6 +331,20 @@ uses
end;
destructor taillvm.Destroy;
var
hp: tai;
begin
while assigned(metadata) do
begin
hp:=tai(metadata.next);
metadata.free;
metadata:=hp;
end;
inherited;
end;
procedure taillvm.loadoper(opidx: longint; o: toper);
var
i: longint;
@ -508,6 +543,12 @@ uses
end;
end;
procedure taillvm.addinsmetadata(insmeta: tai);
begin
insmeta.next:=metadata;
metadata:=insmeta;
end;
procedure taillvm.landingpad_add_clause(op: tllvmop; def: tdef; kind: TAsmSymbol);
var

View File

@ -108,11 +108,12 @@ interface
tai_llvmmetadatareferenceoperand = class(tai_simple)
strict private
fid: ansistring;
fvalue: tai_llvmbasemetadatanode;
fvalue: tai_llvmmetadatareftypedconst;
public
constructor create(const anID: ansistring; aValue: tai_llvmbasemetadatanode);
constructor createreferenceto(const anID: ansistring; aValue: tai_llvmbasemetadatanode);
destructor destroy; override;
property id: ansistring read fid;
property value: tai_llvmbasemetadatanode read fvalue;
property value: tai_llvmmetadatareftypedconst read fvalue;
end;
@ -269,11 +270,17 @@ implementation
end;
constructor tai_llvmmetadatareferenceoperand.create(const anID: ansistring; aValue: tai_llvmbasemetadatanode);
constructor tai_llvmmetadatareferenceoperand.createreferenceto(const anID: ansistring; aValue: tai_llvmbasemetadatanode);
begin
inherited create(ait_llvmmetadatarefoperand);
fid:=anID;
fvalue:=aValue;
fvalue:=tai_llvmmetadatareftypedconst.create(aValue);
end;
destructor tai_llvmmetadatareferenceoperand.destroy;
begin
fvalue.free;
inherited;
end;
/////////////////////////////////////////////////

View File

@ -543,6 +543,7 @@ implementation
procedure TLLVMInstrWriter.WriteInstruction(hp: tai);
var
metatai: tai;
op: tllvmop;
tmpstr,
sep: TSymStr;
@ -767,6 +768,14 @@ implementation
end;
if op=la_alloca then
owner.writer.AsmWrite(getreferencealignstring(taillvm(hp).oper[0]^.ref^));
metatai:=taillvm(hp).metadata;
while assigned(metatai) do
begin
owner.writer.AsmWrite(sep);
sep:=', ';
writetaioper(metatai);
metatai:=tai(metatai.next);
end;
if nested then
owner.writer.AsmWrite(')')
else if owner.fdecllevel=0 then