Skip to content

Commit

Permalink
LLVM 4.0: build llvm_ext (DIBuilder)
Browse files Browse the repository at this point in the history
- new LLVM::DIFlags enum;
- changed DiBuilder::createCompileUnit() signature;
- AlignInBits parameter of DIBuilder->createBasicType was moved
  to createAutoVariable and other methods unused by Crystal
  (createGlobalVariable, createStaticMemberType, createTempGlobalVariableFwdDecl);
  • Loading branch information
ysbaddaden committed Jan 3, 2017
1 parent 8aff8af commit f73ac78
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 21 deletions.
21 changes: 16 additions & 5 deletions src/compiler/crystal/codegen/debug.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ module Crystal
if (ivar_type = ivar.type?) && (ivar_debug_type = get_debug_type(ivar_type))
offset = @program.target_machine.data_layout.offset_of_element(struct_type, idx + (type.struct? ? 0 : 1))
size = @program.target_machine.data_layout.size_in_bits(llvm_embedded_type(ivar_type))
member = di_builder.create_member_type(nil, name[1..-1], nil, 1, size, size, offset * 8, 0, ivar_debug_type)
member = di_builder.create_member_type(nil, name[1..-1], nil, 1, size, size, offset * 8, LLVM::DIFlags::Zero, ivar_debug_type)
element_types << member
end
end

size = @program.target_machine.data_layout.size_in_bits(struct_type)
debug_type = di_builder.create_struct_type(nil, type.to_s, nil, 1, size, size, 0, nil, di_builder.get_or_create_type_array(element_types))
debug_type = di_builder.create_struct_type(nil, type.to_s, nil, 1, size, size, LLVM::DIFlags::Zero, nil, di_builder.get_or_create_type_array(element_types))
unless type.struct?
debug_type = di_builder.create_pointer_type(debug_type, llvm_typer.pointer_size * 8, llvm_typer.pointer_size * 8, type.to_s)
end
Expand Down Expand Up @@ -128,7 +128,17 @@ module Crystal

def declare_variable(var_name, var_type, alloca, location)
declare_local(var_type, alloca, location) do |scope, file, line_number, debug_type|
di_builder.create_auto_variable scope, var_name, file, line_number, debug_type
di_builder.create_auto_variable scope, var_name, file, line_number, debug_type, align_of(var_type)
end
end

private def align_of(type)
case type
when CharType then 32
when IntegerType then type.bits
when FloatType then type.bytes * 8
when BoolType then 8
else 0 # unsupported
end
end

Expand Down Expand Up @@ -238,7 +248,7 @@ module Crystal
file, dir = file_and_dir(filename)
scope = di_builder.create_file(file, dir)
fn_metadata = di_builder.create_function(scope, MAIN_NAME, MAIN_NAME, scope,
0, fun_metadata_type, true, true, 0, 0_u32, false, main_fun)
0, fun_metadata_type, true, true, 0, LLVM::DIFlags::Zero, false, main_fun)
fun_metadatas[main_fun] = fn_metadata
end

Expand All @@ -249,7 +259,8 @@ module Crystal
file, dir = file_and_dir(location.filename)
scope = di_builder.create_file(file, dir)
fn_metadata = di_builder.create_function(scope, target_def.name, target_def.name, scope,
location.line_number, fun_metadata_type, true, true, location.line_number, 0_u32, false, context.fun)
location.line_number, fun_metadata_type, true, true,
location.line_number, LLVM::DIFlags::Zero, false, context.fun)
fun_metadatas[context.fun] = fn_metadata
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/llvm/di_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ struct LLVM::DIBuilder
{% end %}
end

def create_auto_variable(scope, name, file, line, type)
LibLLVMExt.di_builder_create_auto_variable(self, scope, name, file, line, type, 0, 0)
def create_auto_variable(scope, name, file, line, type, align_in_bits)
LibLLVMExt.di_builder_create_auto_variable(self, scope, name, file, line, type, 0, DIFlags::Zero, align_in_bits)
end

def create_parameter_variable(scope, name, argno, file, line, type)
LibLLVMExt.di_builder_create_parameter_variable(self, scope, name, argno, file, line, type, 0, 0)
LibLLVMExt.di_builder_create_parameter_variable(self, scope, name, argno, file, line, type, 0, DIFlags::Zero)
end

def create_expression(addr, length)
Expand Down
28 changes: 28 additions & 0 deletions src/llvm/enums.cr
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,32 @@ module LLVM
UMax
UMin
end

enum DIFlags : UInt32
Zero = 0
Private = 1
Protected = 2
Public = 3
FwdDecl = 1 << 2
AppleBlock = 1 << 3
BlockByrefStruct = 1 << 4
Virtual = 1 << 5
Artificial = 1 << 6
Explicit = 1 << 7
Prototyped = 1 << 8
ObjcClassComplete = 1 << 9
ObjectPointer = 1 << 10
Vector = 1 << 11
StaticMember = 1 << 12
LValueReference = 1 << 13
RValueReference = 1 << 14
ExternalTypeRef = 1 << 15
SingleInheritance = 1 << 16
MultipleInheritance = 2 << 16
VirtualInheritance = 3 << 16
IntroducedVirtual = 1 << 18
BitField = 1 << 19
NoReturn = 1 << 20
MainSubprogram = 1 << 21
end
end
58 changes: 51 additions & 7 deletions src/llvm/ext/llvm_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,38 @@ LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(DIBuilderRef Dref, unsigned Lang,
const char *File,
const char *Dir,
const char *Producer,
int Optimized, const char *Flags,
int Optimized,
const char *Flags,
unsigned RuntimeVersion) {
#if LLVM_VERSION_LE(3, 6)
DIBuilder *D = unwrap(Dref);
DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
Flags, RuntimeVersion);
return wrap(CU);
#else
# if LLVM_VERSION_LE(3, 9)
return wrap(Dref->createCompileUnit(Lang, File, Dir, Producer, Optimized,
Flags, RuntimeVersion));
# else
DIFile *F = Dref->createFile(File, Dir);
return wrap(Dref->createCompileUnit(Lang, F, Producer, Optimized,
Flags, RuntimeVersion));
# endif
#endif
}

LLVMMetadataRef LLVMDIBuilderCreateFunction(
DIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
const char *LinkageName, LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef CompositeType, bool IsLocalToUnit, bool IsDefinition,
unsigned ScopeLine, unsigned Flags, bool IsOptimized, LLVMValueRef Func) {
unsigned ScopeLine,
#if LLVM_VERSION_LE(3, 9)
unsigned Flags,
#else
DINode::DIFlags Flags,
#endif
bool IsOptimized,
LLVMValueRef Func) {
#if LLVM_VERSION_LE(3, 6)
DIBuilder *D = unwrap(Dref);
DISubprogram Sub = D->createFunction(
Expand Down Expand Up @@ -139,7 +153,11 @@ LLVMMetadataRef LLVMDIBuilderCreateBasicType(DIBuilderRef Dref,
DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
return wrap(T);
#else
# if LLVM_VERSION_LE(3, 9)
return wrap(Dref->createBasicType(Name, SizeInBits, AlignInBits, Encoding));
# else
return wrap(Dref->createBasicType(Name, SizeInBits, Encoding));
# endif
#endif
}

Expand Down Expand Up @@ -201,25 +219,42 @@ LLVMDIBuilderCreateSubroutineType(DIBuilderRef Dref, LLVMMetadataRef File,
LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
DIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
int AlwaysPreserve, unsigned Flags) {
int AlwaysPreserve,
#if LLVM_VERSION_LE(3, 9)
unsigned Flags,
#else
DINode::DIFlags Flags,
#endif
uint32_t AlignInBits) {
#if LLVM_VERSION_LE(3, 6)
DIBuilder *D = unwrap(Dref);
DIVariable V = D->createLocalVariable(
llvm::dwarf::DW_TAG_auto_variable, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, 0);
return wrap(V);
#else
# if LLVM_VERSION_LE(3, 9)
DILocalVariable *V = Dref->createAutoVariable(
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
unwrapDI<DIType>(Ty), AlwaysPreserve, Flags);
return wrap(V);
# else
DILocalVariable *V = Dref->createAutoVariable(
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, AlignInBits);
# endif
#endif
return wrap(V);
}

LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
DIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
unsigned ArgNo, LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef Ty, int AlwaysPreserve, unsigned Flags) {
LLVMMetadataRef Ty, int AlwaysPreserve,
#if LLVM_VERSION_LE(3, 9)
unsigned Flags
#else
DINode::DIFlags Flags
#endif
) {
#if LLVM_VERSION_LE(3, 6)
DIBuilder *D = unwrap(Dref);
DIVariable V = D->createLocalVariable(
Expand Down Expand Up @@ -322,7 +357,11 @@ LLVMDIBuilderCreateStructType(DIBuilderRef Dref,
unsigned Line,
uint64_t SizeInBits,
uint64_t AlignInBits,
#if LLVM_VERSION_LE(3, 9)
unsigned Flags,
#else
DINode::DIFlags Flags,
#endif
LLVMMetadataRef DerivedFrom,
LLVMMetadataRef Elements) {
#if LLVM_VERSION_LE(3, 6)
Expand Down Expand Up @@ -374,7 +413,12 @@ LLVMDIBuilderCreateMemberType(DIBuilderRef Dref, LLVMMetadataRef Scope,
const char *Name, LLVMMetadataRef File,
unsigned Line, uint64_t SizeInBits,
uint64_t AlignInBits, uint64_t OffsetInBits,
unsigned Flags, LLVMMetadataRef Ty) {
#if LLVM_VERSION_LE(3, 9)
unsigned Flags,
#else
DINode::DIFlags Flags,
#endif
LLVMMetadataRef Ty) {
#if LLVM_VERSION_LE(3, 6)
DIBuilder *D = unwrap(Dref);
DIDerivedType DT = D->createMemberType(
Expand Down
14 changes: 8 additions & 6 deletions src/llvm/lib_llvm_ext.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ lib LibLLVMExt
builder : DIBuilder, scope : Metadata, name : LibC::Char*,
linkage_name : LibC::Char*, file : Metadata, line : LibC::UInt,
composite_type : Metadata, is_local_to_unit : LibC::Int, is_definition : LibC::Int,
scope_line : LibC::UInt, flags : LibC::UInt, is_optimized : LibC::Int, func : LibLLVM::ValueRef) : Metadata
scope_line : LibC::UInt, flags : LLVM::DIFlags, is_optimized : LibC::Int, func : LibLLVM::ValueRef) : Metadata
{% else %}
fun di_builder_create_function = LLVMDIBuilderCreateFunction(
builder : DIBuilder, scope : Metadata, name : LibC::Char*,
linkage_name : LibC::Char*, file : Metadata, line : LibC::UInt,
composite_type : Metadata, is_local_to_unit : Bool, is_definition : Bool,
scope_line : LibC::UInt, flags : LibC::UInt, is_optimized : Bool, func : LibLLVM::ValueRef) : Metadata
scope_line : LibC::UInt, flags : LLVM::DIFlags, is_optimized : Bool, func : LibLLVM::ValueRef) : Metadata
{% end %}

fun di_builder_create_file = LLVMDIBuilderCreateFile(builder : DIBuilder, file : LibC::Char*, dir : LibC::Char*) : Metadata
Expand All @@ -45,13 +45,15 @@ lib LibLLVMExt
name : LibC::Char*,
file : Metadata, line : LibC::UInt,
type : Metadata,
always_preserve : LibC::Int, flags : LibC::UInt) : Metadata
always_preserve : LibC::Int,
flags : LLVM::DIFlags,
align_in_bits : UInt32) : Metadata

fun di_builder_create_parameter_variable = LLVMDIBuilderCreateParameterVariable(builder : DIBuilder,
scope : Metadata,
name : LibC::Char*, arg_no : LibC::UInt,
file : Metadata, line : LibC::UInt, type : Metadata,
always_preserve : LibC::Int, flags : LibC::UInt) : Metadata
always_preserve : LibC::Int, flags : LLVM::DIFlags) : Metadata

fun di_builder_insert_declare_at_end = LLVMDIBuilderInsertDeclareAtEnd(builder : DIBuilder,
storage : LibLLVM::ValueRef,
Expand All @@ -74,11 +76,11 @@ lib LibLLVMExt

fun di_builder_create_struct_type = LLVMDIBuilderCreateStructType(builder : DIBuilder,
scope : Metadata, name : LibC::Char*, file : Metadata, line : LibC::UInt, size_in_bits : UInt64,
align_in_bits : UInt64, flags : LibC::UInt, derived_from : Metadata, element_types : Metadata) : Metadata
align_in_bits : UInt64, flags : LLVM::DIFlags, derived_from : Metadata, element_types : Metadata) : Metadata

fun di_builder_create_member_type = LLVMDIBuilderCreateMemberType(builder : DIBuilder,
scope : Metadata, name : LibC::Char*, file : Metadata, line : LibC::UInt, size_in_bits : UInt64,
align_in_bits : UInt64, offset_in_bits : UInt64, flags : LibC::UInt, ty : Metadata) : Metadata
align_in_bits : UInt64, offset_in_bits : UInt64, flags : LLVM::DIFlags, ty : Metadata) : Metadata

fun di_builder_create_pointer_type = LLVMDIBuilderCreatePointerType(builder : DIBuilder,
pointee_type : Metadata,
Expand Down

0 comments on commit f73ac78

Please sign in to comment.