Files
brolang/compiler/hir/hir.odin
T

189 lines
4.2 KiB
Odin

package hir
import "../source"
import "../symbol"
import "../types"
import "core:mem"
Expr_Id :: distinct u32
Stmt_Id :: distinct u32
Local_Id :: distinct u32
Function_Id :: distinct u32
Global_Id :: distinct u32
Ref :: distinct u32
INVALID_EXPR :: Expr_Id(0xffff_ffff)
INVALID_STMT :: Stmt_Id(0xffff_ffff)
INVALID_LOCAL :: Local_Id(0xffff_ffff)
INVALID_FUNCTION :: Function_Id(0xffff_ffff)
INVALID_GLOBAL :: Global_Id(0xffff_ffff)
INVALID_REF :: Ref(0xffff_ffff)
expr_id :: proc(index: int) -> Expr_Id {
assert(index >= 0 && u64(index) < u64(INVALID_EXPR))
return Expr_Id(index)
}
stmt_id :: proc(index: int) -> Stmt_Id {
assert(index >= 0 && u64(index) < u64(INVALID_STMT))
return Stmt_Id(index)
}
local_id :: proc(index: int) -> Local_Id {
assert(index >= 0 && u64(index) < u64(INVALID_LOCAL))
return Local_Id(index)
}
function_id :: proc(index: int) -> Function_Id {
assert(index >= 0 && u64(index) < u64(INVALID_FUNCTION))
return Function_Id(index)
}
global_id :: proc(index: int) -> Global_Id {
assert(index >= 0 && u64(index) < u64(INVALID_GLOBAL))
return Global_Id(index)
}
index :: proc(id: $T, invalid: T, count: int) -> (int, bool) {
value := int(id)
return value, id != invalid && value < count
}
local_ref :: proc(id: Local_Id) -> Ref {return Ref(id)}
function_ref :: proc(id: Function_Id) -> Ref {return Ref(id)}
global_ref :: proc(id: Global_Id) -> Ref {return Ref(id)}
as_local :: proc(ref: Ref) -> Local_Id {return Local_Id(ref)}
as_function :: proc(ref: Ref) -> Function_Id {return Function_Id(ref)}
as_global :: proc(ref: Ref) -> Global_Id {return Global_Id(ref)}
Calling_Convention :: enum u8 {
Brolang,
C,
}
Implementation :: enum u8 {
Definition,
Declaration,
}
Linkage :: enum u8 {
Internal,
External,
}
Expr_Kind :: enum u8 {
Invalid,
Integer,
Local,
Global,
Widen,
Add,
Call,
}
Expr :: struct {
span: source.Span,
type: types.Type,
integer: i64,
args: []Expr_Id,
target: Ref,
left: Expr_Id,
right: Expr_Id,
diagnostic: source.Diagnostic_Id,
kind: Expr_Kind,
}
Local :: struct {
name: symbol.Id,
type: types.Type,
mutable: bool,
parameter: bool,
}
Stmt_Kind :: enum u8 {
Declaration,
Assignment,
Return,
Expression,
Sink,
Trap,
}
Stmt :: struct {
kind: Stmt_Kind,
span: source.Span,
local: Local_Id,
expr: Expr_Id,
diagnostic: source.Diagnostic_Id,
}
Function :: struct {
name: symbol.Id,
link_name: string,
calling_convention: Calling_Convention,
implementation: Implementation,
linkage: Linkage,
is_main: bool,
params: []Local_Id,
result: types.Type,
locals: []Local,
body: []Stmt_Id,
direct_global_reads: [dynamic]Global_Id,
calls: []Function_Id,
problematic: bool,
diagnostic: source.Diagnostic_Id,
}
Global :: struct {
name: symbol.Id,
type: types.Type,
expr: Expr_Id,
static_value: i64,
is_static: bool,
dependencies: [dynamic]Global_Id,
calls: []Function_Id,
direct_problem: bool,
problematic: bool,
diagnostic: source.Diagnostic_Id,
}
Module :: struct {
exprs: [dynamic]Expr,
statements: [dynamic]Stmt,
functions: [dynamic]Function,
globals: [dynamic]Global,
allocator: mem.Allocator,
}
init_module :: proc(allocator := context.allocator) -> Module {
module: Module
module.allocator = allocator
module.exprs.allocator = allocator
module.statements.allocator = allocator
module.functions.allocator = allocator
module.globals.allocator = allocator
return module
}
destroy_module :: proc(module: ^Module) {
for expr in module.exprs {
delete(expr.args, module.allocator)
}
for function in module.functions {
delete(function.link_name, module.allocator)
delete(function.params, module.allocator)
delete(function.locals, module.allocator)
delete(function.body, module.allocator)
delete(function.direct_global_reads)
delete(function.calls, module.allocator)
}
for global in module.globals {
delete(global.dependencies)
delete(global.calls, module.allocator)
}
delete(module.exprs)
delete(module.statements)
delete(module.functions)
delete(module.globals)
}