Skip to content

Commit

Permalink
inlining error
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Dec 24, 2023
1 parent 46a4a48 commit f9620e2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 109 deletions.
59 changes: 48 additions & 11 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ pub enum Register {
W31,
}

impl TryFrom<&Identifier> for Register {
type Error = ();
fn try_from(Identifier(bytes): &Identifier) -> Result<Self, Self::Error> {
Self::try_from(bytes.as_slice())
}
}

impl TryFrom<&[u8]> for Register {
type Error = ();
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -345,7 +352,7 @@ impl TryFrom<&Variable> for Register {
) -> Result<Self, Self::Error> {
if *addressing == Addressing::Direct
&& *index == None
&& let Ok(register) = Register::try_from(identifier.as_slice())
&& let Ok(register) = Register::try_from(identifier)
{
Ok(register)
} else {
Expand Down Expand Up @@ -398,7 +405,7 @@ impl Default for Literal {
}
}

#[derive(Default, Clone, Hash, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
pub struct Variable {
pub addressing: Addressing,
pub identifier: Identifier,
Expand Down Expand Up @@ -442,7 +449,7 @@ impl std::fmt::Display for Variable {
f,
"{}{}{}",
self.addressing,
std::str::from_utf8(&self.identifier).unwrap(),
self.identifier,
self.index
.as_ref()
.map(|v| format!("[{v}]"))
Expand All @@ -452,26 +459,50 @@ impl std::fmt::Display for Variable {
}

impl From<&str> for Variable {
fn from(bytes: &str) -> Self {
fn from(s: &str) -> Self {
Self {
addressing: Addressing::Direct,
identifier: Identifier::from(bytes.as_bytes()),
identifier: Identifier::from(s),
index: None,
}
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Default, Hash)]
pub struct Identifier(pub Vec<u8>);

impl PartialEq<&str> for Identifier {
fn eq(&self, other: &&str) -> bool {
self.0 == other.as_bytes()
}
}

pub type Identifier = Vec<u8>;
impl Identifier {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn push(&mut self, x: u8) {
self.0.push(x);
}
}

impl std::fmt::Debug for Variable {
impl From<&str> for Identifier {
fn from(s: &str) -> Self {
Self(Vec::from(s.as_bytes()))
}
}

impl std::fmt::Debug for Identifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Variable")
.field("addressing", &self.addressing)
.field("identifier", &std::str::from_utf8(&self.identifier))
.field("index", &self.index)
f.debug_tuple("Identifier")
.field(&std::str::from_utf8(&self.0))
.finish()
}
}
impl std::fmt::Display for Identifier {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", std::str::from_utf8(&self.0).unwrap())
}
}

#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub enum Index {
Expand Down Expand Up @@ -592,6 +623,12 @@ pub enum Syscall {
FTruncate,
Mmap,
}
impl TryFrom<&Identifier> for Syscall {
type Error = ();
fn try_from(identifier: &Identifier) -> Result<Self, Self::Error> {
Self::try_from(identifier.0.as_slice())
}
}
impl TryFrom<&[u8]> for Syscall {
type Error = ();
fn try_from(x: &[u8]) -> Result<Self, Self::Error> {
Expand Down
106 changes: 28 additions & 78 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ pub unsafe fn instruction_from_node(
Op::Special(Special::Type) => match arg {
[Value::Variable(Variable { identifier, .. }), Value::Type(value_type)] => {
type_data.insert(identifier.clone(), value_type.clone());
writeln!(
bss,
"{}: .skip {}",
std::str::from_utf8(identifier).unwrap(),
value_type.bytes()
)
.unwrap();
writeln!(bss, "{identifier}: .skip {}", value_type.bytes()).unwrap();
}
[Value::Variable(Variable { identifier, .. }), Value::Type(value_type), Value::Literal(literal)] =>
{
Expand Down Expand Up @@ -138,12 +132,7 @@ pub unsafe fn instruction_from_node(
_ => todo!(),
};

writeln!(
data,
"{}: {data_value}",
std::str::from_utf8(identifier).unwrap(),
)
.unwrap();
writeln!(data, "{identifier}: {data_value}").unwrap();
}
[Value::Variable(Variable { identifier, .. }), Value::Type(value_type), Value::Literal(literal), tail @ ..] =>
{
Expand Down Expand Up @@ -188,12 +177,7 @@ pub unsafe fn instruction_from_node(
_ => todo!(),
};

writeln!(
data,
"{}: {data_value}",
std::str::from_utf8(identifier).unwrap(),
)
.unwrap();
writeln!(data, "{identifier}: {data_value}").unwrap();
}
_ => todo!(),
},
Expand All @@ -217,12 +201,11 @@ pub unsafe fn instruction_from_node(
&mut assembly,
"\
mov x8, #{}\n\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
ldrb w0, [x0]\n\
svc #0\n\
",
libc::SYS_exit,
std::str::from_utf8(identifier).unwrap()
libc::SYS_exit
)
.unwrap(),
_ => todo!(),
Expand All @@ -239,21 +222,19 @@ pub unsafe fn instruction_from_node(
Type::U8 => write!(
assembly,
"\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
mov w1, #{y}\n\
strb w1, [x0]\n\
",
std::str::from_utf8(identifier).unwrap()
"
)
.unwrap(),
Type::U64 => write!(
assembly,
"\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
mov x1, #{y}\n\
str x1, [x0]\n\
",
std::str::from_utf8(identifier).unwrap()
"
)
.unwrap(),
_ => todo!(),
Expand All @@ -273,12 +254,7 @@ pub unsafe fn instruction_from_node(
assert_eq!(vec.len(), bytes.len());

// Loads the address of the array.
writeln!(
assembly,
"ldr x0, ={}",
std::str::from_utf8(identifier).unwrap()
)
.unwrap();
writeln!(assembly, "ldr x0, ={identifier}").unwrap();

// Packs 2 bytes into stores of the full 32 bit register.
let mut chunks = bytes.array_chunks::<2>();
Expand Down Expand Up @@ -321,12 +297,7 @@ pub unsafe fn instruction_from_node(
assert_eq!(vec.len(), rest.len());

// Loads the address of the array.
writeln!(
assembly,
"ldr x0, ={}",
std::str::from_utf8(identifier).unwrap()
)
.unwrap();
writeln!(assembly, "ldr x0, ={identifier}").unwrap();

// Packs 2 bytes into stores of the full 32 bit register.
let mut chunks = rest.array_chunks::<2>();
Expand Down Expand Up @@ -375,23 +346,21 @@ pub unsafe fn instruction_from_node(
Type::U8 => write!(
&mut assembly,
"\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
ldr w1, [x0]\n\
add w1, w1, #{y}\n\
strb w1, [x0]\n\
",
std::str::from_utf8(identifier).unwrap()
"
)
.unwrap(),
Type::U64 => write!(
&mut assembly,
"\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
ldr x1, [x0]\n\
add x1, x1, #{y}\n\
str x1, [x0]\n\
",
std::str::from_utf8(identifier).unwrap()
"
)
.unwrap(),
_ => todo!(),
Expand All @@ -409,23 +378,21 @@ pub unsafe fn instruction_from_node(
Type::U8 => write!(
&mut assembly,
"\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
ldr w1, [x0]\n\
sub w1, w1, #{y}\n\
strb w1, [x0]\n\
",
std::str::from_utf8(identifier).unwrap()
"
)
.unwrap(),
Type::U64 => write!(
&mut assembly,
"\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
ldr x1, [x0]\n\
sub x1, x1, #{y}\n\
str x1, [x0]\n\
",
std::str::from_utf8(identifier).unwrap()
"
)
.unwrap(),
_ => todo!(),
Expand All @@ -442,12 +409,11 @@ pub unsafe fn instruction_from_node(
write!(
&mut assembly,
"\
ldr x0, ={}\n\
ldr x0, ={identifier}\n\
ldr x0, [x0]\n\
cmp w0, #{y}\n\
bne block{block_counter}\n\
",
std::str::from_utf8(identifier).unwrap(),
"
)
.unwrap();

Expand All @@ -472,12 +438,11 @@ pub unsafe fn instruction_from_node(
"\
mov x8, #{}\n\
mov x0, #{fd}\n\
ldr x1, ={}\n\
ldr x1, ={identifier}\n\
mov x2, #{}\n\
svc #0\n\
",
libc::SYS_read,
std::str::from_utf8(identifier).unwrap(),
type_data.get(identifier).unwrap().bytes()
)
.unwrap();
Expand All @@ -488,25 +453,18 @@ pub unsafe fn instruction_from_node(
index: None,
}), Value::Type(variable_type), Value::Literal(Literal::Integer(fd))] => {
type_data.insert(identifier.clone(), variable_type.clone());
writeln!(
bss,
"{}: .skip {}",
std::str::from_utf8(identifier).unwrap(),
variable_type.bytes()
)
.unwrap();
writeln!(bss, "{identifier}: .skip {}", variable_type.bytes()).unwrap();

write!(
&mut assembly,
"\
mov x8, #{}\n\
mov x0, #{fd}\n\
ldr x1, ={}\n\
ldr x1, ={identifier}\n\
mov x2, #{}\n\
svc #0\n\
",
libc::SYS_read,
std::str::from_utf8(identifier).unwrap(),
variable_type.bytes()
)
.unwrap();
Expand All @@ -524,12 +482,11 @@ pub unsafe fn instruction_from_node(
"\
mov x8, #{}\n\
mov x0, #{fd}\n\
ldr x1, ={}\n\
ldr x1, ={identifier}\n\
mov x2, #{}\n\
svc #0\n\
",
libc::SYS_write,
std::str::from_utf8(identifier).unwrap(),
type_data.get(identifier).unwrap().bytes()
)
.unwrap();
Expand All @@ -551,11 +508,10 @@ pub unsafe fn instruction_from_node(
ldr x0, =empty\n\
mov x1, #0\n\
svc #0\n\
ldr x1, ={}\n\
ldr x1, ={identifier}\n\
str w0, [x1, 4]\n\
",
libc::SYS_memfd_create,
std::str::from_utf8(identifier).unwrap()
)
.unwrap();

Expand All @@ -573,11 +529,10 @@ pub unsafe fn instruction_from_node(
ldr x0, =empty\n\
mov x1, #0\n\
svc #0\n\
ldr x1, ={}\n\
ldr x1, ={identifier}\n\
str w0, [x1]\n\
",
libc::SYS_memfd_create,
std::str::from_utf8(identifier).unwrap()
)
.unwrap();

Expand All @@ -601,12 +556,7 @@ pub unsafe fn instruction_from_node(
identifier,
index: None,
})] => {
writeln!(
&mut assembly,
"ldr {register}, ={}",
std::str::from_utf8(identifier).unwrap()
)
.unwrap();
writeln!(&mut assembly, "ldr {register}, ={identifier}",).unwrap();
}
x @ _ => todo!("{x:?}"),
},
Expand Down
Loading

0 comments on commit f9620e2

Please sign in to comment.