-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
108 lines (85 loc) · 3.62 KB
/
main.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
from __future__ import annotations
from typing import List
from gd2c.project import load_project, Project
from gd2c.gdscriptclass import GDScriptClass
from gd2c.target import Target
from gd2c import transform
from gd2c import controlflow
from gd2c import domtree
from gd2c import ssa
from gd2c import analysis
def print_stuff(project, print_cfg, print_domtree):
for cls in project.classes():
print(f"---------------------------------------------")
print(f"Class: {cls.name}")
print(f"---------------------------------------------")
for func in cls.functions():
if func.cfg is None:
func.cfg = controlflow.build_control_flow_graph(func)
#to_ssa_form(func)
func.pretty_print(True)
#func.cfg.update_function(func)
if print_cfg:
func.cfg.pretty_print(True, True)
if print_domtree:
print("-- DOMTREE ---------------------------------")
tree = domtree.build_domtree_naive(cfg)
tree.pretty_print()
print("\n")
def get_target(target_name: str, project: Project) -> Target:
if target_name == "gdnative":
from gd2c.targets.gdnative import GDNativeTarget
return GDNativeTarget(project)
raise Exception("target not known")
def assert_nothing_in_ssa_form(project: Project):
for cls in project.iter_classes_in_dependency_order():
for func in cls.functions():
if func.cfg:
assert not func.cfg.is_in_ssa_form
if __name__ == "__main__":
# These should be taken from command line args
project_path = "./example/test"
project_output_path = "./example/out"
project_target = "gdnative"
project = load_project(project_path)
# Phase 0: Analyze
for cls in project.iter_classes_in_dependency_order():
for func in cls.functions():
func.cfg = controlflow.build_control_flow_graph(func)
analysis.annotate_coroutines(func)
analysis.annotate_assigned_parameters(func)
analysis.annotate_loops(func)
# Phase 1: Compile to intermediate
for cls in project.iter_classes_in_dependency_order():
for func in cls.functions():
if func.yields:
transform.make_coroutine(func)
func.cfg = controlflow.build_control_flow_graph(func)
# Transforms not requiring SSA form
transform.strip_debug(func)
#func.cfg.pretty_print()
# Transforms done in SSA form
if False:
ssa.to_ssa_form(func)
transform.promote_typed_arithmetic(func)
transform.substitute_intrinsics(func)
iteration_count = 0
iterate = True
while iterate and iteration_count < 10:
iterate = any([
transform.common_subexpression_elimination(func),
transform.copy_elimination(func),
transform.dead_code_elimination(func),
transform.redundant_phi_arg_elimination(func)])
# Done doing SSA transformations
ssa.from_ssa_form(func)
func.cfg.update_function(func)
func.cfg = None
# Phase 2: Apply target-specific transformations
target = get_target(project_target, project)
target.transform()
# Phase 3: Emit code
target.emit(project_output_path)
# print_stuff(project, False, False)
for cls in project.iter_classes_in_dependency_order():
print(cls.resource_path, cls.name, sep=" -> ")