Skip to content

Commit

Permalink
fixup! Add instruction selection
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed May 30, 2019
1 parent d857548 commit 44a1bb5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
23 changes: 21 additions & 2 deletions tiger/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use frame::Frame;
use temp::{Label, Temp};

#[derive(Debug)]
Expand All @@ -41,7 +42,25 @@ pub enum Instruction {
}

impl Instruction {
pub fn to_string(&self) -> String {
String::new()
pub fn to_string<F: Frame>(&self) -> String {
match *self {
Instruction::Label { ref assembly, .. } => assembly.clone(),
Instruction::Operation { ref assembly, ref destination, ref source, .. } => {
let mut result = assembly.clone();
for (index, temp) in destination.iter().enumerate() {
result = result.replace(&format!("'d{}", index), &temp.to_string::<F>());
}
for (index, temp) in source.iter().enumerate() {
result = result.replace(&format!("'s{}", index), &temp.to_string::<F>());
}
result
},
Instruction::Move { ref assembly, ref destination, ref source } => {
let mut result = assembly.clone();
result = result.replace("'s0", &source.to_string::<F>());
result = result.replace("'d0", &destination.to_string::<F>());
result
},
}
}
}
8 changes: 6 additions & 2 deletions tiger/src/asm_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

use asm::Instruction;
use frame::Frame;
use frame::x86_64::X86_64;
use ir::{
BinOp,
Expand Down Expand Up @@ -86,11 +87,12 @@ impl Gen {
let temp = Temp::new();
match expr {
// Error cases:
Exp::Error | Exp::Name(_) | Exp::ExpSequence(_, _) | Exp::BinOp { left: box Exp::Error, .. }
Exp::Error | Exp::ExpSequence(_, _) | Exp::BinOp { left: box Exp::Error, .. }
| Exp::BinOp { right: box Exp::Error, .. } | Exp::BinOp { left: box Exp::Name(_), .. }
| Exp::BinOp { right: box Exp::Name(_), .. }
=> unreachable!(),

Exp::Name(_) => return temp,
Exp::Mem(box Exp::BinOp { op: BinOp::Plus, left: expr, right: box Exp::Const(num) }) |
Exp::Mem(box Exp::BinOp { op: BinOp::Plus, left: box Exp::Const(num), right: expr }) => {
let instruction = Instruction::Move {
Expand Down Expand Up @@ -589,6 +591,8 @@ impl Gen {
}

pub fn debug(&self) {
println!("{:#?}", self.instructions);
for instruction in &self.instructions {
println!("{}", instruction.to_string::<X86_64>());
}
}
}
7 changes: 4 additions & 3 deletions tiger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ fn drive() -> Result<(), Vec<Error>> {
args.next();
if let Some(filename) = args.next() {
let strings = Rc::new(Strings::new());
let ast = (|| {
let (ast, main_symbol) = (|| {
let file = BufReader::new(File::open(filename)?);
let lexer = Lexer::new(file);
let mut symbols = Symbols::new(Rc::clone(&strings));
let main_symbol = symbols.symbol("main");
let mut parser = Parser::new(lexer, &mut symbols);
parser.parse()
parser.parse().map(|ast| (ast, main_symbol))
})()
.map_err(|error| vec![error])?;
let escape_env = find_escapes(&ast, Rc::clone(&strings));
let mut env = Env::<X86_64>::new(&strings, escape_env);
{
let semantic_analyzer = SemanticAnalyzer::new(&mut env);
let fragments = semantic_analyzer.analyze(&ast)?;
let fragments = semantic_analyzer.analyze(main_symbol, ast)?;

for fragment in fragments {
match fragment {
Expand Down

0 comments on commit 44a1bb5

Please sign in to comment.