feat: preprocess native argv into sentinel slices

This commit is contained in:
2026-10-23 12:07:23 +02:00
parent eddca5c3bf
commit 8ebbf72388
8 changed files with 167 additions and 93 deletions
+13 -14
View File
@@ -1784,6 +1784,7 @@ append_injected_main :: proc(module: ^ir.Module, hir_module: ^hir.Module, alloca
instructions.allocator = allocator
param_types: []types.Type
args: []ir.Instruction_Id
prepared_args := ir.INVALID_INSTRUCTION
if param_index >= 0 {
init_type := main_function.locals[param_index].type
init_fields := types.fields_for(&module.types, init_type)
@@ -1797,7 +1798,7 @@ append_injected_main :: proc(module: ^ir.Module, hir_module: ^hir.Module, alloca
args_index := 1 - io_index
args_type := types.resolve_alias(init_fields[args_index].type, &module.types)
values_type := types.resolve_alias(types.fields_for(&module.types, args_type)[0].type, &module.types)
argument_pointer := types.child_type(values_type, &module.types)
argument_pointer := types.pointer(&module.types, types.U8, false, true, true, 0)
argv_type := types.pointer(&module.types, argument_pointer, false, true)
param_types = make([]types.Type, 2, allocator)
param_types[0] = types.C_INT
@@ -1809,23 +1810,14 @@ append_injected_main :: proc(module: ^ir.Module, hir_module: ^hir.Module, alloca
diagnostic=source.INVALID_DIAGNOSTIC,
})
}
count := ir.instruction_id(len(instructions))
prepared_args = ir.instruction_id(len(instructions))
append(&instructions, ir.Instruction{
op=.Scalar_Cast, type=types.USIZE, target=ir.INVALID_REF,
a=ir.Instruction_Id(0), b=ir.INVALID_INSTRUCTION,
diagnostic=source.INVALID_DIAGNOSTIC,
})
slice_args := make([]ir.Instruction_Id, 2, allocator)
slice_args[0] = ir.INVALID_INSTRUCTION
slice_args[1] = count
values := ir.instruction_id(len(instructions))
append(&instructions, ir.Instruction{
op=.Slice, type=values_type, args=slice_args, target=ir.INVALID_REF,
a=ir.Instruction_Id(1), b=ir.INVALID_INSTRUCTION,
op=.Prepare_Args, type=values_type, target=ir.INVALID_REF,
a=ir.Instruction_Id(0), b=ir.Instruction_Id(1),
diagnostic=source.INVALID_DIAGNOSTIC,
})
values_args := make([]ir.Instruction_Id, 1, allocator)
values_args[0] = values
values_args[0] = prepared_args
process_args := ir.instruction_id(len(instructions))
append(&instructions, ir.Instruction{
op=.Aggregate, type=args_type, args=values_args, target=ir.INVALID_REF,
@@ -1867,6 +1859,13 @@ append_injected_main :: proc(module: ^ir.Module, hir_module: ^hir.Module, alloca
b=ir.INVALID_INSTRUCTION,
diagnostic=source.INVALID_DIAGNOSTIC,
})
if prepared_args != ir.INVALID_INSTRUCTION {
append(&instructions, ir.Instruction{
op=.Free_Args, type=types.VOID, target=ir.INVALID_REF,
a=prepared_args, b=ir.INVALID_INSTRUCTION,
diagnostic=source.INVALID_DIAGNOSTIC,
})
}
if types.kind(main_function.result, &hir_module.types) == .Fallible {
success := types.fallible_success(main_function.result, &hir_module.types)