-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaton.rs
478 lines (469 loc) · 15.3 KB
/
automaton.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
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
use std::rc::Rc;
use ahash::{AHashMap, AHashSet};
use crate::ast::{graph::*, graph_automaton::*};
/*
Transforms the automata in a single dataflow graph, using the following process :
-compute all reset condition so regs can be reset appropriatly.
-Replace every node state var "n" with "last n" (as it is the expected behaviour) (this can be done lazily)
-make a table of which node produces which output usign which ExprNode.
for each node:
-make the mux expression of each Input, using previously computed states
-compute the expressions of its outputs
-Replace them all, in scheduling order.
-Last has dissapeared as it is redundant.
-Everything is wrapped in RCell to stay mutable, for optimisations later on
Once this is done, computes the state variables. They should all be simple shared vars,
and so it should be simple using the previous map.
Then replace all temp values with actual links in the graph, and everything is good
*/
pub fn flatten_automata(prog: &ProgramGraph) -> FlatProgramGraph {
let n_input = prog.inputs.len();
let n_node = n_input + prog.states.len();
let mut shared_map = AHashMap::new();
let mut nodes_mem = vec![AHashMap::new(); prog.states.len()];
let reset_conditions = compute_reset_conditions(
&prog,
&mut shared_map,
&prog.shared,
&mut nodes_mem,
n_input,
);
let init_node = &RCell::new(Node::Reg(1, RCell::new(Node::Const(vec![true]))));
//Link all the inputs and outputs of shared vars.
for state_id in 0..prog.states.len() {
compute_state(
&prog.states[state_id],
&mut shared_map,
&prog.shared,
&mut nodes_mem[state_id],
&reset_conditions,
n_input,
state_id,
init_node,
);
}
//compute all the transitions. (which are node shared variables) order doesn't matter.
compute_transitions(
&prog,
&mut shared_map,
&prog.shared,
&mut nodes_mem,
&reset_conditions,
n_input,
);
//when init value are needed, adds them.
//this is done using "reg 1" to know whether this is the first cycle or not.
add_init_values(&mut shared_map, &prog.shared, n_node, n_input, init_node);
remove_tmp_value(&mut shared_map, &prog.shared);
FlatProgramGraph {
outputs: prog
.outputs
.iter()
.map(|(s, i)| {
(
s.to_string(),
shared_map
.remove(i)
.unwrap_or(RCell::new(Node::Const(prog.shared[*i].clone()))),
)
})
.collect(),
inputs: prog.inputs.clone(),
}
}
fn compute_state(
state: &ProgramState,
shared_map: &mut AHashMap<usize, RCell<Node>>,
shared_sizes: &Vec<Vec<bool>>,
state_mem: &mut AHashMap<Rc<ExprNode>, RCell<Node>>,
reset_conditions: &Vec<Option<Node>>,
n_input: usize,
state_id: usize,
init_node: &RCell<Node>,
) {
for (id, expr_node) in &state.shared_outputs {
let node = compute_node(
expr_node.clone(),
shared_map,
shared_sizes,
state_mem,
reset_conditions,
n_input,
state_id,
);
let new_node = if let Some(prev_node) = shared_map.remove(id) {
Node::Mux(
RCell::new(Node::TmpValueHolder(n_input + state_id)),
node,
prev_node,
)
} else {
//If the shared var is not compyted anywhere, take the previous value, or the init value
//if there is no previous value.
let loop_reg = RCell::new(Node::Reg(
shared_sizes[*id].len(),
RCell::new(Node::TmpValueHolder(*id)),
));
//Add the mux only if the init value is not all zeros
let init_value = if shared_sizes[*id].iter().any(|b| *b) {
RCell::new(Node::Mux(
init_node.clone(),
loop_reg,
RCell::new(Node::Const(shared_sizes[*id].clone())),
))
} else {
loop_reg
};
Node::Mux(
RCell::new(Node::TmpValueHolder(n_input + state_id)),
node,
init_value,
)
};
shared_map.insert(*id, RCell::new(new_node));
}
}
fn compute_reset_conditions(
prog: &ProgramGraph,
shared_map: &mut AHashMap<usize, RCell<Node>>,
shared_sizes: &Vec<Vec<bool>>,
state_mem: &mut Vec<AHashMap<Rc<ExprNode>, RCell<Node>>>,
n_input: usize,
) -> Vec<Option<Node>> {
let mut reset_conditions = vec![None; prog.states.len()];
for (pred_id, node) in prog.states.iter().enumerate() {
for (next_id, expr_node, b) in &node.transition_outputs {
if next_id.is_some() && *b {
let next_id = next_id.unwrap();
let new_node = compute_node(
expr_node.clone(),
shared_map,
shared_sizes,
&mut state_mem[pred_id],
&vec![None; prog.states.len()],
n_input,
pred_id,
);
let condition = Node::BiOp(
BiOp::And,
RCell::new(Node::TmpValueHolder(pred_id + prog.inputs.len())),
new_node,
);
let prev_condition = std::mem::take(&mut reset_conditions[next_id]);
reset_conditions[next_id] = if prev_condition.is_none() {
Some(condition)
} else {
Some(Node::BiOp(
BiOp::Or,
RCell::new(condition),
RCell::new(prev_condition.unwrap()),
))
};
}
}
}
reset_conditions
}
fn compute_transitions(
prog: &ProgramGraph,
shared_map: &mut AHashMap<usize, RCell<Node>>,
shared_sizes: &Vec<Vec<bool>>,
state_mem: &mut Vec<AHashMap<Rc<ExprNode>, RCell<Node>>>,
reset_conditions: &Vec<Option<Node>>,
n_input: usize,
) {
for (pred_id, node) in prog.states.iter().enumerate() {
for (next_id, expr_node, _) in &node.transition_outputs {
if let Some(next_id) = next_id {
let new_node = compute_node(
expr_node.clone(),
shared_map,
shared_sizes,
&mut state_mem[pred_id],
reset_conditions,
n_input,
pred_id,
);
let condition = RCell::new(Node::BiOp(
BiOp::And,
RCell::new(Node::TmpValueHolder(pred_id + n_input)),
new_node,
));
let prev_condition = shared_map.remove(&(*next_id + n_input));
shared_map.insert(
*next_id + n_input,
if prev_condition.is_none() {
condition
} else {
RCell::new(Node::BiOp(BiOp::Or, condition, prev_condition.unwrap()))
},
);
}
}
}
}
fn compute_node(
expr_node: Rc<ExprNode>,
shared_map: &mut AHashMap<usize, RCell<Node>>,
shared_size: &Vec<Vec<bool>>,
state_mem: &mut AHashMap<Rc<ExprNode>, RCell<Node>>,
reset_conditions: &Vec<Option<Node>>,
n_input: usize,
state_id: usize,
) -> RCell<Node> {
if let Some(n) = state_mem.get(&expr_node) {
return n.clone();
}
let ret = match expr_node.op.clone() {
ExprOperation::Input(i) => {
if i < n_input {
RCell::new(Node::Input(i))
} else {
RCell::new(Node::TmpValueHolder(i))
}
}
ExprOperation::Const(c) => RCell::new(Node::Const(c)),
ExprOperation::Not(e) => RCell::new(Node::Not(compute_node(
e,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
))),
ExprOperation::Slice(e, c1, c2) => RCell::new(Node::Slice(
compute_node(
e,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
c1,
c2,
)),
ExprOperation::BiOp(op, e1, e2) => RCell::new(Node::BiOp(
op,
compute_node(
e1,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
compute_node(
e2,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
)),
ExprOperation::Mux(e1, e2, e3) => RCell::new(Node::Mux(
compute_node(
e1,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
compute_node(
e2,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
compute_node(
e3,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
)),
ExprOperation::Reg(s, e) => {
let new_expr = if let Some(e) = e {
compute_node(
e,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
)
} else {
//FIXME: this is due to the other fixme in make_automaton_graph, and currently is not handled
todo!()
};
//make the reg loop instead of computing its value when not in the right node
let tmp_value = RCell::new(Node::TmpValueHolder(0));
let node = RCell::new(Node::Reg(
s,
RCell::new(Node::Mux(
RCell::new(Node::TmpValueHolder(state_id + n_input)),
new_expr,
RCell::new(Node::Reg(s, tmp_value.clone())),
)),
));
*tmp_value.borrow_mut() = node.borrow().clone();
node
}
ExprOperation::Ram(e1, e2, e3, e4) => RCell::new(Node::Ram(
compute_node(
e1,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
compute_node(
e2,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
compute_node(
e3,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
compute_node(
e4,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
)),
ExprOperation::Rom(s, e) => RCell::new(Node::Rom(
s,
compute_node(
e,
shared_map,
shared_size,
state_mem,
reset_conditions,
n_input,
state_id,
),
)),
ExprOperation::Last(i) => {
let inside_node = if let Some(n) = &reset_conditions[state_id] {
Node::Mux(
RCell::new(n.clone()),
RCell::new(Node::Const(shared_size[i].clone())),
RCell::new(Node::TmpValueHolder(i)),
)
} else {
Node::TmpValueHolder(i)
};
RCell::new(Node::Reg(shared_size[i].len(), RCell::new(inside_node)))
}
};
state_mem.insert(expr_node, ret.clone());
ret
}
fn add_init_values(
shared_map: &mut AHashMap<usize, RCell<Node>>,
shared_size: &Vec<Vec<bool>>,
n_states: usize,
n_input: usize,
init_node: &RCell<Node>,
) {
for (i, n) in shared_map.iter_mut() {
if *i >= n_states || *i < n_input {
continue;
}
if shared_size[*i]
.iter()
.fold(false, |prev, next| prev || *next)
{
*n = RCell::new(Node::Mux(
init_node.clone(),
RCell::new(Node::Reg(1, n.clone())),
RCell::new(Node::Const(shared_size[*i].clone())),
));
} else {
*n = RCell::new(Node::Reg(1, n.clone()));
}
}
}
fn remove_tmp_value(shared_map: &mut AHashMap<usize, RCell<Node>>, shared_size: &Vec<Vec<bool>>) {
let mut tmp_values = Vec::new();
for (_, node) in shared_map.iter() {
fetch_tmp_values(node.clone(), &mut tmp_values, &mut AHashSet::new())
}
for val in tmp_values.drain(..) {
let i = if let Node::TmpValueHolder(i) = &*val.borrow() {
*i
} else {
continue;
};
*val.borrow_mut() = shared_map
.get(&i)
.unwrap_or(&RCell::new(Node::Const(shared_size[i].clone())))
.clone()
.borrow()
.clone();
}
}
//get the temp values everywhere in a vec so they can be replaced
fn fetch_tmp_values(
node: RCell<Node>,
tmp_values: &mut Vec<RCell<Node>>,
mem: &mut AHashSet<RCell<Node>>,
) {
if mem.contains(&node) {
return;
}
mem.insert(node.clone());
match &*node.borrow() {
Node::Input(_) | Node::Const(_) => {}
Node::Not(e) => fetch_tmp_values(e.clone(), tmp_values, mem),
Node::Slice(e, _, _) => fetch_tmp_values(e.clone(), tmp_values, mem),
Node::BiOp(_, e1, e2) => {
fetch_tmp_values(e1.clone(), tmp_values, mem);
fetch_tmp_values(e2.clone(), tmp_values, mem)
}
Node::Mux(e1, e2, e3) => {
fetch_tmp_values(e1.clone(), tmp_values, mem);
fetch_tmp_values(e2.clone(), tmp_values, mem);
fetch_tmp_values(e3.clone(), tmp_values, mem)
}
Node::Reg(_, e) => fetch_tmp_values(e.clone(), tmp_values, mem),
Node::Ram(e1, e2, e3, e4) => {
fetch_tmp_values(e1.clone(), tmp_values, mem);
fetch_tmp_values(e2.clone(), tmp_values, mem);
fetch_tmp_values(e3.clone(), tmp_values, mem);
fetch_tmp_values(e4.clone(), tmp_values, mem)
}
Node::Rom(_, e) => fetch_tmp_values(e.clone(), tmp_values, mem),
Node::TmpValueHolder(_) => tmp_values.push(node.clone()),
}
}