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
use std;
use std::fmt;
use std::cell::RefCell;
use std::collections::HashMap;
use moore_common::NodeId;
use moore_common::score::{NodeStorage, Result};
use score::{ScoreBoard, ScoreContext};
use hir;
use typeck::TypeckContext;
use score::*;
use ty::Ty;
pub enum LazyNode<F> {
Running,
Pending(F),
}
impl<F> fmt::Debug for LazyNode<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LazyNode::Running => write!(f, "running"),
LazyNode::Pending(_) => write!(f, "pending"),
}
}
}
pub struct LazyPhaseTable<'sb, 'ast: 'sb, 'ctx: 'sb> {
pub sb: &'sb ScoreBoard<'ast, 'ctx>,
pub hir: LazyPhase<LazyHirTable<'sb, 'ast, 'ctx>>,
pub typeck: RefCell<LazyTypeckTable<'sb, 'ast, 'ctx>>,
pub typeval: RefCell<LazyTypevalTable<'sb, 'ast, 'ctx>>,
}
impl <'sb, 'ast, 'ctx> LazyPhaseTable<'sb, 'ast, 'ctx> {
pub fn new(sb: &'sb ScoreBoard<'ast, 'ctx>) -> LazyPhaseTable<'sb, 'ast, 'ctx> {
LazyPhaseTable {
sb: sb,
hir: LazyPhase::new(),
typeck: RefCell::new(HashMap::new()),
typeval: RefCell::new(HashMap::new()),
}
}
}
pub struct LazyPhase<T> {
pub table: RefCell<T>,
}
impl<T: Default> LazyPhase<T> where T: Default {
pub fn new() -> LazyPhase<T> {
LazyPhase {
table: RefCell::new(Default::default()),
}
}
pub fn schedule<I,F>(&self, id: I, f: F)
where T: NodeStorage<I, Node=LazyNode<F>>
{
self.table.borrow_mut().set(id, LazyNode::Pending(f));
}
pub fn run<'lazy, 'sb, 'ast, 'ctx, I, R>(&self, id: I, ctx: &ScoreContext<'lazy, 'sb, 'ast, 'ctx>) -> Result<R>
where
I: Copy + fmt::Debug,
T: NodeStorage<I, Node=LazyNode<Box<for<'a,'b> Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) -> Result<R> + 'sb>>>
{
let task = self.table.borrow_mut().set(id, LazyNode::Running);
match task {
Some(LazyNode::Pending(f)) => f(ctx),
Some(LazyNode::Running) => panic!("recursion when running task for {:?}", id),
None => panic!("no task scheduled for {:?}", id),
}
}
}
pub type LazyHir<'sb, 'ast, 'ctx, R> = Box<for<'a,'b> Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) -> Result<R> + 'sb>;
pub type LazyTypeck<'sb, 'ast, 'ctx> = Box<for<'a,'b,'c> Fn(&'a TypeckContext<'b, 'c, 'sb, 'ast, 'ctx>) -> Result<()> + 'sb>;
pub type LazyTypeval<'sb, 'ast, 'ctx> = Box<for<'a,'b,'c> Fn(&'a TypeckContext<'b, 'c, 'sb, 'ast, 'ctx>) -> Result<&'ctx Ty> + 'sb>;
node_storage!(LazyHirTable<'sb, 'ast, 'ctx> where ('ast: 'sb, 'ctx: 'sb):
subtype_inds: SubtypeIndRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::SubtypeInd>>,
exprs: ExprRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Expr>>,
aggregates: AggregateRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Aggregate>>,
const_decls: ConstDeclRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Decl<hir::ConstDecl>>>,
signal_decls: SignalDeclRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Decl<hir::SignalDecl>>>,
var_decls: VarDeclRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Decl<hir::VarDecl>>>,
file_decls: FileDeclRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Decl<hir::FileDecl>>>,
wait_stmts: WaitStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::WaitStmt>>>,
assert_stmts: AssertStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::AssertStmt>>>,
report_stmts: ReportStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::ReportStmt>>>,
sig_assign_stmts: SigAssignStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::SigAssignStmt>>>,
var_assign_stmts: VarAssignStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::VarAssignStmt>>>,
call_stmt: CallStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::CallStmt>>>,
if_stmt: IfStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::IfStmt>>>,
case_stmt: CaseStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::CaseStmt>>>,
loop_stmt: LoopStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::LoopStmt>>>,
nexit_stmt: NexitStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::NexitStmt>>>,
return_stmt: ReturnStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::ReturnStmt>>>,
null_stmt: NullStmtRef => LazyNode<LazyHir<'sb, 'ast, 'ctx, hir::Stmt<hir::NullStmt>>>,
);
impl<'sb, 'ast, 'ctx> Default for LazyHirTable<'sb, 'ast, 'ctx> {
fn default() -> LazyHirTable<'sb, 'ast, 'ctx> {
LazyHirTable::new()
}
}
pub type LazyTypeckTable<'sb, 'ast, 'ctx> = HashMap<
NodeId, LazyNode<LazyTypeck<'sb, 'ast ,'ctx>>
>;
pub type LazyTypevalTable<'sb, 'ast, 'ctx> = HashMap<
NodeId, LazyNode<LazyTypeval<'sb, 'ast ,'ctx>>
>;