1
0
Fork 0
forked from koniifer/ableos
This commit is contained in:
mlokr 2024-02-04 10:34:16 +01:00
parent de723980da
commit 8a3dd3001d
2 changed files with 37 additions and 13 deletions

View file

@ -3,7 +3,6 @@ use std::{iter::Cycle, ops::Range};
use crate::{ use crate::{
lexer::Ty, lexer::Ty,
parser::{Exp, Function, Item, Literal, Struct, Type}, parser::{Exp, Function, Item, Literal, Struct, Type},
typechk::Type,
}; };
//| Register | Description | Saver | //| Register | Description | Saver |
@ -75,10 +74,10 @@ pub struct ParamAlloc {
} }
impl ParamAlloc { impl ParamAlloc {
fn new(reg_range: Range<Reg>) -> Self { fn new() -> Self {
Self { Self {
stack: 16, stack: 16,
reg_range, reg_range: 2..12,
} }
} }
@ -103,12 +102,12 @@ impl ParamAlloc {
1 => { 1 => {
let reg = self.reg_range.start; let reg = self.reg_range.start;
self.reg_range.start += 1; self.reg_range.start += 1;
Some(Value::Reg(reg)) Some(Value::Reg(reg, None))
} }
2 => { 2 => {
let reg = self.reg_range.start; let reg = self.reg_range.start;
self.reg_range.start += 2; self.reg_range.start += 2;
Some(Value::Pair(reg, reg + 1)) Some(Value::Reg(reg, Some(reg + 1)))
} }
_ => unreachable!(), _ => unreachable!(),
} }
@ -162,12 +161,11 @@ type Reg = u8;
type Offset = i32; type Offset = i32;
enum Value { enum Value {
Pair(Reg, Reg), Reg(Reg, Option<Reg>),
Reg(Reg),
Stack(Offset), Stack(Offset),
Imm(u64), Imm(u64),
Spilled(Reg, SlotId), Spilled(Reg, SlotId, Option<Reg>, Option<SlotId>),
DoubleSpilled(SlotId, Offset), DoubleSpilled(SlotId, Offset, Option<SlotId>),
} }
type Label = usize; type Label = usize;
@ -195,6 +193,9 @@ pub struct Generator<'a> {
func_labels: Vec<(String, Label)>, func_labels: Vec<(String, Label)>,
stack_size: usize,
pushed_size: usize,
regs: RegAlloc, regs: RegAlloc,
variables: Vec<Variable>, variables: Vec<Variable>,
slots: Vec<Slot>, slots: Vec<Slot>,
@ -222,7 +223,7 @@ impl<'a> Generator<'a> {
fn generate_function(&mut self, f: &Function) { fn generate_function(&mut self, f: &Function) {
let frame = self.push_frame(); let frame = self.push_frame();
let mut param_alloc = ParamAlloc::new(2..12); let mut param_alloc = ParamAlloc::new();
for param in f.args.iter() { for param in f.args.iter() {
let param_size = self.size_of(&param.ty); let param_size = self.size_of(&param.ty);
@ -246,7 +247,7 @@ impl<'a> Generator<'a> {
Literal::Bool(b) => self.add_slot(Type::Builtin(Ty::Bool), Value::Imm(*b as u64)), Literal::Bool(b) => self.add_slot(Type::Builtin(Ty::Bool), Value::Imm(*b as u64)),
}, },
Exp::Variable(ident) => self.lookup_variable(ident).unwrap().location, Exp::Variable(ident) => self.lookup_variable(ident).unwrap().location,
Exp::Call { name, args } => todo!(), Exp::Call { name, args } => self.generate_call(expected, name, args),
Exp::Ctor { name, fields } => todo!(), Exp::Ctor { name, fields } => todo!(),
Exp::Index { base, index } => todo!(), Exp::Index { base, index } => todo!(),
Exp::Field { base, field } => todo!(), Exp::Field { base, field } => todo!(),
@ -274,6 +275,29 @@ impl<'a> Generator<'a> {
Some(value) Some(value)
} }
fn generate_call(&mut self, expected: Option<Type>, name: &str, args: &[Exp]) -> SlotId {
let frame = self.push_frame();
let func = self.lookup_function(name);
let mut arg_alloc = ParamAlloc::new();
for (arg, param) in args.iter().zip(&func.args) {
let arg_slot = self.generate_expr(Some(param.ty.clone()), arg).unwrap();
let arg_size = self.size_of(&param.ty);
let param_slot = arg_alloc.alloc(arg_size);
self.set_temporarly(arg_slot, param_slot);
}
todo!()
}
fn set_temporarly(&mut self, from: SlotId, to: Value) {
match to {
Value::Reg(f, s) => {}
};
todo!()
}
fn size_of(&self, ty: &Type) -> usize { fn size_of(&self, ty: &Type) -> usize {
match ty { match ty {
Type::Builtin(ty) => match ty { Type::Builtin(ty) => match ty {

View file

@ -57,7 +57,7 @@ pub enum Exp {
Literal(Literal), Literal(Literal),
Variable(String), Variable(String),
Call { Call {
name: Box<Exp>, name: String,
args: Vec<Exp>, args: Vec<Exp>,
}, },
Ctor { Ctor {