-
Notifications
You must be signed in to change notification settings - Fork 3
/
mutators.py
2531 lines (1821 loc) · 58.5 KB
/
mutators.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
#!/bin/env python3
from operator import attrgetter
import statements
from tokens import Tokenizer
from statements import *
from expressions import *
from utils import *
from sdscp_errors import *
# for evaluation of expr
import renderers
import math
import config
def synth(source):
""" Parse source & convert to statements """
tk = Tokenizer(source)
tokens = tk.tokenize()
return statements.parse(tokens)
class Mutator:
""" Code mutator
Takes source code and generates some other code,
applying transformations.
"""
def read_pragmas(self, pragmas):
""" The mutator here can configure itself based on pragmas """
pass
def transform(self, code):
""" Apply transformations to the code
Args:
code (Statement[]): source code
Returns:
code transformed by the mutator.
"""
return self._transform(code)
def _transform(self, code):
""" Do the stuff
Args:
code (Statement[]): source
Returns:
transformed code
"""
return code
class M_AddBraces(Mutator):
""" Adds braces to control structures """
def _transform(self, code):
processed = []
for s in code:
append(processed, self._add_braces(s))
return processed
def _add_braces(self, s):
if isinstance(s, S_If):
has_else = (not isinstance(s.else_st, S_Empty))
# wrap THEN
if isinstance(s.then_st, S_Block):
s.then_st = self._add_braces(s.then_st)
else: # not a block
if type(s.then_st) is S_Goto and not has_else:
pass
else:
ss = S_Block(None)
ss.children = [self._add_braces(s.then_st)]
s.then_st = ss
# wrap ELSE
if has_else:
if isinstance(s.else_st, S_Block):
s.else_st = self._add_braces(s.else_st)
else:
ss = S_Block(None)
ss.children = [self._add_braces(s.else_st)]
s.else_st = ss
elif isinstance(s, S_Block):
c = []
for ss in s.children:
c.append(self._add_braces(ss))
s.children = c
elif (isinstance(s, S_For) or
isinstance(s, S_While) or
isinstance(s, S_Switch) or
isinstance(s, S_Function) or
isinstance(s, S_DoWhile)):
if not isinstance(s.body_st, S_Block):
# wrap body
ss = S_Block(None)
ss.children = [self._add_braces(s.body_st)]
s.body_st = ss
else:
s.body_st = self._add_braces(s.body_st)
return s
class M_RemoveDeadCode(Mutator):
""" Removes obvious dead code, unused labels etc. """
def read_pragmas(self, pragmas):
self.do_remove_dead_code = pragmas.get('remove_dead_code', True)
self.keep_banner_comments = pragmas.get('comments', True)
def _transform(self, code):
if not self.do_remove_dead_code:
return code
if not config.QUIET: print('Removing dead code...')
p = 1
while True:
if not config.QUIET: print('Cleaning: Pass %d' % p)
p += 1
self.removed = False
self.used_labels = set()
self.existing_labels = set()
# FIXME
self.do_rm_labels = False
code = self._rm_dead(code)
#print("Existing labels: %s" % str(self.existing_labels))
self.do_rm_labels = True
code = self._rm_dead(code)
if not self.removed:
break
return code
def _rm_dead(self, code):
out = []
was_alone = isinstance(code, Statement)
if was_alone:
code = [code]
length = len(code)
i = 0
while i < length:
s = code[i]
if type(s) is S_Label:
self.existing_labels.add(s.name)
if self.do_rm_labels:
if s.name not in self.used_labels:
i += 1 # skip
self.removed = True
#if not config.QUIET: print('Removing unused label %s' % s.name)
continue
if type(s) is S_Goto:
self.used_labels.add(s.name)
if self.do_rm_labels:
if s.name not in self.existing_labels:
raise SdscpSyntaxError('GOTO to undefined label %s!' % s.name)
# Discard all until next label.
# If the label is the target for this goto, discard the goto too.
cmt = None
j = i
while j < length:
j += 1
if j == length:
out.append(s)
# print('eof, appending '+str(s))
i = j + 1
break
ss = code[j]
# print('ss code[j] ' + str(ss))
# UGLY HACK to avoid removing of FUNC banner comments.
if self.keep_banner_comments:
if type(ss) is S_Comment and 'FUNC' in ss.text:
out.append(s)
out.append(ss)
i = j
break
if type(ss) is S_Label:
if self.do_rm_labels and ss.name not in self.used_labels:
#if not config.QUIET: print('Removing unused label %s' % ss.name)
self.removed = True
continue
if ss.name == s.name:
self.removed = True
else:
append(out, s)
if j > i + 1:
self.removed = True
out.append(ss)
self.existing_labels.add(ss.name)
i = j
break
elif type(s) is S_If:
# go into if's branches
s.then_st = self._rm_dead(s.then_st)
s.else_st = self._rm_dead(s.else_st)
out.append(s)
elif isinstance(s, S_Block):
# handle block contents
s.children = self._rm_dead(s.children)
out.append(s)
elif (isinstance(s, S_For) or
isinstance(s, S_While) or
isinstance(s, S_Switch) or
isinstance(s, S_Function) or
isinstance(s, S_DoWhile)):
s.body_st = self._rm_dead(s.body_st)
out.append(s)
else:
out.append(s)
i += 1 # advance counter
# collapse if was single at start
if was_alone:
if len(out) == 0:
return S_Empty()
elif len(out) == 1:
return out[0]
else:
s = S_Block()
s.children = out
return s
else:
return out
class M_CollectVars(Mutator):
""" Collect global vars at the top """
def _transform(self, code):
""" Move all global variables to the top of the code.
Global = outside functions. Also validates that in root
scope only functions and variables are used.
"""
variables = []
functions = []
for s in code:
if isinstance(s, S_Var):
variables.append(s)
elif isinstance(s, S_Function):
functions.append(s)
elif isinstance(s, S_DocComment):
pass # Simply discard it
else:
raise CompatibilityError('Illegal statement in root scope: %s' % str(s))
return variables + functions
class TmpVarPool:
""" Pool of temporary variables """
def __init__(self):
self.used_cnt = 0
self.locks = {}
def _gen_name(self, index):
return "__t%d" % (index)
def acquire(self):
""" Acquire a free temporary variable """
for (name, used) in self.locks.items():
if not used:
self.used_cnt += 1
self.locks[name] = True
return name
name = self._gen_name(self.used_cnt)
self.used_cnt += 1
self.locks[name] = True
return name
def release(self, name):
""" Release temporary variable(s) """
if type(name) == list:
for n in name:
self.release(n)
return
if not name in self.locks.keys():
raise Exception('Cannot release %s, not defined.' % name)
self.locks[name] = False
self.used_cnt -= 1
def release_all(self):
""" Release all tmp vars """
for n in self.locks.keys():
self.locks[n] = False
self.used_cnt = 0
def get_names(self):
return self.locks.keys()
class ArgPool:
""" Pool of transport variables for arguments """
def __init__(self):
self.used_cnt = 0
self.ptr = 0
self.vars = []
def _gen_name(self, index):
return "__a%d" % (index)
def save(self):
""" Save position """
return self.ptr
def restore(self, saved_ptr):
""" Restore to saved position """
self.ptr = saved_ptr
def rewind(self):
""" Rewind to start """
self.ptr = 0
def acquire(self):
""" Get a free arg variable """
if self.ptr >= self.used_cnt:
# must add new one
self.vars.append(self._gen_name(self.ptr))
self.used_cnt += 1
name = self.vars[self.ptr]
self.ptr += 1
return name
def is_defined(self, v : str):
return v in self.vars
def get_names(self):
return self.vars
class LabelPool:
""" Generates unique label names """
def __init__(self):
self.counters = {}
self.used = []
def acquire(self, prefix='label'):
""" Make a unique label with given prefix """
if prefix in self.counters:
self.counters[prefix] += 1
else:
self.counters[prefix] = 1
name = '__%s_%d' % (prefix, self.counters[prefix])
self.register(name)
return name
def register(self, name):
""" Add a label name to the list - for checking existence
Used also for user labels.
"""
self.used.append(name)
def exists(self, name):
""" Check if given label exists in the program """
return name in self.used
class FnRegistry:
""" Registry of function labels and translations """
def __init__(self, label_pool):
self.counter = 1
# function name to index
self.fnname2fnindex = {}
# function index to name
self.fnindex2fnname = {}
self.fnindex2statement = {}
# name of retpos for call with given index
self.callindex2calllabel = {}
self.label_pool = label_pool
# index of call -> function name
self.callindex2fnname = {}
# call index 2 origin func name
self.callindex2origin = {}
self.call_counter = 1
self.function_labels = {}
self.gr = None # reference to Grande Mutator
def register(self, stmt):
""" Register a function. Returns index. """
if not isinstance(stmt, S_Function):
raise SdscpInternalError('fn_pool.register bad argument: ', str(stmt))
name = stmt.name;
i = self.counter
self.fnname2fnindex[name] = i
self.fnindex2fnname[i] = name # TODO this is redundant now that we have the statement index?
self.fnindex2statement[i] = stmt
begin = self.get_begin(i)
end = self.get_end(i)
self.label_pool.register(begin)
self.label_pool.register(end)
self.function_labels[i] = [begin, end]
self.counter += 1
return i
def get_statement(self, name):
""" Get a function statement by name """
if not name in self.fnname2fnindex:
return None
i = self.fnname2fnindex[name]
return self.fnindex2statement[i]
def register_call(self, called, from_):
""" Register a call. Returns index. """
i = self.call_counter
label = self.get_call_label(i)
self.callindex2calllabel[i] = label
self.label_pool.register(label)
self.callindex2fnname[i] = self.get_name(called)
self.callindex2origin[i] = from_
self.call_counter += 1
return i
def get_fn_args(self, name):
""" Get args for name """
if not name in self.fnname2fnindex.keys():
raise SdscpSyntaxError('Function not found, cannot call: %s' % name)
index = self.fnname2fnindex[name];
stmt = self.fnindex2statement[index];
return stmt.args;
def get_begin(self, index):
""" Get start label for a function
Args:
index: name or function index
Returns:
label name
"""
if self.gr is not None and self.gr.do_preserve_names:
name = index
if type(index) == str:
index = self.fnname2fnindex[index]
else:
name = self.fnindex2fnname[index]
return "__fn%s_%s" % (index, name)
else:
if type(index) == str:
index = self.fnname2fnindex[index]
return "__fn%s" % index
def get_end(self, index):
""" Get end label of a function (for return)
Args:
index: name or function index
Returns:
label name
"""
if type(index) == str:
if index == 'main':
return '__main_loop_end'
if index == 'init':
return '__init_end'
index = self.fnname2fnindex[index]
return "__fn%d_end" % index
def get_ns_label(self, index, label):
""" Get namespaced label in function
Args:
index: name or function index
label: label name
Returns:
label name with namespace prefix
"""
if index == 'main' or index == 'init':
return "__fn%sL_%s" % (index, label)
if type(index) == str:
index = self.fnname2fnindex[index]
return "__fn%sL_%s" % (index, label)
def get_call_label(self, index):
""" Get return-from-call label
Args:
index: index of the call
Returns:
label name
"""
return "__rp%s" % index
def get_fn_addr(self, name):
""" Get address for name """
if not name in self.fnname2fnindex.keys():
raise SdscpSyntaxError('Function not found, cannot call: %s' % name)
return self.fnname2fnindex[name]
def get_name(self, addr):
""" Get name from address """
if not addr in self.fnindex2fnname.keys():
if addr in self.callindex2fnname.keys():
return self.callindex2fnname[addr]
else:
return None
return self.fnindex2fnname[addr]
def get_transformed_name(self, name):
return self.get_begin(self.fnname2fnindex[name])
class M_Grande(Mutator):
""" The master mutator for SDSCP extra features
Attrs:
TODO
"""
def __init__(self):
# list of builtin functions
self.builtin_fn = [
'echo',
'echoclear',
'echoinline',
'wait',
'sprintf',
'textcmp',
'atoi',
'itoa',
'itoh',
'smtp_send',
'ping',
'dns_resolv',
'http_get',
'http_post',
'snmp_send_trap',
'send_udp',
'lcd_echo',
'lcd_clear',
'lcd_newline',
'lcd_setpixel',
'serial_set',
'serial_write',
'serial_text_out',
'serial1_set',
'serial1_write',
'serial1_text_out',
'serial6_set',
'serial6_write',
'serial6_text_out',
'read_dataflash',
'write_dataflash',
'read_dataflash_page_to_ram',
'write_ram_block_to_dataflash_page',
'onewire_rescan',
'sdsc_reset_program',
'sdsc_set_wdg',
'sdsc_kick_wdg',
'mqtt_connect',
'mqtt_disconnect',
'mqtt_publish',
'mqtt_subscribe',
'mqtt_unsubscribe_index',
'mqtt_unsubscribe_name',
'modbus_tcp_connect',
'modbus_tcp_disconnect',
'modbus_tcp_read',
'modbus_tcp_writesingle',
'modbus_tcp_writemultiple',
]
self.builtin_var = [
'sys',
'ram',
'share',
'text',
]
self.sdscp_builtin_fn = [
'reset',
'end',
'push',
'pop',
]
self._halt_used = False
def read_pragmas(self, pragmas):
self.do_check_stack_bounds = pragmas.get('safe_stack', True)
self.stack_start = pragmas.get('stack_start', 300)
self.stack_end = pragmas.get('stack_end', 511)
self.do_preserve_names = pragmas.get('keep_names', False)
self.do_fullspeed = pragmas.get('fullspeed', True)
self.add_debug_trace_logging = pragmas.get('show_trace', False)
self.do_builtin_logging = pragmas.get('builtin_logging', True)
self.do_builtin_error_logging = pragmas.get('builtin_error_logging', True)
self.do_inline_one_use_functions = pragmas.get('inline_one_use_functions', True)
self.do_remove_dead_code = pragmas.get('remove_dead_code', True)
self.do_simplify_ifs = pragmas.get('simplify_ifs', True)
self.do_simplify_expressions = pragmas.get('simplify_expressions', True)
self.do_use_push_pop_trampolines = pragmas.get('push_pop_trampolines', False)
if self.do_check_stack_bounds:
# Push-pop gets larger in this case, so pushpop trampolines are
# worthwhile even with only 2 tmps
config.PUSHPOP_TRAMPOLINE_MIN_TMP_COUNT = 2
if 'push_pop_trampoline_limit' in pragmas:
config.PUSHPOP_TRAMPOLINE_MIN_TMP_COUNT = pragmas.get('push_pop_trampoline_limit')
def _transform(self, code):
self.globals_declare = []
self.globals_assign = []
self.globals_vars = set()
self.global_rename = {}
functions = []
self.user_fn = set()
self.tmp_pool = TmpVarPool()
self.arg_pool = ArgPool()
self.label_pool = LabelPool()
self.fn_pool = FnRegistry(self.label_pool)
self.fn_pool.gr = self
self.inline_return_var = None
self.scope_level = 0
self.scope_locals = {}
self.functions_called = set()
self.labels_used = set()
init_userfn = None
main_userfn = None
# register helper vars
self._add_global_var('__rval') # return value
self._add_global_var('__sp', self.stack_end + 1) # stack pointer at RAMEND (grows towards lower addrs)
self._add_global_var('__addr') # jump address pointer
# iterate through top level statements
# Variables first
for s in code:
if isinstance(s, S_Var):
self._add_global_var(s.var.name, s.value, user=True)
# The rest
for s in code:
if isinstance(s, S_Var):
pass # Processed before
elif isinstance(s, S_DocComment):
pass # Simply discard it
elif isinstance(s, S_Function):
if s.name in self.user_fn:
raise SdscpSyntaxError('Duplicate function: %s()' % s.name)
self.user_fn.add(s.name)
if s.name == 'main':
main_userfn = s
elif s.name == 'init':
init_userfn = s
else:
self.user_fn.add(s.name)
functions.append(s)
self.fn_pool.register(s)
else:
raise SdscpSyntaxError('Illegal statement in root scope: %s' % s)
if main_userfn is None:
raise SdscpSyntaxError('Missing main function!')
# Callgraph is a dict where
# - keys are function names
# - values are lists of function names that call them
callgraph = dict()
if init_userfn is not None:
# functions do not use the first argument, it's there to keep the signature the same in all statements
init_userfn.update_callgraph('', callgraph)
main_userfn.update_callgraph('', callgraph)
for f in functions:
f.update_callgraph('', callgraph)
if config.SHOW_CALLGRAPH: print("Callgraph:")
for callee, callers in sorted(callgraph.items()):
if callee not in self.builtin_fn:
if config.SHOW_CALLGRAPH:
print(" %s <- %s" % (callee, ', '.join(callers)))
st = self.fn_pool.get_statement(callee)
if st is not None and self.do_inline_one_use_functions:
st.inline = len(callers) <= 1
# process init()
pr_init = None
if init_userfn is not None:
pr_init = self._process_fn(init_userfn, naked=True)
pr_main = self._process_fn(main_userfn, naked=True)
# process user functions except main() & init()
pr_userfuncs = {}
for fn in functions:
if fn.name not in callgraph:
if self.do_remove_dead_code:
if not config.QUIET: print('\x1b[33mRemoving unused function "%s()"\x1b[m' % fn.name)
continue
else:
if not config.QUIET: print('\x1b[33mFunction "%s()" is unused\x1b[m' % fn.name)
# Include it in this case!
if fn.inline:
continue
# now we get
pr_userfuncs[fn.name] = self._process_fn(fn)
# find out what funcs are needed
_labels = set()
_gotos = set()
_calls = set()
_labels.update(pr_main.labels)
_calls.update(pr_main.calls)
_gotos.update(pr_main.gotos)
if pr_init is not None:
_labels.update(pr_init.labels)
_calls.update(pr_init.calls)
_gotos.update(pr_init.gotos)
_unresolved_calls = set()
_unresolved_calls.update(_calls)
# _unresolved_calls.remove('main')
# _unresolved_calls.remove('init')
_resolved_calls = set()
# _resolved_calls.add('main')
# _resolved_calls.add('init')
try:
while len(_unresolved_calls) > 0:
for name in list(_unresolved_calls):
if name in _resolved_calls:
continue
_resolved_calls.add(name)
_unresolved_calls.remove(name)
fns = self.fn_pool.get_statement(name)
if fns is None:
continue
if not fns.inline:
fn = pr_userfuncs[name]
_labels.update(fn.labels)
_calls.update(fn.calls)
_gotos.update(fn.gotos)
_unresolved_calls.update(_calls.difference(_resolved_calls))
except KeyError as e:
raise Exception('Error while resolving calls', e)
self.used_labels = _gotos
self.defined_labels = _labels
_calls.add('main')
_calls.add('init')
self.functions_called = _calls
# Add used tmps to globals declare
for name in self.tmp_pool.get_names():
self._add_global_var(name)
# Add args to globals
for name in self.arg_pool.get_names():
self._add_global_var(name)
# Compose output code
output_code = []
append(output_code, S_Comment('Globals declaration'))
append(output_code, sorted(self.globals_declare, key=lambda x: natural_sort_key(x.var.name))) # stupid hacks to get natural sort
# main func body statements
sts = []
# goto reset (skip trampolines)
if self.do_fullspeed:
append(sts, S_Comment('Disable speed limit'))
append(sts, synth('sys[63] = 128;'))
append(sts, self._mk_label('__reset'))
if self.do_builtin_logging:
append(sts, self._mk_echo('[INFO] Program reset.'))
append(sts, self._banner('FUNC: init()'))
append(sts, self._mk_label('__init'))
if self.do_builtin_logging:
append(sts, self._mk_echo('[INFO] Initialization...'))
# assign global vars default values
append(sts, self.globals_assign)
# user init function
if init_userfn is not None:
append(sts, pr_init.code)
append(sts, self._mk_label('__init_end'))
# infinite main loop
append(sts, self._banner('FUNC: main()'))
if self.do_builtin_logging:
append(sts, self._mk_echo('[INFO] main() started.'))
append(sts, self._mk_label('__main_loop'))
if main_userfn is not None:
append(sts, pr_main.code)
append(sts, self._mk_label('__main_loop_end'))
append(sts, self._mk_goto('__main_loop'))
# other user functions (already processed)
# sorted by function label
if self.do_remove_dead_code:
funcs_to_render = sorted(_resolved_calls, key=self.fn_pool.get_transformed_name)
else:
funcs_to_render = sorted(pr_userfuncs.keys(), key=self.fn_pool.get_transformed_name)
for name in funcs_to_render:
fns = self.fn_pool.get_statement(name)
if fns is None:
continue
if not fns.inline:
func = pr_userfuncs[name]
append(sts, func.code)
append(sts, self._build_trampoline_for_func(func.name))
# ERRORS
append(sts, self._build_error_handlers())