Skip to content

Commit e48fe8d

Browse files
committed
add the #const directive and the noemit attribute
1 parent 9ed6549 commit e48fe8d

File tree

15 files changed

+189
-37
lines changed

15 files changed

+189
-37
lines changed

src/asm/defs/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub fn define(
5252

5353
let symbol = asm::Symbol {
5454
item_ref,
55+
no_emit: true,
5556
value_statically_known: true,
5657
value: expr::Value::Function(fn_ref.0),
5758
resolved: true,

src/asm/defs/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::*;
55
pub struct Symbol
66
{
77
pub item_ref: util::ItemRef<Self>,
8+
pub no_emit: bool,
89
pub value_statically_known: bool,
910
pub value: expr::Value,
1011
pub resolved: bool,
@@ -42,6 +43,7 @@ pub fn define(
4243

4344
let symbol = Symbol {
4445
item_ref,
46+
no_emit: node.no_emit,
4547
value_statically_known,
4648
value: expr::Value::Unknown,
4749
resolved: false,

src/asm/parser/directive.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ pub fn parse(
5151
"bits" => Ok(asm::AstAny::DirectiveBits(
5252
asm::parser::directive_bits::parse(report, walker, header_span)?)),
5353

54+
"const" => Ok(asm::AstAny::Symbol(
55+
asm::parser::directive_const::parse(report, walker, header_span)?)),
56+
5457
"fn" => Ok(asm::AstAny::DirectiveFn(
5558
asm::parser::directive_fn::parse(report, walker, header_span)?)),
5659

src/asm/parser/directive_const.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use super::*;
2+
3+
4+
pub fn parse(
5+
report: &mut diagn::Report,
6+
walker: &mut syntax::TokenWalker,
7+
_header_span: diagn::Span)
8+
-> Result<AstSymbol, ()>
9+
{
10+
let mut no_emit = false;
11+
12+
if let Some(_) = walker.maybe_expect(syntax::TokenKind::ParenOpen)
13+
{
14+
let tk_attrb = walker.expect(report, syntax::TokenKind::Identifier)?;
15+
let attrb = tk_attrb.excerpt.as_ref().unwrap();
16+
17+
match attrb.as_ref()
18+
{
19+
"noemit" => no_emit = true,
20+
_ =>
21+
{
22+
report.error_span(
23+
format!("invalid attribute `{}`", attrb),
24+
tk_attrb.span);
25+
26+
return Err(());
27+
}
28+
}
29+
30+
walker.expect(report, syntax::TokenKind::ParenClose)?;
31+
}
32+
33+
34+
let mut decl_span = diagn::Span::new_dummy();
35+
let mut hierarchy_level = 0;
36+
37+
while let Some(tk_dot) = walker.maybe_expect(syntax::TokenKind::Dot)
38+
{
39+
hierarchy_level += 1;
40+
decl_span = decl_span.join(tk_dot.span);
41+
}
42+
43+
let tk_name = walker.expect(report, syntax::TokenKind::Identifier)?;
44+
let name = tk_name.excerpt.clone().unwrap();
45+
decl_span = decl_span.join(tk_name.span);
46+
47+
48+
walker.expect(report, syntax::TokenKind::Equal)?;
49+
50+
let expr = expr::parse(report, walker)?;
51+
walker.expect_linebreak(report)?;
52+
53+
Ok(AstSymbol {
54+
decl_span,
55+
hierarchy_level,
56+
name,
57+
kind: AstSymbolKind::Constant(AstSymbolConstant {
58+
expr,
59+
}),
60+
no_emit,
61+
62+
item_ref: None,
63+
})
64+
}

src/asm/parser/directive_noemit.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,13 @@ pub struct AstDirectiveNoEmit
1111

1212
pub fn parse(
1313
report: &mut diagn::Report,
14-
walker: &mut syntax::TokenWalker,
14+
_walker: &mut syntax::TokenWalker,
1515
header_span: diagn::Span)
1616
-> Result<AstDirectiveNoEmit, ()>
1717
{
18-
let tk_status = walker.expect(report, syntax::TokenKind::Identifier)?;
19-
let status = tk_status.excerpt.as_ref().unwrap().to_ascii_lowercase();
20-
21-
walker.expect_linebreak(report)?;
22-
23-
let status = match status.as_ref()
24-
{
25-
"on" => true,
26-
"off" => false,
27-
_ =>
28-
{
29-
report.error_span(
30-
"unknown noemit state",
31-
tk_status.span,
32-
);
33-
34-
return Err(());
35-
}
36-
};
37-
38-
Ok(AstDirectiveNoEmit {
39-
header_span,
40-
status,
41-
})
18+
report.error_span(
19+
"`#noemit` is deprecated; use `#const(noemit)` at each constant declaration",
20+
header_span);
21+
22+
Err(())
4223
}

src/asm/parser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub use directive_bits::{
2828
AstDirectiveBits,
2929
};
3030

31+
mod directive_const;
32+
3133
mod directive_data;
3234
pub use directive_data::{
3335
AstDirectiveData,

src/asm/parser/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct AstSymbol
88
pub hierarchy_level: usize,
99
pub name: String,
1010
pub kind: AstSymbolKind,
11+
pub no_emit: bool,
1112

1213
pub item_ref: Option<util::ItemRef::<asm::Symbol>>,
1314
}
@@ -59,6 +60,7 @@ pub fn parse(
5960
kind: AstSymbolKind::Constant(AstSymbolConstant {
6061
expr,
6162
}),
63+
no_emit: false,
6264

6365
item_ref: None,
6466
}))
@@ -73,6 +75,7 @@ pub fn parse(
7375
hierarchy_level,
7476
name,
7577
kind: AstSymbolKind::Label,
78+
no_emit: false,
7679

7780
item_ref: None,
7881
}))

src/util/symbol_format.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,31 @@ impl util::SymbolManager<asm::Symbol>
126126
hierarchy.push(child_name.clone());
127127

128128

129-
// TODO: Respect the #noemit directive
130129
let symbol_decl = self.get(*child_ref);
131130
let symbol = defs.symbols.get(symbol_decl.item_ref);
132131

133-
match symbol.value
132+
if !symbol.no_emit
134133
{
135-
expr::Value::Integer(ref bigint) =>
134+
match symbol.value
136135
{
137-
let mut name = String::new();
138-
139-
for i in 0..hierarchy.len()
136+
expr::Value::Integer(ref bigint) =>
140137
{
141-
if i > 0
138+
let mut name = String::new();
139+
140+
for i in 0..hierarchy.len()
142141
{
143-
name.push_str(".");
142+
if i > 0
143+
{
144+
name.push_str(".");
145+
}
146+
147+
name.push_str(&format!("{}", hierarchy[i]));
144148
}
145149

146-
name.push_str(&format!("{}", hierarchy[i]));
150+
formatter(result, symbol_decl, &name, &bigint);
147151
}
148-
149-
formatter(result, symbol_decl, &name, &bigint);
152+
_ => {}
150153
}
151-
_ => {}
152154
}
153155

154156

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ruledef test
2+
{
3+
ld {x: u8} => 0x55 @ x
4+
}
5+
6+
#fn add1(x) => x + 1
7+
8+
start:
9+
x1 = add1(0x10)
10+
ld x1
11+
12+
#const x2 = 0x11
13+
ld x2
14+
15+
#const(noemit) x3 = 0x11
16+
ld x3
17+
18+
loop:
19+
#const y1 = 0x22
20+
ld y1
21+
22+
#const y2 = 0x22
23+
ld y2
24+
25+
#const(noemit) y3 = 0x22
26+
ld y3
27+
28+
.inner:
29+
.z1 = 0x33
30+
ld .z1
31+
32+
#const .z2 = 0x33
33+
ld .z2
34+
35+
#const(noemit) .z3 = 0x33
36+
ld .z3
37+
38+
; command: main.asm -f symbols -o out.txt
39+
; output: out.txt
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
start = 0x0
2+
x1 = 0x11
3+
x2 = 0x11
4+
loop = 0x6
5+
y1 = 0x22
6+
y2 = 0x22
7+
y3.inner = 0xc
8+
y3.z1 = 0x33
9+
y3.z2 = 0x33

0 commit comments

Comments
 (0)