Arbitrary enum and target machine in self-hosted

This commit is contained in:
David Gonzalez Martin 2025-05-31 08:16:06 -06:00
parent a74121567a
commit fe0c0a973a
3 changed files with 57 additions and 2 deletions

View File

@ -823,14 +823,21 @@ MacroInstantiation = struct
LLVMContext = opaque;
LLVMModule = opaque;
LLVMBuilder = opaque;
LLVMDIBuilder = opaque;
LLVMValue = opaque;
LLVMType = opaque;
LLVMMetadata = opaque;
LLVMBasicBlock = opaque;
LLVMIntrinsicId = typealias u32;
LLVMMetadata = opaque;
LLVMDIBuilder = opaque;
LLVMTarget = opaque;
LLVMTargetMachine = opaque;
LLVMTargetMachineOptions = opaque;
LLVMIntrinsicIndex = enum
{
trap,
@ -887,6 +894,13 @@ LLVMCodeGenerationOptimizationLevel = enum
[extern] llvm_context_create_module = fn (context: &LLVMContext, name: []u8) &LLVMModule;
[extern] LLVMCreateBuilderInContext = fn (context: &LLVMContext) &LLVMBuilder;
[extern] LLVMCreateTargetMachineOptions = fn () &LLVMTargetMachineOptions;
[extern] LLVMTargetMachineOptionsSetCPU = fn (target_machine_options: &LLVMTargetMachineOptions, cpu: &u8) void;
[extern] LLVMTargetMachineOptionsSetFeatures = fn (target_machine_options: &LLVMTargetMachineOptions, features: &u8) void;
[extern] LLVMTargetMachineOptionsSetCodeGenOptLevel = fn (target_machine_options: &LLVMTargetMachineOptions, optimization_level: LLVMCodeGenerationOptimizationLevel) void;
[extern] LLVMGetTargetFromTriple = fn (target_triple: &u8, target_pointer: &&LLVMTarget, error_message_pointer: &&u8) s32;
[extern] LLVMCreateTargetMachineWithOptions = fn (target: &LLVMTarget, target_triple: &u8, target_machine_options: &LLVMTargetMachineOptions) &LLVMTargetMachine;
ModuleLLVM = struct
{
context: &LLVMContext,
@ -2690,6 +2704,10 @@ emit = fn (module: &Module) void
#trap();
}
>target_machine_options = LLVMCreateTargetMachineOptions();
LLVMTargetMachineOptionsSetCPU(target_machine_options, cpu_model.pointer);
LLVMTargetMachineOptionsSetFeatures(target_machine_options, cpu_features.pointer);
>code_generation_optimization_level: LLVMCodeGenerationOptimizationLevel = undefined;
switch (module.build_mode)
{
@ -2698,6 +2716,20 @@ emit = fn (module: &Module) void
.optimize_for_speed, .optimize_for_size => { code_generation_optimization_level = .default; },
.aggressively_optimize_for_speed, .aggressively_optimize_for_size => { code_generation_optimization_level = .aggressive; },
}
LLVMTargetMachineOptionsSetCodeGenOptLevel(target_machine_options, code_generation_optimization_level);
>target: &LLVMTarget = zero;
>error_message: &u8 = zero;
>result = LLVMGetTargetFromTriple(target_triple.pointer, &target, &error_message);
if (result != 0)
{
report_error();
}
assert(!error_message);
LLVMCreateTargetMachineWithOptions(target, target_triple.pointer, target_machine_options);
}
compile = fn (arena: &Arena, options: CompileOptions) void

View File

@ -434,6 +434,7 @@ global_variable String names[] =
string_literal("enum_array"),
string_literal("opaque"),
string_literal("basic_struct_passing"),
string_literal("enum_arbitrary_abi"),
};
void entry_point(Slice<char* const> arguments, Slice<char* const> envp)

View File

@ -0,0 +1,22 @@
SomeEnum = enum
{
a,
b,
c,
d,
}
foo = fn (arg: SomeEnum) SomeEnum
{
return arg;
}
[export] main = fn [cc(c)] () s32
{
>some_e: SomeEnum = .c;
>a = foo(some_e);
>b = foo(.d);
if (a != .c) #trap();
if (b != .d) #trap();
return 0;
}