Add data.strz directive for zero-terminated strings

A common pattern in fox32 software is this:

	data.str "Some string here" data.8 0

i.e. a zero- or NUL-terminated string. To make such strings easier to
write, introduce a new directive data.strz, which adds the zero byte
automatically:

	data.strz "Some string here"
This commit is contained in:
jn 2023-02-01 19:56:22 +01:00
parent abd160e6e5
commit eb32be33c8
2 changed files with 12 additions and 0 deletions

View File

@ -19,12 +19,14 @@ data = {
data_half | data_half |
data_word | data_word |
data_str | data_str |
data_strz |
data_fill data_fill
} }
data_byte = { "data.8" ~ operand_value } data_byte = { "data.8" ~ operand_value }
data_half = { "data.16" ~ operand_value } data_half = { "data.16" ~ operand_value }
data_word = { "data.32" ~ operand_value } data_word = { "data.32" ~ operand_value }
data_str = { "data.str" ~ immediate_str } data_str = { "data.str" ~ immediate_str }
data_strz = { "data.strz" ~ immediate_str }
data_fill = { "data.fill" ~ operand_value ~ "," ~ operand_value } data_fill = { "data.fill" ~ operand_value ~ "," ~ operand_value }
constant = { "const" ~ constant_name ~ operand_value } constant = { "const" ~ constant_name ~ operand_value }

View File

@ -291,6 +291,7 @@ enum AstNode {
DataHalf(u16), DataHalf(u16),
DataWord(u32), DataWord(u32),
DataStr(String), DataStr(String),
DataStrZero(String),
DataFill { DataFill {
value: u8, value: u8,
size: u32, size: u32,
@ -630,6 +631,10 @@ fn parse_data(pair: pest::iterators::Pair<Rule>) -> AstNode {
let string = pair.into_inner().next().unwrap().into_inner().next().unwrap().as_str(); let string = pair.into_inner().next().unwrap().into_inner().next().unwrap().as_str();
AstNode::DataStr(string.to_string()) AstNode::DataStr(string.to_string())
}, },
Rule::data_strz => {
let string = pair.into_inner().next().unwrap().into_inner().next().unwrap().as_str();
AstNode::DataStrZero(string.to_string())
},
Rule::data_fill => { Rule::data_fill => {
let value = { let value = {
let ast = parse_operand(pair.clone().into_inner().next().unwrap(), false); let ast = parse_operand(pair.clone().into_inner().next().unwrap(), false);
@ -982,6 +987,11 @@ fn assemble_node(node: AstNode) -> AssembledInstruction {
AstNode::DataStr(string) => { AstNode::DataStr(string) => {
return string.as_bytes().into(); return string.as_bytes().into();
}, },
AstNode::DataStrZero(string) => {
let mut bytes: Vec<u8> = string.as_bytes().into();
bytes.push(0);
return bytes.into();
},
AstNode::LabelOperand {name, size, is_relative} => { AstNode::LabelOperand {name, size, is_relative} => {
// label is used on its own, not as an operand: // label is used on its own, not as an operand:
// LabelOperand was previously only checked as part of operands // LabelOperand was previously only checked as part of operands