irio: Started adding support for entity initializers
[libfirm] / scripts / gen_ir_io.py
1 #!/usr/bin/python
2 import sys
3 from jinja2 import Environment, Template
4 import ir_spec
5
6 def format_args(arglist):
7         #argstrings = map(lambda arg : arg["name"], arglist)
8         #return ", ".join(argstrings)
9         s = ", ".join(arglist)
10         if len(s) == 0:
11           return "";
12         return ", " + s;
13
14 def format_ifnset(string, node, key):
15         if key in node:
16                 return ""
17         return string
18
19 def format_block(node):
20         if node.get("knownBlock"):
21                 return ""
22         else:
23                 return ", get_node(env, preds[0])"
24
25 env = Environment()
26 env.filters['args']   = format_args
27 env.filters['ifnset'] = format_ifnset
28 env.filters['block']  = format_block
29
30 def get_io_type(type, attrname, nodename):
31         if type == "tarval*":
32                 importcmd = "tarval *%s = read_tv(env);" % attrname
33                 exportcmd = "write_tarval(env, %(val)s);";
34         elif type == "ir_mode*":
35                 importcmd = "ir_mode *%s = read_mode(env);" % attrname
36                 exportcmd = "write_mode(env, %(val)s);"
37         elif type == "ir_entity*":
38                 importcmd = "ir_entity *%s = read_entity(env);" % attrname
39                 exportcmd = """fprintf(env->file, "%%ld ", get_entity_nr(%(val)s));"""
40         elif type == "ir_type*":
41                 importcmd = "ir_type *%s = read_type(env);" % attrname
42                 exportcmd = """fprintf(env->file, "%%ld ", get_type_nr(%(val)s));"""
43         elif type == "long" and nodename == "Proj":
44                 importcmd = "long %s = read_long(env);" % attrname
45                 exportcmd = """fprintf(env->file, "%%ld ", %(val)s);"""
46         elif type == "pn_Cmp" or type == "ir_where_alloc":
47                 importcmd = "%s %s = (%s) read_long(env);" % (type, attrname, type)
48                 exportcmd = """fprintf(env->file, "%%ld ", (long) %(val)s);"""
49         elif type == "cons_flags" and nodename == "Store":
50                 importcmd = "ir_cons_flags %s = get_cons_flags(env);" % attrname
51                 exportcmd = """write_pin_state(env, irn);
52                         write_volatility(env, irn);
53                         write_align(env, irn);"""
54         elif type == "cons_flags" and nodename == "Load":
55                 importcmd = "ir_cons_flags %s = get_cons_flags(env);" % attrname
56                 exportcmd = """write_pin_state(env, irn);
57                         write_volatility(env, irn);
58                         write_align(env, irn);"""
59         elif type == "op_pin_state":
60                 importcmd = "op_pin_state %s = read_pin_state(env);" % attrname
61                 exportcmd = "write_pin_state(env, irn);"
62         else:
63                 print "UNKNOWN TYPE: %s" % type
64                 importcmd = """// BAD: %s %s
65                         %s %s = (%s) 0;""" % (type, attrname, type, attrname, type)
66                 exportcmd = "// BAD: %s" % type
67         return (importcmd, exportcmd)
68
69 """     if type == "ir_type*":
70                 java_type    = "firm.Type"
71                 wrap_type    = "Pointer"
72                 to_wrapper   = "%s.ptr"
73                 from_wrapper = "firm.Type.createWrapper(%s)"
74         elif type == "ir_mode*":
75                 java_type    = "firm.Mode"
76                 wrap_type    = "Pointer"
77                 to_wrapper   = "%s.ptr"
78                 from_wrapper = "new firm.Mode(%s)"
79         elif type == "tarval*":
80                 java_type    = "firm.TargetValue"
81                 wrap_type    = "Pointer"
82                 to_wrapper   = "%s.ptr"
83                 from_wrapper = "new firm.TargetValue(%s)"
84         elif type == "pn_Cmp":
85                 java_type    = "int"
86                 wrap_type    = "int"
87                 to_wrapper   = "%s"
88                 from_wrapper = "%s"
89         elif type == "long":
90                 java_type    = "int"
91                 wrap_type    = "com.sun.jna.NativeLong"
92                 to_wrapper   = "new com.sun.jna.NativeLong(%s)"
93                 from_wrapper = "%s.intValue()"
94         elif type == "cons_flags":
95                 java_type    = "firm.bindings.binding_ircons.ir_cons_flags"
96                 wrap_type    = "int"
97                 to_wrapper   = "%s.val"
98                 from_wrapper = "firm.bindings.binding_ircons.ir_cons_flags.getEnum(%s)"
99         elif type == "ir_where_alloc":
100                 java_type    = "firm.bindings.binding_ircons.ir_where_alloc"
101                 wrap_type    = "int"
102                 to_wrapper   = "%s.val"
103                 from_wrapper = "firm.bindings.binding_ircons.ir_where_alloc.getEnum(%s)"
104         elif type == "ir_entity*":
105                 java_type    = "firm.Entity"
106                 wrap_type    = "Pointer"
107                 to_wrapper   = "%s.ptr"
108                 from_wrapper = "new firm.Entity(%s)"
109         else:
110                 print "UNKNOWN TYPE"
111                 java_type    = "BAD"
112                 wrap_type    = "BAD"
113                 to_wrapper   = "BAD"
114                 from_wrapper = "BAD"
115         return (java_type,wrap_type,to_wrapper,from_wrapper)"""
116
117 def prepare_attr(nodename, attr):
118         (importcmd,exportcmd) = get_io_type(attr["type"], attr["name"], nodename)
119         attr["importcmd"] = importcmd
120         attr["exportcmd"] = exportcmd % {"val": "get_%s_%s(irn)" % (nodename, attr["name"])}
121
122 def preprocess_node(nodename, node):
123         if "is_a" in node:
124                 parent = ir_spec.nodes[node["is_a"]]
125                 node["ins"] = parent["ins"]
126                 if "outs" in parent:
127                         node["outs"] = parent["outs"]
128         if "ins" not in node:
129                 node["ins"] = []
130         if "outs" in node:
131                 node["mode"] = "mode_T"
132         if "arity" not in node:
133                 node["arity"] = len(node["ins"])
134         if "attrs" not in node:
135                 node["attrs"] = []
136         if "constructor_args" not in node:
137                 node["constructor_args"] = []
138
139         # construct node arguments
140         arguments = [ ]
141         i = 0
142         for input in node["ins"]:
143                 arguments.append("prednodes[%i]" % i)
144                 i += 1
145
146         if node["arity"] == "variable" or node["arity"] == "dynamic":
147                 arguments.append("numpreds - %i" % (i + 1))
148                 arguments.append("prednodes + %i" % i)
149
150         if "mode" not in node:
151                 arguments.append("mode")
152
153         for attr in node["attrs"]:
154                 prepare_attr(nodename, attr)
155                 arguments.append(attr["name"])
156
157         for arg in node["constructor_args"]:
158                 prepare_attr(nodename, arg)
159                 arguments.append(arg["name"])
160
161         node["arguments"] = arguments
162
163 export_attrs_template = env.from_string('''
164         case iro_{{nodename}}:
165                 {{"write_mode(env, get_irn_mode(irn));"|ifnset(node,"mode")}}
166                 {% for attr in node.attrs %}{{attr.exportcmd}}
167                 {% endfor %}
168                 {% for attr in node.constructor_args %}{{attr.exportcmd}}
169                 {% endfor %}break;''')
170
171 import_attrs_template = env.from_string('''
172         case iro_{{nodename}}:
173         {
174                 {{"ir_mode *mode = read_mode(env);"|ifnset(node,"mode")}}
175                 {% for attr in node.attrs %}{{attr.importcmd}}
176                 {% endfor %}
177                 {% for attr in node.constructor_args %}{{attr.importcmd}}
178                 {% endfor %}newnode = new_r_{{nodename}}(current_ir_graph{{node|block}}{{node["arguments"]|args}});
179                 break;
180         }
181 ''')
182
183 def main(argv):
184         """the main function"""
185
186         if len(argv) < 3:
187                 print "usage: %s specname(ignored) destdirectory" % argv[0]
188                 sys.exit(1)
189
190         gendir = argv[2]
191
192         file = open(gendir + "/gen_irio_export.inl", "w");
193         for nodename, node in ir_spec.nodes.iteritems():
194                 preprocess_node(nodename, node)
195                 if not "abstract" in node:
196                         file.write(export_attrs_template.render(vars()))
197         file.write("\n")
198         file.close()
199
200         file = open(gendir + "/gen_irio_import.inl", "w");
201         for nodename, node in ir_spec.nodes.iteritems():
202                 if not "abstract" in node and nodename != "Start" and nodename != "End" and nodename != "Anchor" and nodename != "SymConst" and nodename != "Block":
203                         file.write(import_attrs_template.render(vars()))
204         # TODO: SymConst
205         file.write("\n")
206         file.close()
207
208         file = open(gendir + "/gen_irio_lex.inl", "w");
209         for nodename, node in ir_spec.nodes.iteritems():
210                 if not "abstract" in node:
211                         file.write("\tINSERT(\"" + nodename + "\", tt_iro, iro_" + nodename + ");\n");
212         file.close()
213
214 if __name__ == "__main__":
215         main(sys.argv)