Skip to content

Commit

Permalink
fix #111: keep track of instruction size guesses from previous iterat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
hlorenzi committed Sep 30, 2021
1 parent 4e1426c commit 79325f2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "customasm"
version = "0.12.0"
version = "0.11.11"
edition = "2018"
authors = ["hlorenzi <https://hlorenzi.com>"]
description = "An assembler for custom, user-defined instruction sets!"
Expand Down
15 changes: 12 additions & 3 deletions src/asm/parser/rule_invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn match_rule_invocation(
false,
&mut expr::EvalContext::new());

//println!("{} = {:?}", fileserver.get_excerpt(&invocation.span), &resolved);
//println!("early value for `{}` = {:?}", fileserver.get_excerpt(&invocation.span), &resolved);

// TODO: can provide an exact guess even if resolution fails,
// if we have an exact candidate, and
Expand All @@ -116,10 +116,19 @@ pub fn match_rule_invocation(
None => 0,
}
}
_ => 0
_ =>
{
// If the production expression couldn't be resolved,
// try using a size guess from a previous iteration.
match asm_state.instruction_size_guesses.get(&invocation.span)
{
Some(guess) => *guess,
None => 0,
}
}
};

//println!("{} = {}", fileserver.get_excerpt(&invocation.span), invocation.size_guess);
//println!("size guess for `{}` = {}", fileserver.get_excerpt(&invocation.span), invocation.size_guess);

return Ok(invocation);
}
Expand Down
23 changes: 21 additions & 2 deletions src/asm/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::*;
use std::collections::HashMap;


static DEBUG_CANDIDATE_RESOLUTION: bool = false;
Expand All @@ -18,6 +19,7 @@ pub struct State
pub bankdata: Vec<asm::BankData>,
pub symbols: asm::SymbolManager,
pub symbol_guesses: asm::SymbolManager,
pub instruction_size_guesses: HashMap<diagn::Span, usize>,
pub rulesets: Vec<asm::Ruleset>,
pub active_rulesets: Vec<RulesetRef>,
pub cur_bank: BankRef,
Expand Down Expand Up @@ -95,19 +97,22 @@ impl Assembler
-> Result<AssemblyOutput, ()>
{
let mut symbol_guesses = asm::SymbolManager::new();
let mut instruction_size_guesses = HashMap::<diagn::Span, usize>::new();

let mut iteration = 0;
loop
{
self.state = State::new();
self.state.is_first_pass = iteration == 0;
std::mem::swap(&mut self.state.symbol_guesses, &mut symbol_guesses);
std::mem::swap(&mut self.state.instruction_size_guesses, &mut instruction_size_guesses);

iteration += 1;
//dbg!(iteration);

//dbg!(&symbol_guesses);
//dbg!(&self.state.symbols);
//dbg!(&self.state.instruction_size_guesses);

let pass_report = diagn::RcReport::new();

Expand Down Expand Up @@ -143,7 +148,8 @@ impl Assembler
pass_report.clone(),
bank,
bankdata,
fileserver);
fileserver,
&mut instruction_size_guesses);

if pass_report.has_errors() || !bank_output.is_ok()
{
Expand Down Expand Up @@ -202,6 +208,7 @@ impl State
bankdata: Vec::new(),
symbols: asm::SymbolManager::new(),
symbol_guesses: asm::SymbolManager::new(),
instruction_size_guesses: HashMap::new(),
rulesets: Vec::new(),
active_rulesets: Vec::new(),
cur_bank: BankRef { index: 0 },
Expand Down Expand Up @@ -430,7 +437,8 @@ impl State
report: diagn::RcReport,
bank: &asm::Bank,
bankdata: &asm::BankData,
fileserver: &dyn util::FileServer)
fileserver: &dyn util::FileServer,
instruction_size_guesses: &mut HashMap<diagn::Span, usize>)
-> Result<util::BitVec, ()>
{
let mut bitvec = util::BitVec::new();
Expand Down Expand Up @@ -508,6 +516,14 @@ impl State
{
Some(size) =>
{
// Set latest resolved value as the size guess
// for next iterations, but only if the early size guess
// couldn't be inferred.
if invoc.size_guess == 0
{
instruction_size_guesses.insert(invoc.span.clone(), size);
}

if size == invoc.size_guess
{
(bigint, size)
Expand Down Expand Up @@ -1376,6 +1392,8 @@ impl State
fileserver,
true,
info.args)?;

//println!(" value = {:?}", value);

let (bigint, size) = match value.get_bigint()
{
Expand Down Expand Up @@ -1423,6 +1441,7 @@ impl State
parser.expect_linebreak()?;
}

//println!(" result size = {:?}", result.size);
Ok(expr::Value::make_integer(result))
}
}
2 changes: 1 addition & 1 deletion src/diagn/span.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::rc::Rc;


#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, Eq)]
pub struct Span
{
pub file: Rc<String>,
Expand Down
3 changes: 1 addition & 2 deletions tests/expr_asm_value/13.asm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
test {x} => asm { emit x }
}

; weird error?!
test 0x12 ; error: converge
test 0x12 ; = 0x12

#ruledef
{
Expand Down
17 changes: 17 additions & 0 deletions tests/issue111/1.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ruledef
{
sub.neg {a: u8} {b: u8} {r: u8} {j: u8} => a @ b @ r @ j

jmp {j: u8} => asm { sub.neg Z Z+1 T j }
}

jmp main ; = 0x08090410_00000000_00000000_00000001

T:
#d32 0

Z:
#d32 0
#d32 1

main:

0 comments on commit 79325f2

Please sign in to comment.