ir_spec.py 25.6 KB
Newer Older
1
2
3
# This file is part of libFirm.
# Copyright (C) 2012 Karlsruhe Institute of Technology.
#
Matthias Braun's avatar
Matthias Braun committed
4
5
6
# Firm node specifications
# The comments are in (standard python) restructured text format and are used
# to generate documentation.
7
from spec_util import abstract, op, Attribute
8

Matthias Braun's avatar
Matthias Braun committed
9
name = "ir"
10

Matthias Braun's avatar
Matthias Braun committed
11
12
13
@abstract
@op
class Binop(object):
14
	"""Binary nodes have exactly 2 inputs"""
15
	name     = "binop"
16
	ins      = [
17
18
		("left",   "first operand"),
		("right", "second operand"),
19
	]
20
21
	op_index       = 0
	arity_override = "oparity_binary"
22

23
24
25
26
27
28
29
@abstract
@op
class EntConst(object):
	"""Symbolic constant that represents an aspect of an entity"""
	name       = "entconst"
	flags      = [ "constlike", "start_block" ]
	attrs      = [
30
		Attribute("entity", type="ir_entity*", comment="entity to operate on"),
31
32
33
34
	]
	attr_struct = "entconst_attr"
	attrs_name  = "entc"

35
36
@abstract
@op
37
class TypeConst(object):
38
39
40
41
	"""A symbolic constant that represents an aspect of a type"""
	name       = "typeconst"
	flags      = [ "constlike", "start_block" ]
	attrs      = [
42
		Attribute("type", type="ir_type*", comment="type to operate on"),
43
44
45
46
	]
	attr_struct = "typeconst_attr"
	attrs_name  = "typec"

Matthias Braun's avatar
Matthias Braun committed
47
@op
48
class Add(Binop):
49
	"""returns the sum of its operands"""
50
	flags = [ "commutative" ]
51

52
53
54
@op
class Address(EntConst):
	"""Symbolic constant that represents the address of an entity (variable or method)"""
55
	mode = "mode_P"
56

57
58
59
60
@op
class Align(TypeConst):
	"""A symbolic constant that represents the alignment of a type"""

Matthias Braun's avatar
Matthias Braun committed
61
62
@op
class Alloc:
63
64
65
66
	"""Allocates a block of memory on the stack."""
	ins = [
		("mem",  "memory dependency" ),
		("size", "size of the block in bytes" ),
67
	]
68
69
70
	outs = [
		("M",   "memory result"),
		("res", "pointer to newly allocated memory"),
71
	]
72
	attrs = [
73
74
		Attribute("alignment", type="unsigned",
		          comment="alignment of the memory block (must be a power of 2)"),
75
	]
76
	flags       = [ "uses_memory", "const_memory" ]
77
	pinned      = "yes"
78
79
	attr_struct = "alloc_attr"

Matthias Braun's avatar
Matthias Braun committed
80
81
@op
class Anchor:
82
83
84
85
86
87
	"""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."""
88
89
90
91
92
93
94
	mode             = "mode_ANY"
	arity            = "variable"
	flags            = [ "dump_noblock" ]
	pinned           = "yes"
	singleton        = True
	noconstructor    = True
	customSerializer = True
95

Matthias Braun's avatar
Matthias Braun committed
96
@op
97
class And(Binop):
98
	"""returns the result of a bitwise and operation of its operands"""
99
100
	flags    = [ "commutative" ]

Matthias Braun's avatar
Matthias Braun committed
101
102
@op
class ASM:
Matthias Braun's avatar
Matthias Braun committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	"""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)
	"""
130
131
	mode             = "mode_T"
	arity            = "variable"
132
	input_name       = "input"
133
	flags            = [ "keep", "uses_memory" ]
134
	pinned           = "exception"
135
136
137
138
	pinned_init      = "op_pin_state_pinned"
	attr_struct      = "asm_attr"
	attrs_name       = "assem"
	customSerializer = True
139
140
141
	ins   = [
		("mem",    "memory dependency"),
	]
142
	attrs = [
143
144
145
146
147
148
149
150
151
152
153
		Attribute("input_constraints", type="ir_asm_constraint*",
		          comment="input constraints"),
		Attribute("n_output_constraints", type="size_t", noprop=True,
		          comment="number of output constraints"),
		Attribute("output_constraints", type="ir_asm_constraint*",
		          comment="output constraints"),
		Attribute("n_clobbers", type="size_t", noprop=True,
		          comment="number of clobbered registers/memory"),
		Attribute("clobbers", type="ident**",
		          comment="list of clobbered registers/memory"),
		Attribute("text", type="ident*", comment="assembler text"),
154
155
156
157
	]
	# constructor is written manually at the moment, because of the clobbers+
	# constraints arrays needing special handling (2 arguments for 1 attribute)
	noconstructor = True
158

Matthias Braun's avatar
Matthias Braun committed
159
160
@op
class Bad:
161
162
163
164
165
166
	"""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).
167
	Optimizations also set block predecessors to Bad when it becomes clear,
168
169
	that a control flow edge can never be executed.

170
	The gigo optimizations ensures that nodes with Bad as their block, get
Christoph Mallon's avatar
Christoph Mallon committed
171
	replaced by Bad themselves. Nodes with at least 1 Bad input get exchanged
172
173
174
175
	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
176
	optimization phase. Block, Phi, Tuple with only Bad inputs however are
177
178
179
180
	replaced by Bad right away.

	In the future we may use the Bad node to model poison values that arise
	from undefined behaviour like reading uninitialized local variables in C."""
Matthias Braun's avatar
Matthias Braun committed
181
	flags         = [ "start_block", "dump_noblock" ]
182
	pinned        = "yes"
183

Matthias Braun's avatar
Matthias Braun committed
184
185
@op
class Deleted:
186
187
	"""Internal node which is temporary set to nodes which are already removed
	from the graph."""
188
189
190
191
	mode             = "mode_Bad"
	flags            = [ ]
	pinned           = "yes"
	noconstructor    = True
Matthias Braun's avatar
Matthias Braun committed
192
	customSerializer = True # this has no serializer
193

Matthias Braun's avatar
Matthias Braun committed
194
195
@op
class Block:
196
	"""A basic block"""
197
198
199
200
	mode             = "mode_BB"
	block            = "NULL"
	pinned           = "yes"
	arity            = "variable"
201
	input_name       = "cfgpred"
202
	flags            = []
203
	attr_struct      = "block_attr"
204
	attrs            = [
205
206
		Attribute("entity", type="ir_entity*", init="NULL",
		          comment="entity representing this block"),
207
	]
208
	customSerializer = True
Moritz Kroll's avatar
Moritz Kroll committed
209
210

	init = '''
211
	res->attr.block.backedge    = new_backedge_arr(get_irg_obstack(irg), arity);
Moritz Kroll's avatar
Moritz Kroll committed
212
	set_Block_matured(res, 1);
213
214

	/* Create and initialize array for Phi-node construction. */
215
	if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_CONSTRUCTION)) {
216
		res->attr.block.graph_arr = NEW_ARR_DZ(ir_node*, get_irg_obstack(irg), irg->n_loc);
217
	}
218
	'''
219

Matthias Braun's avatar
Matthias Braun committed
220
221
@op
class Builtin:
222
	"""performs a backend-specific builtin."""
223
	ins         = [
224
225
		("mem", "memory dependency"),
	]
226
227
228
	arity       = "variable"
	input_name  = "param"
	outs        = [
229
230
		("M", "memory result"),
		# results follow here
231
	]
232
	mode        = "mode_T"
233
234
	flags       = [ "uses_memory" ]
	attrs       = [
235
236
237
		Attribute("kind", type="ir_builtin_kind", comment="kind of builtin"),
		Attribute("type", type="ir_type*",
		          comment="method type for the builtin call"),
238
	]
239
	pinned      = "exception"
240
241
	pinned_init = "op_pin_state_pinned"
	attr_struct = "builtin_attr"
242
	init        = '''
243
244
245
	assert((get_unknown_type() == type) || is_Method_type(type));
	'''

Matthias Braun's avatar
Matthias Braun committed
246
247
@op
class Call:
248
249
250
251
	"""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."""
252
	ins         = [
253
254
255
		("mem",   "memory dependency"),
		("ptr",   "pointer to called code"),
	]
256
257
258
	arity       = "variable"
	input_name  = "param"
	outs        = [
Matthias Braun's avatar
Matthias Braun committed
259
		("M",                "memory result"),
260
		("T_result",         "tuple containing all results"),
Matthias Braun's avatar
Matthias Braun committed
261
262
		("X_regular",        "control flow when no exception occurs"),
		("X_except",         "control flow when exception occured"),
263
	]
264
265
	flags       = [ "fragile", "uses_memory" ]
	attrs       = [
266
267
		Attribute("type", type="ir_type*",
		          comment="type of the call (usually type of the called procedure)"),
268
269
	]
	attr_struct = "call_attr"
270
	pinned      = "exception"
271
	pinned_init = "op_pin_state_pinned"
272
	throws_init = "false"
273
274
	init = '''
	assert((get_unknown_type() == type) || is_Method_type(type));
275
	'''
276

Matthias Braun's avatar
Matthias Braun committed
277
@op
278
class Cmp(Binop):
279
	"""Compares its two operands and checks whether a specified
280
	   relation (like less or equal) is fulfilled."""
281
	flags = []
282
283
	mode  = "mode_b"
	attrs = [
284
285
		Attribute("relation", type="ir_relation",
		          comment="Comparison relation"),
286
287
	]
	attr_struct = "cmp_attr"
288

Matthias Braun's avatar
Matthias Braun committed
289
290
@op
class Cond:
291
	"""Conditionally change control flow."""
292
293
294
	ins      = [
		("selector",  "condition parameter"),
	]
295
296
297
298
	outs     = [
		("false", "control flow if operand is \"false\""),
		("true",  "control flow if operand is \"true\""),
	]
299
300
	flags    = [ "cfopcode", "forking" ]
	pinned   = "yes"
301
	attrs    = [
302
303
304
		Attribute("jmp_pred", type="cond_jmp_predicate",
		          init="COND_JMP_PRED_NONE",
		          comment = "can indicate the most likely jump"),
305
	]
306
	attr_struct = "cond_attr"
307

Matthias Braun's avatar
Matthias Braun committed
308
309
@op
class Switch:
Matthias Braun's avatar
Matthias Braun committed
310
311
312
313
314
315
316
317
318
	"""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"),
	]
319
	mode   = "mode_T"
Matthias Braun's avatar
Matthias Braun committed
320
321
322
	flags  = [ "cfopcode", "forking" ]
	pinned = "yes"
	attrs  = [
323
324
325
326
		Attribute("n_outs", type="unsigned",
		          comment="number of outputs (including pn_Switch_default)"),
		Attribute("table", type="ir_switch_table*",
		          comment="table describing mapping from input values to Proj numbers"),
Matthias Braun's avatar
Matthias Braun committed
327
328
329
330
	]
	attr_struct = "switch_attr"
	attrs_name  = "switcha"

Matthias Braun's avatar
Matthias Braun committed
331
332
@op
class Confirm:
333
334
335
336
337
338
339
340
341
	"""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."""
342
343
344
345
	ins      = [
		("value",  "value to express a constraint for"),
		("bound",  "value to compare against"),
	]
346
	mode     = "get_irn_mode(irn_value)"
Matthias Braun's avatar
Matthias Braun committed
347
	flags    = [ ]
348
	pinned   = "yes"
349
	attrs    = [
350
351
		Attribute("relation", type="ir_relation",
		          comment="relation of value to bound"),
352
353
354
	]
	attr_struct = "confirm_attr"

Matthias Braun's avatar
Matthias Braun committed
355
356
@op
class Const:
357
	"""Returns a constant value."""
358
	flags      = [ "constlike", "start_block" ]
359
	mode       = "get_tarval_mode(tarval)"
360
	attrs      = [
361
362
		Attribute("tarval", type="ir_tarval*",
		          comment="constant value (a tarval object)"),
363
364
	]
	attr_struct = "const_attr"
365
	attrs_name  = "con"
366

Matthias Braun's avatar
Matthias Braun committed
367
@op
368
class Conv:
369
	"""Converts values between modes"""
370
371
372
373
	flags  = []
	ins    = [
		("op", "operand")
	]
374

Matthias Braun's avatar
Matthias Braun committed
375
376
377
378
379
380
381
382
383
@op
class Bitcast:
	"""Converts a value between modes with different arithmetics but same
	number of bits by reinterpreting the bits in the new mode"""
	flags = []
	ins = [
		("op", "operand")
	]

Matthias Braun's avatar
Matthias Braun committed
384
385
@op
class CopyB:
Matthias Braun's avatar
Matthias Braun committed
386
	"""Copies a block of memory with statically known size/type."""
387
388
389
390
391
	ins   = [
		("mem",  "memory dependency"),
		("dst",  "destination address"),
		("src",  "source address"),
	]
392
393
	mode  = "mode_M"
	flags = [ "uses_memory" ]
394
	attrs = [
395
396
397
398
399
		Attribute("type", type="ir_type*", comment="type of copied data"),
		Attribute("volatility", type="ir_volatility",
		          init="flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile",
		          to_flags="%s == volatility_is_volatile ? cons_volatile : cons_none",
		          comment="volatile CopyB nodes have a visible side-effect and may not be optimized"),
400
401
	]
	attr_struct = "copyb_attr"
402
	constructor_args = [
403
404
		Attribute("flags", type="ir_cons_flags",
		          comment="specifies volatility"),
405
	]
406

Matthias Braun's avatar
Matthias Braun committed
407
408
@op
class Div:
409
	"""returns the quotient of its 2 operands"""
410
411
412
413
414
	ins   = [
		("mem",   "memory dependency"),
		("left",  "first operand"),
		("right", "second operand"),
	]
415
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
416
		("M",         "memory result"),
417
		("res",       "result of computation"),
Matthias Braun's avatar
Matthias Braun committed
418
419
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
420
	]
421
	flags = [ "fragile", "uses_memory", "const_memory" ]
422
	attrs = [
423
424
425
		Attribute("resmode", type="ir_mode*",
		          comment="mode of the result value"),
		Attribute("no_remainder", type="int", init="0"),
426
	]
427
	attr_struct = "div_attr"
428
	pinned      = "exception"
429
	throws_init = "false"
430
431
	op_index    = 1
	arity_override = "oparity_binary"
432

Matthias Braun's avatar
Matthias Braun committed
433
434
@op
class Dummy:
435
436
437
	"""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."""
438
439
	ins        = []
	flags      = [ "cfopcode", "start_block", "constlike", "dump_noblock" ]
440
441
	pinned     = "yes"

Matthias Braun's avatar
Matthias Braun committed
442
443
@op
class End:
444
445
	"""Last node of a graph. It references nodes in endless loops (so called
	keepalive edges)"""
446
447
448
	mode             = "mode_X"
	pinned           = "yes"
	arity            = "dynamic"
449
	input_name       = "keepalive"
450
451
452
	flags            = [ "cfopcode" ]
	block            = "get_irg_end_block(irg)"
	singleton        = True
453

Matthias Braun's avatar
Matthias Braun committed
454
@op
455
class Eor(Binop):
Matthias Braun's avatar
Matthias Braun committed
456
457
458
	"""returns the result of a bitwise exclusive or operation of its operands.

	This is also known as the Xor operation."""
459
	flags    = [ "commutative" ]
460

Matthias Braun's avatar
Matthias Braun committed
461
462
@op
class Free:
463
	"""Frees a block of memory previously allocated by an Alloc node"""
464
465
466
	ins = [
		("mem", "memory dependency" ),
		("ptr", "pointer to the object to free"),
467
	]
468
	mode   = "mode_M"
469
	flags  = [ "uses_memory", "const_memory" ]
470
471
	pinned = "yes"

Matthias Braun's avatar
Matthias Braun committed
472
473
@op
class Id:
Matthias Braun's avatar
Matthias Braun committed
474
475
476
477
	"""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."""
478
479
480
	ins    = [
	   ("pred", "the value which is returned unchanged")
	]
481
482
	flags  = []

Matthias Braun's avatar
Matthias Braun committed
483
484
@op
class IJmp:
485
486
487
	"""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"""
488
489
	mode     = "mode_X"
	pinned   = "yes"
490
491
492
	ins      = [
	   ("target", "target address of the jump"),
	]
493
	flags    = [ "cfopcode", "forking", "keep", "unknown_jump" ]
494

Matthias Braun's avatar
Matthias Braun committed
495
496
@op
class Jmp:
497
	"""Jumps to the block connected through the out-value"""
498
499
500
501
502
	mode     = "mode_X"
	pinned   = "yes"
	ins      = []
	flags    = [ "cfopcode" ]

Matthias Braun's avatar
Matthias Braun committed
503
504
@op
class Load:
505
	"""Loads a value from memory (heap or stack)."""
506
507
508
509
	ins   = [
		("mem", "memory dependency"),
		("ptr",  "address to load from"),
	]
510
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
511
		("M",         "memory result"),
512
		("res",       "result of load operation"),
Matthias Braun's avatar
Matthias Braun committed
513
514
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
515
	]
516
	flags    = [ "fragile", "uses_memory", "const_memory" ]
517
	pinned   = "exception"
518
	attrs    = [
519
520
		Attribute("mode", type="ir_mode*",
		          comment="mode of the value to be loaded"),
521
522
                Attribute("type", type="ir_type*",
                          comment="The type of the object which is stored at ptr (need not match with mode)"),
523
524
525
526
527
528
529
530
		Attribute("volatility", type="ir_volatility",
		          init="flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile",
		          to_flags="%s == volatility_is_volatile ? cons_volatile : cons_none",
		          comment="volatile loads are a visible side-effect and may not be optimized"),
		Attribute("unaligned", type="ir_align",
		          init="flags & cons_unaligned ? align_non_aligned : align_is_aligned",
		          to_flags="%s == align_non_aligned ? cons_unaligned : cons_none",
		          comment="pointers to unaligned loads don't need to respect the load-mode/type alignments"),
531
532
	]
	attr_struct = "load_attr"
533
	constructor_args = [
534
535
		Attribute("flags", type="ir_cons_flags",
		          comment="specifies alignment, volatility and pin state"),
536
	]
537
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
538
	throws_init = "(flags & cons_throws_exception) != 0"
539

Matthias Braun's avatar
Matthias Braun committed
540
@op
541
class Minus:
Andreas Seltenreich's avatar
Andreas Seltenreich committed
542
	"""returns the additive inverse of its operand"""
543
544
545
546
	flags  = []
	ins    = [
		("op", "operand")
	]
547

Matthias Braun's avatar
Matthias Braun committed
548
549
@op
class Mod:
550
551
552
	"""returns the remainder of its operands from an implied division.

	Examples:
553

554
555
556
557
558
	* mod(5,3)   produces 2
	* mod(5,-3)  produces 2
	* mod(-5,3)  produces -2
	* mod(-5,-3) produces -2
	"""
559
560
561
562
563
	ins   = [
		("mem",   "memory dependency"),
		("left",  "first operand"),
		("right", "second operand"),
	]
564
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
565
		("M",         "memory result"),
566
		("res",       "result of computation"),
Matthias Braun's avatar
Matthias Braun committed
567
568
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
569
	]
570
	flags = [ "fragile", "uses_memory", "const_memory" ]
571
	attrs = [
572
		Attribute("resmode", type="ir_mode*", comment="mode of the result"),
573
	]
574
	attr_struct = "mod_attr"
575
	pinned      = "exception"
576
	throws_init = "false"
577
578
	op_index    = 1
	arity_override = "oparity_binary"
Matthias Braun's avatar
Matthias Braun committed
579

580
class Mul(Binop):
581
	"""returns the product of its operands"""
582
583
584
	flags = [ "commutative" ]

class Mulh(Binop):
585
586
	"""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)"""
587
588
	flags = [ "commutative" ]

Matthias Braun's avatar
Matthias Braun committed
589
590
@op
class Mux:
591
592
	"""returns the false or true operand depending on the value of the sel
	operand"""
593
594
595
596
597
	ins    = [
	   ("sel",   "value making the output selection"),
	   ("false", "selected if sel input is false"),
	   ("true",  "selected if sel input is true"),
	]
598
599
	flags  = []

Matthias Braun's avatar
Matthias Braun committed
600
601
@op
class NoMem:
602
	"""Placeholder node for cases where you don't need any memory input"""
603
	mode          = "mode_M"
604
	flags         = [ "dump_noblock", "start_block" ]
605
606
	pinned        = "yes"
	singleton     = True
607

Matthias Braun's avatar
Matthias Braun committed
608
@op
609
class Not:
610
	"""returns the bitwise complement of a value. Works for boolean values, too."""
611
612
613
614
	flags  = []
	ins    = [
		("op", "operand")
	]
615

616
617
618
619
@op
class Offset(EntConst):
	"""Symbolic constant that represents the offset of an entity in its owner type."""

Matthias Braun's avatar
Matthias Braun committed
620
@op
621
class Or(Binop):
622
	"""returns the result of a bitwise or operation of its operands"""
623
624
	flags = [ "commutative" ]

Matthias Braun's avatar
Matthias Braun committed
625
626
@op
class Phi:
627
628
629
	"""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."""
630
631
	pinned        = "yes"
	arity         = "variable"
632
	input_name    = "pred"
633
	flags         = []
634
635
636
637
	attrs         = [
		Attribute("loop", type="int", init="0",
		          comment="wether Phi represents the observable effect of a (possibly) nonterminating loop"),
	]
638
	attr_struct   = "phi_attr"
639
	init          = '''
640
	res->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), arity);'''
Matthias Braun's avatar
Matthias Braun committed
641
	customSerializer = True
642

Matthias Braun's avatar
Matthias Braun committed
643
644
@op
class Pin:
645
646
647
	"""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."""
648
649
650
	ins      = [
		("op", "value which is pinned"),
	]
651
	mode     = "get_irn_mode(irn_op)"
Matthias Braun's avatar
Matthias Braun committed
652
	flags    = [ ]
653
654
	pinned   = "yes"

Matthias Braun's avatar
Matthias Braun committed
655
656
@op
class Proj:
657
	"""returns an entry of a tuple value"""
658
	ins        = [
659
660
		("pred", "the tuple value from which a part is extracted"),
	]
661
662
	flags      = []
	block      = "get_nodes_block(irn_pred)"
663
	usesGraph  = False
664
	attrs      = [
665
		Attribute("num", type="unsigned",
666
		          comment="number of tuple component to be extracted"),
667
	]
668
	attr_struct = "proj_attr"
669

Matthias Braun's avatar
Matthias Braun committed
670
671
@op
class Raise:
672
673
674
	"""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."""
675
676
677
678
	ins    = [
		("mem",     "memory dependency"),
		("exo_ptr", "pointer to exception object to be thrown"),
	]
679
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
680
681
		("M", "memory result"),
		("X", "control flow to exception handler"),
682
	]
Matthias Braun's avatar
Matthias Braun committed
683
	flags  = [ "cfopcode" ]
684
685
	pinned = "yes"

Matthias Braun's avatar
Matthias Braun committed
686
687
@op
class Return:
688
689
	"""Returns from the current function. Takes memory and return values as
	operands."""
690
	ins        = [
691
692
		("mem", "memory dependency"),
	]
693
694
695
696
697
	arity      = "variable"
	input_name = "res"
	mode       = "mode_X"
	flags      = [ "cfopcode" ]
	pinned     = "yes"
698

Matthias Braun's avatar
Matthias Braun committed
699
700
@op
class Sel:
701
702
	"""Computes the address of an array element from the array base pointer and
	an index.
703

704
705
706
707
708
709
	A Sel node must only produce a NULL pointer if the ptr input is NULL."""
	ins = [
		("ptr",   "pointer to array to select from"),
		("index", "index to select"),
	]
	flags  = []
710
	mode   = "mode_P"
711
712
713
714
715
716
717
718
719
720
721
722
723
	attrs  = [
		Attribute("type", type="ir_type*",
		          comment="array type"),
	]
	attr_struct = "sel_attr"

@op
class Member:
	"""Computes the address of a compound type member given the base address
	of an instance of the compound type.

	A Member node must only produce a NULL pointer if the ptr input is NULL."""
	ins = [
724
725
		("ptr", "pointer to object to select from"),
	]
726
	flags   = []
727
	mode    = "mode_P"
728
	attrs   = [
729
730
		Attribute("entity", type="ir_entity*",
		          comment="entity which is selected"),
731
	]
732
	attr_struct = "member_attr"
733

Matthias Braun's avatar
Matthias Braun committed
734
@op
735
class Shl(Binop):
736
	"""Returns its first operands bits shifted left by the amount of the 2nd
737
738
739
740
	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."""
741
742
	flags = []

Matthias Braun's avatar
Matthias Braun committed
743
@op
744
class Shr(Binop):
745
	"""Returns its first operands bits shifted right by the amount of the 2nd
746
747
748
749
	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."""
750
751
	flags = []

Matthias Braun's avatar
Matthias Braun committed
752
@op
753
class Shrs(Binop):
754
755
	"""Returns its first operands bits shifted right by the amount of the 2nd
	operand. The leftmost bit (usually the sign bit) stays the same
756
757
758
759
	(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."""
760
761
	flags = []

Matthias Braun's avatar
Matthias Braun committed
762
763
@op
class Start:
764
	"""The first node of a graph. Execution starts with this node."""
765
	outs = [
766
767
768
769
		("M",              "initial memory"),
		("P_frame_base",   "frame base pointer"),
		("T_args",         "function arguments")
	]
770
771
772
773
	mode      = "mode_T"
	pinned    = "yes"
	flags     = [ "start_block" ]
	singleton = True
774

Matthias Braun's avatar
Matthias Braun committed
775
776
@op
class Store:
777
	"""Stores a value into memory (heap or stack)."""
778
779
780
781
782
	ins   = [
	   ("mem",   "memory dependency"),
	   ("ptr",   "address to store to"),
	   ("value", "value to store"),
	]
783
	outs  = [
Matthias Braun's avatar
Matthias Braun committed
784
785
786
		("M",         "memory result"),
		("X_regular", "control flow when no exception occurs"),
		("X_except",  "control flow when exception occured"),
787
	]
788
789
790
791
	flags    = [ "fragile", "uses_memory" ]
	pinned   = "exception"
	attr_struct = "store_attr"
	pinned_init = "flags & cons_floats ? op_pin_state_floats : op_pin_state_pinned"
792
	throws_init = "(flags & cons_throws_exception) != 0"
793
	attrs = [
794
795
                Attribute("type", type="ir_type*",
                          comment="The type of the object which is stored at ptr (need not match with value's type)"),
796
797
798
799
800
801
802
803
		Attribute("volatility", type="ir_volatility",
		          init="flags & cons_volatile ? volatility_is_volatile : volatility_non_volatile",
		          to_flags="%s == volatility_is_volatile ? cons_volatile : cons_none",
		          comment="volatile stores are a visible side-effect and may not be optimized"),
		Attribute("unaligned", type="ir_align",
		          init="flags & cons_unaligned ? align_non_aligned : align_is_aligned",
		          to_flags="%s == align_non_aligned ? cons_unaligned : cons_none",
		          comment="pointers to unaligned stores don't need to respect the load-mode/type alignments"),
804
	]
805
	constructor_args = [
806
807
		Attribute("flags", type="ir_cons_flags",
		          comment="specifies alignment, volatility and pin state"),
808
809
	]

Matthias Braun's avatar
Matthias Braun committed
810
@op
811
class Sub(Binop):
812
	"""returns the difference of its operands"""
813
814
	flags = []

815
@op
816
817
class Size(TypeConst):
	"""A symbolic constant that represents the size of a type"""
818

Matthias Braun's avatar
Matthias Braun committed
819
820
@op
class Sync:
821
822
823
824
825
	"""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."""
826
827
828
829
	mode       = "mode_M"
	flags      = []
	arity      = "dynamic"
	input_name = "pred"
830

Matthias Braun's avatar
Matthias Braun committed
831
832
@op
class Tuple:
833
834
835
836
837
838
839
	"""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
840
	just changing its opcode and giving it a new in array."""
841
842
843
844
	arity      = "variable"
	input_name = "pred"
	mode       = "mode_T"
	flags      = []
845

Matthias Braun's avatar
Matthias Braun committed
846
847
@op
class Unknown:
848
	"""Returns an unknown (at compile- and runtime) value. It is a valid
Matthias Braun's avatar
Matthias Braun committed
849
850
	optimization to replace an Unknown by any other constant value.

Matthias Braun's avatar
Matthias Braun committed
851
	Be careful when optimising Unknown values, you cannot simply replace
Matthias Braun's avatar
Matthias Braun committed
852
853
	Unknown+x or Unknown<x with a new Unknown node if there are multiple
	users of the original unknown node!"""
854
	pinned     = "yes"
Matthias Braun's avatar
Matthias Braun committed
855
	flags      = [ "start_block", "constlike", "dump_noblock" ]