Skip to content

Commit

Permalink
Split LibLLVM by C headers (crystal-lang#13948)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Nov 7, 2023
1 parent 2173371 commit de7d333
Show file tree
Hide file tree
Showing 16 changed files with 597 additions and 499 deletions.
4 changes: 2 additions & 2 deletions src/llvm/generic_value.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class LLVM::GenericValue
end

def to_i64 : Int64
LibLLVM.generic_value_to_int(self, signed: 1).unsafe_as(Int64)
LibLLVM.generic_value_to_int(self, is_signed: 1).to_i64!
end

def to_u64 : UInt64
LibLLVM.generic_value_to_int(self, signed: 0)
LibLLVM.generic_value_to_int(self, is_signed: 0)
end

def to_b : Bool
Expand Down
501 changes: 5 additions & 496 deletions src/llvm/lib_llvm.cr

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/llvm/lib_llvm/analysis.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "./types"

lib LibLLVM
fun verify_module = LLVMVerifyModule(m : ModuleRef, action : LLVM::VerifierFailureAction, out_message : Char**) : Bool
end
7 changes: 7 additions & 0 deletions src/llvm/lib_llvm/bit_writer.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "./types"

lib LibLLVM
fun write_bitcode_to_file = LLVMWriteBitcodeToFile(m : ModuleRef, path : Char*) : Int
fun write_bitcode_to_fd = LLVMWriteBitcodeToFD(m : ModuleRef, fd : Int, should_close : Int, unbuffered : Int) : Int
fun write_bitcode_to_memory_buffer = LLVMWriteBitcodeToMemoryBuffer(mod : ModuleRef) : MemoryBufferRef
end
287 changes: 287 additions & 0 deletions src/llvm/lib_llvm/core.cr

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions src/llvm/lib_llvm/debug_info.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
require "./types"

lib LibLLVM
enum DWARFEmissionKind
Full = 1
end

fun create_di_builder = LLVMCreateDIBuilder(m : ModuleRef) : DIBuilderRef
fun dispose_di_builder = LLVMDisposeDIBuilder(builder : DIBuilderRef)
fun di_builder_finalize = LLVMDIBuilderFinalize(builder : DIBuilderRef)

{% if LibLLVM::IS_LT_110 %}
fun di_builder_create_compile_unit = LLVMDIBuilderCreateCompileUnit(
builder : DIBuilderRef, lang : LLVM::DwarfSourceLanguage, file_ref : MetadataRef, producer : Char*,
producer_len : SizeT, is_optimized : Int, flags : Char*, flags_len : SizeT, runtime_ver : UInt,
split_name : Char*, split_name_len : SizeT, kind : DWARFEmissionKind, dwo_id : UInt,
split_debug_inlining : Int, debug_info_for_profiling : Int
) : MetadataRef
{% else %}
fun di_builder_create_compile_unit = LLVMDIBuilderCreateCompileUnit(
builder : DIBuilderRef, lang : LLVM::DwarfSourceLanguage, file_ref : MetadataRef, producer : Char*,
producer_len : SizeT, is_optimized : Int, flags : Char*, flags_len : SizeT, runtime_ver : UInt,
split_name : Char*, split_name_len : SizeT, kind : DWARFEmissionKind, dwo_id : UInt,
split_debug_inlining : Int, debug_info_for_profiling : Int, sys_root : Char*,
sys_root_len : SizeT, sdk : Char*, sdk_len : SizeT
) : MetadataRef
{% end %}

fun di_builder_create_file = LLVMDIBuilderCreateFile(
builder : DIBuilderRef, filename : Char*, filename_len : SizeT,
directory : Char*, directory_len : SizeT
) : MetadataRef

fun di_builder_create_function = LLVMDIBuilderCreateFunction(
builder : DIBuilderRef, scope : MetadataRef, name : Char*, name_len : SizeT,
linkage_name : Char*, linkage_name_len : SizeT, file : MetadataRef, line_no : UInt,
ty : MetadataRef, is_local_to_unit : Int, is_definition : Int, scope_line : UInt,
flags : LLVM::DIFlags, is_optimized : Int
) : MetadataRef

fun di_builder_create_lexical_block = LLVMDIBuilderCreateLexicalBlock(
builder : DIBuilderRef, scope : MetadataRef, file : MetadataRef, line : UInt, column : UInt
) : MetadataRef
fun di_builder_create_lexical_block_file = LLVMDIBuilderCreateLexicalBlockFile(
builder : DIBuilderRef, scope : MetadataRef, file_scope : MetadataRef, discriminator : UInt
) : MetadataRef

fun di_builder_get_or_create_type_array = LLVMDIBuilderGetOrCreateTypeArray(builder : DIBuilderRef, types : MetadataRef*, length : SizeT) : MetadataRef

fun di_builder_create_subroutine_type = LLVMDIBuilderCreateSubroutineType(
builder : DIBuilderRef, file : MetadataRef, parameter_types : MetadataRef*,
num_parameter_types : UInt, flags : LLVM::DIFlags
) : MetadataRef
{% unless LibLLVM::IS_LT_90 %}
fun di_builder_create_enumerator = LLVMDIBuilderCreateEnumerator(
builder : DIBuilderRef, name : Char*, name_len : SizeT, value : Int64, is_unsigned : Int
) : MetadataRef
{% end %}
fun di_builder_create_enumeration_type = LLVMDIBuilderCreateEnumerationType(
builder : DIBuilderRef, scope : MetadataRef, name : Char*, name_len : SizeT, file : MetadataRef,
line_number : UInt, size_in_bits : UInt64, align_in_bits : UInt32,
elements : MetadataRef*, num_elements : UInt, class_ty : MetadataRef
) : MetadataRef
fun di_builder_create_union_type = LLVMDIBuilderCreateUnionType(
builder : DIBuilderRef, scope : MetadataRef, name : Char*, name_len : SizeT, file : MetadataRef,
line_number : UInt, size_in_bits : UInt64, align_in_bits : UInt32, flags : LLVM::DIFlags,
elements : MetadataRef*, num_elements : UInt, run_time_lang : UInt, unique_id : Char*, unique_id_len : SizeT
) : MetadataRef
fun di_builder_create_array_type = LLVMDIBuilderCreateArrayType(
builder : DIBuilderRef, size : UInt64, align_in_bits : UInt32,
ty : MetadataRef, subscripts : MetadataRef*, num_subscripts : UInt
) : MetadataRef
fun di_builder_create_unspecified_type = LLVMDIBuilderCreateUnspecifiedType(builder : DIBuilderRef, name : Char*, name_len : SizeT) : MetadataRef
fun di_builder_create_basic_type = LLVMDIBuilderCreateBasicType(
builder : DIBuilderRef, name : Char*, name_len : SizeT, size_in_bits : UInt64,
encoding : UInt, flags : LLVM::DIFlags
) : MetadataRef
fun di_builder_create_pointer_type = LLVMDIBuilderCreatePointerType(
builder : DIBuilderRef, pointee_ty : MetadataRef, size_in_bits : UInt64, align_in_bits : UInt32,
address_space : UInt, name : Char*, name_len : SizeT
) : MetadataRef
fun di_builder_create_struct_type = LLVMDIBuilderCreateStructType(
builder : DIBuilderRef, scope : MetadataRef, name : Char*, name_len : SizeT, file : MetadataRef,
line_number : UInt, size_in_bits : UInt64, align_in_bits : UInt32, flags : LLVM::DIFlags,
derived_from : MetadataRef, elements : MetadataRef*, num_elements : UInt,
run_time_lang : UInt, v_table_holder : MetadataRef, unique_id : Char*, unique_id_len : SizeT
) : MetadataRef
fun di_builder_create_member_type = LLVMDIBuilderCreateMemberType(
builder : DIBuilderRef, scope : MetadataRef, name : Char*, name_len : SizeT, file : MetadataRef,
line_no : UInt, size_in_bits : UInt64, align_in_bits : UInt32, offset_in_bits : UInt64,
flags : LLVM::DIFlags, ty : MetadataRef
) : MetadataRef
fun di_builder_create_replaceable_composite_type = LLVMDIBuilderCreateReplaceableCompositeType(
builder : DIBuilderRef, tag : UInt, name : Char*, name_len : SizeT, scope : MetadataRef,
file : MetadataRef, line : UInt, runtime_lang : UInt, size_in_bits : UInt64, align_in_bits : UInt32,
flags : LLVM::DIFlags, unique_identifier : Char*, unique_identifier_len : SizeT
) : MetadataRef

fun di_builder_get_or_create_subrange = LLVMDIBuilderGetOrCreateSubrange(builder : DIBuilderRef, lo : Int64, count : Int64) : MetadataRef
fun di_builder_get_or_create_array = LLVMDIBuilderGetOrCreateArray(builder : DIBuilderRef, data : MetadataRef*, length : SizeT) : MetadataRef

{% if LibLLVM::IS_LT_140 %}
fun di_builder_create_expression = LLVMDIBuilderCreateExpression(builder : DIBuilderRef, addr : Int64*, length : SizeT) : MetadataRef
{% else %}
fun di_builder_create_expression = LLVMDIBuilderCreateExpression(builder : DIBuilderRef, addr : UInt64*, length : SizeT) : MetadataRef
{% end %}

fun metadata_replace_all_uses_with = LLVMMetadataReplaceAllUsesWith(target_metadata : MetadataRef, replacement : MetadataRef)

fun di_builder_insert_declare_at_end = LLVMDIBuilderInsertDeclareAtEnd(
builder : DIBuilderRef, storage : ValueRef, var_info : MetadataRef,
expr : MetadataRef, debug_loc : MetadataRef, block : BasicBlockRef
) : ValueRef

fun di_builder_create_auto_variable = LLVMDIBuilderCreateAutoVariable(
builder : DIBuilderRef, scope : MetadataRef, name : Char*, name_len : SizeT, file : MetadataRef,
line_no : UInt, ty : MetadataRef, always_preserve : Int, flags : LLVM::DIFlags, align_in_bits : UInt32
) : MetadataRef
fun di_builder_create_parameter_variable = LLVMDIBuilderCreateParameterVariable(
builder : DIBuilderRef, scope : MetadataRef, name : Char*, name_len : SizeT, arg_no : UInt,
file : MetadataRef, line_no : UInt, ty : MetadataRef, always_preserve : Int, flags : LLVM::DIFlags
) : MetadataRef

fun set_subprogram = LLVMSetSubprogram(func : ValueRef, sp : MetadataRef)
end
3 changes: 3 additions & 0 deletions src/llvm/lib_llvm/error.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib LibLLVM
type ErrorRef = Void*
end
32 changes: 32 additions & 0 deletions src/llvm/lib_llvm/execution_engine.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "./target"
require "./target_machine"
require "./types"

lib LibLLVM
fun link_in_mc_jit = LLVMLinkInMCJIT

type GenericValueRef = Void*
type ExecutionEngineRef = Void*
type MCJITMemoryManagerRef = Void*

struct MCJITCompilerOptions
opt_level : UInt
code_model : LLVM::CodeModel
no_frame_pointer_elim : Bool
enable_fast_isel : Bool
mcjmm : MCJITMemoryManagerRef
end

fun create_generic_value_of_int = LLVMCreateGenericValueOfInt(ty : TypeRef, n : ULongLong, is_signed : Bool) : GenericValueRef
fun create_generic_value_of_pointer = LLVMCreateGenericValueOfPointer(p : Void*) : GenericValueRef
fun generic_value_to_int = LLVMGenericValueToInt(gen_val : GenericValueRef, is_signed : Bool) : ULongLong
fun generic_value_to_pointer = LLVMGenericValueToPointer(gen_val : GenericValueRef) : Void*
fun generic_value_to_float = LLVMGenericValueToFloat(ty_ref : TypeRef, gen_val : GenericValueRef) : Double
fun dispose_generic_value = LLVMDisposeGenericValue(gen_val : GenericValueRef)

fun create_jit_compiler_for_module = LLVMCreateJITCompilerForModule(out_jit : ExecutionEngineRef*, m : ModuleRef, opt_level : UInt, error : Char**) : Bool
fun create_mc_jit_compiler_for_module = LLVMCreateMCJITCompilerForModule(out_jit : ExecutionEngineRef*, m : ModuleRef, options : MCJITCompilerOptions*, size_of_options : SizeT, out_error : Char**) : Bool
fun dispose_execution_engine = LLVMDisposeExecutionEngine(ee : ExecutionEngineRef)
fun run_function = LLVMRunFunction(ee : ExecutionEngineRef, f : ValueRef, num_args : UInt, args : GenericValueRef*) : GenericValueRef
fun get_pointer_to_global = LLVMGetPointerToGlobal(ee : ExecutionEngineRef, global : ValueRef) : Void*
end
18 changes: 18 additions & 0 deletions src/llvm/lib_llvm/initialization.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% skip_file unless LibLLVM::IS_LT_170 %}

require "./types"

lib LibLLVM
fun initialize_core = LLVMInitializeCore(r : PassRegistryRef)
fun initialize_transform_utils = LLVMInitializeTransformUtils(r : PassRegistryRef)
fun initialize_scalar_opts = LLVMInitializeScalarOpts(r : PassRegistryRef)
fun initialize_obj_c_arc_opts = LLVMInitializeObjCARCOpts(r : PassRegistryRef)
fun initialize_vectorization = LLVMInitializeVectorization(r : PassRegistryRef)
fun initialize_inst_combine = LLVMInitializeInstCombine(r : PassRegistryRef)
fun initialize_ipo = LLVMInitializeIPO(r : PassRegistryRef)
fun initialize_instrumentation = LLVMInitializeInstrumentation(r : PassRegistryRef)
fun initialize_analysis = LLVMInitializeAnalysis(r : PassRegistryRef)
fun initialize_ipa = LLVMInitializeIPA(r : PassRegistryRef)
fun initialize_code_gen = LLVMInitializeCodeGen(r : PassRegistryRef)
fun initialize_target = LLVMInitializeTarget(r : PassRegistryRef)
end
5 changes: 5 additions & 0 deletions src/llvm/lib_llvm/ir_reader.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "./types"

lib LibLLVM
fun parse_ir_in_context = LLVMParseIRInContext(context_ref : ContextRef, mem_buf : MemoryBufferRef, out_m : ModuleRef*, out_message : Char**) : Bool
end
34 changes: 34 additions & 0 deletions src/llvm/lib_llvm/target.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "./types"

lib LibLLVM
type TargetDataRef = Void*

fun initialize_aarch64_target_info = LLVMInitializeAArch64TargetInfo
fun initialize_aarch64_target = LLVMInitializeAArch64Target
fun initialize_aarch64_target_mc = LLVMInitializeAArch64TargetMC
fun initialize_aarch64_asm_printer = LLVMInitializeAArch64AsmPrinter
fun initialize_aarch64_asm_parser = LLVMInitializeAArch64AsmParser
fun initialize_arm_target_info = LLVMInitializeARMTargetInfo
fun initialize_arm_target = LLVMInitializeARMTarget
fun initialize_arm_target_mc = LLVMInitializeARMTargetMC
fun initialize_arm_asm_printer = LLVMInitializeARMAsmPrinter
fun initialize_arm_asm_parser = LLVMInitializeARMAsmParser
fun initialize_webassembly_target_info = LLVMInitializeWebAssemblyTargetInfo
fun initialize_webassembly_target = LLVMInitializeWebAssemblyTarget
fun initialize_webassembly_target_mc = LLVMInitializeWebAssemblyTargetMC
fun initialize_webassembly_asm_printer = LLVMInitializeWebAssemblyAsmPrinter
fun initialize_webassembly_asm_parser = LLVMInitializeWebAssemblyAsmParser
fun initialize_x86_target_info = LLVMInitializeX86TargetInfo
fun initialize_x86_target = LLVMInitializeX86Target
fun initialize_x86_target_mc = LLVMInitializeX86TargetMC
fun initialize_x86_asm_printer = LLVMInitializeX86AsmPrinter
fun initialize_x86_asm_parser = LLVMInitializeX86AsmParser

fun set_module_data_layout = LLVMSetModuleDataLayout(m : ModuleRef, dl : TargetDataRef)

fun dispose_target_data = LLVMDisposeTargetData(td : TargetDataRef)
fun size_of_type_in_bits = LLVMSizeOfTypeInBits(td : TargetDataRef, ty : TypeRef) : ULongLong
fun abi_size_of_type = LLVMABISizeOfType(td : TargetDataRef, ty : TypeRef) : ULongLong
fun abi_alignment_of_type = LLVMABIAlignmentOfType(td : TargetDataRef, ty : TypeRef) : UInt
fun offset_of_element = LLVMOffsetOfElement(td : TargetDataRef, struct_ty : TypeRef, element : UInt) : ULongLong
end
24 changes: 24 additions & 0 deletions src/llvm/lib_llvm/target_machine.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "./target"
require "./types"

lib LibLLVM
type TargetMachineRef = Void*
type TargetRef = Void*

fun get_first_target = LLVMGetFirstTarget : TargetRef
fun get_next_target = LLVMGetNextTarget(t : TargetRef) : TargetRef
fun get_target_from_triple = LLVMGetTargetFromTriple(triple : Char*, t : TargetRef*, error_message : Char**) : Bool
fun get_target_name = LLVMGetTargetName(t : TargetRef) : Char*
fun get_target_description = LLVMGetTargetDescription(t : TargetRef) : Char*

fun create_target_machine = LLVMCreateTargetMachine(t : TargetRef, triple : Char*, cpu : Char*, features : Char*, level : LLVM::CodeGenOptLevel, reloc : LLVM::RelocMode, code_model : LLVM::CodeModel) : TargetMachineRef
fun dispose_target_machine = LLVMDisposeTargetMachine(t : TargetMachineRef)
fun get_target_machine_target = LLVMGetTargetMachineTarget(t : TargetMachineRef) : TargetRef
fun get_target_machine_triple = LLVMGetTargetMachineTriple(t : TargetMachineRef) : Char*
fun create_target_data_layout = LLVMCreateTargetDataLayout(t : TargetMachineRef) : TargetDataRef
fun target_machine_emit_to_file = LLVMTargetMachineEmitToFile(t : TargetMachineRef, m : ModuleRef, filename : Char*, codegen : LLVM::CodeGenFileType, error_message : Char**) : Bool

fun get_default_target_triple = LLVMGetDefaultTargetTriple : Char*
fun normalize_target_triple = LLVMNormalizeTargetTriple(triple : Char*) : Char*
fun get_host_cpu_name = LLVMGetHostCPUName : Char*
end
13 changes: 13 additions & 0 deletions src/llvm/lib_llvm/transforms/pass_builder.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% skip_file if LibLLVM::IS_LT_130 %}

require "../target_machine"
require "../types"

lib LibLLVM
type PassBuilderOptionsRef = Void*

fun run_passes = LLVMRunPasses(m : ModuleRef, passes : Char*, tm : TargetMachineRef, options : PassBuilderOptionsRef) : ErrorRef

fun create_pass_builder_options = LLVMCreatePassBuilderOptions : PassBuilderOptionsRef
fun dispose_pass_builder_options = LLVMDisposePassBuilderOptions(options : PassBuilderOptionsRef)
end
17 changes: 17 additions & 0 deletions src/llvm/lib_llvm/transforms/pass_manager_builder.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% skip_file unless LibLLVM::IS_LT_170 %}

require "../types"

lib LibLLVM
type PassManagerBuilderRef = Void*

fun pass_manager_builder_create = LLVMPassManagerBuilderCreate : PassManagerBuilderRef
fun dispose_pass_manager_builder = LLVMPassManagerBuilderDispose(pmb : PassManagerBuilderRef)
fun pass_manager_builder_set_opt_level = LLVMPassManagerBuilderSetOptLevel(pmb : PassManagerBuilderRef, opt_level : UInt)
fun pass_manager_builder_set_size_level = LLVMPassManagerBuilderSetSizeLevel(pmb : PassManagerBuilderRef, size_level : UInt)
fun pass_manager_builder_set_disable_unroll_loops = LLVMPassManagerBuilderSetDisableUnrollLoops(pmb : PassManagerBuilderRef, value : Bool)
fun pass_manager_builder_set_disable_simplify_lib_calls = LLVMPassManagerBuilderSetDisableSimplifyLibCalls(pmb : PassManagerBuilderRef, value : Bool)
fun pass_manager_builder_use_inliner_with_threshold = LLVMPassManagerBuilderUseInlinerWithThreshold(pmb : PassManagerBuilderRef, threshold : UInt)
fun pass_manager_builder_populate_function_pass_manager = LLVMPassManagerBuilderPopulateFunctionPassManager(pmb : PassManagerBuilderRef, pm : PassManagerRef)
fun pass_manager_builder_populate_module_pass_manager = LLVMPassManagerBuilderPopulateModulePassManager(pmb : PassManagerBuilderRef, pm : PassManagerRef)
end
19 changes: 19 additions & 0 deletions src/llvm/lib_llvm/types.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
lib LibLLVM
# LLVMBool
alias Bool = LibC::Int

type MemoryBufferRef = Void*
type ContextRef = Void*
type ModuleRef = Void*
type TypeRef = Void*
type ValueRef = Void*
type BasicBlockRef = Void*
type MetadataRef = Void*
type BuilderRef = Void*
type DIBuilderRef = Void*
type PassManagerRef = Void*
{% if LibLLVM::IS_LT_170 %}
type PassRegistryRef = Void*
{% end %}
type AttributeRef = Void*
end
2 changes: 1 addition & 1 deletion src/llvm/lib_llvm_ext.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ lib LibLLVMExt
name : LibC::Char*) : LibLLVM::ValueRef

fun target_machine_enable_global_isel = LLVMExtTargetMachineEnableGlobalIsel(machine : LibLLVM::TargetMachineRef, enable : Bool)
fun create_mc_jit_compiler_for_module = LLVMExtCreateMCJITCompilerForModule(jit : LibLLVM::ExecutionEngineRef*, m : LibLLVM::ModuleRef, options : LibLLVM::JITCompilerOptions*, options_length : UInt32, enable_global_isel : Bool, error : UInt8**) : Int32
fun create_mc_jit_compiler_for_module = LLVMExtCreateMCJITCompilerForModule(jit : LibLLVM::ExecutionEngineRef*, m : LibLLVM::ModuleRef, options : LibLLVM::MCJITCompilerOptions*, options_length : UInt32, enable_global_isel : Bool, error : UInt8**) : Int32
end

0 comments on commit de7d333

Please sign in to comment.