x86_64 arch + asm start
This commit is contained in:
@@ -38,6 +38,7 @@ def_tokens! {
|
||||
For: "for",
|
||||
Match: "match",
|
||||
Break: "break",
|
||||
Asm: "asm",
|
||||
}
|
||||
other {
|
||||
Ident(String),
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
use crate::parser::{Node, cursor::Token};
|
||||
|
||||
pub mod x86_64;
|
||||
|
||||
pub enum AsmBlock {
|
||||
X86_64(x86_64::Asm),
|
||||
}
|
||||
|
||||
impl Node for AsmBlock {
|
||||
fn parse(ctx: &mut crate::parser::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
|
||||
ctx.expect(Token::OpenCurly)?;
|
||||
let asm = ctx.parse()?;
|
||||
ctx.expect(Token::CloseCurly)?;
|
||||
Ok(Self::X86_64(asm))
|
||||
}
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: crate::parser::DisplayCtx) -> std::fmt::Result {
|
||||
write!(f, "asm {{ ... }}")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
use crate::{
|
||||
backend::arch::x86_64::RegMode,
|
||||
io::{CompilerMsg, Span},
|
||||
parser::{
|
||||
Node,
|
||||
cursor::{LitTy, Token},
|
||||
},
|
||||
};
|
||||
|
||||
pub struct Asm {
|
||||
instrs: Vec<Instr>,
|
||||
}
|
||||
|
||||
pub enum Instr {
|
||||
Mov { dst: RegMode, src: RegImm },
|
||||
Int { code: u64 },
|
||||
}
|
||||
|
||||
pub enum RegImm {
|
||||
Reg(RegMode),
|
||||
Imm(u64),
|
||||
}
|
||||
|
||||
impl Node for Asm {
|
||||
fn parse(ctx: &mut crate::parser::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
|
||||
let mut instrs = Vec::new();
|
||||
while let Some(Token::Ident(next)) = ctx.peek() {
|
||||
match next.as_str() {
|
||||
"mov" => {
|
||||
ctx.next();
|
||||
let dst = parse_reg(ctx)?;
|
||||
ctx.expect(Token::Comma)?;
|
||||
let src = parse_rmi(ctx)?;
|
||||
instrs.push(Instr::Mov { dst, src });
|
||||
}
|
||||
"int" => {
|
||||
ctx.next();
|
||||
let Token::Lit(LitTy::Number(num)) = ctx.expect_next()? else {
|
||||
return Err("Expected an immediate".into());
|
||||
};
|
||||
let code = parse_imm(&num, ctx.span)?;
|
||||
instrs.push(Instr::Int { code });
|
||||
}
|
||||
_ => {
|
||||
let msg = format!("Unknown instruction {next}");
|
||||
ctx.next();
|
||||
return Err(CompilerMsg {
|
||||
msg,
|
||||
spans: vec![ctx.span],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Self { instrs })
|
||||
}
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: crate::parser::DisplayCtx) -> std::fmt::Result {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_imm(mut s: &str, span: Span) -> Result<u64, CompilerMsg> {
|
||||
let mut radix = 10;
|
||||
if s.starts_with("0x") {
|
||||
radix = 16;
|
||||
s = &s[2..];
|
||||
}
|
||||
u64::from_str_radix(s, radix)
|
||||
.map_err(|_| CompilerMsg::from(("invalid immediate", span)))
|
||||
}
|
||||
|
||||
pub fn parse_rmi(ctx: &mut crate::parser::ParseCtx) -> Result<RegImm, CompilerMsg> {
|
||||
let next = ctx.expect_next()?;
|
||||
let err = || CompilerMsg::unexpected_token(&next, ctx.span, "a register or immediate");
|
||||
Ok(match &next {
|
||||
Token::Ident(ident) => RegImm::Reg(RegMode::parse(ident).ok_or_else(err)?),
|
||||
Token::Lit(LitTy::Number(num)) => RegImm::Imm(parse_imm(num, ctx.span)?),
|
||||
_ => return Err(err()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_reg(ctx: &mut crate::parser::ParseCtx) -> Result<RegMode, CompilerMsg> {
|
||||
let next = ctx.expect_next()?;
|
||||
let err = || CompilerMsg::unexpected_token(&next, ctx.span, "a register");
|
||||
let Token::Ident(next) = &next else {
|
||||
return Err(err());
|
||||
};
|
||||
RegMode::parse(next).ok_or_else(err)
|
||||
}
|
||||
@@ -45,6 +45,7 @@ pub enum ExprTy {
|
||||
Import(Ident),
|
||||
Fn(Box<Func>),
|
||||
Break,
|
||||
Asm(AsmBlock),
|
||||
}
|
||||
|
||||
impl Node for Expr {
|
||||
@@ -162,6 +163,7 @@ impl ExprTy {
|
||||
Self::Break => {
|
||||
write!(f, "break")
|
||||
}
|
||||
Self::Asm(asm) => asm.fmt(f, ctx),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,6 +218,7 @@ impl Expr {
|
||||
let ident = ctx.parse()?;
|
||||
ExprTy::Import(ident)
|
||||
}
|
||||
Token::Asm => ExprTy::Asm(ctx.parse()?),
|
||||
other => return ctx.unexpected(other, "an expression"),
|
||||
};
|
||||
Ok(Self {
|
||||
@@ -258,7 +261,7 @@ impl Expr {
|
||||
| ExprTy::If { body, .. }
|
||||
| ExprTy::Negate(body)
|
||||
| ExprTy::Assign { val: body, .. } => body.ends_with_block(),
|
||||
| ExprTy::Define { val: body, .. } => body.ends_with_block(),
|
||||
ExprTy::Define { val: body, .. } => body.ends_with_block(),
|
||||
ExprTy::Fn(f) => f.ends_with_block(),
|
||||
_ => false,
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
mod asm;
|
||||
mod body;
|
||||
mod expr;
|
||||
mod func;
|
||||
@@ -5,6 +6,7 @@ mod ident;
|
||||
mod param;
|
||||
mod struct_;
|
||||
mod ty;
|
||||
pub use asm::*;
|
||||
pub use body::*;
|
||||
pub use expr::*;
|
||||
pub use func::*;
|
||||
|
||||
Reference in New Issue
Block a user