Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: changes from the past #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum Error {
UndefinedMain,
#[error(transparent)]
Mips(#[from] stationeers_mips::error::Error),
#[error("internal error during code generation: {0}")]
CodeGen(String),
}

pub type Result<T> = std::result::Result<T, Error>;
18 changes: 18 additions & 0 deletions crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,31 @@ pub fn generate_program(program: ayysee_parser::ast::Program) -> Result<String>
generate_statement(statement, &mut stack, &mut codegen, Pass::First)?;
}

let first_pass_count = codegen.instructions.len();
let first_pass = codegen.get_code();

codegen.clear_first_pass();
stack.clear();

for statement in &program.statements {
generate_statement(statement, &mut stack, &mut codegen, Pass::Second)?;
}

let second_pass_count = codegen.instructions.len();
let second_pass = codegen.get_code();

if first_pass_count != second_pass_count {
println!("first pass:");
println!("{}", first_pass);
println!("second pass:");
println!("{}", second_pass);

return Err(Error::CodeGen(format!(
"First pass generated {} instructions, second pass generated {} instructions",
first_pass_count, second_pass_count
)));
}

// ensure the existance of a main function
if !codegen.has_label("main") {
return Err(Error::UndefinedMain);
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl Stack {

/// Clears values between passes.
pub(crate) fn clear(&mut self) {
self.rsp_offset = 0;
self.loop_counter = 0;
self.if_counter = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro_rules! assign_variable {
$codegen.add_instruction(Instruction::from(Arithmetic::Add {
register: Register::Sp,
a: Register::Sp.into(),
b: Number::Int(offset).into(),
b: Number::Int(offset - 1).into(),
}));
}
Location::Register(register) => {
Expand Down