1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#![allow(clippy::needless_range_loop)]
#![allow(clippy::too_many_arguments)]
mod lower_fields;
mod lower_structs;
mod lowering;
mod parse;
mod parse_structs;
use std::num::NonZeroUsize;
pub(crate) use lower_fields::*;
pub(crate) use lower_structs::*;
pub use lower_structs::{unstable_native_inlawi, unstable_native_inlawi_ty};
pub(crate) use lowering::*;
pub(crate) use parse::*;
pub(crate) use parse_structs::*;
use ComponentType::*;
pub(crate) const CONSTANT: &str = "__awint_constant";
pub(crate) const VALUE: &str = "__awint_val";
pub(crate) const WIDTH: &str = "__awint_width";
pub(crate) const BW: &str = "__awint_bw";
pub(crate) const REF: &str = "__awint_ref";
pub(crate) const AWI: &str = "__awint_awi";
pub(crate) const AWI_REF: &str = "__awint_awi_ref";
pub(crate) const SHL: &str = "__awint_shl";
pub fn code_gen(
input: &str,
specified_initialization: bool,
construct_fn: &str,
inlawi: bool,
return_source: bool,
) -> Result<String, String> {
let concats = match parse_concats(input) {
Ok(v) => v,
Err(e) => return Err(e),
};
let mut dynamic_width_i: Option<usize> = None;
let mut static_width_i: Option<(usize, NonZeroUsize)> = None;
let mut unbounded_alignment = (false, false, false);
for (j0, concat) in concats.iter().enumerate() {
let mut deterministic_width = true;
for (j1, component) in concat.concatenation.iter().enumerate() {
if let Filler = component.component_type {
if (j0 == 0) && !specified_initialization && return_source {
return Err(
"a construction macro with unspecified initialization cannot have a \
filler in the source concatenation"
.to_owned(),
)
}
if component.range.end.is_none() {
if concat.concatenation.len() == 1 {
if j0 != 0 {
return Err("there is a sink concatenation that consists of only an \
unbounded filler"
.to_owned())
}
} else {
if j1 == 0 {
unbounded_alignment.0 = true;
}
if j1 == (concat.concatenation.len() - 1) {
unbounded_alignment.2 = true;
}
if (j1 != 0) && (j1 != (concat.concatenation.len() - 1)) {
unbounded_alignment.1 = true;
}
}
deterministic_width = false;
}
}
}
if let Some((j0_orig, width0)) = static_width_i {
if let Some(width1) = concat.total_bw {
if width0 != width1 {
return Err(format!(
"determined statically that concatenations {} and {} have unequal \
bitwidths {} and {}",
j0_orig, j0, width0, width1
))
}
}
} else if let Some(width1) = concat.total_bw {
if dynamic_width_i.is_none() {
dynamic_width_i = Some(j0);
}
static_width_i = Some((j0, width1));
} else if deterministic_width && dynamic_width_i.is_none() {
dynamic_width_i = Some(j0);
}
}
if inlawi && static_width_i.is_none() {
return Err(
"`InlAwi` construction macros need at least one concatenation to have a width that \
can be determined statically by the macro"
.to_owned(),
)
}
let undetermined = dynamic_width_i.is_none() && static_width_i.is_none();
if undetermined && (concats.len() == 1) {
return Err(
"there is a only a source concatenation that has no statically or dynamically \
determinable width"
.to_owned(),
)
}
if undetermined && unbounded_alignment.1 {
return Err(
"there is an unbounded filler in the middle of a concatenation, and no concatenation \
has a statically or dynamically determinable width"
.to_owned(),
)
}
if undetermined && (unbounded_alignment.0 && unbounded_alignment.2) {
return Err(
"there are two concatenations with unbounded fillers aligned opposite each other, and \
no concatenation has a statically or dynamically determinable width"
.to_owned(),
)
}
let total_bw = if let Some((_, bw)) = static_width_i {
Some(bw)
} else {
None
};
if return_source && (concats.len() == 1) && (concats[0].concatenation.len() == 1) {
let comp = &concats[0].concatenation[0];
if let Literal(ref lit) = comp.component_type {
if comp.range.static_width().is_some() {
if inlawi {
return Ok(unstable_native_inlawi(lit))
} else {
return Ok(format!(
"ExtAwi::from_bits({}.const_as_ref())",
unstable_native_inlawi(lit)
))
}
}
}
}
Ok(lower(
&concats,
dynamic_width_i,
total_bw,
specified_initialization,
construct_fn,
inlawi,
return_source,
))
}