summaryrefslogtreecommitdiffhomepage
path: root/scripts/irops.py
blob: 8d65ad7342dbd4b7ad0aec2e914712bc2c144d69 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
from jinjautil import export_filter, export
from jinja2._compat import string_types
from filters import arguments
import imp
import sys


class Node(object):
    '''Node base class'''
    only_regular = False
    pinned = "no"
    flags = []
    ins = []
    attrs = []
    constructor = True
    constructor_args = []
    block = None
    mode = None
    pinned_init = None
    throws_init = None
    arity_override = None


abstracts = set()


def abstract(cls):
    abstracts.add(cls)
    return cls


def is_abstract(nodetype):
    return nodetype in abstracts


def op(cls):
    cls.__is_firm_op = True
    # Without new-style classes it is hard to detect the inheritance hierarchy
    # later.
    assert hasattr(cls, "__class__"), "must use new-style classes"
    return cls


def is_op(nodetype):
    return hasattr(nodetype, "__is_firm_op")


class Attribute(object):
    def __init__(self, name, type, comment="", init=None, to_flags=None,
                 noprop=False, fqname=None):
        self.type = type
        self.name = name
        self.comment = comment
        self.init = init
        self.to_flags = to_flags
        self.noprop = noprop
        if fqname is None:
            fqname = name
        self.fqname = fqname


class Operand(object):
    pass


def Input(name, comment=None):
    op = Operand()
    op.name = name
    op.comment = comment
    return op


def Output(name, comment=None):
    op = Operand()
    op.name = name
    op.comment = comment
    return op


def verify_node(node):
    if node.pinned not in ["yes", "no", "exception"]:
        print("%s: UNKNOWN PINNED MODE: %s" % (node.name, node.pinned))

    if node.pinned_init is not None and not is_dynamic_pinned(node):
        print("ERROR: node %s has pinned_init attribute but is not marked as dynamically pinned" % node.name)
    if "uses_memory" in node.flags:
        if not inout_contains(node.ins, "mem"):
            print("ERROR: memory op %s needs an input named 'mem'" %
                  node.name)
    if "fragile" in node.flags:
        if not is_dynamic_pinned(node):
            print("ERROR: fragile node %s must be dynamically pinned" %
                  node.name)
        if node.throws_init is None:
            print("ERROR: fragile node %s needs a throws_init attribute" %
                  node.name)
        if not inout_contains(node.outs, "X_regular"):
            print("ERROR: fragile node %s needs an output named 'X_regular'" %
                  node.name)
        if not inout_contains(node.outs, "X_except") and not node.only_regular:
            print("ERROR: fragile node %s needs an output named 'X_except'" %
                  node.name)
    else:
        if node.throws_init is not None:
            print("ERROR: throws_init only makes sense for fragile nodes")


def setldefault(node, attr, val):
    # Don't use hasattr, as these things should not be inherited
    if attr not in node.__dict__:
        setattr(node, attr, val)


def setdefault(node, attr, val):
    if not hasattr(node, attr):
        setattr(node, attr, val)


def setnodedefaults(node):
    setldefault(node, "name", node.__name__)

    # As a shortcut you can specify inputs either as a list of strings or
    # as a list of (name, comment) tuples. Normalize it to Input objects
    new_ins = []
    for i in node.ins:
        if isinstance(i, string_types):
            i = Input(i)
        elif isinstance(i, tuple):
            i = Input(name=i[0], comment=i[1])
        new_ins.append(i)
    node.ins = new_ins
    if hasattr(node, "outs"):
        new_outs = []
        for o in node.outs:
            if isinstance(o, string_types):
                o = Output(o)
            elif isinstance(o, tuple):
                o = Output(name=o[0], comment=o[1])
            new_outs.append(o)
        node.outs = new_outs

    if hasattr(node, "__doc__"):
        node.doc = trim_docstring(node.__doc__)
    else:
        node.doc = ""

    if not is_abstract(node):
        setdefault(node, "arity", len(node.ins))
        setdefault(node, "attrs_name", node.name.lower())
        setdefault(node, "serializer", node.constructor)
        if hasattr(node, "outs") and len(node.outs) > 1:
            node.mode = "mode_T"
        if "start_block" in node.flags:
            node.block = "get_irg_start_block(irg)"
        setdefault(node, "usesGraph", node.block is not None)


def trim_docstring(docstring):
    if not docstring:
        return ''
    # Convert tabs to spaces (following the normal Python rules)
    # and split into a list of lines:
    lines = docstring.expandtabs().splitlines()
    # Determine minimum indentation (first line doesn't count):
    indent = sys.maxsize
    for line in lines[1:]:
        stripped = line.lstrip()
        if stripped:
            indent = min(indent, len(line) - len(stripped))
    # Remove indentation (first line is special):
    trimmed = [lines[0].strip()]
    if indent < sys.maxsize:
        for line in lines[1:]:
            trimmed.append(line[indent:].rstrip())
    # Strip off trailing and leading blank lines:
    while trimmed and not trimmed[-1]:
        trimmed.pop()
    while trimmed and not trimmed[0]:
        trimmed.pop(0)
    # Return a single string:
    return '\n'.join(trimmed)


def parameterlist(parameterlist):
    return "\n".join(parameterlist)


def nodearguments(node):
    arguments = [arg.name for arg in node.arguments]
    return parameterlist(arguments)


def nodeparameters(node):
    parameters = ["%s %s" % (arg.type, arg.name) for arg in node.arguments]
    return parameterlist(parameters)


def nodeparametershelp(node):
    res = ""
    for param in node.arguments:
        res += " * @param %-9s %s\n" % (param.name, param.comment)
    return res


def a_an(text):
    if text[0] in "aAeEuUoOiI":
        return "an " + text
    return "a " + text


def blockparameter(node):
    if not node.block:
        return "ir_node *block"
    elif node.usesGraph:
        return "ir_graph *irg"
    else:
        return ""


def blockparameterhelp(node):
    if not node.block:
        return " * @param block     The IR block the node belongs to.\n"
    elif node.usesGraph:
        return " * @param irg       The IR graph the node belongs to.\n"
    else:
        return ""


def blockargument(node):
    if not node.block:
        return "block"
    elif node.usesGraph:
        return "irg"
    else:
        return ""


def blockassign(node):
    if node.block:
        return "ir_node *block = %s;" % node.block
    else:
        return ""


def irgassign(node):
    if node.usesGraph:
        return ""
    else:
        return "ir_graph *irg = get_irn_irg(block);\n"


def curblock(node):
    if not node.block:
        return "get_cur_block()"
    elif node.usesGraph:
        return "current_ir_graph"
    else:
        return ""


def insdecl(node):
    arity = node.arity
    if arity == "dynamic" or arity == "variable":
        if len(node.ins) == 0:
            return ""
        insarity = len(node.ins)
        res = "int r_arity = arity + " + repr(insarity) + ";"
        res += "\n\tir_node **r_in= ALLOCAN(ir_node*, r_arity);"
        i = 0
        for input in node.ins:
            res += "\n\tr_in[" + repr(i) + "] = irn_" + input.name + ";"
            i += 1
        res += "\n\tMEMCPY(&r_in[" + repr(insarity) + "], in, arity);\n\t"
    elif arity == 0:
        return ""
    else:
        res = "ir_node *in[" + repr(arity) + "];"
        i = 0
        for input in node.ins:
            res += "\n\tin[" + repr(i) + "] = irn_" + input.name + ";"
            i += 1
    return res


def arity_and_ins(node):
    arity = node.arity
    if arity == "dynamic" or arity == "variable":
        if len(node.ins) == 0:
            return "arity, in"
        else:
            return "r_arity, r_in"
    elif arity == 0:
        return "0, NULL"
    else:
        return repr(arity) + ", in"


def arity(node):
    if node.arity_override is not None:
        return node.arity_override
    arity = node.arity
    if arity == "dynamic":
        return "oparity_dynamic"
    return "oparity_any"


def pinned(node):
    pinned = node.pinned
    if pinned == "yes":
        return "op_pin_state_pinned"
    if pinned == "no":
        return "op_pin_state_floats"
    if pinned == "exception":
        return "op_pin_state_exc_pinned"
    print("WARNING: Unknown pinned state %s in format pined" % pinned)
    return ""


def flags(node):
    flags = list(map(lambda x: "irop_flag_" + x, node.flags))
    if not flags:
        flags = ["irop_flag_none"]
    return " | ".join(flags)


def stringformat(string, *args):
    return string % args


def attr_size(node):
    if not hasattr(node, "attr_struct"):
        return "0"
    return "sizeof(%s)" % node.attr_struct


def opindex(node):
    if hasattr(node, "op_index"):
        return node.op_index
    return "-1"


keywords = frozenset(["true", "false"])


def escape_keywords(word):
    if word in keywords:
        return word + "_"
    return word


def parameters(string):
    return arguments(string, voidwhenempty=True)


def args(arglist):
    argument_names = [arg.name for arg in arglist]
    return "\n".join(argument_names)


def block(node):
    if not node.block:
        return "block"
    elif node.usesGraph:
        return "env->irg"
    else:
        return ""


def 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


for f in [a_an, args, arity_and_ins, arity, attr_size, blockargument, block,
          blockparameter, blockparameterhelp, curblock, escape_keywords,
          flags, insdecl, blockassign, irgassign, nodearguments,
          nodeparameters, nodeparametershelp, opindex, parameterlist,
          parameters, pinned, simplify_type, stringformat]:
    export_filter(f)


def _preprocess_node(node):
    # construct node arguments
    arguments = []
    initattrs = []
    for input in node.ins:
        arguments.append(
            Attribute("irn_" + input.name, type="ir_node *",
                      comment=input.name))

    if node.arity == "variable" or node.arity == "dynamic":
        arguments.append(
            Attribute("arity", type="int",
                      comment="size of additional inputs array"))
        arguments.append(
            Attribute("in", type="ir_node *const *",
                      comment="additional inputs"))

    if node.mode is None:
        arguments.append(
            Attribute("mode", type="ir_mode *",
                      comment="mode of the operations result"))

    for attr in node.attrs:
        if attr.init is not None:
            continue
        arguments.append(attr)

    # dynamic pin state means more constructor arguments
    if is_dynamic_pinned(node):
        if node.pinned_init is not None:
            initattrs.append(
                Attribute("pinned", fqname="exc.pinned",
                          type="int", init=node.pinned_init))
        else:
            arguments.append(
                Attribute("pinned", type="int", comment="pinned state"))
            initattrs.append(
                Attribute("pin_state", fqname="exc.pinned",
                          type="int", init="pinned"))
    if node.throws_init is not None:
        initattrs.append(
            Attribute("throws_exception", fqname="exc.throws_exception",
                      type="unsigned", init=node.throws_init))

    for arg in node.constructor_args:
        arguments.append(arg)

    node.arguments = arguments
    node.initattrs = initattrs


def prepare_nodes(namespace):
    nodes = []
    for x in namespace.values():
        if not is_op(x):
            continue
        setnodedefaults(x)
        verify_node(x)
        nodes.append(x)
    nodes.sort(key=lambda x: x.name)
    if len(nodes) == 0:
        print("Warning: No nodes found in spec file '%s'" % filename)

    real_nodes = []
    abstract_nodes = []
    for node in nodes:
        if is_abstract(node):
            abstract_nodes.append(node)
        else:
            real_nodes.append(node)
            _preprocess_node(node)

    return (real_nodes, abstract_nodes)


def is_dynamic_pinned(node):
    return node.pinned == "exception"


def is_fragile(node):
    return "fragile" in node.flags


def inout_contains(l, name):
    for entry in l:
        if entry.name == name:
            return True
    return False


def collect_ops(moduledict):
    return [node for node in moduledict.values() if is_op(node)]


def verify_spec(spec):
    if len(spec.nodes) == 0:
        sys.stderr.write("Warning: No nodes found in spec\n")
    if not hasattr(spec, "name"):
        sys.stderr.write("Warning: No name specified in node spec\n")


export(is_dynamic_pinned)
export(is_abstract)