Allow specifying width on zero-operand instructions

nop.8 exists and is valid, so fox32asm should assemble it.

It might be a good idea to reject some invalid instructions, though,
such as out.8.
This commit is contained in:
jn 2023-02-01 21:37:03 +01:00
parent 2c932cef4b
commit 0c47ce79af
2 changed files with 23 additions and 13 deletions

View File

@ -80,7 +80,7 @@ instruction = {
} }
instruction_conditional = { instruction_conditional = {
instruction_zero | instruction_zero ~ size? |
instruction_one ~ size? ~ operand | instruction_one ~ size? ~ operand |
instruction_two ~ size? ~ operand ~ "," ~ operand instruction_two ~ size? ~ operand ~ "," ~ operand
} }

View File

@ -244,6 +244,7 @@ enum LabelKind {
#[derive(PartialEq, Debug, Clone)] #[derive(PartialEq, Debug, Clone)]
enum AstNode { enum AstNode {
OperationZero { OperationZero {
size: Size,
condition: Condition, condition: Condition,
instruction: InstructionZero, instruction: InstructionZero,
}, },
@ -729,7 +730,15 @@ fn parse_instruction(pair: pest::iterators::Pair<Rule>) -> AstNode {
let mut inner_pair = pair.into_inner(); let mut inner_pair = pair.into_inner();
let instruction_conditional_pair = inner_pair.next().unwrap(); let instruction_conditional_pair = inner_pair.next().unwrap();
match instruction_conditional_pair.as_rule() { match instruction_conditional_pair.as_rule() {
Rule::instruction_zero => parse_instruction_zero(instruction_conditional_pair, condition), Rule::instruction_zero => {
if let Some(inner) = inner_pair.peek() {
if inner.as_rule() == Rule::size {
size = parse_size(&inner_pair.next().unwrap());
}
}
*CURRENT_SIZE.lock().unwrap() = size;
parse_instruction_zero(instruction_conditional_pair, size, condition)
}
Rule::instruction_one => { Rule::instruction_one => {
if inner_pair.peek().unwrap().as_rule() == Rule::size { if inner_pair.peek().unwrap().as_rule() == Rule::size {
size = parse_size(&inner_pair.next().unwrap()); size = parse_size(&inner_pair.next().unwrap());
@ -859,8 +868,9 @@ fn parse_operand(mut pair: pest::iterators::Pair<Rule>, is_pointer: bool) -> Ast
} }
} }
fn parse_instruction_zero(pair: pest::iterators::Pair<Rule>, condition: Condition) -> AstNode { fn parse_instruction_zero(pair: pest::iterators::Pair<Rule>, size: Size, condition: Condition) -> AstNode {
AstNode::OperationZero { AstNode::OperationZero {
size: size,
condition: condition, condition: condition,
instruction: match pair.as_str() { instruction: match pair.as_str() {
"nop" => InstructionZero::Nop, "nop" => InstructionZero::Nop,
@ -1044,17 +1054,17 @@ fn size_to_byte(size: &Size) -> u8 {
fn instruction_to_byte(node: &AstNode) -> u8 { fn instruction_to_byte(node: &AstNode) -> u8 {
match node { match node {
AstNode::OperationZero {instruction, ..} => { AstNode::OperationZero {size, instruction, ..} => {
match instruction { match instruction {
InstructionZero::Nop => 0x00 | size_to_byte(&Size::Word), InstructionZero::Nop => 0x00 | size_to_byte(size),
InstructionZero::Halt => 0x10 | size_to_byte(&Size::Word), InstructionZero::Halt => 0x10 | size_to_byte(size),
InstructionZero::Brk => 0x20 | size_to_byte(&Size::Word), InstructionZero::Brk => 0x20 | size_to_byte(size),
InstructionZero::Ret => 0x2A | size_to_byte(&Size::Word), InstructionZero::Ret => 0x2A | size_to_byte(size),
InstructionZero::Reti => 0x3A | size_to_byte(&Size::Word), InstructionZero::Reti => 0x3A | size_to_byte(size),
InstructionZero::Ise => 0x0C | size_to_byte(&Size::Word), InstructionZero::Ise => 0x0C | size_to_byte(size),
InstructionZero::Icl => 0x1C | size_to_byte(&Size::Word), InstructionZero::Icl => 0x1C | size_to_byte(size),
InstructionZero::Mse => 0x0D | size_to_byte(&Size::Word), InstructionZero::Mse => 0x0D | size_to_byte(size),
InstructionZero::Mcl => 0x1D | size_to_byte(&Size::Word), InstructionZero::Mcl => 0x1D | size_to_byte(size),
} }
} }
AstNode::OperationOne {size, instruction, ..} => { AstNode::OperationOne {size, instruction, ..} => {