Added strict_flag to new_r/rd_Conv(). Fixed strict Convs for irio.
[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         elif type == "ir_builtin_kind":
63                 importcmd = "ir_builtin_kind %s = read_builtin_kind(env);" % attrname
64                 exportcmd = "write_builtin_kind(env, irn);"
65         elif type == "int":
66                 importcmd = "int %s = (int) read_long(env);" % attrname
67                 exportcmd = """fprintf(env->file, "%%d ", %(val)s);"""
68         else:
69                 print "UNKNOWN TYPE: %s" % type
70                 importcmd = """// BAD: %s %s
71                         %s %s = (%s) 0;""" % (type, attrname, type, attrname, type)
72                 exportcmd = "// BAD: %s" % type
73         return (importcmd, exportcmd)
74
75 """     if type == "ir_type*":
76                 java_type    = "firm.Type"
77                 wrap_type    = "Pointer"
78                 to_wrapper   = "%s.ptr"
79                 from_wrapper = "firm.Type.createWrapper(%s)"
80         elif type == "ir_mode*":
81                 java_type    = "firm.Mode"
82                 wrap_type    = "Pointer"
83                 to_wrapper   = "%s.ptr"
84                 from_wrapper = "new firm.Mode(%s)"
85         elif type == "tarval*":
86                 java_type    = "firm.TargetValue"
87                 wrap_type    = "Pointer"
88                 to_wrapper   = "%s.ptr"
89                 from_wrapper = "new firm.TargetValue(%s)"
90         elif type == "pn_Cmp":
91                 java_type    = "int"
92                 wrap_type    = "int"
93                 to_wrapper   = "%s"
94                 from_wrapper = "%s"
95         elif type == "long":
96                 java_type    = "int"
97                 wrap_type    = "com.sun.jna.NativeLong"
98                 to_wrapper   = "new com.sun.jna.NativeLong(%s)"
99                 from_wrapper = "%s.intValue()"
100         elif type == "cons_flags":
101                 java_type    = "firm.bindings.binding_ircons.ir_cons_flags"
102                 wrap_type    = "int"
103                 to_wrapper   = "%s.val"
104                 from_wrapper = "firm.bindings.binding_ircons.ir_cons_flags.getEnum(%s)"
105         elif type == "ir_where_alloc":
106                 java_type    = "firm.bindings.binding_ircons.ir_where_alloc"
107                 wrap_type    = "int"
108                 to_wrapper   = "%s.val"
109                 from_wrapper = "firm.bindings.binding_ircons.ir_where_alloc.getEnum(%s)"
110         elif type == "ir_entity*":
111                 java_type    = "firm.Entity"
112                 wrap_type    = "Pointer"
113                 to_wrapper   = "%s.ptr"
114                 from_wrapper = "new firm.Entity(%s)"
115         else:
116                 print "UNKNOWN TYPE"
117                 java_type    = "BAD"
118                 wrap_type    = "BAD"
119                 to_wrapper   = "BAD"
120                 from_wrapper = "BAD"
121         return (java_type,wrap_type,to_wrapper,from_wrapper)"""
122
123 def prepare_attr(nodename, attr):
124         (importcmd,exportcmd) = get_io_type(attr["type"], attr["name"], nodename)
125         attr["importcmd"] = importcmd
126         attr["exportcmd"] = exportcmd % {"val": "get_%s_%s(irn)" % (nodename, attr["name"])}
127
128 def preprocess_node(nodename, node):
129         if "is_a" in node:
130                 parent = ir_spec.nodes[node["is_a"]]
131                 node["ins"] = parent["ins"]
132                 if "outs" in parent:
133                         node["outs"] = parent["outs"]
134         if "ins" not in node:
135                 node["ins"] = []
136         if "outs" in node:
137                 node["mode"] = "mode_T"
138         if "arity" not in node:
139                 node["arity"] = len(node["ins"])
140         if "attrs" not in node:
141                 node["attrs"] = []
142         if "constructor_args" not in node:
143                 node["constructor_args"] = []
144
145         # construct node arguments
146         arguments = [ ]
147         i = 0
148         for input in node["ins"]:
149                 arguments.append("prednodes[%i]" % i)
150                 i += 1
151
152         # Special case for Builtin...
153         if nodename == "Builtin":
154                 for attr in node["attrs"]:
155                         if attr["name"] == "kind":
156                                 prepare_attr(nodename, attr)
157                                 arguments.append(attr["name"])
158
159         if node["arity"] == "variable" or node["arity"] == "dynamic":
160                 arguments.append("numpreds - %i" % (i + 1))
161                 arguments.append("prednodes + %i" % i)
162
163         if "mode" not in node:
164                 arguments.append("mode")
165
166         for attr in node["attrs"]:
167                 if nodename == "Builtin" and attr["name"] == "kind":
168                         continue
169                 prepare_attr(nodename, attr)
170                 arguments.append(attr["name"])
171
172         for arg in node["constructor_args"]:
173                 prepare_attr(nodename, arg)
174                 arguments.append(arg["name"])
175
176         node["arguments"] = arguments
177
178 export_attrs_template = env.from_string('''
179         case iro_{{nodename}}:
180                 {{"write_mode(env, get_irn_mode(irn));"|ifnset(node,"mode")}}
181                 {% for attr in node.attrs %}{{attr.exportcmd}}
182                 {% endfor %}
183                 {% for attr in node.constructor_args %}{{attr.exportcmd}}
184                 {% endfor %}break;''')
185
186 import_attrs_template = env.from_string('''
187         case iro_{{nodename}}:
188         {
189                 {{"ir_mode *mode = read_mode(env);"|ifnset(node,"mode")}}
190                 {% for attr in node.attrs %}{{attr.importcmd}}
191                 {% endfor %}
192                 {% for attr in node.constructor_args %}{{attr.importcmd}}
193                 {% endfor %}newnode = new_r_{{nodename}}(current_ir_graph{{node|block}}{{node["arguments"]|args}});
194                 break;
195         }
196 ''')
197
198 def main(argv):
199         """the main function"""
200
201         if len(argv) < 3:
202                 print "usage: %s specname(ignored) destdirectory" % argv[0]
203                 sys.exit(1)
204
205         gendir = argv[2]
206
207         file = open(gendir + "/gen_irio_export.inl", "w");
208         for nodename, node in ir_spec.nodes.iteritems():
209                 preprocess_node(nodename, node)
210                 if not "abstract" in node:
211                         file.write(export_attrs_template.render(vars()))
212         file.write("\n")
213         file.close()
214
215         file = open(gendir + "/gen_irio_import.inl", "w");
216         for nodename, node in ir_spec.nodes.iteritems():
217                 if not "abstract" in node and nodename != "Start" and nodename != "End" and nodename != "Anchor" and nodename != "SymConst" and nodename != "Block":
218                         file.write(import_attrs_template.render(vars()))
219         # TODO: SymConst
220         file.write("\n")
221         file.close()
222
223         file = open(gendir + "/gen_irio_lex.inl", "w");
224         for nodename, node in ir_spec.nodes.iteritems():
225                 if not "abstract" in node:
226                         file.write("\tINSERT(\"" + nodename + "\", tt_iro, iro_" + nodename + ");\n");
227         file.close()
228
229 if __name__ == "__main__":
230         main(sys.argv)