Skip to content

Commit

Permalink
fixup! fixup! support for DIBuilder on LLVM8
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed Mar 8, 2021
1 parent ed9a4cb commit 294b005
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
20 changes: 9 additions & 11 deletions src/dibuilder.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export DIBuilder, DIFile, DICompileUnit, DILexicalBlock, DIFunction
export DIBuilder, DIFile, DICompileUnit, DILexicalBlock, DISubprogram

@checked struct DIBuilder
ref::API.LLVMDIBuilderRef
Expand Down Expand Up @@ -84,7 +84,7 @@ function lexicalblock!(builder::DIBuilder, scope::Metadata, file::Metadata, disc
Metadata(md)
end

struct DIFunction
struct DISubprogram
name::String
linkageName::String
file::Metadata
Expand All @@ -97,10 +97,10 @@ struct DIFunction
optimized::Core.Bool
end

function subprogram!(builder::DIBuilder, scope::Metadata, f::DIFunction)
function subprogram!(builder::DIBuilder, f::DISubprogram)
md = API.LLVMDIBuilderCreateFunction(
builder,
scope,
f.file,
f.name, convert(Csize_t, length(f.name)),
f.linkageName, convert(Csize_t, length(f.linkageName)),
f.file,
Expand All @@ -120,30 +120,28 @@ end
function basictype!(builder::DIBuilder, name, size, encoding)
md = LLVM.API.LLVMDIBuilderCreateBasicType(
builder,
name,
convert(Csize_t, length(name)),
name, convert(Csize_t, length(name)),
convert(UInt64, size),
encoding,
LLVM.API.LLVMDIFlagZero
)
Metadata(md)
end

function pointertype!(builder::DIBuilder, pointee::Metadata, size, as, align=0, name="")
function pointertype!(builder::DIBuilder, basetype::Metadata, size, as, align=0, name="")
md = LLVM.API.LLVMDIBuilderCreatePointerType(
builder,
pointee,
basetype,
convert(UInt64, size),
convert(UInt32, align),
convert(Cuint, as),
name,
convert(Csize_t, length(name)),
name, convert(Csize_t, length(name)),
)
Metadata(md)
end

function subroutinetype!(builder::DIBuilder, file::Metadata, rettype, paramtypes...)
params = collect(ref(x) for x in (rettype, paramtypes...))
params = collect(x for x in (rettype, paramtypes...))
md = LLVM.API.LLVMDIBuilderCreateSubroutineType(
builder,
file,
Expand Down
19 changes: 16 additions & 3 deletions test/dibuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ LLVM.Module("SomeModule", ctx) do mod
cu_md = LLVM.compileunit!(dibuilder, cu)

Builder(ctx) do builder
ft = LLVM.FunctionType(LLVM.VoidType(ctx), [LLVM.Int32Type(ctx)])
ft = LLVM.FunctionType(LLVM.VoidType(ctx), [LLVM.Int64Type(ctx)])
rt_md = LLVM.basictype!(dibuilder, "Nothing", 0, 0)
param_md = LLVM.basictype!(dibuilder, "Int64", sizeof(Int64), 0)
dift_md = LLVM.subroutinetype!(dibuilder, file_md, rt_md, param_md)
fn = LLVM.Function(mod, "SomeFunction", ft)
difn = DISubprogram(LLVM.name(fn), "linkage", file_md, 3, dift_md, true, true, 5, LLVM.API.LLVMDIFlagPublic, false)
difn_md = LLVM.subprogram!(dibuilder, difn)
LLVM.set_subprogram!(fn, difn_md)

entrybb = BasicBlock(fn, "entry")
position!(builder, entrybb)

ptr = inttoptr!(builder, parameters(fn)[1], LLVM.PointerType(LLVM.Int64Type(ctx)))
ptr_md = LLVM.pointertype!(dibuilder, param_md, sizeof(Ptr{Int64}), LLVM.addrspace(llvmtype(ptr)), 0, "MyPtr")
# TODO: LLVM.dbg_declare!(builder, mod, ptr, ptr_md)
val = ptrtoint!(builder, ptr, LLVM.Int64Type(ctx))

block = DILexicalBlock(file_md, 1, 1)
block_md = LLVM.lexicalblock!(dibuilder, file_md, block) # could also be file
debuglocation!(builder, LLVM.MetadataAsValue(LLVM.API.LLVMMetadataAsValue(ctx, block_md)))
Expand All @@ -33,11 +44,10 @@ LLVM.Module("SomeModule", ctx) do mod

fun = functions(mod)["SomeFunction"]
bb = entry(fun)
inst = first(instructions(bb))
inst = last(instructions(bb))
@test !isempty(metadata(inst))
inst_str = sprint(io->Base.show(io, inst))
@test occursin("!dbg", inst_str)
@show mod inst

@test !isempty(metadata(inst))
mod_str = sprint(io->Base.show(io, mod))
Expand All @@ -46,6 +56,9 @@ LLVM.Module("SomeModule", ctx) do mod
@test occursin("!DIFile", mod_str)
@test occursin("!DILexicalBlock", mod_str)
@test !occursin("scope: null", mod_str)
@test occursin("DISubprogram", mod_str)
@test occursin("DISubroutineType", mod_str)
@test occursin("DIBasicType", mod_str)

strip_debuginfo!(mod)
@test isempty(metadata(inst))
Expand Down

0 comments on commit 294b005

Please sign in to comment.