Skip to content

Commit

Permalink
Update + Fix everything
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Apr 21, 2024
1 parent c611766 commit f68153f
Show file tree
Hide file tree
Showing 19 changed files with 1,127 additions and 1,175 deletions.
804 changes: 394 additions & 410 deletions Cargo.lock

Large diffs are not rendered by default.

617 changes: 307 additions & 310 deletions bencher/src/main.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ binary-heap-plus = "0.5.0"
lalrpop = "0.20.0"

[dev-dependencies]
test_each_file = "0.3.0"
test_each_file = "0.3.2"
tempfile = "3.8.1"
enum-display = "0.1.3"
derive-name = "1.1.0"
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/passes/assign/include_liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,8 @@ pub fn handle_instr<'p>(
}
arg(src, R);
}
InstrSelected::IDiv { .. } => todo!(),
InstrSelected::IMul { .. } => todo!(),
InstrSelected::MovSX { .. } => todo!(),
}
}
3 changes: 1 addition & 2 deletions compiler/src/passes/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod display;
mod include_liveness;

use crate::passes::select::{Block, FunSelected, Instr, InstrSelected, Reg, VarArg, X86Selected};
use crate::passes::validate::Int;
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use functor_derive::Functor;
Expand All @@ -30,7 +29,7 @@ pub type InstrAssigned<'p> = Instr<Arg, UniqueSym<'p>>;
#[derive(Clone, Display)]
pub enum Arg {
#[display(fmt = "${_0}")]
Imm(Int),
Imm(i32),
#[display(fmt = "%{_0}")]
Reg(Reg),
#[display(fmt = "[%{reg} + ${off}]")]
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/passes/atomize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod display;
use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Def, Lit, Typed, UnaryOp};
use crate::passes::select::InstrSelected;
use crate::passes::validate::Int;

use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use itertools::Itertools;
Expand Down Expand Up @@ -81,7 +81,7 @@ pub enum AExpr<'p> {

#[derive(Copy, Clone, Display)]
pub enum Atom<'p> {
Val { val: Lit<Int> },
Val { val: Lit<i64> },
Var { sym: UniqueSym<'p> },
}

Expand Down
141 changes: 70 additions & 71 deletions compiler/src/passes/conclude/conclude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,82 @@ 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::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!()
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!(imm!(0x3C), reg!(RAX)), // todo: can be smaller
syscall!(2)
),
);

let program = X86Concluded { blocks, entry };

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

program
}
}

/// 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::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!()
for instr in &mut block.instrs {
match instr {
InstrAssigned::Add {
src: Arg::Imm(val),
..
}
| InstrAssigned::Sub {
src: Arg::Imm(val),
..
} => {
assert_eq!(*val, 0x1000);
*val = stack_space as i32;
}
InstrAssigned::Add {
src: Arg::Imm(_), ..
}
| InstrAssigned::Sub {
src: Arg::Imm(_), ..
} => {
todo!()
}
_ => {}
}
}
}
Loading

0 comments on commit f68153f

Please sign in to comment.