Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Dec 8, 2023
1 parent e9d48b5 commit 1edc09c
Show file tree
Hide file tree
Showing 20 changed files with 464 additions and 372 deletions.
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = ["joooooooonaaaaa", "juuuuuuuuuuuuuuuuuuliaaaaaaaaaa"]
thiserror = "1.0.50"
bitflags = "2.4.1"
petgraph = "0.6.4"
itertools = "0.11.0"
itertools = "0.12.0"
serde = { version = "1.0.192", features = ["derive"] }
clap = { version = "4.4.7", features = ["derive"] }
miette = { version = "5.10.0", features = ["fancy"] }
Expand Down
30 changes: 15 additions & 15 deletions compiler/src/passes/assign/include_liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,32 @@ pub fn handle_instr<'p>(
use ReadWriteOp::Write as W;

match instr {
Instr::Addq { src, dst }
| Instr::Subq { src, dst }
| Instr::Andq { src, dst }
| Instr::Orq { src, dst }
| Instr::Xorq { src, dst } => {
Instr::Add { src, dst, .. }
| Instr::Sub { src, dst, .. }
| Instr::And{ src, dst, .. }
| Instr::Or { src, dst, .. }
| Instr::Xor { src, dst, .. } => {
arg(dst, RW);
arg(src, R);
}
Instr::Cmpq { src, dst } => {
Instr::Cmp { src, dst, .. } => {
arg(dst, R);
arg(src, R);
}
Instr::Movq { src, dst } => {
Instr::Mov { src, dst, .. } => {
arg(dst, W);
arg(src, R);
}
Instr::Pushq { src } => {
Instr::Push { src, .. } => {
arg(src, R);
}
Instr::Popq { dst } => {
Instr::Pop{ dst, .. } => {
arg(dst, W);
}
Instr::Negq { dst } | Instr::Notq { dst } => {
Instr::Neg { dst, .. } | Instr::Not { dst, .. } => {
arg(dst, RW);
}
Instr::CallqDirect { arity, .. } => {
Instr::CallDirect { arity, .. } => {
for reg in CALLER_SAVED.into_iter().skip(*arity) {
arg(&VarArg::Reg(reg), W);
}
Expand All @@ -151,19 +151,19 @@ pub fn handle_instr<'p>(
arg(&VarArg::Reg(reg), R);
}
}
Instr::Retq => {
Instr::Ret => {
// Because the return value of our function is in RAX, we need to consider it being read at the end of a block.
arg(&VarArg::Reg(Reg::RAX), R);
}
Instr::Setcc { .. } => {
arg(&VarArg::Reg(Reg::RAX), W);
}
Instr::Mulq { src } => {
Instr::Mul{ src, .. } => {
arg(&VarArg::Reg(Reg::RDX), W);
arg(&VarArg::Reg(Reg::RAX), RW);
arg(src, R);
}
Instr::Divq { divisor } => {
Instr::Div { divisor, .. } => {
arg(&VarArg::Reg(Reg::RDX), RW);
arg(&VarArg::Reg(Reg::RAX), RW);
arg(divisor, R);
Expand All @@ -176,7 +176,7 @@ pub fn handle_instr<'p>(
Instr::LoadLbl { dst, .. } => {
arg(dst, W);
}
Instr::CallqIndirect { src, arity } => {
Instr::CallIndirect { src, arity } => {
for reg in CALLER_SAVED.into_iter().skip(*arity) {
arg(&VarArg::Reg(reg), W);
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/passes/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ mod include_liveness;
mod display;

use crate::passes::select::{
Block, FunSelected, Imm, Instr, InstrSelected, Reg, VarArg, X86Selected,
Block, FunSelected, Instr, InstrSelected, Reg, VarArg, X86Selected,
};
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use functor_derive::Functor;
use petgraph::graphmap::GraphMap;
use petgraph::Undirected;
use std::collections::{HashMap, HashSet};
use crate::passes::validate::Int;

pub struct X86Assigned<'p> {
pub fns: HashMap<UniqueSym<'p>, FunAssigned<'p>>,
Expand All @@ -32,7 +33,7 @@ pub type InstrAssigned<'p> = Instr<Arg, UniqueSym<'p>>;
#[derive(Clone, Display)]
pub enum Arg {
#[display(fmt = "${_0}")]
Imm(Imm),
Imm(Int),
#[display(fmt = "%{_0}")]
Reg(Reg),
#[display(fmt = "[%{reg} + ${off}]")]
Expand Down
142 changes: 72 additions & 70 deletions compiler/src/passes/conclude/conclude.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,88 @@
use crate::passes::assign::{Arg, InstrAssigned};
use crate::passes::conclude::X86Concluded;
use crate::passes::patch::X86Patched;
use crate::passes::select::{Block, Imm, Instr};
use crate::passes::select::{Block, Instr};
use crate::utils::unique_sym::gen_sym;
use crate::*;
use std::collections::HashMap;

impl<'p> X86Patched<'p> {
#[must_use]
pub fn conclude(self) -> X86Concluded<'p> {
let entries = self
.fns
.iter()
.map(|(sym, f)| (*sym, f.entry))
.collect::<HashMap<_, _>>();

let mut blocks = self
.fns
.into_iter()
.flat_map(|(_, mut fun)| {
fix_stack_space(fun.blocks.get_mut(&fun.entry).unwrap(), fun.stack_space);
fix_stack_space(fun.blocks.get_mut(&fun.exit).unwrap(), fun.stack_space);

// Replace calls to function labels with calls to the entries of those functions.
fun.blocks.into_iter().map(|(block_sym, mut block)| {
for instr in &mut block.instrs {
match instr {
Instr::CallqDirect { lbl, .. } | Instr::LoadLbl { lbl, .. } => {
*lbl = entries[&lbl];
}
_ => {}
}
}
(block_sym, block)
})
})
.collect::<HashMap<_, _>>();

let entry = gen_sym("runtime");
blocks.insert(
entry,
block!(
callq_direct!(entries[&self.entry], 0),
movq!(reg!(RAX), reg!(RDI)),
movq!(imm32!(0x3C), reg!(RAX)), // todo: can be smaller
syscall!(2)
),
);

let program = X86Concluded { blocks, entry };

// display!(&program, Conclude); // todo
time!("conclude");

program
// let entries = self
// .fns
// .iter()
// .map(|(sym, f)| (*sym, f.entry))
// .collect::<HashMap<_, _>>();
//
// let mut blocks = self
// .fns
// .into_iter()
// .flat_map(|(_, mut fun)| {
// fix_stack_space(fun.blocks.get_mut(&fun.entry).unwrap(), fun.stack_space);
// fix_stack_space(fun.blocks.get_mut(&fun.exit).unwrap(), fun.stack_space);
//
// // Replace calls to function labels with calls to the entries of those functions.
// fun.blocks.into_iter().map(|(block_sym, mut block)| {
// for instr in &mut block.instrs {
// match instr {
// Instr::CallDirect { lbl, .. } | Instr::LoadLbl { lbl, .. } => {
// *lbl = entries[&lbl];
// }
// _ => {}
// }
// }
// (block_sym, block)
// })
// })
// .collect::<HashMap<_, _>>();
//
// let entry = gen_sym("runtime");
// blocks.insert(
// entry,
// block!(
// call_direct!(entries[&self.entry], 0),
// mov!(reg!(RAX), reg!(RDI)),
// mov!(imm32!(0x3C), reg!(RAX)), // todo: can be smaller
// syscall!(2)
// ),
// );
//
// let program = X86Concluded { blocks, entry };
//
// // display!(&program, Conclude); // todo
// time!("conclude");
//
// program
todo!()
}
}

/// Fixes stack allocation for spilled variables.
fn fix_stack_space(block: &mut Block<Arg>, stack_space: usize) {
for instr in &mut block.instrs {
match instr {
InstrAssigned::Addq {
src: Arg::Imm(Imm::Imm32(val)),
..
}
| InstrAssigned::Subq {
src: Arg::Imm(Imm::Imm32(val)),
..
} => {
assert_eq!(*val, 0x1000);
*val = stack_space as u32;
}
InstrAssigned::Addq {
src: Arg::Imm(_), ..
}
| InstrAssigned::Subq {
src: Arg::Imm(_), ..
} => {
todo!()
}
_ => {}
}
}
// for instr in &mut block.instrs {
// match instr {
// InstrAssigned::Addq {
// src: Arg::Imm(Imm::Imm32(val)),
// ..
// }
// | InstrAssigned::Sub {
// src: Arg::Imm(Imm::Imm32(val)),
// ..
// } => {
// assert_eq!(*val, 0x1000);
// *val = stack_space as u32;
// }
// InstrAssigned::Addq {
// src: Arg::Imm(_), ..
// }
// | InstrAssigned::Sub {
// src: Arg::Imm(_), ..
// } => {
// todo!()
// }
// _ => {}
// }
// }
todo!()
}
Loading

0 comments on commit 1edc09c

Please sign in to comment.