Skip to content

Commit a767da0

Browse files
committed
add labelalign directive
1 parent c85b242 commit a767da0

File tree

8 files changed

+127
-17
lines changed

8 files changed

+127
-17
lines changed

src/asm/bank.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub struct Bank
66
pub name: String,
77

88
pub wordsize: usize,
9+
pub labelalign: usize,
910
pub addr_start: util::BigInt,
1011
pub addr_size: Option<usize>,
1112
pub output_offset: Option<usize>,
@@ -29,6 +30,7 @@ impl Bank
2930
Bank {
3031
name: "".to_string(),
3132
wordsize: 8,
33+
labelalign: 0,
3234
addr_start: util::BigInt::from(0),
3335
addr_size: None,
3436
output_offset: Some(0),

src/asm/parser/addr_related.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
use crate::*;
22

33

4+
pub fn parse_directive_bits(
5+
state: &mut asm::parser::State)
6+
-> Result<(), ()>
7+
{
8+
state.asm_state.cur_wordsize = asm::parser::parse_expr_usize_fn(state, |u| match u
9+
{
10+
0 => None,
11+
_ => Some(u)
12+
})?;
13+
14+
Ok(())
15+
}
16+
17+
418
pub fn parse_directive_res(
519
state: &mut asm::parser::State)
620
-> Result<(), ()>
@@ -37,6 +51,20 @@ pub fn parse_directive_align(
3751
}
3852

3953

54+
pub fn parse_directive_labelalign(
55+
state: &mut asm::parser::State)
56+
-> Result<(), ()>
57+
{
58+
state.asm_state.cur_labelalign = asm::parser::parse_expr_usize_fn(state, |u| match u
59+
{
60+
0 => None,
61+
_ => Some(u),
62+
})?;
63+
64+
Ok(())
65+
}
66+
67+
4068
pub fn parse_directive_addr(
4169
state: &mut asm::parser::State)
4270
-> Result<(), ()>

src/asm/parser/bank.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn parse_directive_bankdef(
2121
{
2222
name: name.clone(),
2323
wordsize: state.asm_state.cur_wordsize,
24+
labelalign: state.asm_state.cur_labelalign,
2425
addr_start: util::BigInt::from(0),
2526
addr_size: None,
2627
output_offset: None,
@@ -88,6 +89,12 @@ fn parse_bankdef_field(
8889
_ => Some(u)
8990
})?,
9091

92+
"labelalign" => bank.labelalign = asm::parser::parse_expr_usize_fn(state, |u| match u
93+
{
94+
0 => None,
95+
_ => Some(u)
96+
})?,
97+
9198
"fill" => bank.fill = true,
9299

93100
_ =>
@@ -115,6 +122,7 @@ pub fn parse_directive_bank(
115122

116123
let bank = &state.asm_state.banks[state.asm_state.cur_bank.index];
117124
state.asm_state.cur_wordsize = bank.wordsize;
125+
state.asm_state.cur_labelalign = bank.labelalign;
118126

119127
Ok(())
120128
}

src/asm/parser/file.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub fn parse_directive(state: &mut asm::parser::State)
107107
"include" => asm::parser::parse_directive_include(state)?,
108108
"res" => asm::parser::parse_directive_res(state)?,
109109
"align" => asm::parser::parse_directive_align(state)?,
110+
"labelalign" => asm::parser::parse_directive_labelalign(state)?,
110111
"addr" => asm::parser::parse_directive_addr(state)?,
111112
//"enable" => asm::parser::parse_directive_enable(state)?,
112113
_ =>
@@ -123,20 +124,6 @@ pub fn parse_directive(state: &mut asm::parser::State)
123124
}
124125

125126

126-
pub fn parse_directive_bits(
127-
state: &mut asm::parser::State)
128-
-> Result<(), ()>
129-
{
130-
state.asm_state.cur_wordsize = asm::parser::parse_expr_usize_fn(state, |u| match u
131-
{
132-
0 => None,
133-
_ => Some(u)
134-
})?;
135-
136-
Ok(())
137-
}
138-
139-
140127
pub fn parse_expr_bigint(state: &mut asm::parser::State) -> Result<(util::BigInt, diagn::Span), ()>
141128
{
142129
let expr = expr::Expr::parse(&mut state.parser)?;

src/asm/parser/symbol.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ pub fn parse_symbol(
1717
let tk_name = state.parser.expect(syntax::TokenKind::Identifier)?;
1818
let name = tk_name.excerpt.clone().unwrap();
1919
span = span.join(&tk_name.span);
20-
21-
let ctx = state.asm_state.get_ctx(state);
20+
21+
let ctx;
2222

2323
let value = if state.parser.maybe_expect(syntax::TokenKind::Equal).is_some()
2424
{
25+
ctx = state.asm_state.get_ctx(state);
2526
let expr = expr::Expr::parse(&mut state.parser)?;
2627
let value = state.asm_state.eval_expr(
2728
state.report.clone(),
@@ -36,10 +37,23 @@ pub fn parse_symbol(
3637
}
3738
else
3839
{
40+
if state.asm_state.cur_labelalign != 0
41+
{
42+
println!("{:?}", state.asm_state.cur_labelalign);
43+
let bankdata = state.asm_state.get_bankdata(state.asm_state.cur_bank);
44+
let skip_bits = bankdata.bits_until_aligned(
45+
state.asm_state,
46+
state.asm_state.cur_labelalign);
47+
48+
let bankdata = state.asm_state.get_bankdata_mut(state.asm_state.cur_bank);
49+
bankdata.reserve(skip_bits);
50+
}
51+
3952
let tk_colon = state.parser.expect(syntax::TokenKind::Colon)?;
4053

4154
span = span.join(&tk_colon.span);
4255

56+
ctx = state.asm_state.get_ctx(state);
4357
let addr = state.asm_state.get_addr(
4458
state.report.clone(),
4559
&ctx,

src/asm/state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct State
1818
pub active_rulesets: Vec<RulesetRef>,
1919
pub cur_bank: BankRef,
2020
pub cur_wordsize: usize,
21+
pub cur_labelalign: usize,
2122
}
2223

2324

@@ -147,7 +148,6 @@ impl Assembler
147148
{
148149
//println!("output {:?}, {:x}", bank.output_offset, &bank_output.as_ref().unwrap());
149150

150-
// FIXME: multiplication by wordsize can overflow
151151
full_output.write_bitvec(
152152
output_offset,
153153
&bank_output.unwrap());
@@ -197,6 +197,7 @@ impl State
197197
active_rulesets: Vec::new(),
198198
cur_bank: BankRef { index: 0 },
199199
cur_wordsize: 8,
200+
cur_labelalign: 0,
200201
};
201202

202203
state.create_bank(asm::Bank::new_default(), diagn::RcReport::new()).unwrap();
@@ -321,6 +322,7 @@ impl State
321322

322323
self.cur_bank = bank_ref;
323324
self.cur_wordsize = bank.wordsize;
325+
self.cur_labelalign = bank.labelalign;
324326

325327
self.banks.push(bank);
326328

tests/bank_labelalign.asm

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; ::: include
2+
3+
#ruledef test
4+
{
5+
ld {x} => 0x55 @ x`8
6+
}
7+
8+
; :::
9+
10+
#labelalign 32
11+
#bankdef a { #addr 0, #outp 0 }
12+
ld $ ; = 0x5500
13+
label: ; = 0x0000
14+
ld $ ; = 0x5504
15+
16+
; :::
17+
18+
#bankdef a { #addr 0, #outp 0, #labelalign 32 }
19+
ld $ ; = 0x5500
20+
label: ; = 0x0000
21+
ld $ ; = 0x5504
22+
23+
; :::
24+
25+
#labelalign 32
26+
#bankdef a { #addr 0, #size 8, #outp 0 }
27+
#labelalign 64
28+
#bankdef b { #addr 0, #outp 8 * 8 }
29+
30+
#bank a
31+
ld $
32+
label1:
33+
ld $
34+
35+
#bank b
36+
ld $
37+
label2:
38+
ld $
39+
40+
; = 0x5500000055040000_55000000000000005508

tests/labelalign.asm

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; ::: include
2+
3+
#ruledef test
4+
{
5+
ld {x} => 0x55 @ x`8
6+
}
7+
8+
; :::
9+
10+
#labelalign 32
11+
ld $ ; = 0x5500
12+
label: ; = 0x0000
13+
ld $ ; = 0x5504
14+
15+
; :::
16+
17+
ld $ ; = 0x5500
18+
label1:
19+
ld $ ; = 0x5502
20+
#labelalign 64
21+
label2: ; = 0x00000000
22+
ld $ ; = 0x5508
23+
24+
; :::
25+
26+
#labelalign 33
27+
ld $
28+
label: ; error: not aligned
29+
ld $

0 commit comments

Comments
 (0)