From e59e0ef83d226126487637f655160729412ae24f Mon Sep 17 00:00:00 2001 From: jn Date: Wed, 1 Feb 2023 21:03:46 +0100 Subject: [PATCH 1/2] Reset CURRENT_SIZE to Size::Word before parsing a data directive Previously, the following program: push.16 0 data.16 0 ... crashed fox32asm: thread 'main' panicked at 'internal error: entered unreachable code', src/main.rs:620:22 stack backtrace: 0: rust_begin_unwind at /usr/src/rustc-1.63.0/library/std/src/panicking.rs:584:5 1: core::panicking::panic_fmt at /usr/src/rustc-1.63.0/library/core/src/panicking.rs:142:14 2: core::panicking::panic at /usr/src/rustc-1.63.0/library/core/src/panicking.rs:48:5 3: fox32asm::parse_data 4: fox32asm::build_ast_from_expression 5: fox32asm::main note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. The issue in line 620 is that parse_data expects a constant in the form of AstNode::Immediate32, but finds an AstNode::Immediate16, because CURRENT_SIZE was previously set to Half. Set CURRENT_SIZE to Word when starting to parse a new data directive, in order to fix this issue. Fixes #4 --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 990b3c7..e439509 100644 --- a/src/main.rs +++ b/src/main.rs @@ -604,6 +604,7 @@ fn parse_label(pair: pest::iterators::Pair, next_pair: Option) -> AstNode { //println!("{:#?}", pair); + *CURRENT_SIZE.lock().unwrap() = Size::Word; match pair.as_rule() { Rule::data_byte => { match parse_operand(pair.into_inner().next().unwrap(), false) { From fc925e577dbd8e724fa2d79f4d8a42a7a16ff53f Mon Sep 17 00:00:00 2001 From: jn Date: Wed, 1 Feb 2023 21:19:11 +0100 Subject: [PATCH 2/2] Assemble data.8 CONST_LABEL correctly as one byte Fixes #5 --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index e439509..4b6b442 100644 --- a/src/main.rs +++ b/src/main.rs @@ -609,21 +609,24 @@ fn parse_data(pair: pest::iterators::Pair) -> AstNode { Rule::data_byte => { match parse_operand(pair.into_inner().next().unwrap(), false) { AstNode::Immediate32(half) => AstNode::DataByte(half as u8), - AstNode::LabelOperand {name, size, is_relative} => AstNode::LabelOperand {name, size, is_relative}, + AstNode::LabelOperand {name, size: _, is_relative} => + AstNode::LabelOperand {name, size: Size::Byte, is_relative}, _ => unreachable!(), } }, Rule::data_half => { match parse_operand(pair.into_inner().next().unwrap(), false) { AstNode::Immediate32(half) => AstNode::DataHalf(half as u16), - AstNode::LabelOperand {name, size, is_relative} => AstNode::LabelOperand {name, size, is_relative}, + AstNode::LabelOperand {name, size: _, is_relative} => + AstNode::LabelOperand {name, size: Size::Half, is_relative}, _ => unreachable!(), } }, Rule::data_word => { match parse_operand(pair.into_inner().next().unwrap(), false) { AstNode::Immediate32(word) => AstNode::DataWord(word), - AstNode::LabelOperand {name, size, is_relative} => AstNode::LabelOperand {name, size, is_relative}, + AstNode::LabelOperand {name, size: _, is_relative} => + AstNode::LabelOperand {name, size: Size::Word, is_relative}, _ => unreachable!(), } },