X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FLangRef.html;h=03235ecaa40733b7a67825827695ed25d94594d7;hb=94082397d28b172830ba5f449b9dab301e47e5b7;hp=7b70c43831eb2d4d8e38ec42caf2b87f46f2e65f;hpb=8c6bb90b8ef7acad87d3fb7424dce02ee81fedd1;p=oota-llvm.git diff --git a/docs/LangRef.html b/docs/LangRef.html index 7b70c43831e..03235ecaa40 100644 --- a/docs/LangRef.html +++ b/docs/LangRef.html @@ -39,6 +39,7 @@
This document is a reference manual for the LLVM assembly language. LLVM is - an SSA based representation that provides type safety, low level operations, - flexibility, and the capability of representing 'all' high level languages + an SSA based representation that provides type safety, low-level operations, + flexibility, and the capability of representing 'all' high-level languages cleanly. It is the common code representation used throughout all phases of the LLVM compilation strategy.@@ -116,17 +118,17 @@ The LLVM code representation is designed to be used in three different forms: as -an in-memory compiler IR, as an on-disk bytecode representation, suitable for -fast loading by a dynamic compiler, and as a human readable assembly language -representation. This allows LLVM to provide a powerful intermediate +an in-memory compiler IR, as an on-disk bytecode representation (suitable for +fast loading by a Just-In-Time compiler), and as a human readable assembly +language representation. This allows LLVM to provide a powerful intermediate representation for efficient compiler transformations and analysis, while providing a natural means to debug and visualize the transformations. The three different forms of LLVM are all equivalent. This document describes the human readable representation and notation.
-The LLVM representation aims to be a light weight and low level while being +The LLVM representation aims to be a light-weight and low-level while being expressive, typed, and extensible at the same time. It aims to be a "universal -IR" of sorts, by being at a low enough level that high level ideas may be +IR" of sorts, by being at a low enough level that high-level ideas may be cleanly mapped to it (similar to how microprocessors are "universal IR's", allowing many source languages to be mapped to them). By providing type information, LLVM can be used as the target of optimizations: for example, @@ -166,9 +168,17 @@ passes or input to the parser.
LLVM uses three different forms of identifiers, for different purposes:
LLVM requires the values start with a '%' sign for two reasons: Compilers don't @@ -301,8 +311,11 @@ These different primitive types fall into a few useful classifications:
- - +The first class types are perhaps the most +important. Values of these types are the only ones which can be produced by +instructions, passed as arguments, or used as operands to instructions. This +means that all structures and arrays must be manipulated either by pointer or by +component.
@@ -345,7 +358,7 @@ Here are some examples of multidimensional arrays:
[3 x [4 x int]] | : 3x4 array integer values. |
[12 x [10 x float]] | : 2x10 array of single precision floating point values. |
[12 x [10 x float]] | : 12x10 array of single precision floating point values. |
[2 x [3 x [4 x uint]]] | : 2x3x4 array of unsigned integer values. |
<returntype> (<parameter list>) -Where '<parameter list>' is a comma seperated list of type +Where '<parameter list>' is a comma-separated list of type specifiers. Optionally, the parameter list may include a type ..., -which indicates that the function takes a variable number of arguments. Note -that there currently is no way to define a function in LLVM that takes a -variable number of arguments, but it is possible to call a function that -is vararg.
+which indicates that the function takes a variable number of arguments. +Variable argument functions can access their arguments with the variable argument handling intrinsic functions. +
; Declare the string constant as a global constant... -%.LC0 = internal constant [13 x sbyte] c"hello world\0A\00" ; [13 x sbyte]* +%.LC0 = internal constant [13 x sbyte] c"hello world\0A\00" ; [13 x sbyte]* -; Forward declaration of puts -declare int "puts"(sbyte*) ; int(sbyte*)* +; External declaration of the puts function +declare int %puts(sbyte*) ; int(sbyte*)* ; Definition of main function -int "main"() { ; int()* +int %main() { ; int()* ; Convert [13x sbyte]* to sbyte *... %cast210 = getelementptr [13 x sbyte]* %.LC0, long 0, long 0 ; sbyte* @@ -509,19 +522,64 @@ This example is made up of a global variable named ".LC0", an external declaration of the "puts" function, and a function definition for "main".- + In general, a module is made up of a list of global values, where both functions and global variables are global values. Global values are represented by a pointer to a memory location (in this case, a pointer to an array of char, and a -pointer to a function), and can be either "internal" or externally accessible -(which corresponds to the static keyword in C, when used at global scope).
+pointer to a function), and have one of the following linkage types:
+ +
+ +
- internal + +
- Global values with internal linkage are only directly accessible by objects +in the current module. In particular, linking code into a module with an +internal global value may cause the internal to be renamed as necessary to avoid +collisions. Because the symbol is internal to the module, all references can be +updated. This corresponds to the notion of the 'static' keyword in C, +or the idea of "anonymous namespaces" in C++.
+ + +
- linkonce: + +
- "linkonce" linkage is similar to internal linkage, with +the twist that linking together two modules defining the same linkonce +globals will cause one of the globals to be discarded. This is typically used +to implement inline functions. Unreferenced linkonce globals are +allowed to be discarded.
+ + +
- weak: + +
- "weak" linkage is exactly the same as linkonce linkage, +except that unreferenced weak globals may not be discarded. This is +used to implement constructs in C such as "int X;" at global scope.
+ For example, since the ".LC0" variable is defined to be internal, if another module defined a ".LC0" variable and was linked with this one, one of the two would be renamed, preventing a collision. Since "main" -and "puts" are external (i.e., lacking "internal" -declarations), they are accessible outside of the current module. It is illegal -for a function declaration to be "internal".
+and "puts" are external (i.e., lacking any linkage declarations), they +are accessible outside of the current module. It is illegal for a function +declaration to have any linkage type other than "externally visible".
@@ -546,7 +604,7 @@ of memory, and all memory objects in LLVM are accessed through pointers.
-Function Structure +Functions |
The first basic block in program is special in two ways: it is immediately executed on entrance to the function, and it is not allowed to have predecessor basic blocks (i.e. there can not be any branches to the entry block of a -function).
+function). Because the block can have no predecessors, it also cannot have any +PHI nodes.
@@ -592,11 +651,12 @@ typically yield a 'void' value: they produce control flow, not values (the one exception being the 'invoke' instruction).
-There are four different terminator instructions: the 'ret' instruction, the 'br' instruction, the 'switch' instruction, and the 'invoke' instruction.
+href="#i_switch">switch' instruction, the 'invoke' instruction, and the 'unwind' instruction.
@@ -627,8 +687,13 @@ that returns a value that does not match the return type of the function.
+the calling function's context. If the caller is a "call instruction, execution continues at the +instruction after the call. If the caller was an "invoke" instruction, execution continues at the +beginning "normal" of the destination block. If the instruction returns a +value, that value shall set the call or invoke instruction's return value.
+
@@ -664,8 +729,8 @@ target.Upon execution of a conditional 'br' instruction, the 'bool' argument is evaluated. If the value is true, control flows to the -'iftrue' 'label' argument. If "cond" is false, -control flows to the 'iffalse' 'label' argument.
+'iftrue' label argument. If "cond" is false, +control flows to the 'iffalse' label argument.
Example:
@@ -684,7 +749,7 @@ IfUnequal:Syntax:
- switch int <value>, label <defaultdest> [ int <val>, label &dest>, ... ] + switch uint <value>, label <defaultdest> [ int <val>, label &dest>, ... ]@@ -717,15 +782,15 @@ conditional branches, or with a lookup table.
; Emulate a conditional br instruction %Val = cast bool %value to uint - switch int %Val, label %truedest [int 0, label %falsedest ] + switch uint %Val, label %truedest [int 0, label %falsedest ] ; Emulate an unconditional br instruction - switch int 0, label %dest [ ] + switch uint 0, label %dest [ ] ; Implement a jump table: - switch int %val, label %otherwise [ int 0, label %onzero, - int 1, label %onone, - int 2, label %ontwo ] + switch uint %val, label %otherwise [ int 0, label %onzero, + int 1, label %onone, + int 2, label %ontwo ]@@ -741,13 +806,15 @@ conditional branches, or with a lookup table.
Overview:
-The 'invoke' instruction is used to cause control flow to transfer to a -specified function, with the possibility of control flow transfer to either the -'normal label' label or the 'exception label'. The 'call' instruction is closely related, but guarantees -that control flow either never returns from the called function, or that it -returns to the instruction following the 'call' -instruction.+The 'invoke' instruction causes control to transfer to a specified +function, with the possibility of control flow transfer to either the +'normal' label label or the 'exception' +label. If the callee function returns with the "ret" instruction, control flow will return to the +"normal" label. If the callee (or any indirect callees) returns with the "unwind" instruction, control is interrupted, and +continued at the dynamically nearest "except" label.
+
Arguments:
@@ -757,7 +824,7 @@ This instruction requires several arguments:
+an arbitrary pointer to function value.
+difference is that it establishes an association with a label, which is used by the runtime library to unwind the stack.
- +This instruction is used in languages with destructors to ensure that proper +cleanup is performed in the case of either a longjmp or a thrown +exception. Additionally, this is important for implementation of +'catch' clauses in high-level languages that support them.
%retval = invoke int %Test(int 15) - to label %Continue except label %TestCleanup ; {int}:retval set + to label %Continue + except label %TestCleanup ; {int}:retval set+ +
+ unwind ++ +
Binary operators are used to do most of the computation in a program. They require two operands, execute an operation on them, and produce a single value. -The result value of a binary operator is not neccesarily the same type as its +The result value of a binary operator is not necessarily the same type as its operands.
There are several different binary operators:
@@ -965,13 +1056,10 @@ Forum.
boolean value based on a comparison of their two operands.
- -The 'setlt', 'setgt', 'setle', and 'setge' -instructions do not operate on 'bool' typed arguments.
+instructions must be of first class type (it is not +possible to compare 'label's, 'array's, 'structure' +or 'void' values, etc...). Both arguments must have identical +types.
+two operands. The xor is used to implement the "one's complement" +operation, which is the "~" operator in C.
<result> = xor int 4, %var ; yields {int}:result = 4 ^ %var <result> = xor int 15, 40 ; yields {int}:result = 39 <result> = xor int 4, 8 ; yields {int}:result = 12 + <result> = xor int %V, -1 ; yields {int}:result = ~%V @@ -1209,7 +1299,9 @@ argument is unsigned, zero bits shall fill the empty positions.
Memory Access Operations
+A key design point of an SSA-based representation is how it represents memory.
+In LLVM, no memory locations are in SSA form, which makes things very simple.
+This section describes how to read, write, allocate and free memory in LLVM.
@@ -1232,10 +1324,12 @@ system, and returns a pointer of the appropriate type to the program. The
second form of the instruction is a shorter version of the first instruction
that defaults to allocating one element.
-'type' must be a sized type
+'type' must be a sized type.
+
+Memory is allocated using the system "malloc" function, and a pointer
+is returned.
Memory is allocated, a pointer is returned. 'alloca'd memory is
automatically released when the function returns. The 'alloca'
instruction is commonly used to represent automatic variables that must have an
-address available, as well as spilled variables.
+address available. When the function returns (either with the ret or invoke
+instructions), the memory is reclaimed.
+The argument to the 'load' instruction specifies the memory address to
+load from. The pointer must point to a first class
+type. If the load is marked as volatile then the optimizer is
+not allowed to modify the number or order of execution of this load
+with other volatile load and store
+instructions.
There are two arguments to the 'store' instruction: a value to store
and an address to store it into. The type of the '<pointer>'
-operand must be a pointer to the type of the '<value>'
-operand.
+operand must be a pointer to the type of the '<value>' operand.
+If the store is marked as volatile then the optimizer is not
+allowed to modify the number or order of execution of this store with
+other volatile load and store
+instructions.
+The instructions in this catagory are the "miscellaneous" instructions, which defy better classification.
@@ -1498,7 +1604,9 @@ graph representing the function.
The type of the incoming values are specified with the first type field. After
this, the 'phi' instruction takes a list of pairs as arguments, with
-one pair for each predecessor basic block of the current block.
+one pair for each predecessor basic block of the current block. Only values of
+first class type may be used as the value arguments
+to the PHI node. Only labels may be used as the label arguments.
There must be no non-phi instructions between the start of a basic block and the
PHI instructions: i.e. PHI instructions must be first in a basic block.
@@ -1536,7 +1644,8 @@ casting pointers).
+class value, and a type to cast it to, which must also be a first class type.
+This instruction takes a valist value and the type of the argument. It
+returns another valist.
+The 'vanext' instruction advances the specified valist past
+an argument of the specified type. In conjunction with the vaarg instruction, it is used to implement the
+va_arg macro available in C. For more information, see the variable
+argument handling Intrinsic Functions.
It is legal for this instruction to be called in a function which does not take
a variable number of arguments, for example, the vfprintf function.
-va_arg is an LLVM instruction instead of an intrinsic function because the return type depends on an
-argument.
+vanext is an LLVM instruction instead of an intrinsic function because it takes an type as an
+argument.
+
+
+
+
+
+
+
+It is legal for this instruction to be called in a function which does not take
+a variable number of arguments, for example, the vfprintf function.
+
+vaarg is an LLVM instruction instead of an intrinsic function because it takes an type as an
+argument.
+
+
+
+
+
-Accessing memory in SSA form is, well, sticky at best. This section describes how to read, write, allocate and free memory in LLVM.
Semantics:
-Memory is allocated, a pointer is returned.Example:
@@ -1306,7 +1400,9 @@ one element.
Example:
@@ -1321,6 +1417,7 @@ address available, as well as spilled variables.
-Syntax:
<result> = load <ty>* <pointer>
+ <result> = volatile load <ty>* <pointer>
Overview:
@@ -1328,7 +1425,12 @@ The 'load' instruction is used to read from memory.Arguments:
-The argument to the 'load' instruction specifies the memory address to load from. The pointer must point to a first class type.Semantics:
@@ -1350,6 +1452,7 @@ The location of memory pointed to is loaded.
Syntax:
store <ty> <value>, <ty>* <pointer> ; yields {void}
+ volatile store <ty> <value>, <ty>* <pointer> ; yields {void}
Overview:
@@ -1359,8 +1462,11 @@ The 'store' instruction is used to write to memory.Semantics:
The contents of memory are updated to contain
'<value>' at the location specified by the
@@ -1478,7 +1584,7 @@ int* "foo"(%ST* %s) {
Other Operations
-The instructions in this catagory are the "miscellaneous" functions, that defy better classification.
Arguments:
The 'cast' instruction takes a value to cast, which must be a first
-class value, and a type to cast it to, which must also be a first class type.Semantics:
@@ -1608,44 +1717,87 @@ case of the invoke instruction.
'va_arg' Instruction
+
'vanext' Instruction
Syntax:
- <result> = va_arg <va_list>* <arglist>, <retty>
+ <resultarglist> = vanext <va_list> <arglist>, <argty>
Overview:
-The 'va_arg' instruction is used to access arguments passed through the
-"variable argument" area of a function call. It corresponds directly to the
+The 'vanext' instruction is used to access arguments passed through
+the "variable argument" area of a function call. It is used to implement the
va_arg macro in C.Arguments:
-This instruction takes a pointer to a valist value to read a new
-argument from. The return type of the instruction is defined by the second
-argument, a type.Semantics:
-The 'va_arg' instruction works just like the va_arg macro
-available in C. In a target-dependent way, it reads the argument indicated by
-the value the arglist points to, updates the arglist, then returns a value of
-the specified type. This instruction should be used in conjunction with the
-variable argument handling Intrinsic Functions.Example:
See the variable argument processing section.
'vaarg' Instruction
+
+
Syntax:
+
+ <resultval> = vaarg <va_list> <arglist>, <argty>
+
+
+Overview:
+
+The 'vaarg' instruction is used to access arguments passed through
+the "variable argument" area of a function call. It is used to implement the
+va_arg macro in C.Arguments:
+
+This instruction takes a valist value and the type of the argument. It
+returns a value of the specified argument type.
+
+Semantics:
+
+The 'vaarg' instruction loads an argument of the specified type from
+the specified va_list. In conjunction with the vanext instruction, it is used to implement the
+va_arg macro available in C. For more information, see the variable
+argument handling Intrinsic Functions.Example:
+
+See the variable argument processing section.
@@ -1679,37 +1831,35 @@ function.
|
+href="#i_vanext">vanext instruction and these three intrinsic +functions. These functions are related to the similarly named macros defined in +the <stdarg.h> header file.
-All of these functions operate on arguments that use a target-specific type -"va_list". The LLVM assembly language reference manual does not define -what this type is, so all transformations should be prepared to handle +All of these functions operate on arguments that use a target-specific value +type "va_list". The LLVM assembly language reference manual does not +define what this type is, so all transformations should be prepared to handle intrinsics with any type used.
-This example shows how the va_arg instruction +This example shows how the vanext instruction and the variable argument handling intrinsic functions are used.
int %test(int %X, ...) { - ; Allocate two va_list items. On this target, va_list is of type sbyte* - %ap = alloca sbyte* - %aq = alloca sbyte* - ; Initialize variable argument processing - call void (sbyte**)* %llvm.va_start(sbyte** %ap) + %ap = call sbyte*()* %llvm.va_start() ; Read a single integer argument - %tmp = va_arg sbyte** %ap, int + %tmp = vaarg sbyte* %ap, int - ; Demonstrate usage of llvm.va_copy and llvm_va_end - %apv = load sbyte** %ap - call void %llvm.va_copy(sbyte** %aq, sbyte* %apv) - call void %llvm.va_end(sbyte** %aq) + ; Advance to the next argument + %ap2 = vanext sbyte* %ap, int + + ; Demonstrate usage of llvm.va_copy and llvm.va_end + %aq = call sbyte* (sbyte*)* %llvm.va_copy(sbyte* %ap2) + call void %llvm.va_end(sbyte* %aq) ; Stop processing of arguments. - call void %llvm.va_end(sbyte** %ap) + call void %llvm.va_end(sbyte* %ap2) ret int %tmp }@@ -1719,28 +1869,25 @@ int %test(int %X, ...) {
- call void (va_list*)* %llvm.va_start(<va_list>* <arglist>) + call va_list ()* %llvm.va_start()
- -
+The 'llvm.va_start' intrinsic returns a new <arglist> +for subsequent use by the variable argument intrinsics.
+macro available in C. In a target-dependent way, it initializes and returns a +va_list element, so that the next vaarg will produce the first +variable argument passed to the function. Unlike the C va_start macro, +this intrinsic does not need to know the last argument of the function, the +compiler can figure that out.
+ +Note that this intrinsic function is only legal to be called from within the +body of a variable argument function.
@@ -1748,25 +1895,25 @@ last argument of the function, the compiler can figure that out.
- call void (va_list*)* %llvm.va_end(<va_list>* <arglist>) + call void (va_list)* %llvm.va_end(va_list <arglist>)
+The 'llvm.va_end' intrinsic destroys <arglist> which has +been initialized previously with llvm.va_start or llvm.va_copy.
+The argument is a va_list to destroy.
@@ -1777,8 +1924,7 @@ to llvm.va_end.
- call void (va_list*, va_list)* %va_copy(<va_list>* <destarglist>, - <va_list> <srcarglist>) + call va_list (va_list)* %llvm.va_copy(va_list <destarglist>)
- +The argument is the va_list to copy.
@@ -1809,9 +1953,11 @@ arbitrarily complex and require memory allocation, for example.