From 8a3dd3001d1b6d20c4170015459de59b9d761a42 Mon Sep 17 00:00:00 2001 From: mlokr Date: Sun, 4 Feb 2024 10:34:16 +0100 Subject: [PATCH] smh --- hblang/src/codegen.rs | 48 ++++++++++++++++++++++++++++++++----------- hblang/src/parser.rs | 2 +- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index df70544..480e7eb 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -3,7 +3,6 @@ use std::{iter::Cycle, ops::Range}; use crate::{ lexer::Ty, parser::{Exp, Function, Item, Literal, Struct, Type}, - typechk::Type, }; //| Register | Description | Saver | @@ -75,10 +74,10 @@ pub struct ParamAlloc { } impl ParamAlloc { - fn new(reg_range: Range) -> Self { + fn new() -> Self { Self { - stack: 16, - reg_range, + stack: 16, + reg_range: 2..12, } } @@ -103,12 +102,12 @@ impl ParamAlloc { 1 => { let reg = self.reg_range.start; self.reg_range.start += 1; - Some(Value::Reg(reg)) + Some(Value::Reg(reg, None)) } 2 => { let reg = self.reg_range.start; self.reg_range.start += 2; - Some(Value::Pair(reg, reg + 1)) + Some(Value::Reg(reg, Some(reg + 1))) } _ => unreachable!(), } @@ -162,12 +161,11 @@ type Reg = u8; type Offset = i32; enum Value { - Pair(Reg, Reg), - Reg(Reg), + Reg(Reg, Option), Stack(Offset), Imm(u64), - Spilled(Reg, SlotId), - DoubleSpilled(SlotId, Offset), + Spilled(Reg, SlotId, Option, Option), + DoubleSpilled(SlotId, Offset, Option), } type Label = usize; @@ -195,6 +193,9 @@ pub struct Generator<'a> { func_labels: Vec<(String, Label)>, + stack_size: usize, + pushed_size: usize, + regs: RegAlloc, variables: Vec, slots: Vec, @@ -222,7 +223,7 @@ impl<'a> Generator<'a> { fn generate_function(&mut self, f: &Function) { 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() { let param_size = self.size_of(¶m.ty); @@ -246,7 +247,7 @@ impl<'a> Generator<'a> { 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::Call { name, args } => todo!(), + Exp::Call { name, args } => self.generate_call(expected, name, args), Exp::Ctor { name, fields } => todo!(), Exp::Index { base, index } => todo!(), Exp::Field { base, field } => todo!(), @@ -274,6 +275,29 @@ impl<'a> Generator<'a> { Some(value) } + fn generate_call(&mut self, expected: Option, 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(¶m.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 { match ty { Type::Builtin(ty) => match ty { diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index 4bb0282..3f1b032 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -57,7 +57,7 @@ pub enum Exp { Literal(Literal), Variable(String), Call { - name: Box, + name: String, args: Vec, }, Ctor {