Compare commits

...

3 Commits

3 changed files with 25 additions and 13 deletions

View File

@ -65,6 +65,7 @@ pub struct StackValue(StackValueKind, Type);
pub enum StackValueKind { pub enum StackValueKind {
Immutable(InstructionValue), Immutable(InstructionValue),
Mutable(InstructionValue), Mutable(InstructionValue),
Any(InstructionValue),
} }
impl StackValueKind { impl StackValueKind {
@ -72,6 +73,7 @@ impl StackValueKind {
match self { match self {
StackValueKind::Immutable(val) => val, StackValueKind::Immutable(val) => val,
StackValueKind::Mutable(val) => val, StackValueKind::Mutable(val) => val,
StackValueKind::Any(val) => val,
} }
} }
@ -79,6 +81,7 @@ impl StackValueKind {
match self { match self {
StackValueKind::Immutable(_) => StackValueKind::Immutable(instr), StackValueKind::Immutable(_) => StackValueKind::Immutable(instr),
StackValueKind::Mutable(_) => StackValueKind::Mutable(instr), StackValueKind::Mutable(_) => StackValueKind::Mutable(instr),
StackValueKind::Any(_) => StackValueKind::Any(instr),
} }
} }
} }
@ -110,7 +113,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
} }
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, Clone, Copy)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
struct State { struct State {
should_load: bool, should_load: bool,
} }
@ -124,6 +127,12 @@ impl State {
} }
} }
impl Default for State {
fn default() -> Self {
Self { should_load: true }
}
}
impl mir::Module { impl mir::Module {
fn codegen<'ctx>(&self, context: &'ctx Context) -> ModuleCodegen<'ctx> { fn codegen<'ctx>(&self, context: &'ctx Context) -> ModuleCodegen<'ctx> {
let mut module = context.module(&self.name, self.is_main); let mut module = context.module(&self.name, self.is_main);
@ -213,8 +222,8 @@ impl mir::Module {
}; };
match &mir_function.kind { match &mir_function.kind {
mir::FunctionDefinitionKind::Local(block, _) => { mir::FunctionDefinitionKind::Local(block, _) => {
let mut state = State::default(); let state = State::default();
if let Some(ret) = block.codegen(&mut scope, &mut state) { if let Some(ret) = block.codegen(&mut scope, &state) {
scope.block.terminate(Term::Ret(ret)).unwrap(); scope.block.terminate(Term::Ret(ret)).unwrap();
} else { } else {
if !scope.block.delete_if_unused().unwrap() { if !scope.block.delete_if_unused().unwrap() {
@ -245,7 +254,7 @@ impl mir::Block {
if let Some((kind, expr)) = &self.return_expression { if let Some((kind, expr)) = &self.return_expression {
match kind { match kind {
mir::ReturnKind::Hard => { mir::ReturnKind::Hard => {
let ret = expr.codegen(&mut scope, &mut state.load(true))?; let ret = expr.codegen(&mut scope, &state)?;
scope.block.terminate(Term::Ret(ret)).unwrap(); scope.block.terminate(Term::Ret(ret)).unwrap();
None None
} }
@ -265,7 +274,7 @@ impl mir::Statement {
) -> Option<InstructionValue> { ) -> Option<InstructionValue> {
match &self.0 { match &self.0 {
mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => { mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => {
let value = expression.codegen(scope, &state.load(true)).unwrap(); let value = expression.codegen(scope, &state).unwrap();
scope.stack_values.insert( scope.stack_values.insert(
name.clone(), name.clone(),
StackValue( StackValue(
@ -302,7 +311,7 @@ impl mir::Statement {
} }
mir::StmtKind::Set(lhs, rhs) => { mir::StmtKind::Set(lhs, rhs) => {
let lhs_value = lhs let lhs_value = lhs
.codegen(scope, &mut state.load(false)) .codegen(scope, &state.load(false))
.expect("non-returning LHS snuck into codegen!"); .expect("non-returning LHS snuck into codegen!");
let rhs_value = rhs.codegen(scope, state)?; let rhs_value = rhs.codegen(scope, state)?;
@ -346,6 +355,7 @@ impl mir::Expression {
val val
} }
} }
_ => panic!("Found an unknown-mutable variable!"),
}) })
} }
mir::ExprKind::Literal(lit) => Some(lit.as_const(&mut scope.block)), mir::ExprKind::Literal(lit) => Some(lit.as_const(&mut scope.block)),
@ -415,12 +425,11 @@ impl mir::Expression {
} }
} }
mir::ExprKind::Indexed(expression, val_t, idx_expr) => { mir::ExprKind::Indexed(expression, val_t, idx_expr) => {
dbg!(&expression, &idx_expr);
let array = expression let array = expression
.codegen(scope, state) .codegen(scope, &state.load(true))
.expect("array returned none!"); .expect("array returned none!");
let idx = idx_expr let idx = idx_expr
.codegen(scope, state) .codegen(scope, &state.load(true))
.expect("index returned none!"); .expect("index returned none!");
let mut ptr = scope let mut ptr = scope
@ -488,6 +497,8 @@ impl mir::Expression {
let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap(); let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap();
let idx = struct_ty.find_index(field).unwrap(); let idx = struct_ty.find_index(field).unwrap();
dbg!(&scope.context);
dbg!(&struct_val);
let mut value = scope let mut value = scope
.block .block
.build(Instr::GetStructElemPtr(struct_val, idx as u32)) .build(Instr::GetStructElemPtr(struct_val, idx as u32))

View File

@ -1,13 +1,14 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
fn array() -> [u16; 4] { fn array() -> [[[u16; 4];1];1] {
return [10, 15, 7, 9]; return [[[10, 15, 7, 9]]];
} }
fn main() -> u16 { fn main() -> u16 {
let mut list = array(); let mut list = array();
list[0][0][2] = 19;
let a = 1; let a = 1;
return list[a + 1]; return list[0][0][a + 1];
} }

View File

@ -19,5 +19,5 @@ fn main() -> u32 {
let mut b = value[val1]; let mut b = value[val1];
b.second[2] = 99; b.second[2] = 99;
return value[0].second[2]; return value[val1].second[2];
} }