gen_ir_io.py 2.97 KB
Newer Older
Christoph Mallon's avatar
Christoph Mallon committed
1
#!/usr/bin/env python
2
3
4
#
# This file is part of libFirm.
# Copyright (C) 2012 Karlsruhe Institute of Technology.
5
import sys
Matthias Braun's avatar
Matthias Braun committed
6
from jinja2 import Environment, Template, FileSystemLoader
Matthias Braun's avatar
Matthias Braun committed
7
8
from spec_util import is_dynamic_pinned, isAbstract, load_spec
from filters import format_arguments, filter_isnot, filter_hasnot, filter_notset
9

10
11
12
13
14
15
16
17
def error(msg):
	"""writes an error message to stderr"""
	sys.stderr.write("Error: " + msg + "\n");

def warning(msg):
	"""writes a warning message to stderr"""
	sys.stderr.write("Warning: " + msg + "\n");

18
def format_args(arglist):
19
	return "\n".join(arglist)
20
21

def format_block(node):
22
	if hasattr(node, "knownBlock"):
23
24
		if hasattr(node, "knownGraph"):
			return ""
Matthias Braun's avatar
Matthias Braun committed
25
		return "env->irg"
26
	else:
Matthias Braun's avatar
Matthias Braun committed
27
		return "block"
28

29
30
31
32
33
34
35
36
def format_simplify_type(string):
	"""Returns a simplified version of a C type for use in a function name.
	Stars are replaced with _ref, spaces removed and the ir_ firm namespace
	prefix stripped."""
	res = string.replace("*", "_ref").replace(" ", "")
	if res.startswith("ir_"):
		res = res[3:]
	return res
37

38
39
40
41
42
43
44
45
env = Environment(loader=FileSystemLoader([".", "/"]), keep_trailing_newline=True)
env.filters['args']          = format_args
env.filters['block']         = format_block
env.filters['arguments']     = format_arguments
env.filters['simplify_type'] = format_simplify_type
env.filters['isnot']         = filter_isnot
env.filters['notset']        = filter_notset
env.filters['hasnot']        = filter_hasnot
46
47

def preprocess_node(node):
Matthias Braun's avatar
Matthias Braun committed
48
49
50
	if node.customSerializer:
		return

51
52
	# construct node arguments
	arguments = [ ]
Matthias Braun's avatar
Matthias Braun committed
53
	extraattrs = [ ]
54
	for input in node.ins:
Matthias Braun's avatar
Matthias Braun committed
55
		arguments.append("in_%s" % input[0])
56

57
	if node.arity == "variable" or node.arity == "dynamic":
Matthias Braun's avatar
Matthias Braun committed
58
59
		arguments.append("n_preds")
		arguments.append("preds")
60

61
	if not hasattr(node, "mode"):
62
63
		arguments.append("mode")

64
	for attr in node.attrs:
Matthias Braun's avatar
Matthias Braun committed
65
66
67
68
69
		if "to_flags" in attr:
			node.constructorFlags = True
			attr['to_flags'] = attr['to_flags'] % (attr["name"])
		elif "init" in attr:
			extraattrs.append(attr)
70
71
		else:
			arguments.append(attr["name"])
72

73
	for arg in node.constructor_args:
Matthias Braun's avatar
Matthias Braun committed
74
75
76
77
78
79
80
81
82
83
		if arg['type'] != "ir_cons_flags" and arg['name'] != "flags":
			error("only ir_cons_flags constructor arg supported in irio")
			continue
		node.constructorFlags = True
		arguments.append("flags")

	node.arguments  = arguments
	node.extraattrs = extraattrs
	node.dynamic_pinned = is_dynamic_pinned(node)

84
def main(argv):
Moritz Kroll's avatar
Moritz Kroll committed
85
	if len(argv) < 3:
86
		print("usage: %s specname(ignored) destdirectory" % argv[0])
87
88
		sys.exit(1)

Matthias Braun's avatar
Matthias Braun committed
89
90
	specfile     = argv[1]
	templatefile = argv[2]
91

Matthias Braun's avatar
Matthias Braun committed
92
	spec  = load_spec(specfile)
Matthias Braun's avatar
Matthias Braun committed
93
	nodes = spec.nodes
94
	real_nodes = []
Matthias Braun's avatar
Matthias Braun committed
95
	for node in nodes:
96
97
98
		if isAbstract(node):
			continue
		preprocess_node(node)
Matthias Braun's avatar
Matthias Braun committed
99
		real_nodes.append(node)
100

Matthias Braun's avatar
Matthias Braun committed
101
102
103
104
105
106
	env.globals['nodes']   = real_nodes
	env.globals['spec']    = spec
	env.globals['warning'] = "/* Warning: automatically generated file */"
	env.globals['hasattr'] = hasattr

	template = env.get_template(templatefile)
107
108
	result = template.render()
	sys.stdout.write(result)
109

Matthias Braun's avatar
Matthias Braun committed
110
111
if __name__ == "__main__":
	main(sys.argv)