slices (offsets now real in backend)

This commit is contained in:
2024-12-07 23:56:00 -05:00
parent 0e0dbd647d
commit 606cb30c6b
14 changed files with 305 additions and 215 deletions

View File

@@ -1,6 +1,6 @@
use crate::ir::{FileSpan, NamespaceGuard, Origin, Type, VarDef};
use super::{Node, ParserMsg, ParserOutput, PType, PVarDef};
use super::{Node, PType, PVarDef, ParserMsg, ParserOutput};
impl Node<PVarDef> {
pub fn lower(
@@ -54,8 +54,16 @@ impl PType {
if let Ok(num) = self.name.parse::<u32>() {
Type::Bits(num)
} else {
output.err(ParserMsg::from_span(span, "Type not found".to_string()));
Type::Error
match self.name.as_str() {
"slice" => {
let inner = self.args[0].lower(namespace, output);
Type::Slice(Box::new(inner))
}
_ => {
output.err(ParserMsg::from_span(span, "Type not found".to_string()));
Type::Error
}
}
}
}
}

View File

@@ -1,5 +1,5 @@
use super::{func::FnLowerCtx, FnLowerable, PExpr, UnaryOp};
use crate::ir::{IRUInstruction, Type, VarID};
use crate::ir::{IRUInstruction, Size, Type, VarID};
impl FnLowerable for PExpr {
type Output = VarID;
@@ -7,13 +7,15 @@ impl FnLowerable for PExpr {
Some(match self {
PExpr::Lit(l) => match l.as_ref()? {
super::PLiteral::String(s) => {
let dest = ctx.map.temp_var(l.span, Type::Bits(64).arr().rf());
let src = ctx.map.def_data(s.as_bytes().to_vec());
ctx.push(IRUInstruction::LoadData { dest, src });
let dest = ctx.map.temp_var(l.span, Type::Bits(8).slice());
let data = s.as_bytes().to_vec();
let len = data.len() as Size;
let src = ctx.map.def_data(data);
ctx.push(IRUInstruction::LoadSlice { dest, src, len });
dest
}
super::PLiteral::Char(c) => {
let dest = ctx.map.temp_var(l.span, Type::Bits(64).arr().rf());
let dest = ctx.map.temp_var(l.span, Type::Bits(8).slice());
let src = ctx.map.def_data(c.to_string().as_bytes().to_vec());
ctx.push(IRUInstruction::LoadData { dest, src });
dest
@@ -79,9 +81,15 @@ impl FnLowerable for PExpr {
let ty = match def {
Some(def) => def.ret.clone(),
None => {
ctx.err_at(e.span, format!("Expected function, found {}", ctx.map.type_name(&ctx.map.get_var(fe).ty)));
ctx.err_at(
e.span,
format!(
"Expected function, found {}",
ctx.map.type_name(&ctx.map.get_var(fe).ty)
),
);
Type::Error
},
}
};
let temp = ctx.temp(ty);
ctx.push(IRUInstruction::Call {

View File

@@ -1,8 +1,8 @@
use super::{FnLowerable, PFunction, Node, ParserMsg, ParserOutput};
use super::{FnLowerable, Node, PFunction, ParserMsg, ParserOutput};
use crate::{
ir::{
BuiltinType, FileSpan, FnDef, FnID, IRUFunction, Idents, IRUInstruction, IRInstructions,
NamespaceGuard, Origin, Type, VarDef, VarID,
FileSpan, FnDef, FnID, IRInstructions, IRUFunction, IRUInstruction, Idents, NamespaceGuard,
Origin, Type, VarDef, VarID,
},
parser,
};
@@ -46,7 +46,7 @@ impl PFunction {
.collect();
let ret = match &header.ret {
Some(ty) => ty.lower(map, output),
None => Type::Concrete(BuiltinType::Unit.id()),
None => Type::Unit,
};
Some(map.def_fn(FnDef {
name: name.to_string(),
@@ -63,9 +63,7 @@ impl PFunction {
) -> IRUFunction {
let mut instructions = IRInstructions::new();
let def = map.get_fn(id).clone();
let args = def.args.iter().map(|a| {
map.named_var(a.clone())
}).collect();
let args = def.args.iter().map(|a| map.named_var(a.clone())).collect();
let mut ctx = FnLowerCtx {
instructions: &mut instructions,
map,
@@ -79,67 +77,6 @@ impl PFunction {
}
}
// impl Node<AsmFunction> {
// pub fn lower_header(
// &self,
// map: &mut NamespaceGuard,
// output: &mut ParserOutput,
// ) -> Option<FnIdent> {
// self.as_ref()?.lower_header(map, output)
// }
// pub fn lower_body(
// &self,
// map: &mut NamespaceGuard,
// output: &mut ParserOutput,
// ) -> Option<Function> {
// Some(self.as_ref()?.lower_body(map, output))
// }
// }
//
// impl AsmFunction {
// pub fn lower_header(
// &self,
// map: &mut NamespaceGuard,
// output: &mut ParserOutput,
// ) -> Option<FnIdent> {
// let header = self.header.as_ref()?;
// let name = header.name.as_ref()?;
// let args = header
// .args
// .iter()
// .map(|a| {
// // a.lower(map, output).unwrap_or(VarDef {
// // name: "{error}".to_string(),
// // origin: Origin::File(a.span),
// // ty: Type::Error,
// // })
// })
// .collect();
// // let ret = match &header.ret {
// // Some(ty) => ty.lower(map, output),
// // None => Type::Concrete(BuiltinType::Unit.id()),
// // };
// let ret = Type::Concrete(BuiltinType::Unit.id());
// Some(map.def_fn(FnDef {
// name: name.to_string(),
// origin: Origin::File(self.header.span),
// args,
// ret,
// }))
// }
// pub fn lower_body(&self, map: &mut NamespaceGuard, output: &mut ParserOutput) -> Function {
// let mut instructions = Instructions::new();
// let mut ctx = FnLowerCtx {
// instructions: &mut instructions,
// map,
// output,
// span: self.body.span,
// };
// self.body.lower(&mut ctx);
// Function::new(instructions)
// }
// }
pub struct FnLowerCtx<'a, 'n> {
pub map: &'a mut NamespaceGuard<'n>,
pub instructions: &'a mut IRInstructions,

View File

@@ -7,15 +7,6 @@ pub struct PType {
pub args: Vec<Node<PType>>,
}
impl PType {
pub fn unit() -> Self {
Self {
name: "()".to_string(),
args: Vec::new(),
}
}
}
impl Parsable for PType {
fn parse(ctx: &mut ParserCtx) -> ParseResult<Self> {
let next = ctx.expect_peek()?;