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 {
Immutable(InstructionValue),
Mutable(InstructionValue),
Any(InstructionValue),
}
impl StackValueKind {
@ -72,6 +73,7 @@ impl StackValueKind {
match self {
StackValueKind::Immutable(val) => val,
StackValueKind::Mutable(val) => val,
StackValueKind::Any(val) => val,
}
}
@ -79,6 +81,7 @@ impl StackValueKind {
match self {
StackValueKind::Immutable(_) => StackValueKind::Immutable(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 {
should_load: bool,
}
@ -124,6 +127,12 @@ impl State {
}
}
impl Default for State {
fn default() -> Self {
Self { should_load: true }
}
}
impl mir::Module {
fn codegen<'ctx>(&self, context: &'ctx Context) -> ModuleCodegen<'ctx> {
let mut module = context.module(&self.name, self.is_main);
@ -213,8 +222,8 @@ impl mir::Module {
};
match &mir_function.kind {
mir::FunctionDefinitionKind::Local(block, _) => {
let mut state = State::default();
if let Some(ret) = block.codegen(&mut scope, &mut state) {
let state = State::default();
if let Some(ret) = block.codegen(&mut scope, &state) {
scope.block.terminate(Term::Ret(ret)).unwrap();
} else {
if !scope.block.delete_if_unused().unwrap() {
@ -245,7 +254,7 @@ impl mir::Block {
if let Some((kind, expr)) = &self.return_expression {
match kind {
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();
None
}
@ -265,7 +274,7 @@ impl mir::Statement {
) -> Option<InstructionValue> {
match &self.0 {
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(
name.clone(),
StackValue(
@ -302,7 +311,7 @@ impl mir::Statement {
}
mir::StmtKind::Set(lhs, rhs) => {
let lhs_value = lhs
.codegen(scope, &mut state.load(false))
.codegen(scope, &state.load(false))
.expect("non-returning LHS snuck into codegen!");
let rhs_value = rhs.codegen(scope, state)?;
@ -346,6 +355,7 @@ impl mir::Expression {
val
}
}
_ => panic!("Found an unknown-mutable variable!"),
})
}
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) => {
dbg!(&expression, &idx_expr);
let array = expression
.codegen(scope, state)
.codegen(scope, &state.load(true))
.expect("array returned none!");
let idx = idx_expr
.codegen(scope, state)
.codegen(scope, &state.load(true))
.expect("index returned none!");
let mut ptr = scope
@ -488,6 +497,8 @@ impl mir::Expression {
let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap();
let idx = struct_ty.find_index(field).unwrap();
dbg!(&scope.context);
dbg!(&struct_val);
let mut value = scope
.block
.build(Instr::GetStructElemPtr(struct_val, idx as u32))

View File

@ -1,13 +1,14 @@
// Arithmetic, function calls and imports!
fn array() -> [u16; 4] {
return [10, 15, 7, 9];
fn array() -> [[[u16; 4];1];1] {
return [[[10, 15, 7, 9]]];
}
fn main() -> u16 {
let mut list = array();
list[0][0][2] = 19;
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];
b.second[2] = 99;
return value[0].second[2];
return value[val1].second[2];
}