Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
🚧 wip ir
Browse files Browse the repository at this point in the history
  • Loading branch information
mumu12641 committed Sep 20, 2023
1 parent 4f43b67 commit 97f0ec3
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 91 deletions.
77 changes: 64 additions & 13 deletions src/llvm/ast_ir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
grammar::ast::{
class,
expr::{Expr, Self_},
expr::{Dispatch, DispatchExpr, Expr, Return, Self_},
Type,
},
llvm::ir,
Expand Down Expand Up @@ -30,25 +30,69 @@ impl Expr {
.const_int(*e, false),
);
}
Expr::Dispatch(e) => {
return e.emit_llvm_ir(ir_genrator);
}
Expr::Self_(_) => {
return ir_genrator
.env
.curr_function
.unwrap()
.get_first_param()
.unwrap();
}

Expr::Return(e) => {
return e.emit_llvm_ir(ir_genrator);
}

_ => {}
}
unreachable!()
ir_genrator.get_llvm_type(LLVMType::I32).const_zero()
}
}

impl Dispatch {
pub fn emit_llvm_ir<'a>(&self, ir_genrator: &'a IrGenerator) -> BasicValueEnum<'a> {
match &self.expr {
DispatchExpr::Field(field) => {
let off = ir_genrator
.env
.var_env
.get(&ir_genrator.env.curr_class)
.unwrap()
.find(field)
.unwrap()
.into_offset();

let target = self.target.emit_llvm_ir(ir_genrator);
let result = ir_genrator
.builder
.build_struct_gep(target.into_pointer_value(), off, "a")
.unwrap();
return result.into();
}
DispatchExpr::Method(_) => {}
}
unimplemented!()
}
}
// impl EmitLLVMIR<'_> for Expr {
// fn emit_llvm_ir<V: BasicValue<'a>>(&self, ir_genrator: &mut IrGenerator) -> V {
// todo!()
// }
// }
// impl EmitLLVMIR for {

// }
impl Return {
pub fn emit_llvm_ir<'a>(&self, ir_genrator: &'a IrGenerator) -> BasicValueEnum<'a> {
if let Some(e) = self.val.as_deref() {
ir_genrator
.builder
.build_return(Some(&e.emit_llvm_ir(ir_genrator)));
} else {
ir_genrator.builder.build_return(None);
}
ir_genrator.get_llvm_type(LLVMType::I32).const_zero()
}
}

impl Class {
pub fn emit_llvm_type<'a>(
&self,
ir_genrator: &'a mut IrGenerator,
) {
pub fn emit_llvm_type<'a>(&self, ir_genrator: &'a mut IrGenerator) {
//* class prototype */
//* NULL flag
//* _dispatch_table
Expand Down Expand Up @@ -88,6 +132,13 @@ impl Class {
.env
.field_offset_map
.insert((self.name.clone(), attr.name.clone()), offset);

ir_genrator
.env
.var_env
.get_mut(&self.name)
.unwrap()
.add(&attr.name, &super::env::VarEnv::Field(offset));
offset += 1;
}

Expand Down
80 changes: 80 additions & 0 deletions src/llvm/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::collections::HashMap;

use crate::{
grammar::ast::{Identifier, Type},
utils::table::SymbolTable,
};

use inkwell::{
basic_block::BasicBlock,
types::StructType,
values::{BasicValueEnum, FunctionValue},
};

#[derive(Clone, Eq)]
pub enum VarEnv<'a> {
//* class's field offset */
Field(u32),
//* */
Value(BasicValueEnum<'a>),
}

impl PartialEq for VarEnv<'_> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Field(l0), Self::Field(r0)) => l0 == r0,
(Self::Value(l0), Self::Value(r0)) => l0 == r0,
_ => false,
}
}
}
impl VarEnv<'_> {
pub fn into_offset(&self) -> u32 {
if let Self::Field(off) = self {
return *off;
}
panic!("error")
}
}

pub struct Env<'a> {
//* (class, field) -> offset */
pub field_offset_map: HashMap<(Type, Type), u32>,

//* (class, method) -> offset of method table */
pub method_offset_map: HashMap<(Type, Type), usize>,

//* for struct place holder */
pub struct_type_place_holders: HashMap<Type, StructType<'a>>,

//* curr class */
pub curr_class: Type,

pub curr_function: Option<FunctionValue<'a>>,

pub curr_block: Option<BasicBlock<'a>>,

//* */
// pub var_env: HashMap<Type, SymbolTable<Identifier, BasicValueEnum<'a>>>,
pub var_env: HashMap<Type, SymbolTable<Identifier, VarEnv<'a>>>,
// pub var_env: SymbolTable<Identifier, BasicValueEnum<'a>>,
}

impl Env<'_> {
pub fn new() -> Self {
Env {
field_offset_map: HashMap::new(),
method_offset_map: HashMap::new(),
struct_type_place_holders: HashMap::new(),
curr_class: String::from(""),
curr_function: None,
curr_block: None,
// var_env: SymbolTable::new(),
var_env: HashMap::new(),
}
}

pub fn get_curr_env(&mut self) -> &SymbolTable<std::string::String, VarEnv<'_>> {
self.var_env.get_mut(&self.curr_class).unwrap()
}
}
Loading

0 comments on commit 97f0ec3

Please sign in to comment.