This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathsetter.rs
317 lines (291 loc) · 9.73 KB
/
setter.rs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Zinc, the bare metal stack for rust.
// Copyright 2014 Ben Gamari <bgamari@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::iter::FromIterator;
use std::ops::Deref;
use syntax::ast;
use syntax::ptr::P;
use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
use super::Builder;
use super::super::node;
use super::utils;
/// A visitor to build the field setters for primitive registers
pub struct BuildSetters<'a, 'b> where 'b : 'a {
builder: &'a mut Builder,
cx: &'a ExtCtxt<'b>,
}
impl<'a, 'b> BuildSetters<'a, 'b> {
pub fn new(builder: &'a mut Builder, cx: &'a ExtCtxt<'b>)
-> BuildSetters<'a, 'b> {
BuildSetters { builder: builder, cx: cx }
}
}
impl<'a, 'c> node::RegVisitor for BuildSetters<'a, 'c> {
fn visit_prim_reg<'b>(&'b mut self, path: &Vec<String>,
reg: &'b node::Reg, fields: &Vec<node::Field>)
{
if fields.iter().any(|f| f.access != node::Access::ReadOnly) {
let it = build_type(self.cx, path, reg, fields);
self.builder.push_item(it);
let it = build_drop(self.cx, path, reg, fields);
self.builder.push_item(it);
let it = build_impl(self.cx, path, reg, fields);
self.builder.push_item(it);
}
}
}
fn build_type(cx: &ExtCtxt, path: &Vec<String>,
reg: &node::Reg, _fields: &Vec<node::Field>) -> P<ast::Item>
{
let packed_ty = utils::reg_primitive_type(cx, reg)
.expect("Unexpected non-primitive register");
let name = utils::setter_name(cx, path);
let reg_ty = cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let reg_doc = match reg.docstring {
Some(d) => d.node.name.to_string(),
None => "no documentation".to_string(),
};
let docstring = format!("Update value of `{}` register: {}",
reg.name.node,
reg_doc);
let doc_attr = utils::doc_attribute(cx, utils::intern_string(cx, docstring));
let item = quote_item!(cx,
$doc_attr
#[allow(non_camel_case_types, dead_code)]
pub struct $name<'a> {
value: $packed_ty,
mask: $packed_ty,
write_only: bool,
reg: &'a $reg_ty,
}
);
let mut item: ast::Item = item.unwrap().deref().clone();
item.span = reg.name.span;
P(item)
}
fn build_new<'a>(cx: &'a ExtCtxt, path: &Vec<String>, reg: &node::Reg)
-> P<ast::ImplItem> {
let reg_ty: P<ast::Ty> =
cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let setter_ident = utils::setter_name(cx, path);
utils::unwrap_impl_item(quote_item!(cx,
impl<'a> $setter_ident<'a> {
#[doc="Create a new updater"]
#[inline(always)]
pub fn new(reg: &'a $reg_ty) -> $setter_ident<'a> {
$setter_ident {
value: 0,
mask: 0,
write_only: false,
reg: reg,
}
}
}
).unwrap())
}
fn build_new_ignoring_state<'a>(cx: &'a ExtCtxt, path: &Vec<String>,
reg: &node::Reg) -> P<ast::ImplItem> {
let reg_ty: P<ast::Ty> =
cx.ty_ident(reg.name.span, utils::path_ident(cx, path));
let setter_ident = utils::setter_name(cx, path);
utils::unwrap_impl_item(quote_item!(cx,
impl<'a> $setter_ident<'a> {
#[doc="Create a new updater that ignores current state"]
#[inline(always)]
pub fn new_ignoring_state(reg: &'a $reg_ty) -> $setter_ident<'a> {
$setter_ident {
value: 0,
mask: 0,
write_only: true,
reg: reg,
}
}
}
).unwrap())
}
fn build_drop(cx: &ExtCtxt, path: &Vec<String>,
reg: &node::Reg, fields: &Vec<node::Field>) -> P<ast::Item>
{
let setter_ident = utils::setter_name(cx, path);
let unpacked_ty = utils::reg_primitive_type(cx, reg)
.expect("Unexpected non-primitive register");
// ensure we don't unintentionally clear a set-to-clear flag
let mut clear: u32 = 0;
for f in fields.iter() {
match f.access {
node::Access::SetToClear => {
let mask = 1 << (f.count.node * f.width) - 1;
clear |= mask;
},
_ => {},
}
}
// no need to read write-only registers
let wo_reg: bool = fields.iter().all(|f| f.access == node::Access::WriteOnly);
let initial_value =
if wo_reg {
quote_expr!(cx, 0)
} else {
quote_expr!(cx, if self.write_only { 0 } else { self.reg.value.get() })
};
let item = quote_item!(cx,
#[doc = "This performs the register update"]
impl<'a> Drop for $setter_ident<'a> {
#[inline(always)]
fn drop(&mut self) {
let clear_mask: $unpacked_ty = $clear as $unpacked_ty;
if self.mask != 0 {
let v: $unpacked_ty = $initial_value & ! clear_mask & ! self.mask;
self.reg.value.set(self.value | v);
}
}
}
);
item.unwrap()
}
fn build_done(ctx: &ExtCtxt, path: &Vec<String>) -> P<ast::ImplItem> {
let setter_ident = utils::setter_name(ctx, path);
utils::unwrap_impl_item(quote_item!(ctx,
impl<'a> $setter_ident<'a> {
#[doc = "Commit changes to register. Allows for chaining of set"]
#[inline(always)]
pub fn done(self) {}
}
).unwrap())
}
fn build_impl(cx: &ExtCtxt, path: &Vec<String>, reg: &node::Reg,
fields: &Vec<node::Field>) -> P<ast::Item>
{
let new = build_new(cx, path, reg);
let new_is = build_new_ignoring_state(cx, path, reg);
let setter_ident = utils::setter_name(cx, path);
let methods: Vec<P<ast::ImplItem>> =
FromIterator::from_iter(
fields.iter()
.filter_map(|field| build_field_fn(cx, path, reg, field)));
let done = build_done(cx, path);
quote_item!(cx,
#[allow(dead_code)]
impl<'a> $setter_ident<'a> {
$new
$new_is
$methods
$done
}
).unwrap()
}
fn build_field_fn(cx: &ExtCtxt, path: &Vec<String>, reg: &node::Reg,
field: &node::Field) -> Option<P<ast::ImplItem>>
{
match field.access {
node::Access::ReadOnly => None,
node::Access::SetToClear => Some(build_field_clear_fn(cx, path, reg, field)),
_ => Some(build_field_set_fn(cx, path, reg, field)),
}
}
/// Build a setter for a field
fn build_field_set_fn(cx: &ExtCtxt, path: &Vec<String>, reg: &node::Reg,
field: &node::Field) -> P<ast::ImplItem>
{
let setter_ty = utils::setter_name(cx, path);
let unpacked_ty = utils::reg_primitive_type(cx, reg)
.expect("Unexpected non-primitive register");
let fn_name =
cx.ident_of((String::from("set_")+field.name.node.as_str()).as_str());
let field_ty: P<ast::Ty> =
cx.ty_path(utils::field_type_path(cx, path, reg, field));
let mask = utils::mask(cx, field);
let field_doc = match field.docstring {
Some(d) => d.node.name.to_string(),
None => "no documentation".to_string(),
};
let docstring = format!("Set value of `{}` field: {}",
field.name.node,
field_doc);
let doc_attr = utils::doc_attribute(cx, utils::intern_string(cx, docstring));
if field.count.node == 1 {
let shift = utils::shift(cx, None, field);
utils::unwrap_impl_item(quote_item!(cx,
impl<'a> $setter_ty<'a> {
$doc_attr
#[inline(always)]
pub fn $fn_name<'b>(&'b mut self,
new_value: $field_ty) -> &'b mut $setter_ty<'a> {
self.value = (self.value & !($mask << $shift)) | ((new_value as $unpacked_ty) & $mask) << $shift;
self.mask |= $mask << $shift;
self
}
}
).unwrap())
} else {
let shift = utils::shift(cx, Some(quote_expr!(cx, idx)), field);
utils::unwrap_impl_item(quote_item!(cx,
impl<'a> $setter_ty<'a> {
$doc_attr
#[inline(always)]
pub fn $fn_name<'b>(&'b mut self,
idx: usize,
new_value: $field_ty) -> &'b mut $setter_ty<'a> {
self.value |= (self.value & ! $mask) | ((new_value as $unpacked_ty) & $mask) << $shift;
self.mask |= $mask << $shift;
self
}
}
).unwrap())
}
}
fn build_field_clear_fn(cx: &ExtCtxt, path: &Vec<String>,
_: &node::Reg, field: &node::Field) -> P<ast::ImplItem>
{
let setter_ty = utils::setter_name(cx, path);
let fn_name =
cx.ident_of((String::from("clear_")+field.name.node.as_str()).as_str());
let mask = utils::mask(cx, field);
let field_doc = match field.docstring {
Some(d) => d.node.name.to_string(),
None => "no documentation".to_string(),
};
let docstring = format!("Clear `{}` flag: {}",
field.name.node,
field_doc);
let doc_attr = utils::doc_attribute(cx, utils::intern_string(cx, docstring));
if field.count.node == 1 {
let shift = utils::shift(cx, None, field);
utils::unwrap_impl_item(quote_item!(cx,
impl<'a> $setter_ty<'a> {
$doc_attr
#[inline(always)]
pub fn $fn_name<'b>(&'b mut self) -> &'b mut $setter_ty<'a> {
self.value |= $mask << $shift;
self.mask |= $mask << $shift;
self
}
}
).unwrap())
} else {
let shift = utils::shift(cx, Some(quote_expr!(cx, idx)), field);
utils::unwrap_impl_item(quote_item!(cx,
impl<'a> $setter_ty<'a> {
$doc_attr
#[inline(always)]
pub fn $fn_name<'b>(&'b mut self, idx: usize) -> &'b mut $setter_ty<'a> {
self.value |= $mask << $shift;
self.mask |= $mask << $shift;
self
}
}
).unwrap())
}
}