3 from jinja2 import Environment, Template
4 from jinja2.filters import do_dictsort
7 def format_args(arglist):
8 #argstrings = map(lambda arg : arg["name"], arglist)
9 #return ", ".join(argstrings)
10 s = ", ".join(arglist)
15 def format_ifnset(string, node, key):
20 def format_block(node):
21 if node.get("knownBlock"):
24 return ", get_node(env, preds[0])"
27 env.filters['args'] = format_args
28 env.filters['ifnset'] = format_ifnset
29 env.filters['block'] = format_block
31 def get_io_type(type, attrname, nodename):
33 importcmd = "tarval *%s = read_tv(env);" % attrname
34 exportcmd = "write_tarval(env, %(val)s);";
35 elif type == "ir_mode*":
36 importcmd = "ir_mode *%s = read_mode(env);" % attrname
37 exportcmd = "write_mode(env, %(val)s);"
38 elif type == "ir_entity*":
39 importcmd = "ir_entity *%s = read_entity(env);" % attrname
40 exportcmd = """fprintf(env->file, "%%ld ", get_entity_nr(%(val)s));"""
41 elif type == "ir_type*":
42 importcmd = "ir_type *%s = read_type(env);" % attrname
43 exportcmd = """fprintf(env->file, "%%ld ", get_type_nr(%(val)s));"""
44 elif type == "long" and nodename == "Proj":
45 importcmd = "long %s = read_long(env);" % attrname
46 exportcmd = """fprintf(env->file, "%%ld ", %(val)s);"""
47 elif type == "pn_Cmp" or type == "ir_where_alloc":
48 importcmd = "%s %s = (%s) read_long(env);" % (type, attrname, type)
49 exportcmd = """fprintf(env->file, "%%ld ", (long) %(val)s);"""
50 elif type == "ir_cons_flags" and nodename == "Store":
51 importcmd = "ir_cons_flags %s = get_cons_flags(env);" % attrname
52 exportcmd = """write_pin_state(env, irn);
53 write_volatility(env, irn);
54 write_align(env, irn);"""
55 elif type == "ir_cons_flags" and nodename == "Load":
56 importcmd = "ir_cons_flags %s = get_cons_flags(env);" % attrname
57 exportcmd = """write_pin_state(env, irn);
58 write_volatility(env, irn);
59 write_align(env, irn);"""
60 elif type == "op_pin_state":
61 importcmd = "op_pin_state %s = read_pin_state(env);" % attrname
62 exportcmd = "write_pin_state(env, irn);"
63 elif type == "ir_builtin_kind":
64 importcmd = "ir_builtin_kind %s = read_builtin_kind(env);" % attrname
65 exportcmd = "write_builtin_kind(env, irn);"
66 elif type == "cond_kind":
67 importcmd = "cond_kind %s = read_cond_kind(env);" % attrname
68 exportcmd = "write_cond_kind(env, irn);"
69 elif type == "cond_jmp_predicate":
70 importcmd = "cond_jmp_predicate %s = read_cond_jmp_predicate(env);" % attrname
71 exportcmd = "write_cond_jmp_predicate(env, irn);"
73 importcmd = "int %s = (int) read_long(env);" % attrname
74 exportcmd = """fprintf(env->file, "%%d ", %(val)s);"""
76 importcmd = "long %s = read_long(env);" % attrname
77 exportcmd = """fprintf(env->file, "%%ld ", %(val)s);"""
79 print "UNKNOWN TYPE: %s" % type
80 importcmd = """// BAD: %s %s
81 %s %s = (%s) 0;""" % (type, attrname, type, attrname, type)
82 exportcmd = "// BAD: %s" % type
83 return (importcmd, exportcmd)
85 """ if type == "ir_type*":
86 java_type = "firm.Type"
89 from_wrapper = "firm.Type.createWrapper(%s)"
90 elif type == "ir_mode*":
91 java_type = "firm.Mode"
94 from_wrapper = "new firm.Mode(%s)"
95 elif type == "tarval*":
96 java_type = "firm.TargetValue"
99 from_wrapper = "new firm.TargetValue(%s)"
100 elif type == "pn_Cmp":
107 wrap_type = "com.sun.jna.NativeLong"
108 to_wrapper = "new com.sun.jna.NativeLong(%s)"
109 from_wrapper = "%s.intValue()"
110 elif type == "cons_flags":
111 java_type = "firm.bindings.binding_ircons.ir_cons_flags"
113 to_wrapper = "%s.val"
114 from_wrapper = "firm.bindings.binding_ircons.ir_cons_flags.getEnum(%s)"
115 elif type == "ir_where_alloc":
116 java_type = "firm.bindings.binding_ircons.ir_where_alloc"
118 to_wrapper = "%s.val"
119 from_wrapper = "firm.bindings.binding_ircons.ir_where_alloc.getEnum(%s)"
120 elif type == "ir_entity*":
121 java_type = "firm.Entity"
122 wrap_type = "Pointer"
123 to_wrapper = "%s.ptr"
124 from_wrapper = "new firm.Entity(%s)"
131 return (java_type,wrap_type,to_wrapper,from_wrapper)"""
133 def prepare_attr(nodename, attr):
134 (importcmd,exportcmd) = get_io_type(attr["type"], attr["name"], nodename)
135 attr["importcmd"] = importcmd
136 attr["exportcmd"] = exportcmd % {"val": "get_%s_%s(irn)" % (nodename, attr["name"])}
138 def preprocess_node(nodename, node):
140 parent = ir_spec.nodes[node["is_a"]]
141 node["ins"] = parent["ins"]
143 node["outs"] = parent["outs"]
144 if "ins" not in node:
147 node["mode"] = "mode_T"
148 if "arity" not in node:
149 node["arity"] = len(node["ins"])
150 if "attrs" not in node:
152 if "constructor_args" not in node:
153 node["constructor_args"] = []
155 # construct node arguments
160 for input in node["ins"]:
161 arguments.append("prednodes[%i]" % i)
164 # Special case for Builtin...
165 if nodename == "Builtin":
166 for attr in node["attrs"]:
167 if attr["name"] == "kind":
168 prepare_attr(nodename, attr)
169 arguments.append(attr["name"])
171 if node["arity"] == "variable" or node["arity"] == "dynamic":
172 arguments.append("numpreds - %i" % (i + 1))
173 arguments.append("prednodes + %i" % i)
175 if "mode" not in node:
176 arguments.append("mode")
178 attrs_with_special = 0
179 for attr in node["attrs"]:
180 if nodename == "Builtin" and attr["name"] == "kind":
182 prepare_attr(nodename, attr)
183 if "special" in attr:
184 if not "init" in attr:
185 print "Node type %s has an attribute with a \"special\" entry but without \"init\"" % nodename
188 if attrs_with_special != 0:
189 print "Node type %s has more than one attribute with a \"special\" entry" % nodename
192 attrs_with_special += 1
194 if "prefix" in attr["special"]:
195 specialname = attr["special"]["prefix"] + nodename
196 elif "suffix" in attr["special"]:
197 specialname = nodename + attr["special"]["suffix"]
199 print "Unknown special constructor type for node type %s" %nodename
202 specialconstrs.append(
204 constrname = specialname,
205 attrname = attr["name"],
206 value = attr["special"]["init"]
210 if attr["type"] == "op_pin_state":
211 initfunc = "set_irn_pinned"
213 initfunc = "set_" + nodename + "_" + attr["name"]
214 initargs.append((attr["name"], initfunc))
216 arguments.append(attr["name"])
218 for arg in node["constructor_args"]:
219 prepare_attr(nodename, arg)
220 arguments.append(arg["name"])
222 node["arguments"] = arguments
223 node["initargs"] = initargs
224 node["special_constructors"] = specialconstrs
226 export_attrs_template = env.from_string('''
227 case iro_{{nodename}}:
228 {{"write_mode(env, get_irn_mode(irn));"|ifnset(node,"mode")}}
229 {% for attr in node.attrs %}{{attr.exportcmd}}
231 {% for attr in node.constructor_args %}{{attr.exportcmd}}
232 {% endfor %}break;''')
234 import_attrs_template = env.from_string('''
235 case iro_{{nodename}}:
237 {{"ir_mode *mode = read_mode(env);"|ifnset(node,"mode")}}
238 {% for attr in node.attrs %}{{attr.importcmd}}
240 {% for attr in node.constructor_args %}{{attr.importcmd}}
242 {% for special in node.special_constructors %}if({{special.attrname}} == {{special.value}})
243 newnode = new_r_{{special.constrname}}(current_ir_graph{{node|block}}{{node["arguments"]|args}});
245 newnode = new_r_{{nodename}}(current_ir_graph{{node|block}}{{node["arguments"]|args}});
246 {% for (initarg, initfunc) in node.initargs %}{{initfunc}}(newnode, {{initarg}});
253 """the main function"""
256 print "usage: %s specname(ignored) destdirectory" % argv[0]
261 sortednodes = do_dictsort(ir_spec.nodes)
263 file = open(gendir + "/gen_irio_export.inl", "w");
264 for nodename, node in sortednodes:
265 preprocess_node(nodename, node)
266 if not "abstract" in node:
267 file.write(export_attrs_template.render(vars()))
271 file = open(gendir + "/gen_irio_import.inl", "w");
272 for nodename, node in sortednodes:
273 if not "abstract" in node and nodename != "Start" and nodename != "End" and nodename != "Anchor" and nodename != "SymConst" and nodename != "Block":
274 file.write(import_attrs_template.render(vars()))
279 file = open(gendir + "/gen_irio_lex.inl", "w");
280 for nodename, node in sortednodes:
281 if not "abstract" in node:
282 file.write("\tINSERT(\"" + nodename + "\", tt_iro, iro_" + nodename + ");\n");
285 if __name__ == "__main__":