thalir_core/codegen/
context.rs1use cranelift_codegen::ir::{self as clif_ir};
2use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
3
4use crate::types::Type;
5
6pub struct CodegenContext<'a> {
7 pub func: &'a mut clif_ir::Function,
8 pub builder_context: FunctionBuilderContext,
9}
10
11impl<'a> CodegenContext<'a> {
12 pub fn new(func: &'a mut clif_ir::Function) -> Self {
13 Self {
14 func,
15 builder_context: FunctionBuilderContext::new(),
16 }
17 }
18
19 pub fn func_builder(&mut self) -> FunctionBuilder<'_> {
20 FunctionBuilder::new(self.func, &mut self.builder_context)
21 }
22
23 pub fn get_clif_type(&self, ty: &Type) -> clif_ir::Type {
24 match ty {
25 Type::Bool => clif_ir::types::I8,
26 Type::Uint(8) | Type::Int(8) => clif_ir::types::I8,
27 Type::Uint(16) | Type::Int(16) => clif_ir::types::I16,
28 Type::Uint(32) | Type::Int(32) => clif_ir::types::I32,
29 Type::Uint(64) | Type::Int(64) => clif_ir::types::I64,
30 Type::Uint(256) | Type::Int(256) | Type::Address => clif_ir::types::I128,
31
32 _ => clif_ir::types::I64,
33 }
34 }
35}