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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
use quote::{Ident, Tokens};
use syn::{Lit, StrStyle};
use analyze::Ownerships;
use check::{App, Kind};
fn krate() -> Ident {
Ident::from("rtfm")
}
pub fn app(app: &App, ownerships: &Ownerships) -> Tokens {
let mut root = vec![];
let mut main = vec![];
::trans::init(app, &mut main, &mut root);
::trans::idle(app, ownerships, &mut main, &mut root);
::trans::resources(app, ownerships, &mut root);
::trans::tasks(app, ownerships, &mut root);
root.push(quote! {
#[allow(unsafe_code)]
fn main() {
#(#main)*
}
});
quote!(#(#root)*)
}
fn idle(app: &App, ownerships: &Ownerships, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
let krate = krate();
let mut mod_items = vec![];
let mut tys = vec![];
let mut exprs = vec![];
if !app.idle.resources.is_empty() {
tys.push(quote!(&mut #krate::Threshold));
exprs.push(quote!(unsafe { &mut #krate::Threshold::new(0) }));
}
if !app.idle.resources.is_empty() {
let mut needs_reexport = false;
for name in &app.idle.resources {
if ownerships[name].is_owned() {
if app.resources.get(name).is_some() {
needs_reexport = true;
break;
}
}
}
let super_ = if needs_reexport {
None
} else {
Some(Ident::new("super"))
};
let mut rexprs = vec![];
let mut rfields = vec![];
for name in &app.idle.resources {
if ownerships[name].is_owned() {
let resource = app.resources.get(name).expect(&format!(
"BUG: resource {} assigned to `idle` has no definition",
name
));
let ty = &resource.ty;
rfields.push(quote! {
pub #name: &'static mut #ty,
});
let _name = Ident::new(format!("_{}", name.as_ref()));
rexprs.push(if resource.expr.is_some() {
quote! {
#name: &mut #super_::#_name,
}
} else {
quote! {
#name: #super_::#_name.as_mut(),
}
});
} else {
rfields.push(quote! {
pub #name: ::idle::#name,
});
rexprs.push(quote! {
#name: ::idle::#name { _0: core::marker::PhantomData },
});
}
}
if needs_reexport {
root.push(quote! {
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub struct _idleResources {
#(#rfields)*
}
});
mod_items.push(quote! {
pub use ::_idleResources as Resources;
});
} else {
mod_items.push(quote! {
#[allow(non_snake_case)]
pub struct Resources {
#(#rfields)*
}
});
}
mod_items.push(quote! {
#[allow(unsafe_code)]
impl Resources {
pub unsafe fn new() -> Self {
Resources {
#(#rexprs)*
}
}
}
});
tys.push(quote!(idle::Resources));
exprs.push(quote!(unsafe { idle::Resources::new() }));
}
let device = &app.device;
for name in &app.idle.resources {
let ceiling = ownerships[name].ceiling();
// owned resource
if ceiling == 0 {
continue
}
let _name = Ident::new(format!("_{}", name.as_ref()));
let resource = app.resources
.get(name)
.expect(&format!("BUG: resource {} has no definition", name));
let ty = &resource.ty;
let _static = if resource.expr.is_some() {
quote!(#_name)
} else {
quote!(#_name.some)
};
mod_items.push(quote! {
#[allow(non_camel_case_types)]
pub struct #name { _0: core::marker::PhantomData<*const ()> }
});
root.push(quote! {
#[allow(unsafe_code)]
unsafe impl #krate::Resource for idle::#name {
type Data = #ty;
fn borrow<'cs>(&'cs self, t: &'cs Threshold) -> &'cs Self::Data {
assert!(t.value() >= #ceiling);
unsafe { &#_static }
}
fn borrow_mut<'cs>(
&'cs mut self,
t: &'cs Threshold,
) -> &'cs mut Self::Data {
assert!(t.value() >= #ceiling);
unsafe { &mut #_static }
}
fn claim<R, F>(&self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&Self::Data, &mut Threshold) -> R
{
unsafe {
#krate::claim(
&#_static,
#ceiling,
#device::NVIC_PRIO_BITS,
t,
f,
)
}
}
fn claim_mut<R, F>(&mut self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&mut Self::Data, &mut Threshold) -> R
{
unsafe {
#krate::claim(
&mut #_static,
#ceiling,
#device::NVIC_PRIO_BITS,
t,
f,
)
}
}
}
});
}
if !mod_items.is_empty() {
root.push(quote! {
#[allow(unsafe_code)]
mod idle {
#(#mod_items)*
}
});
}
let idle = &app.idle.path;
main.push(quote! {
// type check
let idle: fn(#(#tys),*) -> ! = #idle;
idle(#(#exprs),*);
});
}
fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
let device = &app.device;
let krate = krate();
let mut tys = vec![quote!(init::Peripherals)];
let mut exprs = vec![
quote!{
init::Peripherals {
core: ::#device::CorePeripherals::steal(),
device: ::#device::Peripherals::steal(),
}
},
];
let mut ret = None;
let mut mod_items = vec![];
let (init_resources, late_resources): (Vec<_>, Vec<_>) = app.resources
.iter()
.partition(|&(_, res)| res.expr.is_some());
if !init_resources.is_empty() {
let mut fields = vec![];
let mut lifetime = None;
let mut rexprs = vec![];
for (name, resource) in init_resources {
let ty = &resource.ty;
if app.init.resources.contains(name) {
fields.push(quote! {
pub #name: &'static mut #ty,
});
let expr = &resource.expr;
rexprs.push(quote!(#name: {
static mut #name: #ty = #expr;
&mut #name
},));
} else {
let _name = Ident::new(format!("_{}", name.as_ref()));
lifetime = Some(quote!('a));
fields.push(quote! {
pub #name: &'a mut #ty,
});
rexprs.push(quote! {
#name: &mut ::#_name,
});
}
}
root.push(quote! {
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub struct _initResources<#lifetime> {
#(#fields)*
}
});
mod_items.push(quote! {
pub use ::_initResources as Resources;
#[allow(unsafe_code)]
impl<#lifetime> Resources<#lifetime> {
pub unsafe fn new() -> Self {
Resources {
#(#rexprs)*
}
}
}
});
tys.push(quote!(init::Resources));
exprs.push(quote!(init::Resources::new()));
}
// Initialization statements for late resources
let mut late_resource_init = vec![];
if !late_resources.is_empty() {
// `init` must initialize and return resources
let mut fields = vec![];
for (name, resource) in late_resources {
let _name = Ident::new(format!("_{}", name.as_ref()));
let ty = &resource.ty;
fields.push(quote! {
pub #name: #ty,
});
late_resource_init.push(quote! {
#_name = #krate::UntaggedOption { some: _late_resources.#name };
});
}
root.push(quote! {
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub struct _initLateResources {
#(#fields)*
}
});
mod_items.push(quote! {
pub use ::_initLateResources as LateResources;
});
// `init` must return the initialized resources
ret = Some(quote!( -> ::init::LateResources));
}
root.push(quote! {
#[allow(unsafe_code)]
mod init {
pub struct Peripherals {
pub core: ::#device::CorePeripherals,
pub device: ::#device::Peripherals,
}
#(#mod_items)*
}
});
let mut exceptions = vec![];
let mut interrupts = vec![];
for (name, task) in &app.tasks {
match task.kind {
Kind::Exception(ref e) => {
if exceptions.is_empty() {
exceptions.push(quote! {
let scb = &*#device::SCB::ptr();
});
}
let nr = e.nr();
let priority = task.priority;
exceptions.push(quote! {
let prio_bits = #device::NVIC_PRIO_BITS;
let hw = ((1 << prio_bits) - #priority) << (8 - prio_bits);
scb.shpr[#nr - 4].write(hw);
});
}
Kind::Interrupt { enabled } => {
// Interrupt. These are enabled / disabled through the NVIC
if interrupts.is_empty() {
interrupts.push(quote! {
let mut nvic: #device::NVIC = core::mem::transmute(());
});
}
let priority = task.priority;
interrupts.push(quote! {
let prio_bits = #device::NVIC_PRIO_BITS;
let hw = ((1 << prio_bits) - #priority) << (8 - prio_bits);
nvic.set_priority(#device::Interrupt::#name, hw);
});
if enabled {
interrupts.push(quote! {
nvic.enable(#device::Interrupt::#name);
});
} else {
interrupts.push(quote! {
nvic.disable(#device::Interrupt::#name);
});
}
}
}
}
let init = &app.init.path;
main.push(quote! {
// type check
let init: fn(#(#tys,)*) #ret = #init;
#krate::atomic(unsafe { &mut #krate::Threshold::new(0) }, |_t| unsafe {
let _late_resources = init(#(#exprs,)*);
#(#late_resource_init)*
#(#exceptions)*
#(#interrupts)*
});
});
}
fn resources(app: &App, ownerships: &Ownerships, root: &mut Vec<Tokens>) {
let krate = krate();
for name in ownerships.keys() {
let _name = Ident::new(format!("_{}", name.as_ref()));
// Declare the static that holds the resource
let resource = app.resources
.get(name)
.expect(&format!("BUG: resource {} has no definition", name));
let expr = &resource.expr;
let ty = &resource.ty;
root.push(match *expr {
Some(ref expr) => quote! {
static mut #_name: #ty = #expr;
},
None => quote! {
// Resource initialized in `init`
static mut #_name: #krate::UntaggedOption<#ty> =
#krate::UntaggedOption { none: () };
},
});
}
}
fn tasks(app: &App, ownerships: &Ownerships, root: &mut Vec<Tokens>) {
let device = &app.device;
let krate = krate();
for (tname, task) in &app.tasks {
let mut exprs = vec![];
let mut fields = vec![];
let mut items = vec![];
let has_resources = !task.resources.is_empty();
if has_resources {
for rname in &task.resources {
let ceiling = ownerships[rname].ceiling();
let _rname = Ident::new(format!("_{}", rname.as_ref()));
let resource = app.resources
.get(rname)
.expect(&format!("BUG: resource {} has no definition", rname));
let ty = &resource.ty;
let _static = if resource.expr.is_some() {
quote!(#_rname)
} else {
quote!(#_rname.some)
};
items.push(quote! {
#[allow(non_camel_case_types)]
pub struct #rname { _0: PhantomData<*const ()> }
});
root.push(quote! {
#[allow(unsafe_code)]
unsafe impl #krate::Resource for #tname::#rname {
type Data = #ty;
fn borrow<'cs>(&'cs self, t: &'cs Threshold) -> &'cs Self::Data {
assert!(t.value() >= #ceiling);
unsafe { &#_static }
}
fn borrow_mut<'cs>(
&'cs mut self,
t: &'cs Threshold,
) -> &'cs mut Self::Data {
assert!(t.value() >= #ceiling);
unsafe { &mut #_static }
}
fn claim<R, F>(&self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&Self::Data, &mut Threshold) -> R
{
unsafe {
#krate::claim(
&#_static,
#ceiling,
#device::NVIC_PRIO_BITS,
t,
f,
)
}
}
fn claim_mut<R, F>(&mut self, t: &mut Threshold, f: F) -> R
where
F: FnOnce(&mut Self::Data, &mut Threshold) -> R
{
unsafe {
#krate::claim(
&mut #_static,
#ceiling,
#device::NVIC_PRIO_BITS,
t,
f,
)
}
}
}
});
if ceiling <= task.priority {
root.push(quote! {
#[allow(unsafe_code)]
impl core::ops::Deref for #tname::#rname {
type Target = #ty;
fn deref(&self) -> &Self::Target {
unsafe { &#_static }
}
}
#[allow(unsafe_code)]
impl core::ops::DerefMut for #tname::#rname {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut #_static }
}
}
})
}
fields.push(quote! {
pub #rname: #rname,
});
exprs.push(quote! {
#rname: #rname { _0: PhantomData },
});
}
items.push(quote! {
#[allow(non_snake_case)]
pub struct Resources {
#(#fields)*
}
});
items.push(quote! {
#[allow(unsafe_code)]
impl Resources {
pub unsafe fn new() -> Self {
Resources {
#(#exprs)*
}
}
}
});
}
let mut tys = vec![];
let mut exprs = vec![];
let priority = task.priority;
if has_resources {
tys.push(quote!(&mut #krate::Threshold));
exprs.push(quote! {
&mut if #priority == 1 << #device::NVIC_PRIO_BITS {
#krate::Threshold::new(::core::u8::MAX)
} else {
#krate::Threshold::new(#priority)
}
});
}
if has_resources {
tys.push(quote!(#tname::Resources));
exprs.push(quote!(#tname::Resources::new()));
}
let path = &task.path;
let _tname = Ident::new(format!("_{}", tname));
let export_name = Lit::Str(tname.as_ref().to_owned(), StrStyle::Cooked);
root.push(quote! {
#[allow(non_snake_case)]
#[allow(unsafe_code)]
#[export_name = #export_name]
pub unsafe extern "C" fn #_tname() {
let f: fn(#(#tys,)*) = #path;
f(#(#exprs,)*)
}
});
root.push(quote!{
#[allow(non_snake_case)]
#[allow(unsafe_code)]
mod #tname {
use core::marker::PhantomData;
#[allow(dead_code)]
#[deny(const_err)]
const CHECK_PRIORITY: (u8, u8) = (
#priority - 1,
(1 << ::#device::NVIC_PRIO_BITS) - #priority,
);
#(#items)*
}
});
}
}
|