-
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.
type system is better now. + i64 has been implemented.
- Loading branch information
1 parent
413fa88
commit 0d7e67a
Showing
19 changed files
with
912 additions
and
423 deletions.
There are no files selected for viewing
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
266 changes: 153 additions & 113 deletions
266
src/code_gen/firstpass.rs → src/code_gen/convert_pass.rs
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::code_gen::nodes; | ||
|
||
// all this does is make sure that the mov doesnt have an immediate thats too big (i.e. 32 bits) | ||
|
||
pub struct Pass { | ||
pub program: nodes::Program, | ||
} | ||
|
||
impl Pass { | ||
pub fn new(program: &nodes::Program) -> Pass { | ||
Pass { program: program.clone() } | ||
} | ||
|
||
pub fn run(&self) -> nodes::Program { | ||
let mut program = nodes::Program { | ||
statements: Vec::new(), | ||
}; | ||
|
||
for function in self.program.statements.clone() { | ||
let mut instructions: Vec<nodes::Instruction> = Vec::new(); | ||
|
||
instructions.push(nodes::Instruction::AllocateStack((function.context.stack_offset + 15) & !15)); // Align stack to 16 bytes | ||
|
||
for statement in function.instructions { | ||
self.emit_instruction(&statement, &mut instructions); | ||
} | ||
|
||
program.statements.push(nodes::FunctionDefinition::new( | ||
function.function_name.clone(), | ||
instructions, | ||
function.context, | ||
function.return_type.clone(), | ||
)); | ||
} | ||
|
||
program | ||
} | ||
|
||
fn arg_is_immediate(&self, arg: &nodes::Operand) -> (bool, i64) { | ||
match arg { | ||
nodes::Operand::Immediate(val) => (true, *val), | ||
_ => (false, 0), | ||
} | ||
} | ||
|
||
fn emit_instruction(&self, statement: &nodes::Instruction, instructions: &mut Vec<nodes::Instruction>) { | ||
match statement { | ||
nodes::Instruction::Mov(ref mov) => { | ||
let src_is_immediate = self.arg_is_immediate(&mov.src); | ||
let dest_is_memory = match &mov.dest { | ||
nodes::Operand::StackAllocate(_) => true, | ||
_ => false, | ||
}; | ||
|
||
if src_is_immediate.0 && src_is_immediate.1 > i32::MAX as i64 && dest_is_memory { | ||
instructions.push(nodes::Instruction::Mov(nodes::BinOp { | ||
dest: nodes::Operand::Register(nodes::Reg::R10), | ||
src: mov.src.clone(), | ||
suffix: mov.suffix.clone(), | ||
})); | ||
|
||
instructions.push(nodes::Instruction::Mov(nodes::BinOp { | ||
dest: mov.dest.clone(), | ||
src: nodes::Operand::Register(nodes::Reg::R10), | ||
suffix: mov.suffix.clone(), | ||
})); | ||
} else { | ||
instructions.push(statement.clone()); | ||
} | ||
}, | ||
_ => { | ||
instructions.push(statement.clone()); | ||
}, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.