Most of the work of the compiler is done on an intermediate representation called register transfer language. In this language, the instructions to be output are described, pretty much one by one, in an algebraic form that describes what the instruction does.
RTL is inspired by Lisp lists. It has both an internal form, made up of structures that point at other structures, and a textual form that is used in the machine description and in printed debugging dumps. The textual form uses nested parentheses to indicate the pointers in the internal form.
RTL uses five kinds of objects: expressions, integers, wide integers,
strings and vectors. Expressions are the most important ones. An RTL
expression ("RTX", for short) is a C structure, but it is usually
referred to with a pointer; a type that is given the typedef name
rtx.
An integer is simply an int; their written form uses decimal digits.
A wide integer is an integral object whose type is HOST_WIDE_INT
(see section The Configuration File); their written form uses decimal digits.
A string is a sequence of characters. In core it is represented as a
char * in usual C fashion, and it is written in C syntax as well.
However, strings in RTL may never be null. If you write an empty string in
a machine description, it is represented in core as a null pointer rather
than as a pointer to a null character. In certain contexts, these null
pointers instead of strings are valid. Within RTL code, strings are most
commonly found inside symbol_ref expressions, but they appear in
other contexts in the RTL expressions that make up machine descriptions.
A vector contains an arbitrary number of pointers to expressions. The number of elements in the vector is explicitly present in the vector. The written form of a vector consists of square brackets (`[...]') surrounding the elements, in sequence and with whitespace separating them. Vectors of length zero are not created; null pointers are used instead.
Expressions are classified by expression codes (also called RTX
codes). The expression code is a name defined in `rtl.def', which is
also (in upper case) a C enumeration constant. The possible expression
codes and their meanings are machine-independent. The code of an RTX can
be extracted with the macro GET_CODE (x) and altered with
PUT_CODE (x, newcode).
The expression code determines how many operands the expression contains,
and what kinds of objects they are. In RTL, unlike Lisp, you cannot tell
by looking at an operand what kind of object it is. Instead, you must know
from its context--from the expression code of the containing expression.
For example, in an expression of code subreg, the first operand is
to be regarded as an expression and the second operand as an integer. In
an expression of code plus, there are two operands, both of which
are to be regarded as expressions. In a symbol_ref expression,
there is one operand, which is to be regarded as a string.
Expressions are written as parentheses containing the name of the expression type, its flags and machine mode if any, and then the operands of the expression (separated by spaces).
Expression code names in the `md' file are written in lower case,
but when they appear in C code they are written in upper case. In this
manual, they are shown as follows: const_int.
In a few contexts a null pointer is valid where an expression is normally
wanted. The written form of this is (nil).
For each expression type `rtl.def' specifies the number of
contained objects and their kinds, with four possibilities: `e' for
expression (actually a pointer to an expression), `i' for integer,
`w' for wide integer, `s' for string, and `E' for vector
of expressions. The sequence of letters for an expression code is
called its format. Thus, the format of subreg is
`ei'.
A few other format characters are used occasionally:
u
n
note insn.
S
V
0
There are macros to get the number of operands, the format, and the class of an expression code:
GET_RTX_LENGTH (code)
GET_RTX_FORMAT (code)
GET_RTX_CLASS (code)
o
reg or
mem. subreg is not in this class.
<
NE, EQ, LE, LT, GE, GT,
LEU, LTU, GEU, GTU.
1
neg.
c
NE
and EQ (which have class `<').
2
MINUS.
b
ZERO_EXTRACT or
SIGN_EXTRACT.
3
IF_THEN_ELSE.
i
INSN, JUMP_INSN, and
CALL_INSN).
m
MATCH_DUP.
x
Operands of expressions are accessed using the macros XEXP,
XINT, XWINT and XSTR. Each of these macros takes
two arguments: an expression-pointer (RTX) and an operand number
(counting from zero). Thus,
XEXP (x, 2)
accesses operand 2 of expression x, as an expression.
XINT (x, 2)
accesses the same operand as an integer. XSTR, used in the same
fashion, would access it as a string.
Any operand can be accessed as an integer, as an expression or as a string. You must choose the correct method of access for the kind of value actually stored in the operand. You would do this based on the expression code of the containing expression. That is also how you would know how many operands there are.
For example, if x is a subreg expression, you know that it has
two operands which can be correctly accessed as XEXP (x, 0)
and XINT (x, 1). If you did XINT (x, 0), you
would get the address of the expression operand but cast as an integer;
that might occasionally be useful, but it would be cleaner to write
(int) XEXP (x, 0). XEXP (x, 1) would also
compile without error, and would return the second, integer operand cast as
an expression pointer, which would probably result in a crash when
accessed. Nothing stops you from writing XEXP (x, 28) either,
but this will access memory past the end of the expression with
unpredictable results.
Access to operands which are vectors is more complicated. You can use the
macro XVEC to get the vector-pointer itself, or the macros
XVECEXP and XVECLEN to access the elements and length of a
vector.
XVEC (exp, idx)
XVECLEN (exp, idx)
int.
XVECEXP (exp, idx, eltnum)
XVECLEN (exp, idx).
All the macros defined in this section expand into lvalues and therefore can be used to assign the operands, lengths and vector elements as well as to access them.
RTL expressions contain several flags (one-bit bitfields) that are used in certain types of expression. Most often they are accessed with the following macros:
MEM_VOLATILE_P (x)
mem expressions, nonzero for volatile memory references.
Stored in the volatil field and printed as `/v'.
MEM_IN_STRUCT_P (x)
mem expressions, nonzero for reference to an entire
structure, union or array, or to a component of one. Zero for
references to a scalar variable or through a pointer to a scalar.
Stored in the in_struct field and printed as `/s'.
REG_LOOP_TEST_P
reg expressions, nonzero if this register's entire life is
contained in the exit test code for some loop. Stored in the
in_struct field and printed as `/s'.
REG_USERVAR_P (x)
reg, nonzero if it corresponds to a variable present in
the user's source code. Zero for temporaries generated internally by
the compiler. Stored in the volatil field and printed as
`/v'.
REG_FUNCTION_VALUE_P (x)
reg if it is the place in which this function's
value is going to be returned. (This happens only in a hard
register.) Stored in the integrated field and printed as
`/i'.
The same hard register may be used also for collecting the values of
functions called by this one, but REG_FUNCTION_VALUE_P is zero
in this kind of use.
SUBREG_PROMOTED_VAR_P
subreg if it was made when accessing an object that
was promoted to a wider mode in accord with the PROMOTED_MODE machine
description macro (see section Storage Layout). In this case, the mode of
the subreg is the declared mode of the object and the mode of
SUBREG_REG is the mode of the register that holds the object.
Promoted variables are always either sign- or zero-extended to the wider
mode on every assignment. Stored in the in_struct field and
printed as `/s'.
SUBREG_PROMOTED_UNSIGNED_P
subreg that has SUBREG_PROMOTED_VAR_P nonzero
if the object being referenced is kept zero-extended and zero if it
is kept sign-extended. Stored in the unchanging field and
printed as `/u'.
RTX_UNCHANGING_P (x)
reg or mem if the value is not changed.
(This flag is not set for memory references via pointers to constants.
Such pointers only guarantee that the object will not be changed
explicitly by the current function. The object might be changed by
other functions or by aliasing.) Stored in the
unchanging field and printed as `/u'.
RTX_INTEGRATED_P (insn)
integrated field and printed as `/i'. This
may be deleted; nothing currently depends on it.
SYMBOL_REF_USED (x)
symbol_ref, indicates that x has been used. This is
normally only used to ensure that x is only declared external
once. Stored in the used field.
SYMBOL_REF_FLAG (x)
symbol_ref, this is used as a flag for machine-specific purposes.
Stored in the volatil field and printed as `/v'.
LABEL_OUTSIDE_LOOP_P
label_ref expressions, nonzero if this is a reference to a
label that is outside the innermost loop containing the reference to the
label. Stored in the in_struct field and printed as `/s'.
INSN_DELETED_P (insn)
volatil field and printed as `/v'.
INSN_ANNULLED_BRANCH_P (insn)
insn in the delay slot of a branch insn, indicates that an
annulling branch should be used. See the discussion under
sequence below. Stored in the unchanging field and printed
as `/u'.
INSN_FROM_TARGET_P (insn)
insn in a delay slot of a branch, indicates that the insn
is from the target of the branch. If the branch insn has
INSN_ANNULLED_BRANCH_P set, this insn should only be executed if
the branch is taken. For annulled branches with this bit clear, the
insn should be executed only if the branch is not taken. Stored in the
in_struct field and printed as `/s'.
CONSTANT_POOL_ADDRESS_P (x)
symbol_ref if it refers to part of the current
function's "constants pool". These are addresses close to the
beginning of the function, and GNU CC assumes they can be addressed
directly (perhaps with the help of base registers). Stored in the
unchanging field and printed as `/u'.
CONST_CALL_P (x)
call_insn, indicates that the insn represents a call to a const
function. Stored in the unchanging field and printed as `/u'.
LABEL_PRESERVE_P (x)
code_label, indicates that the label can never be deleted.
Labels referenced by a non-local goto will have this bit set. Stored
in the in_struct field and printed as `/s'.
SCHED_GROUP_P (insn)
use insns before a call_insn may
not be separated from the call_insn. Stored in the in_struct
field and printed as `/s'.
These are the fields which the above macros refer to:
used
symbol_ref, it indicates that an external declaration for
the symbol has already been written.
In a reg, it is used by the leaf register renumbering code to ensure
that each register is only renumbered once.
volatil
mem, symbol_ref and reg
expressions and in insns. In RTL dump files, it is printed as
`/v'.
In a mem expression, it is 1 if the memory reference is volatile.
Volatile memory references may not be deleted, reordered or combined.
In a symbol_ref expression, it is used for machine-specific
purposes.
In a reg expression, it is 1 if the value is a user-level variable.
0 indicates an internal compiler temporary.
In an insn, 1 means the insn has been deleted.
in_struct
mem expressions, it is 1 if the memory datum referred to is
all or part of a structure or array; 0 if it is (or might be) a scalar
variable. A reference through a C pointer has 0 because the pointer
might point to a scalar variable. This information allows the compiler
to determine something about possible cases of aliasing.
In an insn in the delay slot of a branch, 1 means that this insn is from
the target of the branch.
During instruction scheduling, in an insn, 1 means that this insn must be
scheduled as part of a group together with the previous insn.
In reg expressions, it is 1 if the register has its entire life
contained within the test expression of some loop.
In subreg expressions, 1 means that the subreg is accessing
an object that has had its mode promoted from a wider mode.
In label_ref expressions, 1 means that the referenced label is
outside the innermost loop containing the insn in which the label_ref
was found.
In code_label expressions, it is 1 if the label may never be deleted.
This is used for labels which are the target of non-local gotos.
In an RTL dump, this flag is represented as `/s'.
unchanging
reg and mem expressions, 1 means
that the value of the expression never changes.
In subreg expressions, it is 1 if the subreg references an
unsigned object whose mode has been promoted to a wider mode.
In an insn, 1 means that this is an annulling branch.
In a symbol_ref expression, 1 means that this symbol addresses
something in the per-function constants pool.
In a call_insn, 1 means that this instruction is a call to a
const function.
In an RTL dump, this flag is represented as `/u'.
integrated
reg expression, this flag indicates the register
containing the value to be returned by the current function. On
machines that pass parameters in registers, the same register number
may be used for parameters as well, but this flag is not set on such
uses.
A machine mode describes a size of data object and the representation used
for it. In the C code, machine modes are represented by an enumeration
type, enum machine_mode, defined in `machmode.def'. Each RTL
expression has room for a machine mode and so do certain kinds of tree
expressions (declarations and types, to be precise).
In debugging dumps and machine descriptions, the machine mode of an RTL
expression is written after the expression code with a colon to separate
them. The letters `mode' which appear at the end of each machine mode
name are omitted. For example, (reg:SI 38) is a reg
expression with machine mode SImode. If the mode is
VOIDmode, it is not written at all.
Here is a table of machine modes. The term "byte" below refers to an
object of BITS_PER_UNIT bits (see section Storage Layout).
QImode
HImode
PSImode
SImode
PDImode
DImode
TImode
SFmode
DFmode
XFmode
TFmode
CCmode
cc0 (see see section Condition Code Status).
BLKmode
BLKmode will not appear in RTL.
VOIDmode
const_int have mode
VOIDmode because they can be taken to have whatever mode the context
requires. In debugging dumps of RTL, VOIDmode is expressed by
the absence of any mode.
SCmode, DCmode, XCmode, TCmode
SFmode,
DFmode, XFmode, and TFmode, respectively.
CQImode, CHImode, CSImode, CDImode, CTImode, COImode
QImode, HImode,
SImode, DImode, TImode, and OImode,
respectively.
The machine description defines Pmode as a C macro which expands
into the machine mode used for addresses. Normally this is the mode
whose size is BITS_PER_WORD, SImode on 32-bit machines.
The only modes which a machine description must support are
QImode, and the modes corresponding to BITS_PER_WORD,
FLOAT_TYPE_SIZE and DOUBLE_TYPE_SIZE.
The compiler will attempt to use DImode for 8-byte structures and
unions, but this can be prevented by overriding the definition of
MAX_FIXED_MODE_SIZE. Alternatively, you can have the compiler
use TImode for 16-byte structures and unions. Likewise, you can
arrange for the C type short int to avoid using HImode.
Very few explicit references to machine modes remain in the compiler and
these few references will soon be removed. Instead, the machine modes
are divided into mode classes. These are represented by the enumeration
type enum mode_class defined in `machmode.h'. The possible
mode classes are:
MODE_INT
QImode, HImode,
SImode, DImode, and TImode.
MODE_PARTIAL_INT
PSImode and PDImode.
MODE_FLOAT
SFmode, DFmode,
XFmode and TFmode.
MODE_COMPLEX_INT
MODE_COMPLEX_FLOAT
SCmode,
DCmode, XCmode, and TCmode.
MODE_FUNCTION
MODE_CC
CCmode plus
any modes listed in the EXTRA_CC_MODES macro. See section Defining Jump Instruction Patterns,
also see section Condition Code Status.
MODE_RANDOM
VOIDmode and BLKmode are in
MODE_RANDOM.
Here are some C macros that relate to machine modes:
GET_MODE (x)
PUT_MODE (x, newmode)
NUM_MACHINE_MODES
GET_MODE_NAME (m)
GET_MODE_CLASS (m)
GET_MODE_WIDER_MODE (m)
GET_MODE_WIDER_MODE (QImode) returns HImode.
GET_MODE_SIZE (m)
GET_MODE_BITSIZE (m)
GET_MODE_MASK (m)
HOST_BITS_PER_INT.
GET_MODE_ALIGNMENT (m))
GET_MODE_UNIT_SIZE (m)
GET_MODE_SIZE except in the case of complex
modes. For them, the unit size is the size of the real or imaginary
part.
GET_MODE_NUNITS (m)
GET_MODE_SIZE divided by GET_MODE_UNIT_SIZE.
GET_CLASS_NARROWEST_MODE (c)
The global variables byte_mode and word_mode contain modes
whose classes are MODE_INT and whose bitsizes are either
BITS_PER_UNIT or BITS_PER_WORD, respectively. On 32-bit
machines, these are QImode and SImode, respectively.
The simplest RTL expressions are those that represent constant values.
(const_int i)
INTVAL as in
INTVAL (exp), which is equivalent to XWINT (exp, 0).
There is only one expression object for the integer value zero; it is
the value of the variable const0_rtx. Likewise, the only
expression for integer value one is found in const1_rtx, the only
expression for integer value two is found in const2_rtx, and the
only expression for integer value negative one is found in
constm1_rtx. Any attempt to create an expression of code
const_int and value zero, one, two or negative one will return
const0_rtx, const1_rtx, const2_rtx or
constm1_rtx as appropriate.
Similarly, there is only one object for the integer whose value is
STORE_FLAG_VALUE. It is found in const_true_rtx. If
STORE_FLAG_VALUE is one, const_true_rtx and
const1_rtx will point to the same object. If
STORE_FLAG_VALUE is -1, const_true_rtx and
constm1_rtx will point to the same object.
(const_double:m addr i0 i1 ...)
HOST_BITS_PER_WIDE_INT
bits but small enough to fit within twice that number of bits (GNU CC
does not provide a mechanism to represent even larger constants). In
the latter case, m will be VOIDmode.
addr is used to contain the mem expression that corresponds
to the location in memory that at which the constant can be found. If
it has not been allocated a memory location, but is on the chain of all
const_double expressions in this compilation (maintained using an
undisplayed field), addr contains const0_rtx. If it is not
on the chain, addr contains cc0_rtx. addr is
customarily accessed with the macro CONST_DOUBLE_MEM and the
chain field via CONST_DOUBLE_CHAIN.
If m is VOIDmode, the bits of the value are stored in
i0 and i1. i0 is customarily accessed with the macro
CONST_DOUBLE_LOW and i1 with CONST_DOUBLE_HIGH.
If the constant is floating point (regardless of its precision), then
the number of integers used to store the value depends on the size of
REAL_VALUE_TYPE (see section Cross Compilation and Floating Point). The integers
represent a floating point number, but not precisely in the target
machine's or host machine's floating point format. To convert them to
the precise bit pattern used by the target machine, use the macro
REAL_VALUE_TO_TARGET_DOUBLE and friends (see section Output of Data).
The macro CONST0_RTX (mode) refers to an expression with
value 0 in mode mode. If mode mode is of mode class
MODE_INT, it returns const0_rtx. Otherwise, it returns a
CONST_DOUBLE expression in mode mode. Similarly, the macro
CONST1_RTX (mode) refers to an expression with value 1 in
mode mode and similarly for CONST2_RTX.
(const_string str)
(symbol_ref:mode symbol)
symbol_ref contains a mode, which is usually Pmode.
Usually that is the only mode for which a symbol is directly valid.
(label_ref label)
code_label that appears
in the instruction sequence to identify the place where the label
should go.
The reason for using a distinct expression type for code label
references is so that jump optimization can distinguish them.
(const:m exp)
const_int, symbol_ref and
label_ref expressions) combined with plus and
minus. However, not all combinations are valid, since the
assembler cannot do arbitrary arithmetic on relocatable symbols.
m should be Pmode.
(high:m exp)
symbol_ref. The number of bits is machine-dependent and is
normally the number of bits specified in an instruction that initializes
the high order bits of a register. It is used with lo_sum to
represent the typical two-instruction sequence used in RISC machines to
reference a global memory location.
m should be Pmode.
Here are the RTL expression types for describing access to machine registers and to main memory.
(reg:m n)
FIRST_PSEUDO_REGISTER), this stands for a reference to machine
register number n: a hard register. For larger values of
n, it stands for a temporary value or pseudo register.
The compiler's strategy is to generate code assuming an unlimited
number of such pseudo registers, and later convert them into hard
registers or into memory references.
m is the machine mode of the reference. It is necessary because
machines can generally refer to each register in more than one mode.
For example, a register may contain a full word but there may be
instructions to refer to it as a half word or as a single byte, as
well as instructions to refer to it as a floating point number of
various precisions.
Even for a register that the machine can access in only one mode,
the mode must always be specified.
The symbol FIRST_PSEUDO_REGISTER is defined by the machine
description, since the number of hard registers on the machine is an
invariant characteristic of the machine. Note, however, that not
all of the machine registers must be general registers. All the
machine registers that can be used for storage of data are given
hard register numbers, even those that can be used only in certain
instructions or can hold only certain types of data.
A hard register may be accessed in various modes throughout one
function, but each pseudo register is given a natural mode
and is accessed only in that mode. When it is necessary to describe
an access to a pseudo register using a nonnatural mode, a subreg
expression is used.
A reg expression with a machine mode that specifies more than
one word of data may actually stand for several consecutive registers.
If in addition the register number specifies a hardware register, then
it actually represents several consecutive hardware registers starting
with the specified one.
Each pseudo register number used in a function's RTL code is
represented by a unique reg expression.
Some pseudo register numbers, those within the range of
FIRST_VIRTUAL_REGISTER to LAST_VIRTUAL_REGISTER only
appear during the RTL generation phase and are eliminated before the
optimization phases. These represent locations in the stack frame that
cannot be determined until RTL generation for the function has been
completed. The following virtual register numbers are defined:
VIRTUAL_INCOMING_ARGS_REGNUM
ARG_POINTER_REGNUM and the
value of FIRST_PARM_OFFSET.
VIRTUAL_STACK_VARS_REGNUM
FRAME_GROWS_DOWNWARD is defined, this points to immediately
above the first variable on the stack. Otherwise, it points to the
first variable on the stack.
VIRTUAL_STACK_VARS_REGNUM is replaced with the sum of the
register given by FRAME_POINTER_REGNUM and the value
STARTING_FRAME_OFFSET.
VIRTUAL_STACK_DYNAMIC_REGNUM
STACK_POINTER_REGNUM and the value STACK_DYNAMIC_OFFSET.
VIRTUAL_OUTGOING_ARGS_REGNUM
STACK_POINTER_REGNUM).
This virtual register is replaced by the sum of the register given by
STACK_POINTER_REGNUM and the value STACK_POINTER_OFFSET.
(subreg:m reg wordnum)
subreg expressions are used to refer to a register in a machine
mode other than its natural one, or to refer to one register of
a multi-word reg that actually refers to several registers.
Each pseudo-register has a natural mode. If it is necessary to
operate on it in a different mode--for example, to perform a fullword
move instruction on a pseudo-register that contains a single
byte--the pseudo-register must be enclosed in a subreg. In
such a case, wordnum is zero.
Usually m is at least as narrow as the mode of reg, in which
case it is restricting consideration to only the bits of reg that
are in m.
Sometimes m is wider than the mode of reg. These
subreg expressions are often called paradoxical. They are
used in cases where we want to refer to an object in a wider mode but do
not care what value the additional bits have. The reload pass ensures
that paradoxical references are only made to hard registers.
The other use of subreg is to extract the individual registers of
a multi-register value. Machine modes such as DImode and
TImode can indicate values longer than a word, values which
usually require two or more consecutive registers. To access one of the
registers, use a subreg with mode SImode and a
wordnum that says which register.
Storing in a non-paradoxical subreg has undefined results for
bits belonging to the same word as the subreg. This laxity makes
it easier to generate efficient code for such instructions. To
represent an instruction that preserves all the bits outside of those in
the subreg, use strict_low_part around the subreg.
The compilation parameter WORDS_BIG_ENDIAN, if set to 1, says
that word number zero is the most significant part; otherwise, it is
the least significant part.
Between the combiner pass and the reload pass, it is possible to have a
paradoxical subreg which contains a mem instead of a
reg as its first operand. After the reload pass, it is also
possible to have a non-paradoxical subreg which contains a
mem; this usually occurs when the mem is a stack slot
which replaced a pseudo register.
Note that it is not valid to access a DFmode value in SFmode
using a subreg. On some machines the most significant part of a
DFmode value does not have the same format as a single-precision
floating value.
It is also not valid to access a single word of a multi-word value in a
hard register when less registers can hold the value than would be
expected from its size. For example, some 32-bit machines have
floating-point registers that can hold an entire DFmode value.
If register 10 were such a register (subreg:SI (reg:DF 10) 1)
would be invalid because there is no way to convert that reference to
a single machine register. The reload pass prevents subreg
expressions such as these from being formed.
The first operand of a subreg expression is customarily accessed
with the SUBREG_REG macro and the second operand is customarily
accessed with the SUBREG_WORD macro.
(scratch:m)