Files
brolang/compiler/ir/ir.odin
T
2026-06-10 21:46:26 +02:00

90 lines
1.7 KiB
Odin

package ir
import "../source"
import "../symbol"
import "../types"
import "core:mem"
INVALID_ID :: -1
Opcode :: enum {
Param,
Const,
Load_Global,
Alloca,
Load,
Store,
Widen,
Add_Checked,
Call,
Trap,
Return,
Return_Void,
}
Instruction :: struct {
op: Opcode,
span: source.Span,
type: types.Type,
integer: i64,
target: int,
a: int,
b: int,
args: []int,
diagnostic: int,
}
Function :: struct {
link_name: string,
c_abi: bool,
is_main: bool,
param_types: []types.Type,
result: types.Type,
instructions: []Instruction,
problematic: bool,
}
Global :: struct {
name: symbol.Id,
type: types.Type,
is_static: bool,
static_value: i64,
initializer: []Instruction,
problematic: bool,
diagnostic: int,
}
Module :: struct {
functions: [dynamic]Function,
globals: [dynamic]Global,
allocator: mem.Allocator,
}
init_module :: proc(allocator := context.allocator) -> Module {
module: Module
module.functions.allocator = allocator
module.globals.allocator = allocator
module.allocator = allocator
return module
}
destroy_instructions :: proc(instructions: []Instruction, allocator: mem.Allocator) {
for instruction in instructions {
delete(instruction.args, allocator)
}
delete(instructions, allocator)
}
destroy_module :: proc(module: ^Module) {
for function in module.functions {
delete(function.link_name, module.allocator)
delete(function.param_types, module.allocator)
destroy_instructions(function.instructions, module.allocator)
}
for global in module.globals {
destroy_instructions(global.initializer, module.allocator)
}
delete(module.functions)
delete(module.globals)
}