-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9d48b5
commit 1edc09c
Showing
20 changed files
with
464 additions
and
372 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() | ||
} |
Oops, something went wrong.