/
implement.py
executable file
·1351 lines (1069 loc) · 43.8 KB
/
implement.py
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/python
import dfs
import core
from collections import namedtuple, OrderedDict
from functools import partial
from itertools import groupby, chain, count, islice
from pprint import pprint
import sys
from hashlib import md5
import os
from utils import here
if sys.version_info.major == 3 :
from functools import reduce
# ------------------------------------------------------------------------------------------------------------
adjs_t = namedtuple("a", [ "p", "s", ])
# ------------------------------------------------------------------------------------------------------------
def groupby_to_dict(it, key_selector, value_selector, value_grouper) :
return { key : value_grouper([ value_selector(val) for val in values ])
for key, values in groupby(sorted(it, key=key_selector), key_selector) }
def reverse_dict_of_lists(d, key_grouper) :
l = list(chain(*[ [ (v, k) for v in values ] for k, values in d.items() ]))
return groupby_to_dict(l, lambda i: i[0], lambda i: i[1], key_grouper)
def dict_map(d, k_map, v_map, item_filter=lambda k, v: True) :
return { k_map(*i): v_map(*i) for i in d.items() if item_filter(*i) }
# ------------------------------------------------------------------------------------------------------------
def __dag_sanity_check(g, stop_on_first=True) :
"""
1) dangling references (because of joints and macroes) :
a) terms in neigbourhood but not in prototype
b) the same as above for neigbours
c) blocks in neigbours but not in graph
2) basic rules :
a) one predeccessor to input (equals unconnected inputs)
b) matching i/o directions
"""
#TODO matching predeccessors and successors lists
#TODO duplicities in neighbour lists
for b, (p, s) in g.items() :
for t, nr, preds in p :
if not t in b.prototype.terms :
return (False, 0)
if t.direction != core.INPUT_TERM :
return (False, 1)
if len(preds) != 1 :
return (False, 2, (b, t, preds))
for b_pred, t_pred, n_pred in preds :
if not b_pred in g :
return (False, 3)
if t_pred.direction != core.OUTPUT_TERM :
return (False, 4)
if not t_pred in b_pred.prototype.terms :
return (False, 5)
for t, nr, succs in s :
if not t in b.prototype.terms :
return (False, 6)
if t.direction != core.OUTPUT_TERM :
return (False, 7)
for b_succ, t_succ, n_succ in succs :
if not b_succ in g :
return (False, 8)
if t_succ.direction != core.INPUT_TERM :
return (False, 9)
if not t_succ in b_succ.prototype.terms :
return (False, 10)
return (True, )
# ------------------------------------------------------------------------------------------------------------
#def __neighbourhood_remove(neighbourhood, bt, block, term, term_nr) :
# (neighbours, ) = [ succs for t, nr, succs in neighbours if t == bt]
## if (block, term) in neighbours :
# neighbours.remove((block, term, term_nr))
#def __neighbourhood_add(neighbourhood, bt, block, term, term_nr) :
# (neighbours, ) = [ succs for t, succs in neighbours?!?!?!? if t == bt]
# assert(not (block, term) in neighbours) # raise should be better
# neighbours.append((block, term))
# assert(len(neighbours)==1 if bt.direction == core.INPUT_TERM else True)
def __neighbourhood_safe_replace(neighbourhood, term, term_nr, old_tuple, new_tuple) :
"""
new/old_tuple = (block, block_term, block_term_number)
"""
# print "__neighbourhood_safe_replace:", neighbourhood, term, term_nr
(neighbours, ) = [ succs for t, nr, succs in neighbourhood if t == term and nr == term_nr ]
if old_tuple != None and old_tuple in neighbours :
neighbours.remove(old_tuple)
if new_tuple != None and not new_tuple in neighbours :
neighbours.append(new_tuple)
# ------------------------------------------------------------------------------------------------------------
# to implement __expand_joints_new and macro expansion
def __replace_block_with_subgraph(g, n, subgraph, map_in, map_out) :
"""
replace single block from g with subgraph, subgraph may be empty dict and function might be used
to map block terminal to other blocks in g
map_in = { (n_in_term, n_in_term_nr) : [ (subgraph_block, subgraph_term, subgraph_term_nr), ... ], ... }
map_out = { (n_out_term, n_out_term_nr) : (subgraph_block, subgraph_term, subgraph_term_nr), ... }
"""
return remove_block_and_patch(g, n, subgraph, map_in,
{ k : (v,) for k, v in map_out.items() })
#def __check_mapping_sanity(mapping, dir_from, use_assert=True) :
# def check(term) :
# if use_assert :
# assert(term.direction == dir_from)
# else :
# return term.direction == dir_from
# for (in_t, in_t_nr), succs in mapping.items() :
# check(in_t.direction)
# for b, t, nr in succs :
# check(t.direction)
#def neighbourhood_from_term_dir(ps, direction) :
# """
# return (p, s) or (s, p) based on value of direction, that is list with terms of given direction first
# """
# p, s = ps
# if direction == core.INPUT_TERM :
# return s, p
# elif direction == core.OUTPUT_TERM :
# return p, s
# else :
# raise Exception("unknown term direction")
#def __do_part_block_replace(g, n, adj, mapping, dir_from, dir_to) :
##TODO
# pass
#XXX because of symmetry, there should be only single map
def remove_block_and_patch(g, n, subgraph, map_in, map_out) :
"""
replace single block from g with subgraph, subgraph may be empty dict and function might be used
to map block terminal to other blocks in g
map_in = { (n_in_term, n_in_term_nr) : [ (subgraph_block, subgraph_term, subgraph_term_nr), ... ], ... }
map_out = { (n_out_term, n_out_term_nr) : [ (subgraph_block, subgraph_term, subgraph_term_nr), ... ], ... }
"""
p, s = g.pop(n)
g.update(subgraph)
#TODO unify loops
# __do_part_block_replace(g, n, s, map_out, core.OUTPUT_TERM, core.INPUT_TERM)
# __do_part_block_replace(g, n, p, map_in, core.INPUT_TERM, core.OUTPUT_TERM)
# return None
for in_t, in_t_nr, values in p :
assert(in_t.direction == core.INPUT_TERM)
succs = map_in[in_t, in_t_nr] if (in_t, in_t_nr) in map_in else []
for b_pred, t_pred, t_pred_nr in values :
assert(t_pred.direction == core.OUTPUT_TERM)
b_pred_succ = g[b_pred].s
__neighbourhood_safe_replace(
b_pred_succ, t_pred, t_pred_nr, (n, in_t, in_t_nr), None)# now remove connection to n
for b, t, nr in succs :
assert(t.direction == core.INPUT_TERM)
__neighbourhood_safe_replace(b_pred_succ, t_pred, t_pred_nr, (n, in_t, in_t_nr), (b, t, nr))
__neighbourhood_safe_replace(g[b].p, t, nr, None, (b_pred, t_pred, t_pred_nr))
for out_t, out_t_nr, values in s :
assert(out_t.direction == core.OUTPUT_TERM)
preds = map_out[out_t, out_t_nr] if (out_t, out_t_nr) in map_out else []
for b_succ, t_succ, t_succ_nr in values :
assert(t_succ.direction == core.INPUT_TERM)
b_succ_pred = g[b_succ].p
__neighbourhood_safe_replace(
b_succ_pred, t_succ, t_succ_nr, (n, out_t, out_t_nr), None) # remove connection to n
for b, t, nr in preds :
assert(t.direction == core.OUTPUT_TERM)
__neighbourhood_safe_replace(b_succ_pred, t_succ, t_succ_nr, (n, out_t, out_t_nr), (b, t, nr))
__neighbourhood_safe_replace(g[b].s, t, nr, None, (b_succ, t_succ, t_succ_nr))
return None
def printg(g) :
"""
print grap g in somehow readable form
"""
for b, (_, s) in g.items() :
for t, x in s :
print(str(b)+str(t))
for nb, nt in x :
print("\t -> {}{}".format(str(nb), str(nt)))
def dag_merge(l) :
"""
l is list of tuples produced by make_dag [ (grap, delays), ... ]
return single (grap, delays) tuple made of input list
"""
g, d = {}, {}
for graph0, delays0 in l :
g.update(graph0)
d.update(delays0)
return g, d
def chain_blocks(g, n, m) :
"""
creates artificial edge n -> m, so that order of evaluation is guaranteed to be n m
motivated by need to express calls in main function, this is probably BAD THING
"""
n_out = core.VirtualOut("y")
m_in = core.VirtualIn("x")
g[n].s.insert(0, ((n_out, 0, [ (m, m_in, 0) ])))
g[m].p.insert(0, ((m_in, 0, [ (n, n_out, 0) ])))
def replace_block(g, n, m) :
p, s = g.pop(n)
for t, t_nr, adj in p :
new_term, = (tnew for tnew in m.terms if tnew.name == t.name)
for b, bt, bt_nr in adj :
__neighbourhood_safe_replace(g[b].s, bt, bt_nr, (n, t, t_nr), (m, new_term, t_nr))
for t, t_nr, adj in s :
new_term, = (tnew for tnew in m.terms if tnew.name == t.name)
for b, bt, bt_nr in adj :
__neighbourhood_safe_replace(g[b].p, bt, bt_nr, (n, t, t_nr), (m, new_term, t_nr))
g[m] = adjs_t(
[ ( [tnew for tnew in m.terms if tnew.name == t.name][0], t_nr, adj) for t, t_nr, adj in p ],
[ ( [tnew for tnew in m.terms if tnew.name == t.name][0], t_nr, adj) for t, t_nr, adj in s ])
def remove_block(g, n) :
"""
remove block n from graph g
"""
map_in = { (t, t_nr) : tuple() for t, t_nr in in_terms(n) }
map_out = { (t, t_nr) : tuple() for t, t_nr in out_terms(n) }
remove_block_and_patch(g, n, {}, map_in, map_out)
def block_value_by_name(n, value_name) :
"""
n is BlockModel instance
search for position of value_name in prototype value names and
return value from block instance on given position
"""
return { name : value for (name, _), value in zip(n.prototype.values, n.value) }[value_name]
# ------------------------------------------------------------------------------------------------------------
def __cut_joint_alt(g, j) :
((it, it_nr, ((pb, pt, pt_nr),)),), succs = g[j]
# map_in = { (it, it_nr) : [ (b, t, nr) for (ot, ot_nr, ((b, t, nr),)) in succs ] } # works only for joints!
map_in = { (it, it_nr) : [ v for _, _, vertices in succs for v in vertices ] } # works only for joints!
map_out = { (out_term, out_term_nr) : (pb, pt, pt_nr) for out_term, out_term_nr, _ in succs }
__replace_block_with_subgraph(g, j, {}, map_in, map_out)
#XXX is there a way how to do it functionally?
def __expand_joints_new(g) :
for j in [ b for b in g if core.compare_proto_to_type(b.prototype, core.JointProto) ] :
__cut_joint_alt(g, j)
# ------------------------------------------------------------------------------------------------------------
def __join_one_tap(g, tap_ends_lst, tap) :
p, s = g[tap]
((_, _, ((pb, pt, pt_nr),)),) = p
succs = []
for tap_end in tap_ends_lst :
tap_end_preds, tap_end_succs = g[tap_end]
succs += tap_end_succs
map_out = { (out_term, out_term_nr) : (pb, pt, pt_nr)
for out_term, out_term_nr, _ in tap_end_succs }
__replace_block_with_subgraph(g, tap_end, {}, {}, map_out)
assert(not tap_end in g)
map_in = { (tap.terms[0], 0) : [ (b, t, nr) for (ot, ot_nr, ((b, t, nr),)) in succs ] }
__replace_block_with_subgraph(g, tap, {}, map_in, {})
def get_taps(g) :
"""
return { tap_name : tap, ... }
"""
tap_list = [ b for b, (p, s) in g.items() if core.compare_proto_to_type(b.prototype, core.TapProto) ]
taps = { b.value : b for b in tap_list }
assert(len(tap_list)==len(taps))
return taps
def get_tap_ends(g) :
"""
return { tap_name : [ tap_end1, ...], ... }
"""
tap_ends_list = { b for b in g.keys() if core.compare_proto_to_type(b.prototype, core.TapEndProto) }
tap_ends = groupby_to_dict(tap_ends_list, lambda b: b.value, lambda b: b, lambda x: list(x))
assert( len(tap_ends_list) == sum([len(v) for v in tap_ends.values()]) )
return tap_ends
def join_taps(g) :
taps = get_taps(g)
tap_ends = get_tap_ends(g)
additions = {}
for tap_name, tap in taps.items() :
tap_end = tap_ends.pop(tap.value) #TODO do not pop
__join_one_tap(g, tap_end, tap)
# ------------------------------------------------------------------------------------------------------------
def t_unpack(term) :
return term if isinstance(term, tuple) else (term, 0)
def __expddel(d, nr) :
i = dfs.BlockModel(core.DelayInProto(), None)
# print here(), d, nr
if core.compare_proto_to_type(d.prototype, core.InitDelayProto) :
# print here()
o = dfs.BlockModel(core.InitDelayOutProto(), None)
else :
o = dfs.BlockModel(core.DelayOutProto(), None)
i.nr = o.nr = nr
i.delay = o.delay = d
return d, (i, o)
def __mkvert(src, expd) :
b, t0 = src
t, _ = t_unpack(t0)
if core.compare_proto_to_type(b.prototype, core.DelayProto, core.InitDelayProto) :
block = expd[b][ 1 if t.name in ("y", "init") else 0 ]
term, = get_terms_flattened(block, direction=t.direction)
# if core.compare_proto_to_type(b.prototype, core.DelayProto) :
# io = 1 if t.direction == core.OUTPUT_TERM else 0
# block = expd[b][io]
# term, = block.terms
# elif core.compare_proto_to_type(b.prototype, core.InitDelayProto) :
# io = 1 if t.name in ("y", "init") else 0
# block = expd[b][io]
# print here(), src, block, tuple(get_terms_flattened(block, direction=t.direction)), io
# term, = get_terms_flattened(block, direction=t.direction)
else :
block, term = src
result_t, result_t_nr = t_unpack(term)
assert(t.direction==result_t.direction)
return (block, result_t, result_t_nr)
def __expand_delays(blocks, conns, delay_numbering_start) :
delays = frozenset(b for b in blocks
if core.compare_proto_to_type(b.prototype, core.DelayProto, core.InitDelayProto))
# delays = { b for b in blocks if core.compare_proto_to_type(b.prototype, core.DelayProto) }
# print here(), delays
expd = dict(__expddel(delay, delay_numbering_start + nr) for delay, nr in zip(delays, count()))
# print here(), expd
# def mkvert(src, io) :
# b, t = src
# block, term = ( ( expd[b][io], expd[b][io].terms[0] )
# if core.compare_proto_to_type(b.prototype, core.DelayProto)
# else (b, t) )
# return (block, ) + t_unpack(term)
conns2 = {}
for s, dests in conns.items() :
k = __mkvert(s, expd)
v = [ __mkvert(d, expd) for d in dests ]
# print here(), "s:", s, "dests:", dests, #"k:", k, "v:", v
conns2[k] = v
# sys.exit(0)
# conns2 = { __mkvert(s, 1, expd) : [ __mkvert(d, 0, expd) for d in dests ] for s, dests in conns.items() }
return list((set(blocks)-delays).union(chain(*expd.values()))), conns2, expd
#TODO TODO TODO proper sorting by prototype stack order
def get_terms_flattened(block, direction=None, fill_for_unconnected_var_terms=False) :
for t in block.terms :
if not direction is None and direction != t.direction :
continue
if t.variadic :
for nr, index in sorted(block.get_indexed_terms(t), key=lambda x: x[1])[:-1] :
yield t, nr
else :
if fill_for_unconnected_var_terms :
yield t, None
else :
yield t, 0
def in_terms(block) :
return tuple(get_terms_flattened(block, direction=core.INPUT_TERM))
def out_terms(block) :
return tuple(get_terms_flattened(block, direction=core.OUTPUT_TERM))
# ------------------------------------------------------------------------------------------------------------
value_t = namedtuple("value_t", [ "value_type", "value"])#, "resource"
def __parse_num_lit(value, base=10, known_types=None) :
s = value.strip().lower()
if (s[-1] == "f" and not s.startswith("0x")) or "." in s :
return ("vm_float_t", float(s))
else :
if s[-1] == "l" :
return ("vm_dword_t", int(s[:-1], base))
else :
v = int(s, base)
val_type = "vm_word_t"
if known_types :
w_bytes = known_types["vm_word_t"].size_in_bytes
over = v > (2 ** ((w_bytes * 8 ) - 1) - 1)
val_type = "vm_dword_t" if over else "vm_word_t"
return (val_type, v)
def parse_literal(s, known_types=None, variables={}) :
#TODO datetime values, physical units
x = s.strip()
num_sig = (x[1:].strip()[0:2] if x[0] == "-" else x[0:2]).lower()
if x[0] == x[-1] == '"' :
return ("vm_dword_t", s.strip("\"'"))
elif num_sig == "0x" :
return __parse_num_lit(x, base=16, known_types=known_types)
elif num_sig == "0d" :
return __parse_num_lit(x, base=10, known_types=known_types)
elif num_sig == "0o" :
return __parse_num_lit(x, base=8, known_types=known_types)
elif num_sig == "0b" :
return __parse_num_lit(x, base=2, known_types=known_types)
elif num_sig[0] in ".0123456789" :
return __parse_num_lit(x)
elif x[0] in "_abcdefghijklmnoprstuvwxyz" :
if x in variables :
return variables[x]
else :
return (None, x)
else :
raise Exception("can not parse value")
def compare_types(known_types, a, b) :
"""
a, b are type names, keys in known_types dict with type_t tuples
"""
return known_types[a].priority - known_types[b].priority
def infer_block_type(block, preds, types, known_types) :
inferred = None
# print(here(), block)
for t, t_nr, preds in preds :
if t.type_name == core.TYPE_INFERRED :
inherited = types[block, t, t_nr]
# print(here(), inherited)
if inferred is None or compare_types(known_types, inherited, inferred) > 0 :
inferred = inherited
# return sorted(inherited,
# cmp=partial(compare_types, known_types))[-1]
return inferred
def __infer_types_pre_dive(g, delays, types, known_types, n, nt, nt_nr, m, mt, mt_nr, visited) :
mt_type_name = mt.type_name
# print(here(), n, nt, nt_nr, "<-", m, mt, mt_nr, "type:", mt_type_name)
if mt_type_name == core.TYPE_INFERRED :
if core.compare_proto_to_type(m.prototype, core.DelayOutProto) :
value_type, _ = parse_literal(delays[m], known_types=known_types)
mt_type_name = types[m, mt, mt_nr] = value_type
elif core.compare_proto_to_type(m.prototype, core.ConstProto) :
value_type, _ = parse_literal(m.value[0], known_types=known_types)
mt_type_name = types[m, mt, mt_nr] = value_type
else :
types[m, mt, mt_nr] = mt_type_name = infer_block_type(m, g[m].p, types, known_types)
if core.compare_proto_to_type(m.prototype, core.DelayOutProto) :
pass
if nt.type_name == core.TYPE_INFERRED :
types[n, nt, nt_nr] = mt_type_name
def __infer_types_post_visit(g, types, known_types, n, visited) :
p, s = g[n]
# print(here(), n, s)
for t, t_nr, succs in s :
if not succs :
types[n, t, t_nr] = infer_block_type(n, p, types, known_types)
def __inferr_types_dft_roots_sorter(g, roots) :
return sorted(dft_alt_roots_sorter(g, roots),
key=lambda n: 0 if core.compare_proto_to_type(n.prototype, core.InitDelayOutProto) else 1)
def infer_types(g, expd_dels, known_types, allready_inferred=None) :
"""
types of outputs are inferred from types of inferred (in fact, inherited) inputs
block with inferred output type must have at least one inferred input type
if block have more than one inferred input type, highest priority type is used for all outputs
type of Delay is derived from initial value
optional allready_inferred contains types allready inferred in form { m, mt, mt_nr : type_name, ... }
"""
delays = {}
for k, (din, dout) in expd_dels.items() :
delays[din] = delays[dout] = k.value[0]
types = {} if allready_inferred is None else allready_inferred
dft_alt(g,
post_dive=partial(__infer_types_pre_dive, g, delays, types, known_types),
post_visit=partial(__infer_types_post_visit, g, types, known_types),
roots_sorter=__inferr_types_dft_roots_sorter,
sinks_to_sources=True)
return types
# ------------------------------------------------------------------------------------------------------------
#TODO __check_directions(conns)
def make_dag(model, meta, known_types, do_join_taps=True, delay_numbering_start=0) :
conns0 = { k : v for k, v in model.connections.items() if v }
model_blocks = tuple(b for b in model.blocks if not core.compare_proto_to_type(b.prototype, core.TextAreaProto))
blocks, conns1, delays = __expand_delays(model_blocks, conns0, delay_numbering_start)
conns_rev = reverse_dict_of_lists(conns1, lambda values: list(set(values)))
graph = { b : adjs_t(
[ (t, n, conns_rev[(b, t, n)] if (b, t, n) in conns_rev else []) for t, n in in_terms(b) ],
[ (t, n, conns1[(b, t, n)] if (b, t, n) in conns1 else []) for t, n in out_terms(b) ])
for b in blocks }
is_sane = __dag_sanity_check(graph, stop_on_first=False)
if not is_sane :
raise Exception(here() + ": produced graph is insane")
__expand_joints_new(graph)
if do_join_taps :
join_taps(graph)
return graph, delays
# ------------------------------------------------------------------------------------------------------------
def dft_alt_simple_roots_sorter(g, roots) :
return roots
def dft_alt_roots_sorter(g, roots) :
comps = {}
for comp in graph_components(g) :
hsh = md5()
comp_loc_ids = { n : location_id(g, n, term=None) for n in comp }
for m in sorted(comp, key=lambda n: comp_loc_ids[n]) :
hsh.update(comp_loc_ids[m].encode())
comps.update({ n : hsh.hexdigest() for n in comp})
sortable = sortable_sinks(g, roots)
# print(here(), "sortable=", sortable, "comps=", comps)
# def comparer(a, b) :
# per_comp = cmp(comps[a], comps[b])
# return per_comp if per_comp else cmp(sortable[a], sortable[b])
# return sorted(sortable, cmp=comparer)
return sorted(sortable, key=lambda x: comps[x]+"_"+sortable[x])
def __dft_alt_term_sorter(g, block, preds) :
for t, t_nr, neighbours in preds :
if len(neighbours) > 1 :
#TODO TODO TODO
# print("__dft_alt_term_sorter:", neighbours, "sort needed")
lid = location_id(g, block, term=(t, t_nr))
keys = { (b, mt, nr) : location_id(g, b, term=(mt, nr)) for b, mt, nr in neighbours }
neighbours_list = sorted(neighbours, key=lambda i: keys[i])
else :
neighbours_list = neighbours
# neighbours_list = neighbours
for b, mt, nr in neighbours : #XXX
yield t, t_nr, b, mt, nr
# ------------------------------------------------------------------------------------------------------------
def __sort_sinks_post_dive(hsh, n, nt, nt_nr, m, mt, mt_nr, visited) :
edge = (n.to_string(), ".", nt.name, "/", str(nt_nr),
"<-", m.to_string(), ".", mt.name, "/", str(mt_nr))
# print("\t", "".join(edge))
hsh.update("".join(edge).encode())
def location_id(g, block, term=None) :
assert(term==None or (term!=None and len(term) == 2))
hsh = md5()
dft(g, block, undirected=True,
post_dive=partial(__sort_sinks_post_dive, hsh), term=term)
digest = hsh.hexdigest()
return digest
def sortable_sinks(g, sinks) :
sortable = {}
for s in sinks :
digest = location_id(g, s, term=None)
sortable[s] = digest
return sortable
# ------------------------------------------------------------------------------------------------------------
def dft_alt_succs_count(s):
return sum([ len(succ_blocks) for t, nr, succ_blocks in s ])
# block is root if have no successors (no outputs, or all outputs are unconnected)
def __dft_alt_roots_selector(g, sinks_to_sources, roots_sorter) :
p_or_s = 1 if sinks_to_sources else 0
# s = roots_sorter([ v for v, neighbourhood in g.items()
# if all([ len(follows) == 0 for t, follows in neighbourhood[p_or_s] ]) ])
s = roots_sorter(g, [ v for v, nbrhd in g.items() if dft_alt_succs_count(nbrhd[p_or_s]) == 0 ])
return s
# ------------------------------------------------------------------------------------------------------------
def __where_to_go(neighbourhood, sinks_to_sources, undirected) :
if undirected :
return neighbourhood.p + neighbourhood.s
elif sinks_to_sources :
return neighbourhood.p
else :
return neighbourhood.s
def __dft_alt_nr_tree(g, root, pre_visit, pre_dive, post_dive, post_visit,
sort_successors, visited, sinks_to_sources, undir,
term_list=None, follow_visited=False) :
# terms = list(__dft_alt_term_sorter(g, root, __where_to_go(g[root], sinks_to_sources, undir)))
# pre_visit(root, visited, terms)
# stack = [ (root, None, terms.__iter__()) ]
if term_list == None :
terms = list(__dft_alt_term_sorter(g, root, __where_to_go(g[root], sinks_to_sources, undir)))
else :
terms = list(__dft_alt_term_sorter(g, root,
[ (term_list[0], term_list[1], []) ]))
# terms = [ term_list ]
pre_visit(root, visited, terms)
stack = [ (root, None, terms.__iter__()) ]
while stack :
n, prev, it = stack[-1]
if prev != None :
nt, nt_nr, m, mt, mt_nr = prev
assert(n in visited)
assert(m in visited)
post_dive(n, nt, nt_nr, m, mt, mt_nr, visited)
try :
((nt, nt_nr, m, mt, mt_nr), ) = islice(it, 1)
stack[-1] = n, (nt, nt_nr, m, mt, mt_nr), it
dive = pre_dive(n, nt, nt_nr, m, mt, mt_nr, visited)
do_dive = dive is None or (len(dive) > 0 and dive[0] != True)
if (follow_visited or not m in visited) and do_dive :
visited[m] = True
terms = list(__dft_alt_term_sorter(g, m, __where_to_go(g[m], sinks_to_sources, undir)))
# print "\t", here(), m
pre_visit(m, visited, terms)
stack.append((m, None, terms.__iter__()))
except ValueError : #StopIteration :
stack.pop(-1)
post_visit(n, visited)
def dft(g, v,
pre_visit = lambda *a, **b: None,
pre_dive = lambda *a, **b: None,
post_dive = lambda *a, **b: None,
post_visit = lambda *a, **b: None,
sort_successors = lambda *a, **b: None,
sinks_to_sources=True,
undirected=False,
visited=None,
term=None,
follow_visited=False) :
"""
graph structure:
{
blockA :
(p=[ (blockA->term, blockA->term->term_number,
[ (blockB, blockB->term, blockB->term->term_number ] ), ... ],
s=[ ]), ...
}
"""
if visited is None :
visited = {}
visited[v] = True
term_list = None
if term != None :
t, t_nr = term
(term_list, ) = [ (t, t_nr, nbh) for t, t_nr, nbh in
__where_to_go(g[v], sinks_to_sources, undirected) if (t, t_nr) == term]
return __dft_alt_nr_tree(g, v, pre_visit, pre_dive, post_dive,
post_visit, sort_successors, visited, sinks_to_sources, undirected,
term_list=term_list,
follow_visited=follow_visited)
# ------------------------------------------------------------------------------------------------------------
def dft_alt(g,
pre_visit = lambda *a, **b: None,
pre_dive = lambda *a, **b: None,
post_dive = lambda *a, **b: None,
post_visit = lambda *a, **b: None,
pre_tree = lambda *a, **b: None,
post_tree = lambda *a, **b: None,
roots_sorter=dft_alt_roots_sorter,
sinks_to_sources=True,
follow_visited=False) :
# s = roots_sorter([ v for v, (p, s) in g.items() if not ( s if sinks_to_sources else p ) ])
s = __dft_alt_roots_selector(g, sinks_to_sources, roots_sorter)
visited = {}
for v in s :
pre_tree(v, visited)
assert(not v in visited)
dft(g, v,
pre_visit = pre_visit,
pre_dive = pre_dive,
post_dive = post_dive,
post_visit = post_visit,
sinks_to_sources=sinks_to_sources,
undirected=False,
visited=visited,
follow_visited=follow_visited)
post_tree(v, visited)
# ------------------------------------------------------------------------------------------------------------
def graph_components(g) :
comps = []
visited = {}
for v in g.keys() :
comp = {}
if not v in visited :
dft(g, v, undirected=True, visited=comp)
visited.update(comp)
comps.append(comp.keys())
return comps
# ------------------------------------------------------------------------------------------------------------
def __su_get_number(numbering, src_blocks_tuple) :
t, nr, src_b, i = src_blocks_tuple
return numbering[src_b][0]
def __su_post_visit(g, numbering, n, visited) :
#TODO add documentation
#TODO take into account temp variables?
"""
commutativity comes in two flavours, it may be commutative block,
or numbered instances of variadic terminal
"""
# print here(), n
p, s = g[n] # XXX s might be used to analyze spill space usage
src_blocks1 = [ (t, nr, src_b, i) for ((t, nr, ((src_b, src_t, src_t_nt),)), i) in zip(p, count()) ]
src_blocks1.sort(key=lambda sb : __su_get_number(numbering, sb))
if n.prototype.commutative :
# src_grouped = [ ( None, sorted(src_blocks1, key=lambda sb : __su_get_number(numbering, sb)) ) ]
src_grouped = [ ( None, src_blocks1 ) ]
# src_grouped_old = [ ( None, sorted(src_blocks1, key=lambda (t, nr, src_b, i) : -numbering[src_b][0]) ) ]
# print here(), src_grouped_old == src_grouped
else :
# print here()
# src_grouped = [ (t, list(rest)) for t, rest in groupby(src_blocks1, lambda (term, _0, _1, _2): term) ]
src_grouped = [ (t, list(rest)) for t, rest in groupby(src_blocks1, lambda sb: sb[0]) ]
index = 0
evaluated_blocks = []
usages = []
indices = []
for group_term, src_blocks in src_grouped :
if not n.prototype.commutative and group_term.commutative:
# src_blocks_old = sorted(src_blocks, key=lambda (t, nr, src_b, i) : -numbering[src_b][0])
src_blocks.sort(key=lambda sb : __su_get_number(numbering, sb))
# print(here(), src_blocks_old == src_blocks)
for term, nr, src_b, i in src_blocks :
if not src_b in evaluated_blocks :
# print here(), numbering, src_b
usages.append(numbering[src_b][0] + index)
index += 1
else :
index += 1
usages.append(index)
evaluated_blocks.append(src_b)
indices.append(i)
slots = max( usages + [ len(s) ] )
numbering[n] = ( slots, indices )
def sethi_ullman(g) :
#TODO testing, is it (easily) possible to algorithmically create graph with given numbering?
print(here())
numbering = {}
dft_alt(g, post_visit = partial(__su_post_visit, g, numbering))
return numbering
# ------------------------------------------------------------------------------------------------------------
def temp_init(known_types) :
tmp = { tp_name : []
for tp_name in known_types if not tp_name in (core.TYPE_VOID, core.TYPE_INFERRED) }
return tmp
def get_tmp_slot(tmp, slot_type=None) :
assert(not slot_type is None)
if "empty" in tmp[slot_type] :
slot = tmp[slot_type].index("empty")
else :
slot = len(tmp[slot_type])
tmp[slot_type].append("empty")
return slot
def add_tmp_ref(tmp, refs, slot_type=None) :
assert(not slot_type is None)
assert(len(refs)>0)
assert(slot_type != core.TYPE_INFERRED)
slot = get_tmp_slot(tmp, slot_type=slot_type)
tmp[slot_type][slot] = list(refs)
return slot
def pop_tmp_ref(tmp, b, t, t_nr) :
for slot_type, t_tmp in tmp.items() :
for slot, nr in zip(t_tmp, count()) :
if slot != "empty" and (b, t, t_nr) in slot :
slot.remove((b, t, t_nr))
if len(slot) == 0 :
t_tmp[nr] = "empty"
return slot_type, nr
return None
def tmp_used_slots(tmp) :
"""
returns current number of non-empty slots of all types
"""
return sum([ sum([ 1 for slot in t_tmp if slot != "empty" ])
for tp, t_tmp in tmp.items() ])
def tmp_max_slots_used(tmp, slot_type=None) :
"""
returns peak number of slots in use to this time
returns results for single data type if slot_type argument set
"""
slots = [ t_tmp for tp, t_tmp in tmp.items()
if slot_type == None or tp == slot_type ]
return sum([ sum([ 1 for slot in t_tmp ]) for t_tmp in slots ])
def tmp_merge(tmp0, tmp1) :
tmp = dict(tmp1)
for t, slots in tmp0.items() :
if t in tmp :
tmp[t].extend(slots)
else :
tmp[t] = slots
return tmp
# ------------------------------------------------------------------------------------------------------------
def init_pipe_protos(known_types) :
#TODO generalize for other IPC schemes
g_protos = {}
for type_name in known_types :
if type_name != core.TYPE_INFERRED :
gw_proto = core.GlobalWriteProto(type_name)
gr_proto = core.GlobalReadProto(type_name)
g_protos[type_name] = (gw_proto, gr_proto)
return g_protos
def extract_pipes(g, known_types, g_protos, pipe_replacement) :
pipes = { n for n in g if core.compare_proto_to_type(n.prototype, core.PipeProto) }
for n in pipes :
pipe_name = block_value_by_name(n, "Name")
pipe_default = block_value_by_name(n, "Default")
pipe_type, v = parse_literal(pipe_default, known_types=known_types, variables={})
gw_proto, gr_proto = g_protos[pipe_type]
pipe_replacement[pipe_name] = (pipe_type, pipe_default, gw_proto, gr_proto)
#TODO maybe return something useful for generation of globals
def replace_pipes(g, g_protos, pipe_replacement) :
pipe_ends = { n : block_value_by_name(n, "Name") for n in g if core.compare_proto_to_type(n.prototype, core.PipeEndProto) }
unmatched = [ pe for pe in pipe_ends
if not (block_value_by_name(pe, "Name") in pipe_replacement) ]
if unmatched :
raise Exception("unmatched pipes found! {0}".format(str(unmatched)))
pipes = { n for n in g if core.compare_proto_to_type(n.prototype, core.PipeProto) }
for n in pipes :
pipe_name = block_value_by_name(n, "Name")
pipe_type, pipe_default, gw_proto, gr_proto = pipe_replacement[pipe_name]
gw_proto, gr_proto = g_protos[pipe_type]
m = dfs.BlockModel(gw_proto, "itentionally left blank")
m.value = (pipe_name, )
replace_block(g, n, m)
for pipe_end, pipe_name in pipe_ends.items() :
pipe_type, pipe_default, gw_proto, gr_proto = pipe_replacement[pipe_name]
m = dfs.BlockModel(gr_proto, "itentionally left blank")
m.value = (pipe_name, )
replace_block(g, pipe_end, m)
# ------------------------------------------------------------------------------------------------------------
def __expand_macro(g, library, n, known_types, cache, local_block_sheets) :
name = n.prototype.exe_name
full_name = n.prototype.library + "." + name
# print here(), full_name
sheet = None
if n.prototype.library == "<local>" :
# print here(), full_name
sheet_name = "@macro:" + name
if sheet_name in local_block_sheets :
sheet = core.clone_sheet(local_block_sheets[sheet_name][1], library)
else :
sheet = core.load_library_sheet(library, full_name, "@macro:" + name)
if sheet is None :
raise Exception("failed to expand macro '" + full_name + "'")
offset = reduce(max, (b.nr for b in g if core.compare_proto_to_type(b.prototype, core.DelayInProto)), 0)#TODO
gm, delays = make_dag(sheet, None, known_types, do_join_taps=False, delay_numbering_start=offset+1)
#TODO
# print here(), full_name, full_name in cache
# if full_name in cache :
# block = cache[full_name]
# else :
# block = __instantiate_macro(library, full_name)
# cache[full_name] = block
inputs = { b.value[0] : (b, s[0][2]) for b, (_, s) in gm.items() if core.compare_proto_to_type(b.prototype, core.InputProto) }
outputs = { b.value[0] : (b, p[0][2]) for b, (p, _) in gm.items() if core.compare_proto_to_type(b.prototype, core.OutputProto) }
for io_blocks in (inputs, outputs) :
for io, _ in io_blocks.values() :
remove_block(gm, io)
p, s = g[n]
#XXX to handle variadic terms map p -> inputs