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

class Anchor(Op):
67
68
69
70
71
72
	"""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."""
73
74
75
76
77
78
79
80
81
	mode             = "mode_ANY"
	arity            = "variable"
	flags            = [ "dump_noblock" ]
	pinned           = "yes"
	attr_struct      = "irg_attr"
	knownBlock       = True
	singleton        = True
	noconstructor    = True
	customSerializer = True
82
83

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

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

class Bad(Op):
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
	"""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
156
	flags         = [ "start_block", "dump_noblock" ]
157
158
	pinned        = "yes"
	knownBlock    = True
159
	block         = "get_irg_start_block(irg)"
160
	attr_struct   = "bad_attr"
161
	init = '''
162
	res->attr.bad.irg.irg = irg;
163
164
	'''

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

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

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

	/* Create and initialize array for Phi-node construction. */
199
200
201
	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*));
202
	}
203
	'''
204

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

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

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

260
class Call(Op):
261
262
263
264
	"""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."""
265
266
267
268
	ins      = [
		("mem",   "memory dependency"),
		("ptr",   "pointer to called code"),
	]
269
	arity    = "variable"
270
	outs     = [
Matthias Braun's avatar
Matthias Braun committed
271
		("M",                "memory result"),
272
		("T_result",         "tuple containing all results"),
Matthias Braun's avatar
Matthias Braun committed
273
274
		("X_regular",        "control flow when no exception occurs"),
		("X_except",         "control flow when exception occured"),
275
	]
276
	flags    = [ "fragile", "uses_memory" ]
277
	attrs    = [
278
		dict(
279
280
281
			type    = "ir_type*",
			name    = "type",
			comment = "type of the call (usually type of the called procedure)",
282
		),
283
284
285
286
	]
	attr_struct = "call_attr"
	pinned      = "memory"
	pinned_init = "op_pin_state_pinned"
287
	throws_init = "false"
288
289
	init = '''
	assert((get_unknown_type() == type) || is_Method_type(type));
290
	'''
291

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

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

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

class Cond(Op):
326
	"""Conditionally change control flow."""
327
328
329
	ins      = [
		("selector",  "condition parameter"),
	]
330
331
332
333
	outs     = [
		("false", "control flow if operand is \"false\""),
		("true",  "control flow if operand is \"true\""),
	]
334
335
	flags    = [ "cfopcode", "forking" ]
	pinned   = "yes"
336
337
	attrs    = [
		dict(
338
339
340
341
			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
342
		),
343
	]
344
	attr_struct = "cond_attr"
345

Matthias Braun's avatar
Matthias Braun committed
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
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"

373
class Confirm(Op):
374
375
376
377
378
379
380
381
382
	"""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."""
383
384
385
386
	ins      = [
		("value",  "value to express a constraint for"),
		("bound",  "value to compare against"),
	]
387
388
389
	mode     = "get_irn_mode(irn_value)"
	flags    = [ "highlevel" ]
	pinned   = "yes"
390
391
	attrs    = [
		dict(
392
393
394
			name    = "relation",
			type    = "ir_relation",
			comment = "relation of value to bound",
395
		),
396
397
398
399
	]
	attr_struct = "confirm_attr"

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

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

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

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

486
class Dummy(Op):
487
488
489
	"""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."""
490
491
	ins        = []
	flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
492
493
494
495
496
	knownBlock = True
	pinned     = "yes"
	block      = "get_irg_start_block(irg)"

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

class Eor(Binop):
Matthias Braun's avatar
Matthias Braun committed
508
509
510
	"""returns the result of a bitwise exclusive or operation of its operands.

	This is also known as the Xor operation."""
511
	flags    = [ "commutative" ]
512

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

class Id(Op):
Matthias Braun's avatar
Matthias Braun committed
538
539
540
541
	"""Returns its operand unchanged.

	This is mainly used when exchanging nodes. Usually you shouldn't see Id
	nodes since the getters/setters for node inputs skip them automatically."""
542
543
544
	ins    = [
	   ("pred", "the value which is returned unchanged")
	]
545
546
547
548
	pinned = "no"
	flags  = []

class IJmp(Op):
549
550
551
	"""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"""
552
553
	mode     = "mode_X"
	pinned   = "yes"
554
555
556
	ins      = [
	   ("target", "target address of the jump"),
	]
557
	flags    = [ "cfopcode", "forking", "keep", "unknown_jump" ]
558
559

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

class Jmp(Op):
584
	"""Jumps to the block connected through the out-value"""
585
586
587
588
589
590
	mode     = "mode_X"
	pinned   = "yes"
	ins      = []
	flags    = [ "cfopcode" ]

class Load(Op):
591
	"""Loads a value from memory (heap or stack)."""
592
593
594
595
	ins   = [
		("mem", "memory dependency"),
		("ptr",  "address to load from"),
	]
596
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
597
		("M",         "memory result"),
598
		("res",       "result of load operation"),
Matthias Braun's avatar
Matthias Braun committed
599
600
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
601
	]
602
603
	flags    = [ "fragile", "uses_memory" ]
	pinned   = "exception"
604
605
	attrs    = [
		dict(
606
607
608
			type      = "ir_mode*",
			name      = "mode",
			comment   = "mode of the value to be loaded",
609
		),
610
611
612
613
614
		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",
615
			to_flags  = "%s == volatility_is_volatile ? cons_volatile : cons_none"
616
617
618
619
620
621
		),
		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",
622
			to_flags  = "%s == align_non_aligned ? cons_unaligned : cons_none"
623
		),
624
625
	]
	attr_struct = "load_attr"
626
627
	constructor_args = [
		dict(
628
629
630
			type    = "ir_cons_flags",
			name    = "flags",
			comment = "specifies alignment, volatility and pin state",
631
		),
632
	]
633
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
634
	throws_init = "(flags & cons_throws_exception) != 0"
635
636

class Minus(Unop):
637
	"""returns the difference between its operands"""
638
639
640
	flags = []

class Mod(Op):
641
642
643
	"""returns the remainder of its operands from an implied division.

	Examples:
644

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

675
class Mul(Binop):
676
	"""returns the product of its operands"""
677
678
679
	flags = [ "commutative" ]

class Mulh(Binop):
680
681
	"""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)"""
682
683
684
	flags = [ "commutative" ]

class Mux(Op):
685
686
	"""returns the false or true operand depending on the value of the sel
	operand"""
687
688
689
690
691
	ins    = [
	   ("sel",   "value making the output selection"),
	   ("false", "selected if sel input is false"),
	   ("true",  "selected if sel input is true"),
	]
692
693
694
695
	flags  = []
	pinned = "no"

class NoMem(Op):
696
	"""Placeholder node for cases where you don't need any memory input"""
697
	mode          = "mode_M"
698
	flags         = [ "dump_noblock" ]
699
700
	pinned        = "yes"
	knownBlock    = True
701
	block         = "get_irg_start_block(irg)"
702
	singleton     = True
703
704

class Not(Unop):
705
	"""returns the bitwise complement of a value. Works for boolean values, too."""
706
707
708
	flags = []

class Or(Binop):
709
	"""returns the result of a bitwise or operation of its operands"""
710
711
712
	flags = [ "commutative" ]

class Phi(Op):
713
714
715
	"""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."""
716
717
718
719
	pinned        = "yes"
	arity         = "variable"
	flags         = []
	attr_struct   = "phi_attr"
720
721
722
	init          = '''
	res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);'''
	init_after_opt = '''
Matthias Braun's avatar
Matthias Braun committed
723
724
	/* Memory Phis in endless loops must be kept alive.
	   As we can't distinguish these easily we keep all of them alive. */
725
726
	if (is_Phi(res) && mode == mode_M)
		add_End_keepalive(get_irg_end(irg), res);'''
Matthias Braun's avatar
Matthias Braun committed
727
	customSerializer = True
728
729

class Pin(Op):
730
731
732
	"""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."""
733
734
735
	ins      = [
		("op", "value which is pinned"),
	]
736
737
738
739
740
	mode     = "get_irn_mode(irn_op)"
	flags    = [ "highlevel" ]
	pinned   = "yes"

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

class Raise(Op):
761
762
763
	"""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."""
764
765
766
767
	ins    = [
		("mem",     "memory dependency"),
		("exo_ptr", "pointer to exception object to be thrown"),
	]
768
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
769
770
		("M", "memory result"),
		("X", "control flow to exception handler"),
771
	]
772
773
774
775
	flags  = [ "highlevel", "cfopcode" ]
	pinned = "yes"

class Return(Op):
776
777
	"""Returns from the current function. Takes memory and return values as
	operands."""
778
779
780
	ins      = [
		("mem", "memory dependency"),
	]
781
782
783
784
785
786
	arity    = "variable"
	mode     = "mode_X"
	flags    = [ "cfopcode" ]
	pinned   = "yes"

class Rotl(Binop):
787
788
	"""Returns its first operand bits rotated left by the amount in the 2nd
	operand"""
789
790
791
	flags    = []

class Sel(Op):
792
	"""Computes the address of a entity of a compound type given the base
793
794
795
796
	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."""
797
798
799
800
	ins    = [
		("mem", "memory dependency"),
		("ptr", "pointer to object to select from"),
	]
801
802
803
804
	arity  = "variable"
	flags  = []
	mode   = "is_Method_type(get_entity_type(entity)) ? mode_P_code : mode_P_data"
	pinned = "no"
805
	attrs  = [
806
		dict(
807
808
809
			type    = "ir_entity*",
			name    = "entity",
			comment = "entity which is selected",
810
		)
811
812
813
814
	]
	attr_struct = "sel_attr"

class Shl(Binop):
815
	"""Returns its first operands bits shifted left by the amount of the 2nd
816
817
818
819
	operand.
	The right input (shift amount) must be an unsigned integer value.
	If the result mode has modulo_shift!=0, then the effective shift amount is
	the right input modulo this modulo_shift amount."""
820
821
822
	flags = []

class Shr(Binop):
823
	"""Returns its first operands bits shifted right by the amount of the 2nd
824
825
826
827
	operand. No special handling for the sign bit is performed (zero extension).
	The right input (shift amount) must be an unsigned integer value.
	If the result mode has modulo_shift!=0, then the effective shift amount is
	the right input modulo this modulo_shift amount."""
828
829
830
	flags = []

class Shrs(Binop):
831
832
	"""Returns its first operands bits shifted right by the amount of the 2nd
	operand. The leftmost bit (usually the sign bit) stays the same
833
834
835
836
	(sign extension).
	The right input (shift amount) must be an unsigned integer value.
	If the result mode has modulo_shift!=0, then the effective shift amount is
	the right input modulo this modulo_shift amount."""
837
838
839
	flags = []

class Start(Op):
840
	"""The first node of a graph. Execution starts with this node."""
841
842
843
844
845
846
	outs       = [
		("X_initial_exec", "control flow"),
		("M",              "initial memory"),
		("P_frame_base",   "frame base pointer"),
		("T_args",         "function arguments")
	]
847
848
849
850
851
852
	mode             = "mode_T"
	pinned           = "yes"
	flags            = [ "cfopcode" ]
	singleton        = True
	knownBlock       = True
	block            = "get_irg_start_block(irg)"
853
854

class Store(Op):
855
	"""Stores a value into memory (heap or stack)."""
856
857
858
859
860
	ins   = [
	   ("mem",   "memory dependency"),
	   ("ptr",   "address to store to"),
	   ("value", "value to store"),
	]
861
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
862
863
864
		("M",         "memory result"),
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
865
	]
866
867
868
869
	flags    = [ "fragile", "uses_memory" ]
	pinned   = "exception"
	attr_struct = "store_attr"
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
870
	throws_init = "(flags & cons_throws_exception) != 0"
871
872
873
874
875
876
	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",
877
			to_flags  = "%s == volatility_is_volatile ? cons_volatile : cons_none"
878
879
880
881
882
883
		),
		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",
884
			to_flags  = "%s == align_non_aligned ? cons_unaligned : cons_none"
885
886
		),
	]
887
	constructor_args = [
888
		dict(
889
890
891
			type    = "ir_cons_flags",
			name    = "flags",
			comment = "specifies alignment, volatility and pin state",
892
		),
893
894
895
	]

class Sub(Binop):
896
	"""returns the difference of its operands"""
897
898
899
	flags = []

class SymConst(Op):
900
901
	"""A symbolic constant.

902
903
904
905
906
907
908
909
910
911
912
913
914
	 - *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."""
915
916
917
918
	mode       = "mode_P"
	flags      = [ "constlike", "start_block" ]
	knownBlock = True
	pinned     = "no"
919
	attrs      = [
920
		dict(
921
922
923
924
			type    = "ir_entity*",
			name    = "entity",
			noprop  = True,
			comment = "entity whose address is returned",
925
		)
926
927
	]
	attr_struct = "symconst_attr"
928
	customSerializer = True
929
930
931
	# constructor is written manually at the moment, because of the strange
	# union argument
	noconstructor = True
932
933

class Sync(Op):
934
935
936
937
938
	"""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."""
939
940
941
	mode     = "mode_M"
	flags    = []
	pinned   = "no"
942
	arity    = "dynamic"
943

944
class Tuple(Op):
945
946
947
948
949
950
951
	"""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
952
	just changing its opcode and giving it a new in array."""
953
954
955
	arity  = "variable"
	mode   = "mode_T"
	pinned = "no"
956
	flags  = []
957
958

class Unknown(Op):
959
960
	"""Returns an unknown (at compile- and runtime) value. It is a valid
	optimisation to replace an Unknown by any other constant value."""
961
962
963
	knownBlock = True
	pinned     = "yes"
	block      = "get_irg_start_block(irg)"
Matthias Braun's avatar
Matthias Braun committed
964
	flags      = [ "start_block", "constlike", "dump_noblock" ]
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980

# 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))