ir_spec.py 28.8 KB
Newer Older
Matthias Braun's avatar
Matthias Braun committed
1
2
3
# Firm node specifications
# The comments are in (standard python) restructured text format and are used
# to generate documentation.
4
5
6
from spec_util import abstract, setnodedefaults

class Op(object):
7
	"""Base class for firm nodes"""
8
9
10
abstract(Op)

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

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

class Add(Binop):
32
	"""returns the sum of its operands"""
33
	flags = [ "commutative" ]
34
35

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

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

class And(Binop):
87
	"""returns the result of a bitwise and operation of its operands"""
88
89
90
	flags    = [ "commutative" ]

class ASM(Op):
Matthias Braun's avatar
Matthias Braun committed
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
	"""executes assembler fragments of the target machine.

	The node contains a template for an assembler snippet. The compiler will
	replace occurences of %0 to %9 with input/output registers,
	%% with a single % char. Some backends allow additional specifiers (for
	example %w3, %l3, %h3 on x86 to get a 16bit, 8hit low, 8bit high part
	of a register).
	After the replacements the text is emitted into the final assembly.

	The clobber list contains names of registers which have an undefined value
	after the assembler instruction is executed; it may also contain 'memory'
	or 'cc' if global state/memory changes or the condition code registers
	(some backends implicitely set cc, memory clobbers on all ASM statements).

	Example (an i386 instruction)::

		ASM(text="btsl %1, %0",
			input_constraints = ["=m", "r"],
			clobbers = ["cc"])

	As there are no output, the %0 references the first input which is just an
	address which the asm operation writes to. %1 references to an input which
	is passed as a register. The condition code register has an unknown value
	after the instruction.

	(This format is inspired by the gcc extended asm syntax)
	"""
118
119
120
121
122
123
124
125
	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
126
127
128
	ins   = [
		("mem",    "memory dependency"),
	]
129
130
	attrs = [
		dict(
131
132
133
			name    = "input_constraints",
			type    = "ir_asm_constraint*",
			comment = "input constraints",
134
135
		),
		dict(
136
			name    = "n_output_constraints",
137
			type    = "size_t",
138
139
			noprop  = True,
			comment = "number of output constraints",
140
141
		),
		dict(
142
143
144
			name    = "output_constraints",
			type    = "ir_asm_constraint*",
			comment = "output constraints",
145
146
		),
		dict(
147
			name    = "n_clobbers",
148
			type    = "size_t",
149
150
			noprop  = True,
			comment = "number of clobbered registers/memory",
151
152
		),
		dict(
153
154
155
			name    = "clobbers",
			type    = "ident**",
			comment = "list of clobbered registers/memory",
156
157
		),
		dict(
158
159
160
			name    = "text",
			type    = "ident*",
			comment = "assembler text",
161
162
163
164
165
		),
	]
	# constructor is written manually at the moment, because of the clobbers+
	# constraints arrays needing special handling (2 arguments for 1 attribute)
	noconstructor = True
166
167

class Bad(Op):
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
	"""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
185
	flags         = [ "start_block", "dump_noblock" ]
186
187
	pinned        = "yes"
	knownBlock    = True
188
	block         = "get_irg_start_block(irg)"
189
	attr_struct   = "bad_attr"
190
	init = '''
191
	res->attr.bad.irg.irg = irg;
192
193
	'''

194
class Deleted(Op):
195
196
	"""Internal node which is temporary set to nodes which are already removed
	from the graph."""
197
198
199
200
	mode             = "mode_Bad"
	flags            = [ ]
	pinned           = "yes"
	noconstructor    = True
Matthias Braun's avatar
Matthias Braun committed
201
	customSerializer = True # this has no serializer
202

203
class Block(Op):
204
	"""A basic block"""
205
206
207
208
209
	mode             = "mode_BB"
	knownBlock       = True
	block            = "NULL"
	pinned           = "yes"
	arity            = "variable"
210
	flags            = []
211
	attr_struct      = "block_attr"
212
213
214
215
216
217
218
219
	attrs            = [
		dict(
			name    = "entity",
			type    = "ir_entity*",
			comment = "entity representing this block",
			init    = "NULL",
		),
	]
220
	customSerializer = True
Moritz Kroll's avatar
Moritz Kroll committed
221
222

	init = '''
223
	res->attr.block.irg.irg     = irg;
Moritz Kroll's avatar
Moritz Kroll committed
224
225
	res->attr.block.backedge    = new_backedge_arr(irg->obst, arity);
	set_Block_matured(res, 1);
226
227

	/* Create and initialize array for Phi-node construction. */
228
229
230
	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*));
231
	}
232
	'''
233

234
class Borrow(Binop):
235
	"""Returns the borrow bit from and implied subtractions of its 2 operands"""
236
237
	flags = []

238
class Bound(Op):
239
240
	"""Performs a bounds-check: if lower <= index < upper then return index,
	otherwise throw an exception."""
241
242
243
244
245
246
	ins    = [
		("mem",    "memory dependency"),
		("index",  "value to test"),
		("lower",  "lower bound (inclusive)"),
		("upper",  "upper bound (exclusive)"),
	]
247
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
248
		("M",         "memory result"),
249
		("res",       "the checked index"),
Matthias Braun's avatar
Matthias Braun committed
250
251
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
252
	]
253
254
255
 	flags  = [ "fragile", "highlevel" ]
	pinned = "exception"
	pinned_init = "op_pin_state_pinned"
256
	throws_init = "false"
257
258
259
	attr_struct = "bound_attr"

class Builtin(Op):
260
	"""performs a backend-specific builtin."""
261
262
263
	ins      = [
		("mem", "memory dependency"),
	]
264
	arity    = "variable"
265
	outs     = [
266
267
		("M", "memory result"),
		# results follow here
268
	]
269
	flags    = [ "uses_memory" ]
270
	attrs    = [
271
		dict(
272
273
274
			type    = "ir_builtin_kind",
			name    = "kind",
			comment = "kind of builtin",
275
		),
276
		dict(
277
278
279
			type    = "ir_type*",
			name    = "type",
			comment = "method type for the builtin call",
280
		)
281
282
283
284
	]
	pinned      = "memory"
	pinned_init = "op_pin_state_pinned"
	attr_struct = "builtin_attr"
285
	init   = '''
286
287
288
	assert((get_unknown_type() == type) || is_Method_type(type));
	'''

289
class Call(Op):
290
291
292
293
	"""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."""
294
295
296
297
	ins      = [
		("mem",   "memory dependency"),
		("ptr",   "pointer to called code"),
	]
298
	arity    = "variable"
299
	outs     = [
Matthias Braun's avatar
Matthias Braun committed
300
		("M",                "memory result"),
301
		("T_result",         "tuple containing all results"),
Matthias Braun's avatar
Matthias Braun committed
302
303
		("X_regular",        "control flow when no exception occurs"),
		("X_except",         "control flow when exception occured"),
304
	]
305
	flags    = [ "fragile", "uses_memory" ]
306
	attrs    = [
307
		dict(
308
309
310
			type    = "ir_type*",
			name    = "type",
			comment = "type of the call (usually type of the called procedure)",
311
		),
312
313
314
315
	]
	attr_struct = "call_attr"
	pinned      = "memory"
	pinned_init = "op_pin_state_pinned"
316
	throws_init = "false"
317
318
	init = '''
	assert((get_unknown_type() == type) || is_Method_type(type));
319
	'''
320

321
class Carry(Binop):
322
323
	"""Computes the value of the carry-bit that would result when adding the 2
	operands"""
324
325
	flags = [ "commutative" ]

Michael Beck's avatar
Michael Beck committed
326
class Cast(Unop):
327
	"""perform a high-level type cast"""
328
329
	mode     = "get_irn_mode(irn_op)"
	flags    = [ "highlevel" ]
330
331
	attrs    = [
		dict(
332
333
334
			type    = "ir_type*",
			name    = "type",
			comment = "target type of the case",
335
		)
336
337
	]
	attr_struct = "cast_attr"
338
	init     = "assert(is_atomic_type(type));"
339
340

class Cmp(Binop):
341
	"""Compares its two operands and checks whether a specified
342
	   relation (like less or equal) is fulfilled."""
343
	flags = []
344
345
346
347
348
349
350
351
352
	mode  = "mode_b"
	attrs = [
		dict(
			type    = "ir_relation",
			name    = "relation",
			comment = "Comparison relation"
		)
	]
	attr_struct = "cmp_attr"
353
354

class Cond(Op):
355
	"""Conditionally change control flow."""
356
357
358
	ins      = [
		("selector",  "condition parameter"),
	]
359
360
361
362
	outs     = [
		("false", "control flow if operand is \"false\""),
		("true",  "control flow if operand is \"true\""),
	]
363
364
	flags    = [ "cfopcode", "forking" ]
	pinned   = "yes"
365
366
	attrs    = [
		dict(
367
368
369
370
			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
371
		),
372
	]
373
	attr_struct = "cond_attr"
374

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

402
class Confirm(Op):
403
404
405
406
407
408
409
410
411
	"""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."""
412
413
414
415
	ins      = [
		("value",  "value to express a constraint for"),
		("bound",  "value to compare against"),
	]
416
417
418
	mode     = "get_irn_mode(irn_value)"
	flags    = [ "highlevel" ]
	pinned   = "yes"
419
420
	attrs    = [
		dict(
421
422
423
			name    = "relation",
			type    = "ir_relation",
			comment = "relation of value to bound",
424
		),
425
426
427
428
	]
	attr_struct = "confirm_attr"

class Const(Op):
429
	"""Returns a constant value."""
430
	flags      = [ "constlike", "start_block" ]
431
432
	block      = "get_irg_start_block(irg)"
	mode       = "get_tarval_mode(tarval)"
433
434
	knownBlock = True
	pinned     = "no"
435
436
	attrs      = [
		dict(
437
438
439
			type    = "ir_tarval*",
			name    = "tarval",
			comment = "constant value (a tarval object)",
440
		)
441
442
	]
	attr_struct = "const_attr"
443
	attrs_name  = "con"
444

445
class Conv(Unop):
446
	"""Converts values between modes"""
447
	flags = []
448
449
	attrs = [
		dict(
450
451
452
453
			name    = "strict",
			type    = "int",
			init    = "0",
			comment = "force floating point to restrict precision even if backend computes in higher precision (deprecated)",
454
		)
455
456
457
458
	]
	attr_struct = "conv_attr"

class CopyB(Op):
Matthias Braun's avatar
Matthias Braun committed
459
	"""Copies a block of memory with statically known size/type."""
460
461
462
463
464
	ins   = [
		("mem",  "memory dependency"),
		("dst",  "destination address"),
		("src",  "source address"),
	]
465
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
466
467
468
		("M",         "memory result"),
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
469
	]
470
	flags = [ "fragile", "uses_memory" ]
471
472
	attrs = [
		dict(
473
474
475
			name    = "type",
			type    = "ir_type*",
			comment = "type of copied data",
476
		)
477
478
479
480
	]
	attr_struct = "copyb_attr"
	pinned      = "memory"
	pinned_init = "op_pin_state_pinned"
481
	throws_init = "false"
482

483
class Div(Op):
484
	"""returns the quotient of its 2 operands"""
485
486
487
488
489
	ins   = [
		("mem",   "memory dependency"),
		("left",  "first operand"),
		("right", "second operand"),
	]
490
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
491
		("M",         "memory result"),
492
		("res",       "result of computation"),
Matthias Braun's avatar
Matthias Braun committed
493
494
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
495
	]
496
	flags = [ "fragile", "uses_memory" ]
497
498
	attrs = [
		dict(
499
500
501
			type    = "ir_mode*",
			name    = "resmode",
			comment = "mode of the result value",
502
		),
Moritz Kroll's avatar
Moritz Kroll committed
503
504
505
506
		dict(
			name = "no_remainder",
			type = "int",
			init = "0",
507
		)
508
	]
509
	attr_struct = "div_attr"
510
	pinned      = "exception"
511
	throws_init = "false"
512
513
	op_index    = 1
	arity_override = "oparity_binary"
514

515
class Dummy(Op):
516
517
518
	"""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."""
519
520
	ins        = []
	flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
521
522
523
524
525
	knownBlock = True
	pinned     = "yes"
	block      = "get_irg_start_block(irg)"

class End(Op):
526
527
	"""Last node of a graph. It references nodes in endless loops (so called
	keepalive edges)"""
528
529
530
531
532
533
534
	mode             = "mode_X"
	pinned           = "yes"
	arity            = "dynamic"
	flags            = [ "cfopcode" ]
	knownBlock       = True
	block            = "get_irg_end_block(irg)"
	singleton        = True
535
536

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

	This is also known as the Xor operation."""
540
	flags    = [ "commutative" ]
541

542
class Free(Op):
543
	"""Frees a block of memory previously allocated by an Alloc node"""
544
545
546
	ins    = [
		("mem",   "memory dependency" ),
		("ptr",   "pointer to the object to free"),
547
		("count", "number of objects to allocate" ),
548
	]
549
550
551
	mode   = "mode_M"
	flags  = [ "uses_memory" ]
	pinned = "yes"
552
	attrs  = [
553
		dict(
554
555
556
			name    = "type",
			type    = "ir_type*",
			comment = "type of the allocated variable",
557
558
		),
		dict(
559
560
561
			name    = "where",
			type    = "ir_where_alloc",
			comment = "whether allocation was on the stack or heap",
562
		)
563
564
565
566
	]
	attr_struct = "free_attr"

class Id(Op):
Matthias Braun's avatar
Matthias Braun committed
567
568
569
570
	"""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."""
571
572
573
	ins    = [
	   ("pred", "the value which is returned unchanged")
	]
574
575
576
577
	pinned = "no"
	flags  = []

class IJmp(Op):
578
579
580
	"""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"""
581
582
	mode     = "mode_X"
	pinned   = "yes"
583
584
585
	ins      = [
	   ("target", "target address of the jump"),
	]
586
	flags    = [ "cfopcode", "forking", "keep", "unknown_jump" ]
587
588

class InstOf(Op):
589
	"""Tests whether an object is an instance of a class-type"""
590
591
592
593
	ins   = [
	   ("store", "memory dependency"),
	   ("obj",   "pointer to object being queried")
	]
594
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
595
		("M",         "memory result"),
596
		("res",       "checked object pointer"),
Matthias Braun's avatar
Matthias Braun committed
597
598
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
599
	]
600
	flags = [ "highlevel" ]
601
602
	attrs = [
		dict(
603
604
605
			name    = "type",
			type    = "ir_type*",
			comment = "type to check ptr for",
606
		)
607
608
609
610
611
612
	]
	attr_struct = "io_attr"
	pinned      = "memory"
	pinned_init = "op_pin_state_floats"

class Jmp(Op):
613
	"""Jumps to the block connected through the out-value"""
614
615
616
617
618
619
	mode     = "mode_X"
	pinned   = "yes"
	ins      = []
	flags    = [ "cfopcode" ]

class Load(Op):
620
	"""Loads a value from memory (heap or stack)."""
621
622
623
624
	ins   = [
		("mem", "memory dependency"),
		("ptr",  "address to load from"),
	]
625
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
626
		("M",         "memory result"),
627
		("res",       "result of load operation"),
Matthias Braun's avatar
Matthias Braun committed
628
629
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
630
	]
631
632
	flags    = [ "fragile", "uses_memory" ]
	pinned   = "exception"
633
634
	attrs    = [
		dict(
635
636
637
			type      = "ir_mode*",
			name      = "mode",
			comment   = "mode of the value to be loaded",
638
		),
639
640
641
642
643
		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",
644
			to_flags  = "%s == volatility_is_volatile ? cons_volatile : cons_none"
645
646
647
648
649
650
		),
		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",
651
			to_flags  = "%s == align_non_aligned ? cons_unaligned : cons_none"
652
		),
653
654
	]
	attr_struct = "load_attr"
655
656
	constructor_args = [
		dict(
657
658
659
			type    = "ir_cons_flags",
			name    = "flags",
			comment = "specifies alignment, volatility and pin state",
660
		),
661
	]
662
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
663
	throws_init = "(flags & cons_throws_exception) != 0"
664
665

class Minus(Unop):
666
	"""returns the difference between its operands"""
667
668
669
	flags = []

class Mod(Op):
670
671
672
	"""returns the remainder of its operands from an implied division.

	Examples:
673

674
675
676
677
678
	* mod(5,3)   produces 2
	* mod(5,-3)  produces 2
	* mod(-5,3)  produces -2
	* mod(-5,-3) produces -2
	"""
679
680
681
682
683
	ins   = [
		("mem",   "memory dependency"),
		("left",  "first operand"),
		("right", "second operand"),
	]
684
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
685
		("M",         "memory result"),
686
		("res",       "result of computation"),
Matthias Braun's avatar
Matthias Braun committed
687
688
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
689
	]
690
	flags = [ "fragile", "uses_memory" ]
691
	attrs = [
692
		dict(
693
694
695
			type    = "ir_mode*",
			name    = "resmode",
			comment = "mode of the result",
696
		),
697
	]
698
	attr_struct = "mod_attr"
699
	pinned      = "exception"
700
	throws_init = "false"
701
702
	op_index    = 1
	arity_override = "oparity_binary"
Matthias Braun's avatar
Matthias Braun committed
703

704
class Mul(Binop):
705
	"""returns the product of its operands"""
706
707
708
	flags = [ "commutative" ]

class Mulh(Binop):
709
710
	"""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)"""
711
712
713
	flags = [ "commutative" ]

class Mux(Op):
714
715
	"""returns the false or true operand depending on the value of the sel
	operand"""
716
717
718
719
720
	ins    = [
	   ("sel",   "value making the output selection"),
	   ("false", "selected if sel input is false"),
	   ("true",  "selected if sel input is true"),
	]
721
722
723
724
	flags  = []
	pinned = "no"

class NoMem(Op):
725
	"""Placeholder node for cases where you don't need any memory input"""
726
	mode          = "mode_M"
727
	flags         = [ "dump_noblock" ]
728
729
	pinned        = "yes"
	knownBlock    = True
730
	block         = "get_irg_start_block(irg)"
731
	singleton     = True
732
733

class Not(Unop):
734
	"""returns the bitwise complement of a value. Works for boolean values, too."""
735
736
737
	flags = []

class Or(Binop):
738
	"""returns the result of a bitwise or operation of its operands"""
739
740
741
	flags = [ "commutative" ]

class Phi(Op):
742
743
744
	"""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."""
745
746
747
748
	pinned        = "yes"
	arity         = "variable"
	flags         = []
	attr_struct   = "phi_attr"
749
750
751
	init          = '''
	res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);'''
	init_after_opt = '''
Matthias Braun's avatar
Matthias Braun committed
752
753
	/* Memory Phis in endless loops must be kept alive.
	   As we can't distinguish these easily we keep all of them alive. */
754
755
	if (is_Phi(res) && mode == mode_M)
		add_End_keepalive(get_irg_end(irg), res);'''
Matthias Braun's avatar
Matthias Braun committed
756
	customSerializer = True
757
758

class Pin(Op):
759
760
761
	"""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."""
762
763
764
	ins      = [
		("op", "value which is pinned"),
	]
765
766
767
768
769
	mode     = "get_irn_mode(irn_op)"
	flags    = [ "highlevel" ]
	pinned   = "yes"

class Proj(Op):
770
	"""returns an entry of a tuple value"""
771
772
773
	ins              = [
		("pred", "the tuple value from which a part is extracted"),
	]
774
775
776
777
778
779
	flags            = []
	pinned           = "no"
	knownBlock       = True
	knownGraph       = True
	block            = "get_nodes_block(irn_pred)"
	graph            = "get_irn_irg(irn_pred)"
780
	attrs      = [
781
		dict(
782
783
784
			type    = "long",
			name    = "proj",
			comment = "number of tuple component to be extracted",
785
		),
786
	]
787
	attr_struct = "proj_attr"
788
789

class Raise(Op):
790
791
792
	"""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."""
793
794
795
796
	ins    = [
		("mem",     "memory dependency"),
		("exo_ptr", "pointer to exception object to be thrown"),
	]
797
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
798
799
		("M", "memory result"),
		("X", "control flow to exception handler"),
800
	]
801
802
803
804
	flags  = [ "highlevel", "cfopcode" ]
	pinned = "yes"

class Return(Op):
805
806
	"""Returns from the current function. Takes memory and return values as
	operands."""
807
808
809
	ins      = [
		("mem", "memory dependency"),
	]
810
811
812
813
814
815
	arity    = "variable"
	mode     = "mode_X"
	flags    = [ "cfopcode" ]
	pinned   = "yes"

class Rotl(Binop):
816
817
	"""Returns its first operand bits rotated left by the amount in the 2nd
	operand"""
818
819
820
	flags    = []

class Sel(Op):
821
	"""Computes the address of a entity of a compound type given the base
822
823
824
825
	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."""
826
827
828
829
	ins    = [
		("mem", "memory dependency"),
		("ptr", "pointer to object to select from"),
	]
830
831
832
833
	arity  = "variable"
	flags  = []
	mode   = "is_Method_type(get_entity_type(entity)) ? mode_P_code : mode_P_data"
	pinned = "no"
834
	attrs  = [
835
		dict(
836
837
838
			type    = "ir_entity*",
			name    = "entity",
			comment = "entity which is selected",
839
		)
840
841
842
843
	]
	attr_struct = "sel_attr"

class Shl(Binop):
844
	"""Returns its first operands bits shifted left by the amount of the 2nd
845
846
847
848
	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."""
849
850
851
	flags = []

class Shr(Binop):
852
	"""Returns its first operands bits shifted right by the amount of the 2nd
853
854
855
856
	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."""
857
858
859
	flags = []

class Shrs(Binop):
860
861
	"""Returns its first operands bits shifted right by the amount of the 2nd
	operand. The leftmost bit (usually the sign bit) stays the same
862
863
864
865
	(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."""
866
867
868
	flags = []

class Start(Op):
869
	"""The first node of a graph. Execution starts with this node."""
870
871
872
873
874
875
	outs       = [
		("X_initial_exec", "control flow"),
		("M",              "initial memory"),
		("P_frame_base",   "frame base pointer"),
		("T_args",         "function arguments")
	]
876
877
878
879
880
881
	mode             = "mode_T"
	pinned           = "yes"
	flags            = [ "cfopcode" ]
	singleton        = True
	knownBlock       = True
	block            = "get_irg_start_block(irg)"
882
883

class Store(Op):
884
	"""Stores a value into memory (heap or stack)."""
885
886
887
888
889
	ins   = [
	   ("mem",   "memory dependency"),
	   ("ptr",   "address to store to"),
	   ("value", "value to store"),
	]
890
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
891
892
893
		("M",         "memory result"),
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
894
	]
895
896
897
898
	flags    = [ "fragile", "uses_memory" ]
	pinned   = "exception"
	attr_struct = "store_attr"
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
899
	throws_init = "(flags & cons_throws_exception) != 0"
900
901
902
903
904
905
	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",
906
			to_flags  = "%s == volatility_is_volatile ? cons_volatile : cons_none"
907
908
909
910
911
912
		),
		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",
913
			to_flags  = "%s == align_non_aligned ? cons_unaligned : cons_none"
914
915
		),
	]
916
	constructor_args = [
917
		dict(
918
919
920
			type    = "ir_cons_flags",
			name    = "flags",
			comment = "specifies alignment, volatility and pin state",
921
		),
922
923
924
	]

class Sub(Binop):
925
	"""returns the difference of its operands"""
926
927
928
	flags = []

class SymConst(Op):
929
930
	"""A symbolic constant.

931
932
933
934
935
936
937
938
939
940
941
942
943
	 - *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."""
944
945
946
947
	mode       = "mode_P"
	flags      = [ "constlike", "start_block" ]
	knownBlock = True
	pinned     = "no"
948
	attrs      = [
949
		dict(
950
951
952
953
			type    = "ir_entity*",
			name    = "entity",
			noprop  = True,
			comment = "entity whose address is returned",
954
		)
955
956
	]
	attr_struct = "symconst_attr"
957
	customSerializer = True
958
959
960
	# constructor is written manually at the moment, because of the strange
	# union argument
	noconstructor = True
961
962

class Sync(Op):
963
964
965
966
967
	"""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."""
968
969
970
	mode     = "mode_M"
	flags    = []
	pinned   = "no"
971
	arity    = "dynamic"
972

973
class Tuple(Op):
974
975
976
977
978
979
980
	"""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
981
	just changing its opcode and giving it a new in array."""
982
983
984
	arity  = "variable"
	mode   = "mode_T"
	pinned = "no"
985
	flags  = []
986
987

class Unknown(Op):
988
989
	"""Returns an unknown (at compile- and runtime) value. It is a valid
	optimisation to replace an Unknown by any other constant value."""
990
991
992
	knownBlock = True
	pinned     = "yes"
	block      = "get_irg_start_block(irg)"
Matthias Braun's avatar
Matthias Braun committed
993
	flags      = [ "start_block", "constlike", "dump_noblock" ]
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009

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