ir_spec.py 26.7 KB
Newer Older
1
2
3
from spec_util import abstract, setnodedefaults

class Op(object):
4
	"""Base class for firm nodes"""
5
6
7
abstract(Op)

class Unop(Op):
8
	"""Unary nodes have exactly 1 input"""
9
	name     = "unop"
10
11
12
	ins      = [
		("op",  "operand"),
	]
13
14
15
16
17
	op_index = 0
	pinned   = "no"
abstract(Unop)

class Binop(Op):
18
	"""Binary nodes have exactly 2 inputs"""
19
	name     = "binop"
20
21
22
23
	ins      = [
		( "left",   "first operand" ),
		( "right", "second operand" ),
	]
24
25
26
27
28
	op_index = 0
	pinned   = "no"
abstract(Binop)

class Add(Binop):
29
	"""returns the sum of its operands"""
30
	flags = [ "commutative" ]
31
32

class Alloc(Op):
33
34
35
	"""allocates a block of memory.
	It can be specified whether the variable should be allocated to the stack
	or to the heap."""
36
37
38
39
	ins   = [
		("mem",   "memory dependency" ),
		("count", "number of objects to allocate" ),
	]
40
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
41
		("M",         "memory result"),
42
		("res",       "pointer to newly allocated memory"),
Matthias Braun's avatar
Matthias Braun committed
43
44
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
45
	]
46
47
	attrs = [
		dict(
48
49
50
			name    = "type",
			type    = "ir_type*",
			comment = "type of the allocated variable",
51
52
		),
		dict(
53
54
55
			name    = "where",
			type    = "ir_where_alloc",
			comment = "whether to allocate the variable on the stack or heap",
56
		)
57
	]
58
	flags       = [ "fragile", "uses_memory" ]
59
	pinned      = "exception"
60
	throws_init = "false"
61
	pinned_init = "op_pin_state_pinned"
62
63
64
	attr_struct = "alloc_attr"

class Anchor(Op):
65
66
67
68
69
70
	"""utiliy node used to "hold" nodes in a graph that might possibly not be
	reachable by other means or which should be reachable immediately without
	searching through the graph.
	Each firm-graph contains exactly one anchor node whose address is always
	known. All other well-known graph-nodes like Start, End, NoMem, Bad, ...
	are found by looking at the respective Anchor operand."""
71
72
73
74
75
76
77
78
79
	mode             = "mode_ANY"
	arity            = "variable"
	flags            = [ "dump_noblock" ]
	pinned           = "yes"
	attr_struct      = "irg_attr"
	knownBlock       = True
	singleton        = True
	noconstructor    = True
	customSerializer = True
80
81

class And(Binop):
82
	"""returns the result of a bitwise and operation of its operands"""
83
84
85
	flags    = [ "commutative" ]

class ASM(Op):
86
	"""executes assembler fragments of the target machine"""
87
88
89
90
91
92
93
94
	mode             = "mode_T"
	arity            = "variable"
	flags            = [ "keep", "uses_memory" ]
	pinned           = "memory"
	pinned_init      = "op_pin_state_pinned"
	attr_struct      = "asm_attr"
	attrs_name       = "assem"
	customSerializer = True
95
96
97
	ins   = [
		("mem",    "memory dependency"),
	]
98
99
	attrs = [
		dict(
100
101
102
			name    = "input_constraints",
			type    = "ir_asm_constraint*",
			comment = "input constraints",
103
104
		),
		dict(
105
			name    = "n_output_constraints",
106
			type    = "size_t",
107
108
			noprop  = True,
			comment = "number of output constraints",
109
110
		),
		dict(
111
112
113
			name    = "output_constraints",
			type    = "ir_asm_constraint*",
			comment = "output constraints",
114
115
		),
		dict(
116
			name    = "n_clobbers",
117
			type    = "size_t",
118
119
			noprop  = True,
			comment = "number of clobbered registers/memory",
120
121
		),
		dict(
122
123
124
			name    = "clobbers",
			type    = "ident**",
			comment = "list of clobbered registers/memory",
125
126
		),
		dict(
127
128
129
			name    = "text",
			type    = "ident*",
			comment = "assembler text",
130
131
132
133
134
		),
	]
	# constructor is written manually at the moment, because of the clobbers+
	# constraints arrays needing special handling (2 arguments for 1 attribute)
	noconstructor = True
135
136

class Bad(Op):
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	"""Bad nodes indicate invalid input, which is values which should never be
	computed.

	The typical use case for the Bad node is removing unreachable code.
	Frontends should set the current_block to Bad when it is clear that
	following code must be unreachable (ie. after a goto or return statement).
	Optimisations also set block predecessors to Bad when it becomes clear,
	that a control flow edge can never be executed.

	The gigo optimisations ensures that nodes with Bad as their block, get
	replaced by Bad themselfes. Nodes with at least 1 Bad input get exchanged
	with Bad too. Exception to this rule are Block, Phi, Tuple and End node;
	This is because removing inputs from a Block is hairy operation (requiring,
	Phis to be shortened too for example). So instead of removing block inputs
	they are set to Bad, and the actual removal is left to the control flow
	optimisation phase. Block, Phi, Tuple with only Bad inputs however are
	replaced by Bad right away."""
Matthias Braun's avatar
Matthias Braun committed
154
	flags         = [ "start_block", "dump_noblock" ]
155
156
	pinned        = "yes"
	knownBlock    = True
157
	block         = "get_irg_start_block(irg)"
158
	attr_struct   = "bad_attr"
159
	init = '''
160
	res->attr.bad.irg.irg = irg;
161
162
	'''

163
class Deleted(Op):
164
165
	"""Internal node which is temporary set to nodes which are already removed
	from the graph."""
166
167
168
169
	mode             = "mode_Bad"
	flags            = [ ]
	pinned           = "yes"
	noconstructor    = True
Matthias Braun's avatar
Matthias Braun committed
170
	customSerializer = True # this has no serializer
171

172
class Block(Op):
173
	"""A basic block"""
174
175
176
177
178
179
180
	mode             = "mode_BB"
	knownBlock       = True
	block            = "NULL"
	pinned           = "yes"
	arity            = "variable"
	flags            = [ "labeled" ]
	attr_struct      = "block_attr"
181
182
183
184
185
186
187
188
	attrs            = [
		dict(
			name    = "entity",
			type    = "ir_entity*",
			comment = "entity representing this block",
			init    = "NULL",
		),
	]
189
	customSerializer = True
Moritz Kroll's avatar
Moritz Kroll committed
190
191

	init = '''
192
	res->attr.block.irg.irg     = irg;
Moritz Kroll's avatar
Moritz Kroll committed
193
194
	res->attr.block.backedge    = new_backedge_arr(irg->obst, arity);
	set_Block_matured(res, 1);
195
196

	/* Create and initialize array for Phi-node construction. */
197
198
199
	if (get_irg_phase_state(irg) == phase_building) {
		res->attr.block.graph_arr = NEW_ARR_D(ir_node *, irg->obst, irg->n_loc);
		memset(res->attr.block.graph_arr, 0, irg->n_loc * sizeof(ir_node*));
200
	}
201
	'''
202

203
class Borrow(Binop):
204
	"""Returns the borrow bit from and implied subtractions of its 2 operands"""
205
206
	flags = []

207
class Bound(Op):
208
209
	"""Performs a bounds-check: if lower <= index < upper then return index,
	otherwise throw an exception."""
210
211
212
213
214
215
	ins    = [
		("mem",    "memory dependency"),
		("index",  "value to test"),
		("lower",  "lower bound (inclusive)"),
		("upper",  "upper bound (exclusive)"),
	]
216
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
217
		("M",         "memory result"),
218
		("res",       "the checked index"),
Matthias Braun's avatar
Matthias Braun committed
219
220
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
221
	]
222
223
224
 	flags  = [ "fragile", "highlevel" ]
	pinned = "exception"
	pinned_init = "op_pin_state_pinned"
225
	throws_init = "false"
226
227
228
	attr_struct = "bound_attr"

class Builtin(Op):
229
	"""performs a backend-specific builtin."""
230
231
232
	ins      = [
		("mem", "memory dependency"),
	]
233
	arity    = "variable"
234
	outs     = [
235
236
		("M", "memory result"),
		# results follow here
237
	]
238
	flags    = [ "uses_memory" ]
239
	attrs    = [
240
		dict(
241
242
243
			type    = "ir_builtin_kind",
			name    = "kind",
			comment = "kind of builtin",
244
		),
245
		dict(
246
247
248
			type    = "ir_type*",
			name    = "type",
			comment = "method type for the builtin call",
249
		)
250
251
252
253
	]
	pinned      = "memory"
	pinned_init = "op_pin_state_pinned"
	attr_struct = "builtin_attr"
254
	init   = '''
255
256
257
	assert((get_unknown_type() == type) || is_Method_type(type));
	'''

258
class Call(Op):
259
260
261
262
	"""Calls other code. Control flow is transfered to ptr, additional
	operands are passed to the called code. Called code usually performs a
	return operation. The operands of this return operation are the result
	of the Call node."""
263
264
265
266
	ins      = [
		("mem",   "memory dependency"),
		("ptr",   "pointer to called code"),
	]
267
	arity    = "variable"
268
	outs     = [
Matthias Braun's avatar
Matthias Braun committed
269
		("M",                "memory result"),
270
		("T_result",         "tuple containing all results"),
Matthias Braun's avatar
Matthias Braun committed
271
272
		("X_regular",        "control flow when no exception occurs"),
		("X_except",         "control flow when exception occured"),
273
	]
274
	flags    = [ "fragile", "uses_memory" ]
275
	attrs    = [
276
		dict(
277
278
279
			type    = "ir_type*",
			name    = "type",
			comment = "type of the call (usually type of the called procedure)",
280
		),
281
282
283
284
	]
	attr_struct = "call_attr"
	pinned      = "memory"
	pinned_init = "op_pin_state_pinned"
285
	throws_init = "false"
286
287
	init = '''
	assert((get_unknown_type() == type) || is_Method_type(type));
288
	'''
289

290
class Carry(Binop):
291
292
	"""Computes the value of the carry-bit that would result when adding the 2
	operands"""
293
294
	flags = [ "commutative" ]

Michael Beck's avatar
Michael Beck committed
295
class Cast(Unop):
296
	"""perform a high-level type cast"""
297
298
	mode     = "get_irn_mode(irn_op)"
	flags    = [ "highlevel" ]
299
300
	attrs    = [
		dict(
301
302
303
			type    = "ir_type*",
			name    = "type",
			comment = "target type of the case",
304
		)
305
306
	]
	attr_struct = "cast_attr"
307
	init     = "assert(is_atomic_type(type));"
308
309

class Cmp(Binop):
310
	"""Compares its two operands and checks whether a specified
311
	   relation (like less or equal) is fulfilled."""
312
	flags = []
313
314
315
316
317
318
319
320
321
	mode  = "mode_b"
	attrs = [
		dict(
			type    = "ir_relation",
			name    = "relation",
			comment = "Comparison relation"
		)
	]
	attr_struct = "cmp_attr"
322
323

class Cond(Op):
324
	"""Conditionally change control flow."""
325
326
327
	ins      = [
		("selector",  "condition parameter"),
	]
328
329
330
331
	outs     = [
		("false", "control flow if operand is \"false\""),
		("true",  "control flow if operand is \"true\""),
	]
332
333
	flags    = [ "cfopcode", "forking" ]
	pinned   = "yes"
334
335
	attrs    = [
		dict(
336
337
338
339
			name    = "jmp_pred",
			type    = "cond_jmp_predicate",
			init    = "COND_JMP_PRED_NONE",
			comment = "can indicate the most likely jump",
Matthias Braun's avatar
Matthias Braun committed
340
		),
341
	]
342
	attr_struct = "cond_attr"
343

Matthias Braun's avatar
Matthias Braun committed
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
class Switch(Op):
	"""Change control flow. The destination is choosen based on an integer input value which is looked up in a table.

	Backends can implement this efficiently using a jump table."""
	ins    = [
		("selector", "input selector"),
	]
	outs   = [
		("default", "control flow if no other case matches"),
	]
	flags  = [ "cfopcode", "forking" ]
	pinned = "yes"
	attrs  = [
		dict(
			name    = "n_outs",
			type    = "unsigned",
			comment = "number of outputs (including pn_Switch_default)",
		),
		dict(
			name    = "table",
			type    = "ir_switch_table*",
			comment = "table describing mapping from input values to Proj numbers",
		),
	]
	attr_struct = "switch_attr"
	attrs_name  = "switcha"

371
class Confirm(Op):
372
373
374
375
376
377
378
379
380
	"""Specifies constraints for a value. This allows explicit representation
	of path-sensitive properties. (Example: This value is always >= 0 on 1
	if-branch then all users within that branch are rerouted to a confirm-node
	specifying this property).

	A constraint is specified for the relation between value and bound.
	value is always returned.
	Note that this node does NOT check or assert the constraint, it merely
	specifies it."""
381
382
383
384
	ins      = [
		("value",  "value to express a constraint for"),
		("bound",  "value to compare against"),
	]
385
386
387
	mode     = "get_irn_mode(irn_value)"
	flags    = [ "highlevel" ]
	pinned   = "yes"
388
389
	attrs    = [
		dict(
390
391
392
			name    = "relation",
			type    = "ir_relation",
			comment = "relation of value to bound",
393
		),
394
395
396
397
	]
	attr_struct = "confirm_attr"

class Const(Op):
398
	"""Returns a constant value."""
399
	flags      = [ "constlike", "start_block" ]
400
401
	block      = "get_irg_start_block(irg)"
	mode       = "get_tarval_mode(tarval)"
402
403
	knownBlock = True
	pinned     = "no"
404
405
	attrs      = [
		dict(
406
407
408
			type    = "ir_tarval*",
			name    = "tarval",
			comment = "constant value (a tarval object)",
409
		)
410
411
	]
	attr_struct = "const_attr"
412
	attrs_name  = "con"
413

414
class Conv(Unop):
415
	"""Converts values between modes"""
416
	flags = []
417
418
	attrs = [
		dict(
419
420
421
422
			name    = "strict",
			type    = "int",
			init    = "0",
			comment = "force floating point to restrict precision even if backend computes in higher precision (deprecated)",
423
		)
424
425
426
427
	]
	attr_struct = "conv_attr"

class CopyB(Op):
428
	"""Copies a block of memory"""
429
430
431
432
433
	ins   = [
		("mem",  "memory dependency"),
		("dst",  "destination address"),
		("src",  "source address"),
	]
434
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
435
436
437
		("M",         "memory result"),
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
438
	]
439
	flags = [ "fragile", "uses_memory" ]
440
441
	attrs = [
		dict(
442
443
444
			name    = "type",
			type    = "ir_type*",
			comment = "type of copied data",
445
		)
446
447
448
449
	]
	attr_struct = "copyb_attr"
	pinned      = "memory"
	pinned_init = "op_pin_state_pinned"
450
	throws_init = "false"
451

452
class Div(Op):
453
	"""returns the quotient of its 2 operands"""
454
455
456
457
458
	ins   = [
		("mem",   "memory dependency"),
		("left",  "first operand"),
		("right", "second operand"),
	]
459
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
460
		("M",         "memory result"),
461
		("res",       "result of computation"),
Matthias Braun's avatar
Matthias Braun committed
462
463
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
464
	]
465
	flags = [ "fragile", "uses_memory" ]
466
467
	attrs = [
		dict(
468
469
470
			type    = "ir_mode*",
			name    = "resmode",
			comment = "mode of the result value",
471
		),
Moritz Kroll's avatar
Moritz Kroll committed
472
473
474
475
		dict(
			name = "no_remainder",
			type = "int",
			init = "0",
476
		)
477
	]
478
	attr_struct = "div_attr"
479
	pinned      = "exception"
480
	throws_init = "false"
481
482
	op_index    = 1
	arity_override = "oparity_binary"
483

484
class Dummy(Op):
485
486
487
	"""A placeholder value. This is used when constructing cyclic graphs where
	you have cases where not all predecessors of a phi-node are known. Dummy
	nodes are used for the unknown predecessors and replaced later."""
488
489
	ins        = []
	flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
490
491
492
493
494
	knownBlock = True
	pinned     = "yes"
	block      = "get_irg_start_block(irg)"

class End(Op):
495
496
	"""Last node of a graph. It references nodes in endless loops (so called
	keepalive edges)"""
497
498
499
500
501
502
503
	mode             = "mode_X"
	pinned           = "yes"
	arity            = "dynamic"
	flags            = [ "cfopcode" ]
	knownBlock       = True
	block            = "get_irg_end_block(irg)"
	singleton        = True
504
505

class Eor(Binop):
506
	"""returns the result of a bitwise exclusive or operation of its operands"""
507
	flags    = [ "commutative" ]
508

509
class Free(Op):
510
	"""Frees a block of memory previously allocated by an Alloc node"""
511
512
513
	ins    = [
		("mem",   "memory dependency" ),
		("ptr",   "pointer to the object to free"),
514
		("count", "number of objects to allocate" ),
515
	]
516
517
518
	mode   = "mode_M"
	flags  = [ "uses_memory" ]
	pinned = "yes"
519
	attrs  = [
520
		dict(
521
522
523
			name    = "type",
			type    = "ir_type*",
			comment = "type of the allocated variable",
524
525
		),
		dict(
526
527
528
			name    = "where",
			type    = "ir_where_alloc",
			comment = "whether allocation was on the stack or heap",
529
		)
530
531
532
533
	]
	attr_struct = "free_attr"

class Id(Op):
534
	"""Returns its operand unchanged."""
535
536
537
	ins    = [
	   ("pred", "the value which is returned unchanged")
	]
538
539
540
541
	pinned = "no"
	flags  = []

class IJmp(Op):
542
543
544
	"""Jumps to the code in its argument. The code has to be in the same
	function and the the destination must be one of the blocks reachable
	by the tuple results"""
545
546
	mode     = "mode_X"
	pinned   = "yes"
547
548
549
	ins      = [
	   ("target", "target address of the jump"),
	]
550
	flags    = [ "cfopcode", "forking", "keep", "unknown_jump" ]
551
552

class InstOf(Op):
553
	"""Tests whether an object is an instance of a class-type"""
554
555
556
557
	ins   = [
	   ("store", "memory dependency"),
	   ("obj",   "pointer to object being queried")
	]
558
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
559
		("M",         "memory result"),
560
		("res",       "checked object pointer"),
Matthias Braun's avatar
Matthias Braun committed
561
562
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
563
	]
564
	flags = [ "highlevel" ]
565
566
	attrs = [
		dict(
567
568
569
			name    = "type",
			type    = "ir_type*",
			comment = "type to check ptr for",
570
		)
571
572
573
574
575
576
	]
	attr_struct = "io_attr"
	pinned      = "memory"
	pinned_init = "op_pin_state_floats"

class Jmp(Op):
577
	"""Jumps to the block connected through the out-value"""
578
579
580
581
582
583
	mode     = "mode_X"
	pinned   = "yes"
	ins      = []
	flags    = [ "cfopcode" ]

class Load(Op):
584
	"""Loads a value from memory (heap or stack)."""
585
586
587
588
	ins   = [
		("mem", "memory dependency"),
		("ptr",  "address to load from"),
	]
589
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
590
		("M",         "memory result"),
591
		("res",       "result of load operation"),
Matthias Braun's avatar
Matthias Braun committed
592
593
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
594
	]
595
596
	flags    = [ "fragile", "uses_memory" ]
	pinned   = "exception"
597
598
	attrs    = [
		dict(
599
600
601
			type      = "ir_mode*",
			name      = "mode",
			comment   = "mode of the value to be loaded",
602
		),
603
604
605
606
607
		dict(
			type      = "ir_volatility",
			name      = "volatility",
			comment   = "volatile loads are a visible side-effect and may not be optimized",
			init      = "flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile",
Matthias Braun's avatar
Matthias Braun committed
608
			to_flags  = "%s == volatility_is_volatile ? cons_volatile : 0"
609
610
611
612
613
614
		),
		dict(
			type      = "ir_align",
			name      = "unaligned",
			comment   = "pointers to unaligned loads don't need to respect the load-mode/type alignments",
			init      = "flags & cons_unaligned ? align_non_aligned : align_is_aligned",
Matthias Braun's avatar
Matthias Braun committed
615
			to_flags  = "%s == align_non_aligned ? cons_unaligned : 0"
616
		),
617
618
	]
	attr_struct = "load_attr"
619
620
	constructor_args = [
		dict(
621
622
623
			type    = "ir_cons_flags",
			name    = "flags",
			comment = "specifies alignment, volatility and pin state",
624
		),
625
	]
626
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
627
	throws_init = "(flags & cons_throws_exception) != 0"
628
629

class Minus(Unop):
630
	"""returns the difference between its operands"""
631
632
633
	flags = []

class Mod(Op):
634
635
636
	"""returns the remainder of its operands from an implied division.

	Examples:
637

638
639
640
641
642
	* mod(5,3)   produces 2
	* mod(5,-3)  produces 2
	* mod(-5,3)  produces -2
	* mod(-5,-3) produces -2
	"""
643
644
645
646
647
	ins   = [
		("mem",   "memory dependency"),
		("left",  "first operand"),
		("right", "second operand"),
	]
648
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
649
		("M",         "memory result"),
650
		("res",       "result of computation"),
Matthias Braun's avatar
Matthias Braun committed
651
652
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
653
	]
654
	flags = [ "fragile", "uses_memory" ]
655
	attrs = [
656
		dict(
657
658
659
			type    = "ir_mode*",
			name    = "resmode",
			comment = "mode of the result",
660
		),
661
	]
662
	attr_struct = "mod_attr"
663
	pinned      = "exception"
664
	throws_init = "false"
665
666
	op_index    = 1
	arity_override = "oparity_binary"
Matthias Braun's avatar
Matthias Braun committed
667

668
class Mul(Binop):
669
	"""returns the product of its operands"""
670
671
672
	flags = [ "commutative" ]

class Mulh(Binop):
673
674
	"""returns the upper word of the product of its operands (the part which
	would not fit into the result mode of a normal Mul anymore)"""
675
676
677
	flags = [ "commutative" ]

class Mux(Op):
678
679
	"""returns the false or true operand depending on the value of the sel
	operand"""
680
681
682
683
684
	ins    = [
	   ("sel",   "value making the output selection"),
	   ("false", "selected if sel input is false"),
	   ("true",  "selected if sel input is true"),
	]
685
686
687
688
	flags  = []
	pinned = "no"

class NoMem(Op):
689
	"""Placeholder node for cases where you don't need any memory input"""
690
691
692
693
	mode          = "mode_M"
	flags         = [ "dump_noblock", "dump_noinput" ]
	pinned        = "yes"
	knownBlock    = True
694
	block         = "get_irg_start_block(irg)"
695
	singleton     = True
696
697

class Not(Unop):
698
	"""returns the bitwise complement of a value. Works for boolean values, too."""
699
700
701
	flags = []

class Or(Binop):
702
	"""returns the result of a bitwise or operation of its operands"""
703
704
705
	flags = [ "commutative" ]

class Phi(Op):
706
707
708
	"""Choose a value based on control flow. A phi node has 1 input for each
	predecessor of its block. If a block is entered from its nth predecessor
	all phi nodes produce their nth input as result."""
709
710
711
712
	pinned        = "yes"
	arity         = "variable"
	flags         = []
	attr_struct   = "phi_attr"
713
714
715
	init          = '''
	res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);'''
	init_after_opt = '''
Matthias Braun's avatar
Matthias Braun committed
716
717
	/* Memory Phis in endless loops must be kept alive.
	   As we can't distinguish these easily we keep all of them alive. */
718
719
	if (is_Phi(res) && mode == mode_M)
		add_End_keepalive(get_irg_end(irg), res);'''
Matthias Braun's avatar
Matthias Braun committed
720
	customSerializer = True
721
722

class Pin(Op):
723
724
725
	"""Pin the value of the node node in the current block. No users of the Pin
	node can float above the Block of the Pin. The node cannot float behind
	this block. Often used to Pin the NoMem node."""
726
727
728
	ins      = [
		("op", "value which is pinned"),
	]
729
730
731
732
733
	mode     = "get_irn_mode(irn_op)"
	flags    = [ "highlevel" ]
	pinned   = "yes"

class Proj(Op):
734
	"""returns an entry of a tuple value"""
735
736
737
	ins              = [
		("pred", "the tuple value from which a part is extracted"),
	]
738
739
740
741
742
743
	flags            = []
	pinned           = "no"
	knownBlock       = True
	knownGraph       = True
	block            = "get_nodes_block(irn_pred)"
	graph            = "get_irn_irg(irn_pred)"
744
	attrs      = [
745
		dict(
746
747
748
			type    = "long",
			name    = "proj",
			comment = "number of tuple component to be extracted",
749
		),
750
	]
751
	attr_struct = "proj_attr"
752
753

class Raise(Op):
754
755
756
	"""Raises an exception. Unconditional change of control flow. Writes an
	explicit Except variable to memory to pass it to the exception handler.
	Must be lowered to a Call to a runtime check function."""
757
758
759
760
	ins    = [
		("mem",     "memory dependency"),
		("exo_ptr", "pointer to exception object to be thrown"),
	]
761
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
762
763
		("M", "memory result"),
		("X", "control flow to exception handler"),
764
	]
765
766
767
768
	flags  = [ "highlevel", "cfopcode" ]
	pinned = "yes"

class Return(Op):
769
770
	"""Returns from the current function. Takes memory and return values as
	operands."""
771
772
773
	ins      = [
		("mem", "memory dependency"),
	]
774
775
776
777
778
779
	arity    = "variable"
	mode     = "mode_X"
	flags    = [ "cfopcode" ]
	pinned   = "yes"

class Rotl(Binop):
780
781
	"""Returns its first operand bits rotated left by the amount in the 2nd
	operand"""
782
783
784
	flags    = []

class Sel(Op):
785
	"""Computes the address of a entity of a compound type given the base
786
787
788
789
	address of an instance of the compound type.

	Optimisations assume that a Sel node can only produce a NULL pointer if the
	ptr input was NULL."""
790
791
792
793
	ins    = [
		("mem", "memory dependency"),
		("ptr", "pointer to object to select from"),
	]
794
795
796
797
	arity  = "variable"
	flags  = []
	mode   = "is_Method_type(get_entity_type(entity)) ? mode_P_code : mode_P_data"
	pinned = "no"
798
	attrs  = [
799
		dict(
800
801
802
			type    = "ir_entity*",
			name    = "entity",
			comment = "entity which is selected",
803
		)
804
805
806
807
	]
	attr_struct = "sel_attr"

class Shl(Binop):
808
809
	"""Returns its first operands bits shifted left by the amount of the 2nd
	operand"""
810
811
812
	flags = []

class Shr(Binop):
813
814
	"""Returns its first operands bits shifted right by the amount of the 2nd
	operand. No special handling for the sign bit (zero extension)"""
815
816
817
	flags = []

class Shrs(Binop):
818
819
820
	"""Returns its first operands bits shifted right by the amount of the 2nd
	operand. The leftmost bit (usually the sign bit) stays the same
	(sign extension)"""
821
822
823
	flags = []

class Start(Op):
824
	"""The first node of a graph. Execution starts with this node."""
825
826
827
828
829
830
	outs       = [
		("X_initial_exec", "control flow"),
		("M",              "initial memory"),
		("P_frame_base",   "frame base pointer"),
		("T_args",         "function arguments")
	]
831
832
833
834
835
836
	mode             = "mode_T"
	pinned           = "yes"
	flags            = [ "cfopcode" ]
	singleton        = True
	knownBlock       = True
	block            = "get_irg_start_block(irg)"
837
838

class Store(Op):
839
	"""Stores a value into memory (heap or stack)."""
840
841
842
843
844
	ins   = [
	   ("mem",   "memory dependency"),
	   ("ptr",   "address to store to"),
	   ("value", "value to store"),
	]
845
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
846
847
848
		("M",         "memory result"),
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
849
	]
850
851
852
853
	flags    = [ "fragile", "uses_memory" ]
	pinned   = "exception"
	attr_struct = "store_attr"
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
854
	throws_init = "(flags & cons_throws_exception) != 0"
855
856
857
858
859
860
	attrs = [
		dict(
			type      = "ir_volatility",
			name      = "volatility",
			comment   = "volatile stores are a visible side-effect and may not be optimized",
			init      = "flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile",
Matthias Braun's avatar
Matthias Braun committed
861
			to_flags  = "%s == volatility_is_volatile ? cons_volatile : 0"
862
863
864
865
866
867
		),
		dict(
			type      = "ir_align",
			name      = "unaligned",
			comment   = "pointers to unaligned stores don't need to respect the load-mode/type alignments",
			init      = "flags & cons_unaligned ? align_non_aligned : align_is_aligned",
Matthias Braun's avatar
Matthias Braun committed
868
			to_flags  = "%s == align_non_aligned ? cons_unaligned : 0"
869
870
		),
	]
871
	constructor_args = [
872
		dict(
873
874
875
			type    = "ir_cons_flags",
			name    = "flags",
			comment = "specifies alignment, volatility and pin state",
876
		),
877
878
879
	]

class Sub(Binop):
880
	"""returns the difference of its operands"""
881
882
883
	flags = []

class SymConst(Op):
884
885
	"""A symbolic constant.

886
887
888
889
890
891
892
893
894
895
896
897
898
	 - *symconst_type_size* The symbolic constant represents the size of a type.
	                        The type of which the constant represents the size
	                        is given explicitly.
	 - *symconst_type_align* The symbolic constant represents the alignment of a
	                        type.  The type of which the constant represents the
	                        size is given explicitly.
	 - *symconst_addr_ent*  The symbolic constant represents the address of an
	                        entity (variable or method).  The variable is given
	                        explicitly by a firm entity.
	 - *symconst_ofs_ent*   The symbolic constant represents the offset of an
	                        entity in its owner type.
	 - *symconst_enum_const* The symbolic constant is a enumeration constant of
	                        an enumeration type."""
899
900
901
902
	mode       = "mode_P"
	flags      = [ "constlike", "start_block" ]
	knownBlock = True
	pinned     = "no"
903
	attrs      = [
904
		dict(
905
906
907
908
			type    = "ir_entity*",
			name    = "entity",
			noprop  = True,
			comment = "entity whose address is returned",
909
		)
910
911
	]
	attr_struct = "symconst_attr"
912
	customSerializer = True
913
914
915
	# constructor is written manually at the moment, because of the strange
	# union argument
	noconstructor = True
916
917

class Sync(Op):
918
919
920
921
922
	"""The Sync operation unifies several partial memory blocks. These blocks
	have to be pairwise disjunct or the values in common locations have to
	be identical.  This operation allows to specify all operations that
	eventually need several partial memory blocks as input with a single
	entrance by unifying the memories with a preceding Sync operation."""
923
924
925
	mode     = "mode_M"
	flags    = []
	pinned   = "no"
926
	arity    = "dynamic"
927

928
class Tuple(Op):
929
930
931
932
933
934
935
	"""Builds a Tuple from single values.

	This is needed to implement optimizations that remove a node that produced
	a tuple.  The node can be replaced by the Tuple operation so that the
	following Proj nodes have not to be changed. (They are hard to find due to
	the implementation with pointers in only one direction.) The Tuple node is
	smaller than any other node, so that a node can be changed into a Tuple by
936
	just changing its opcode and giving it a new in array."""
937
938
939
940
941
942
	arity  = "variable"
	mode   = "mode_T"
	pinned = "no"
	flags  = [ "labeled" ]

class Unknown(Op):
943
944
	"""Returns an unknown (at compile- and runtime) value. It is a valid
	optimisation to replace an Unknown by any other constant value."""
945
946
947
	knownBlock = True
	pinned     = "yes"
	block      = "get_irg_start_block(irg)"
Matthias Braun's avatar
Matthias Braun committed
948
	flags      = [ "start_block", "constlike", "dump_noblock" ]
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964

# Prepare node list

def getOpList(namespace):
	nodes = []
	for t in namespace.values():
		if type(t) != type:
			continue

		if issubclass(t, Op):
			setnodedefaults(t)
			nodes.append(t)
	return nodes

nodes = getOpList(globals())
nodes = sorted(nodes, lambda x,y: cmp(x.name, y.name))