1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><title>llvm Assembly Language Reference Manual</title></head>
5 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
6 <tr><td> <font size=+5 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>llvm Assembly Language Reference Manual</b></font></td>
10 <li><a href="#abstract">Abstract</a>
11 <li><a href="#introduction">Introduction</a>
12 <li><a href="#identifiers">Identifiers</a>
13 <li><a href="#typesystem">Type System</a>
15 <li><a href="#t_primitive">Primitive Types</a>
17 <li><a href="#t_classifications">Type Classifications</a>
19 <li><a href="#t_derived">Derived Types</a>
21 <li><a href="#t_array" >Array Type</a>
22 <li><a href="#t_function">Function Type</a>
23 <li><a href="#t_pointer">Pointer Type</a>
24 <li><a href="#t_struct" >Structure Type</a>
25 <li><a href="#t_packed" >Packed Type</a>
28 <li><a href="#highlevel">High Level Structure</a>
30 <li><a href="#modulestructure">Module Structure</a>
31 <li><a href="#functionstructure">Function Structure</a>
33 <li><a href="#instref">Instruction Reference</a>
35 <li><a href="#terminators">Terminator Instructions</a>
37 <li><a href="#i_ret" >'<tt>ret</tt>' Instruction</a>
38 <li><a href="#i_br" >'<tt>br</tt>' Instruction</a>
39 <li><a href="#i_switch">'<tt>switch</tt>' Instruction</a>
40 <li><a href="#i_invoke">'<tt>invoke</tt>' Instruction</a>
42 <li><a href="#unaryops">Unary Operations</a>
44 <li><a href="#i_not" >'<tt>not</tt>' Instruction</a>
46 <li><a href="#binaryops">Binary Operations</a>
48 <li><a href="#i_add" >'<tt>add</tt>' Instruction</a>
49 <li><a href="#i_sub" >'<tt>sub</tt>' Instruction</a>
50 <li><a href="#i_mul" >'<tt>mul</tt>' Instruction</a>
51 <li><a href="#i_div" >'<tt>div</tt>' Instruction</a>
52 <li><a href="#i_rem" >'<tt>rem</tt>' Instruction</a>
53 <li><a href="#i_setcc">'<tt>set<i>cc</i></tt>' Instructions</a>
55 <li><a href="#bitwiseops">Bitwise Binary Operations</a>
57 <li><a href="#i_and">'<tt>and</tt>' Instruction</a>
58 <li><a href="#i_or" >'<tt>or</tt>' Instruction</a>
59 <li><a href="#i_xor">'<tt>xor</tt>' Instruction</a>
60 <li><a href="#i_shl">'<tt>shl</tt>' Instruction</a>
61 <li><a href="#i_shr">'<tt>shr</tt>' Instruction</a>
63 <li><a href="#memoryops">Memory Access Operations</a>
65 <li><a href="#i_malloc" >'<tt>malloc</tt>' Instruction</a>
66 <li><a href="#i_free" >'<tt>free</tt>' Instruction</a>
67 <li><a href="#i_alloca" >'<tt>alloca</tt>' Instruction</a>
68 <li><a href="#i_getelementptr">'<tt>getelementptr</tt>' Instruction</a>
69 <li><a href="#i_load" >'<tt>load</tt>' Instruction</a>
70 <li><a href="#i_store" >'<tt>store</tt>' Instruction</a>
72 <li><a href="#otherops">Other Operations</a>
74 <li><a href="#i_cast">'<tt>cast .. to</tt>' Instruction</a>
75 <li><a href="#i_call" >'<tt>call</tt>' Instruction</a>
76 <li><a href="#i_icall">'<tt>icall</tt>' Instruction</a>
77 <li><a href="#i_phi" >'<tt>phi</tt>' Instruction</a>
79 <li><a href="#builtinfunc">Builtin Functions</a>
81 <li><a href="#todo">TODO List</a>
83 <li><a href="#exception">Exception Handling Instructions</a>
84 <li><a href="#synchronization">Synchronization Instructions</a>
86 <li><a href="#extensions">Possible Extensions</a>
88 <li><a href="#i_tailcall">'<tt>tailcall</tt>' Instruction</a>
89 <li><a href="#globalvars">Global Variables</a>
90 <li><a href="#explicitparrellelism">Explicit Parrellelism</a>
92 <li><a href="#related">Related Work</a>
96 <!-- *********************************************************************** -->
97 <p><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
98 <a name="abstract">Abstract
99 </b></font></td></tr></table><ul>
100 <!-- *********************************************************************** -->
103 This document describes the LLVM assembly language. LLVM is an SSA based
104 representation that is a useful midlevel IR, providing type safety, low level
105 operations, flexibility, and the capability to represent 'all' high level
112 <!-- *********************************************************************** -->
113 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
114 <a name="introduction">Introduction
115 </b></font></td></tr></table><ul>
116 <!-- *********************************************************************** -->
118 The LLVM code representation is designed to be used in three different forms: as
119 an in-memory compiler IR, as an on-disk bytecode representation, suitable for
120 fast loading by a dynamic compiler, and as a human readable assembly language
121 representation. This allows LLVM to provide a powerful intermediate
122 representation for efficient compiler transformations and analysis, while
123 providing a natural means to debug and visualize the transformations. The three
124 different forms of LLVM are all equivalent. This document describes the human
125 readable representation and notation.<p>
127 The LLVM representation aims to be a light weight and low level while being
128 expressive, type safe, and extensible at the same time. It aims to be a
129 "universal IR" of sorts, by being at a low enough level that high level ideas
130 may be cleanly mapped to it (similar to how microprocessors are "universal
131 IR's", allowing many source languages to be mapped to them). By providing type
132 safety, LLVM can be used as the target of optimizations: for example, through
133 pointer analysis, it can be proven that a C automatic variable is never accessed
134 outside of the current function... allowing it to be promoted to a simple SSA
135 value instead of a memory location.<p>
137 <!-- _______________________________________________________________________ -->
138 </ul><a name="wellformed"><h4><hr size=0>Well Formedness</h4><ul>
140 It is important to note that this document describes 'well formed' llvm assembly
141 language. There is a difference between what the parser accepts and what is
142 considered 'well formed'. For example, the following instruction is
143 syntactically okay, but not well formed:<p>
146 %x = <a href="#i_add">add</a> int 1, %x
149 ...because only a <tt><a href="#i_phi">phi</a></tt> node may refer to itself.
150 The LLVM api provides a verification pass (created by the
151 <tt>createVerifierPass</tt> function) that may be used to verify that an LLVM
152 module is well formed. This pass is automatically run by the parser after
153 parsing input assembly, and by the optimizer before it outputs bytecode. Often,
154 violations pointed out by the verifier pass indicate bugs in transformation
158 Describe the typesetting conventions here.
161 <!-- *********************************************************************** -->
162 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
163 <a name="identifiers">Identifiers
164 </b></font></td></tr></table><ul>
165 <!-- *********************************************************************** -->
167 LLVM uses three different forms of identifiers, for different purposes:<p>
170 <li>Numeric constants are represented as you would expect: 12, -3 123.421, etc.
171 <li>Named values are represented as a string of characters with a '%' prefix. For example, %foo, %DivisionByZero, %a.really.long.identifier. The actual regular expression used is '<tt>%[a-zA-Z$._][a-zA-Z$._0-9]*</tt>'.
172 <li>Unnamed values are represented as an unsigned numeric value with a '%' prefix. For example, %12, %2, %44.
175 LLVM requires the values start with a '%' sign for two reasons: Compilers don't
176 need to worry about name clashes with reserved words, and the set of reserved
177 words may be expanded in the future without penalty. Additionally, unnamed
178 identifiers allow a compiler to quickly come up with a temporary variable
179 without having to avoid symbol table conflicts.<p>
181 Reserved words in LLVM are very similar to reserved words in other languages.
182 There are keywords for different opcodes ('<tt><a href="#i_add">add</a></tt>',
183 '<tt><a href="#i_cast">cast</a></tt>', '<tt><a href="#i_ret">ret</a></tt>',
184 etc...), for primitive type names ('<tt><a href="#t_void">void</a></tt>',
185 '<tt><a href="#t_uint">uint</a></tt>', etc...), and others. These reserved
186 words cannot conflict with variable names, because none of them start with a '%'
189 Here is an example of LLVM code to multiply the integer variable '<tt>%X</tt>'
194 %result = <a href="#i_mul">mul</a> int %X, 8
197 After strength reduction:
199 %result = <a href="#i_shl">shl</a> int %X, ubyte 3
204 <a href="#i_add">add</a> int %X, %X <i>; yields {int}:%0</i>
205 <a href="#i_add">add</a> int %0, %0 <i>; yields {int}:%1</i>
206 %result = <a href="#i_add">add</a> int %1, %1
209 This last way of multiplying <tt>%X</tt> by 8 illustrates several important lexical features of LLVM:<p>
212 <li>Comments are delimited with a '<tt>;</tt>' and go until the end of line.
213 <li>Unnamed temporaries are created when the result of a computation is not
214 assigned to a named value.
215 <li>Unnamed temporaries are numbered sequentially
218 ...and it also show a convention that we follow in this document. When
219 demonstrating instructions, we will follow an instruction with a comment that
220 defines the type and name of value produced. Comments are shown in italic
225 <!-- *********************************************************************** -->
226 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
227 <a name="typesystem">Type System
228 </b></font></td></tr></table><ul>
229 <!-- *********************************************************************** -->
231 The LLVM type system is critical to the overall usefulness of the language and
232 runtime. Being strongly typed enables a number of optimizations to be performed
233 on the IR directly, without having to do extra analyses on the side before the
234 transformation. A strong type system makes it easier to read the generated code
235 and enables novel analyses and transformations that are not feasible to perform
236 on normal three address code representations.<p>
238 The assembly language form for the type system was heavily influenced by the
239 type problems in the C language<sup><a href="#rw_stroustrup">1</a></sup>.<p>
243 <!-- ======================================================================= -->
244 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
245 <a name="t_primitive">Primitive Types
246 </b></font></td></tr></table><ul>
248 The primitive types are the fundemental building blocks of the LLVM system. The
249 current set of primitive types are as follows:<p>
251 <table border=0 align=center><tr><td>
253 <table border=1 cellspacing=0 cellpadding=4 align=center>
254 <tr><td><tt>void</tt></td> <td>No value</td></tr>
255 <tr><td><tt>ubyte</tt></td> <td>Unsigned 8 bit value</td></tr>
256 <tr><td><tt>ushort</tt></td><td>Unsigned 16 bit value</td></tr>
257 <tr><td><tt>uint</tt></td> <td>Unsigned 32 bit value</td></tr>
258 <tr><td><tt>ulong</tt></td> <td>Unsigned 64 bit value</td></tr>
259 <tr><td><tt>float</tt></td> <td>32 bit floating point value</td></tr>
260 <tr><td><tt>label</tt></td> <td>Branch destination</td></tr>
265 <table border=1 cellspacing=0 cellpadding=4 align=center>
266 <tr><td><tt>bool</tt></td> <td>True or False value</td></tr>
267 <tr><td><tt>sbyte</tt></td> <td>Signed 8 bit value</td></tr>
268 <tr><td><tt>short</tt></td> <td>Signed 16 bit value</td></tr>
269 <tr><td><tt>int</tt></td> <td>Signed 32 bit value</td></tr>
270 <tr><td><tt>long</tt></td> <td>Signed 64 bit value</td></tr>
271 <tr><td><tt>double</tt></td><td>64 bit floating point value</td></tr>
274 </td></tr></table><p>
278 <!-- _______________________________________________________________________ -->
279 </ul><a name="t_classifications"><h4><hr size=0>Type Classifications</h4><ul>
281 These different primitive types fall into a few useful classifications:<p>
283 <table border=1 cellspacing=0 cellpadding=4 align=center>
284 <tr><td><a name="t_signed">signed</td> <td><tt>sbyte, short, int, long, float, double</tt></td></tr>
285 <tr><td><a name="t_unsigned">unsigned</td><td><tt>ubyte, ushort, uint, ulong</tt></td></tr>
286 <tr><td><a name="t_integral">integral</td><td><tt>ubyte, sbyte, ushort, short, uint, int, ulong, long</tt></td></tr>
287 <tr><td><a name="t_floating">floating point</td><td><tt>float, double</tt></td></tr>
288 <tr><td><a name="t_firstclass">first class</td><td><tt>bool, ubyte, sbyte, ushort, short,<br> uint, int, ulong, long, float, double</tt></td></tr>
295 <!-- ======================================================================= -->
296 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
297 <a name="t_derived">Derived Types
298 </b></font></td></tr></table><ul>
300 The real power in LLVM comes from the derived types in the system. This is what
301 allows a programmer to represent arrays, functions, pointers, and other useful
302 types. Note that these derived types may be recursive: For example, it is
303 possible to have a two dimensional array.<p>
307 <!-- _______________________________________________________________________ -->
308 </ul><a name="t_array"><h4><hr size=0>Array Type</h4><ul>
312 The array type is a very simple derived type that arranges elements sequentially
313 in memory. The array type requires a size (number of elements) and an
314 underlying data type.<p>
318 [<# elements> x <elementtype>]
321 The number of elements is a constant integer value, elementtype may be any time
326 <tt>[40 x int ]</tt>: Array of 40 integer values.<br>
327 <tt>[41 x int ]</tt>: Array of 41 integer values.<br>
328 <tt>[40 x uint]</tt>: Array of 40 unsigned integer values.<p>
331 Here are some examples of multidimensional arrays:<p>
333 <table border=0 cellpadding=0 cellspacing=0>
334 <tr><td><tt>[3 x [4 x int]]</tt></td><td>: 3x4 array integer values.</td></tr>
335 <tr><td><tt>[12 x [10 x float]]</tt></td><td>: 2x10 array of single precision floating point values.</td></tr>
336 <tr><td><tt>[2 x [3 x [4 x uint]]]</tt></td><td>: 2x3x4 array of unsigned integer values.</td></tr>
341 <!-- _______________________________________________________________________ -->
342 </ul><a name="t_function"><h4><hr size=0>Function Type</h4><ul>
346 The function type can be thought of as a function signature. It consists of a
347 return type and a list of formal parameter types. Function types are usually
348 used when to build virtual function tables (which are structures of pointers to
349 functions), for indirect function calls, and when defining a function.<p>
353 <returntype> (<parameter list>)
356 Where '<tt><parameter list></tt>' is a comma seperated list of type
357 specifiers. Optionally, the parameter list may include a type <tt>...</tt>,
358 which indicates that the function takes a variable number of arguments. Note
359 that there currently is no way to define a function in LLVM that takes a
360 variable number of arguments, but it is possible to <b>call</b> a function that
365 <table border=0 cellpadding=0 cellspacing=0>
367 <tr><td><tt>int (int)</tt></td><td>: function taking an <tt>int</tt>, returning
368 an <tt>int</tt></td></tr>
370 <tr><td><tt>float (int, int *) *</tt></td><td>: <a href="#t_pointer">Pointer</a>
371 to a function that takes an <tt>int</tt> and a <a href="#t_pointer">pointer</a>
372 to <tt>int</tt>, returning <tt>float</tt>.</td></tr>
374 <tr><td><tt>int (sbyte *, ...)</tt></td><td>: A vararg function that takes at
375 least one <a href="#t_pointer">pointer</a> to <tt>sbyte</tt> (signed char in C),
376 which returns an integer. This is the signature for <tt>printf</tt> in
384 <!-- _______________________________________________________________________ -->
385 </ul><a name="t_struct"><h4><hr size=0>Structure Type</h4><ul>
389 The structure type is used to represent a collection of data members together in memory. Although the runtime is allowed to lay out the data members any way that it would like, they are guaranteed to be "close" to each other.<p>
391 Structures are accessed using '<tt><a href="#i_load">load</a></tt> and '<tt><a href="#i_store">store</a></tt>' by getting a pointer to a field with the '<tt><a href="#i_getelementptr">getelementptr</a></tt>' instruction.<p>
395 { <type list> }
400 <table border=0 cellpadding=0 cellspacing=0>
402 <tr><td><tt>{ int, int, int }</tt></td><td>: a triple of three <tt>int</tt>
405 <tr><td><tt>{ float, int (int *) * }</tt></td><td>: A pair, where the first
406 element is a <tt>float</tt> and the second element is a <a
407 href="#t_pointer">pointer</a> to a <a href="t_function">function</a> that takes
408 an <tt>int</tt>, returning an <tt>int</tt>.</td></tr>
413 <!-- _______________________________________________________________________ -->
414 </ul><a name="t_pointer"><h4><hr size=0>Pointer Type</h4><ul>
418 As in many languages, the pointer type represents a pointer or reference to
419 another object, which must live in memory.<p>
428 <table border=0 cellpadding=0 cellspacing=0>
430 <tr><td><tt>[4x int]*</tt></td><td>: <a href="#t_pointer">pointer</a> to <a
431 href="#t_array">array</a> of four <tt>int</tt> values</td></tr>
433 <tr><td><tt>int (int *) *</tt></td><td>: A <a href="#t_pointer">pointer</a> to a
434 <a href="t_function">function</a> that takes an <tt>int</tt>, returning an
435 <tt>int</tt>.</td></tr>
441 <!-- _______________________________________________________________________ -->
443 </ul><a name="t_packed"><h4><hr size=0>Packed Type</h4><ul>
445 Mention/decide that packed types work with saturation or not. Maybe have a packed+saturated type in addition to just a packed type.<p>
447 Packed types should be 'nonsaturated' because standard data types are not saturated. Maybe have a saturated packed type?<p>
452 <!-- *********************************************************************** -->
453 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
454 <a name="highlevel">High Level Structure
455 </b></font></td></tr></table><ul>
456 <!-- *********************************************************************** -->
459 <!-- ======================================================================= -->
460 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
461 <a name="modulestructure">Module Structure
462 </b></font></td></tr></table><ul>
465 talk about the elements of a module: constant pool and function list.<p>
468 <!-- ======================================================================= -->
469 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
470 <a name="functionstructure">Function Structure
471 </b></font></td></tr></table><ul>
474 talk about the optional constant pool<p>
475 talk about how basic blocks delinate labels<p>
476 talk about how basic blocks end with terminators<p>
479 <!-- *********************************************************************** -->
480 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
481 <a name="instref">Instruction Reference
482 </b></font></td></tr></table><ul>
483 <!-- *********************************************************************** -->
485 List all of the instructions, list valid types that they accept. Tell what they
488 <!-- ======================================================================= -->
489 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
490 <a name="terminators">Terminator Instructions
491 </b></font></td></tr></table><ul>
495 As was mentioned <a href="#functionstructure">previously</a>, every basic block
496 in a program ends with a "Terminator" instruction. All of these terminator
497 instructions yield a '<tt>void</tt>' value: they produce control flow, not
500 There are four different terminator instructions: the '<a
501 href="#i_ret"><tt>ret</tt></a>' instruction, the '<a
502 href="#i_br"><tt>br</tt></a>' instruction, the '<a
503 href="#i_switch"><tt>switch</tt></a>' instruction, and the '<a
504 href="#i_invoke"><tt>invoke</tt></a>' instruction.<p>
507 <!-- _______________________________________________________________________ -->
508 </ul><a name="i_ret"><h4><hr size=0>'<tt>ret</tt>' Instruction</h4><ul>
512 ret <type> <value> <i>; Return a value from a non-void function</i>
513 ret void <i>; Return from void function</i>
518 The '<tt>ret</tt>' instruction is used to return control flow (and optionally a
519 value) from a function, back to the caller.<p>
521 There are two forms of the '<tt>ret</tt>' instructruction: one that returns a
522 value and then causes control flow, and one that just causes control flow to
527 The '<tt>ret</tt>' instruction may return any '<a href="#t_firstclass">first
528 class</a>' type. Notice that a function is not <a href="#wellformed">well
529 formed</a> if there exists a '<tt>ret</tt>' instruction inside of the function
530 that returns a value that does not match the return type of the function.<p>
534 When the '<tt>ret</tt>' instruction is executed, control flow returns back to
535 the calling function's context. If the instruction returns a value, that value
536 shall be propogated into the calling function's data space.<p>
540 ret int 5 <i>; Return an integer value of 5</i>
541 ret void <i>; Return from a void function</i>
545 <!-- _______________________________________________________________________ -->
546 </ul><a name="i_br"><h4><hr size=0>'<tt>br</tt>' Instruction</h4><ul>
550 br bool <cond>, label <iftrue>, label <iffalse>
551 br label <dest> <i>; Unconditional branch</i>
556 The '<tt>br</tt>' instruction is used to cause control flow to transfer to a
557 different basic block in the current function. There are two forms of this
558 instruction, corresponding to a conditional branch and an unconditional
563 The conditional branch form of the '<tt>br</tt>' instruction takes a single
564 '<tt>bool</tt>' value and two '<tt>label</tt>' values. The unconditional form
565 of the '<tt>br</tt>' instruction takes a single '<tt>label</tt>' value as a
570 Upon execution of a conditional '<tt>br</tt>' instruction, the '<tt>bool</tt>'
571 argument is evaluated. If the value is <tt>true</tt>, control flows to the
572 '<tt>iftrue</tt>' '<tt>label</tt>' argument. If "cond" is <tt>false</tt>,
573 control flows to the '<tt>iffalse</tt>' '<tt>label</tt>' argument.<p>
578 %cond = <a href="#i_setcc">seteq</a> int %a, %b
579 br bool %cond, label %IfEqual, label %IfUnequal
581 <a href="#i_ret">ret</a> bool true
583 <a href="#i_ret">ret</a> bool false
587 <!-- _______________________________________________________________________ -->
588 </ul><a name="i_switch"><h4><hr size=0>'<tt>switch</tt>' Instruction</h4><ul>
592 <i>; Definitions for lookup indirect branch</i>
593 %switchtype = type [<anysize> x { uint, label }]
595 <i>; Lookup indirect branch</i>
596 switch uint <value>, label <defaultdest>, %switchtype <switchtable>
598 <i>; Indexed indirect branch</i>
599 switch uint <idxvalue>, label <defaultdest>, [<anysize> x label] <desttable>
604 The '<tt>switch</tt>' instruction is used to transfer control flow to one of
605 several different places. It is a generalization of the '<tt>br</tt>'
606 instruction, allowing a branch to occur to one of many possible destinations.<p>
608 The '<tt>switch</tt>' statement supports two different styles of indirect
609 branching: lookup branching and indexed branching. Lookup branching is
610 generally useful if the values to switch on are spread far appart, where index
611 branching is useful if the values to switch on are generally dense.<p>
613 The two different forms of the '<tt>switch</tt>' statement are simple hints to
614 the underlying virtual machine implementation. For example, a virtual machine
615 may choose to implement a small indirect branch table as a series of predicated
616 comparisons: if it is faster for the target architecture.<p>
620 The lookup form of the '<tt>switch</tt>' instruction uses three parameters: a
621 '<tt>uint</tt>' comparison value '<tt>value</tt>', a default '<tt>label</tt>'
622 destination, and an array of pairs of comparison value constants and
623 '<tt>label</tt>'s. The sized array must be a constant value.<p>
625 The indexed form of the '<tt>switch</tt>' instruction uses three parameters: an
626 '<tt>uint</tt>' index value, a default '<tt>label</tt>' and a sized array of
627 '<tt>label</tt>'s. The '<tt>dests</tt>' array must be a constant array.
631 The lookup style switch statement specifies a table of values and destinations.
632 When the '<tt>switch</tt>' instruction is executed, this table is searched for
633 the given value. If the value is found, the corresponding destination is
636 The index branch form simply looks up a label element directly in a table and
639 In either case, the compiler knows the static size of the array, because it is
640 provided as part of the constant values type.<p>
644 <i>; Emulate a conditional br instruction</i>
645 %Val = <a href="#i_cast">cast</a> bool %value to uint
646 switch uint %Val, label %truedest, [1 x label] [label %falsedest ]
648 <i>; Emulate an unconditional br instruction</i>
649 switch uint 0, label %dest, [ 0 x label] [ ]
651 <i>; Implement a jump table using the constant pool:</i>
652 void "testmeth"(int %arg0)
653 %switchdests = [3 x label] [ label %onzero, label %onone, label %ontwo ]
656 switch uint %val, label %otherwise, [3 x label] %switchdests...
660 <i>; Implement the equivilent jump table directly:</i>
661 switch uint %val, label %otherwise, [3 x label] [ label %onzero,
669 <!-- _______________________________________________________________________ -->
670 </ul><a name="i_invoke"><h4><hr size=0>'<tt>invoke</tt>' Instruction</h4><ul>
674 <result> = invoke <ptr to function ty> %<function ptr val>(<function args>)
675 to label <normal label> except label <exception label>
678 <h5>Overview:</h5> The '<tt>invoke</tt>' instruction is used to cause control
679 flow to transfer to a specified function, with the possibility of control flow
680 transfer to either the '<tt>normal label</tt>' label or the '<tt>exception
681 label</tt>'. The '<tt><a href="#i_call">call</a></tt>' instruction is closely
682 related, but guarantees that control flow either never returns from the called
683 function, or that it returns to the instruction succeeding the '<tt><a
684 href="#i_call">call</a></tt>' instruction.<p>
688 This instruction requires several arguments:<p>
691 <li>'<tt>ptr to function ty</tt>': shall be the signature of the pointer to
692 function value being invoked. In most cases, this is a direct method
693 invocation, but indirect <tt>invoke</tt>'s are just as possible, branching off
694 an arbitrary pointer to function value.<p>
696 <li>'<tt>function ptr val</tt>': An LLVM value containing a pointer to a
697 function to be invoked.
699 <li>'<tt>function args</tt>': argument list whose types match the function
700 signature argument types.
702 <li>'<tt>normal label</tt>': the label reached when the called function executes
703 a '<tt><a href="#i_ret">ret</a></tt>' instruction.
705 <li>'<tt>exception label</tt>': the label reached when an exception is thrown.
710 This instruction is designed to operate as a standard '<tt><a href="#i_call">call</a></tt>' instruction in most regards. The primary difference is that it assiciates a label with the function invocation that may be accessed via the runtime library provided by the execution environment. This instruction is used in languages with destructors to ensure that proper cleanup is performed in the case of either a <tt>longjmp</tt> or a thrown exception. Additionally, this is important for implementation of '<tt>catch</tt>' clauses in high-level languages that support them.<p>
712 For a more comprehensive explanation of this instruction look in the llvm/docs/2001-05-18-ExceptionHandling.txt document.<p>
716 %retval = invoke int %Test(int 15)
717 to label %Continue except label %TestCleanup <i>; {int}:retval set</i>
722 <!-- ======================================================================= -->
723 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
724 <a name="unaryops">Unary Operations
725 </b></font></td></tr></table><ul>
727 Unary operators are used to do a simple operation to a single value.<p>
729 There is only one unary operator: the '<a href="#i_not"><tt>not</tt></a>' instruction.<p>
732 <!-- _______________________________________________________________________ -->
733 </ul><a name="i_not"><h4><hr size=0>'<tt>not</tt>' Instruction</h4><ul>
737 <result> = not <ty> <var> <i>; yields {ty}:result</i>
741 The '<tt>not</tt>' instruction returns the <a href="#logical_integrals">logical</a> inverse of its operand.<p>
744 The single argument to '<tt>not</tt>' must be of of <a href="#t_integral">integral</a> type.<p>
748 The '<tt>not</tt>' instruction returns the <a href="#logical_integrals">logical</a> inverse of an <a href="#t_integral">integral</a> type.<p>
751 <result> = xor bool true, <var> <i>; yields {bool}:result</i>
756 %x = not int 1 <i>; {int}:x is now equal to 0</i>
757 %x = not bool true <i>; {bool}:x is now equal to false</i>
762 <!-- ======================================================================= -->
763 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
764 <a name="binaryops">Binary Operations
765 </b></font></td></tr></table><ul>
767 Binary operators are used to do most of the computation in a program. They
768 require two operands, execute an operation on them, and produce a single value.
769 The result value of a binary operator is not neccesarily the same type as its
772 There are several different binary operators:<p>
775 <!-- _______________________________________________________________________ -->
776 </ul><a name="i_add"><h4><hr size=0>'<tt>add</tt>' Instruction</h4><ul>
780 <result> = add <ty> <var1>, <var2> <i>; yields {ty}:result</i>
784 The '<tt>add</tt>' instruction returns the sum of its two operands.<p>
787 The two arguments to the '<tt>add</tt>' instruction must be either <a href="#t_integral">integral</a> or <a href="#t_floating">floating point</a> values. Both arguments must have identical types.<p>
794 <result> = add int 4, %var <i>; yields {int}:result = 4 + %var</i>
798 <!-- _______________________________________________________________________ -->
799 </ul><a name="i_sub"><h4><hr size=0>'<tt>sub</tt>' Instruction</h4><ul>
803 <result> = sub <ty> <var1>, <var2> <i>; yields {ty}:result</i>
808 The '<tt>sub</tt>' instruction returns the difference of its two operands.<p>
810 Note that the '<tt>sub</tt>' instruction is used to represent the '<tt>neg</tt>'
811 instruction present in most other intermediate representations.<p>
815 The two arguments to the '<tt>sub</tt>' instruction must be either <a
816 href="#t_integral">integral</a> or <a href="#t_floating">floating point</a>
817 values. Both arguments must have identical types.<p>
824 <result> = sub int 4, %var <i>; yields {int}:result = 4 - %var</i>
825 <result> = sub int 0, %val <i>; yields {int}:result = -%var</i>
828 <!-- _______________________________________________________________________ -->
829 </ul><a name="i_mul"><h4><hr size=0>'<tt>mul</tt>' Instruction</h4><ul>
833 <result> = mul <ty> <var1>, <var2> <i>; yields {ty}:result</i>
837 The '<tt>mul</tt>' instruction returns the product of its two operands.<p>
840 The two arguments to the '<tt>mul</tt>' instruction must be either <a href="#t_integral">integral</a> or <a href="#t_floating">floating point</a> values. Both arguments must have identical types.<p>
845 There is no signed vs unsigned multiplication. The appropriate action is taken
846 based on the type of the operand. <p>
851 <result> = mul int 4, %var <i>; yields {int}:result = 4 * %var</i>
855 <!-- _______________________________________________________________________ -->
856 </ul><a name="i_div"><h4><hr size=0>'<tt>div</tt>' Instruction</h4><ul>
860 <result> = div <ty> <var1>, <var2> <i>; yields {ty}:result</i>
865 The '<tt>div</tt>' instruction returns the quotient of its two operands.<p>
869 The two arguments to the '<tt>div</tt>' instruction must be either <a
870 href="#t_integral">integral</a> or <a href="#t_floating">floating point</a>
871 values. Both arguments must have identical types.<p>
878 <result> = div int 4, %var <i>; yields {int}:result = 4 / %var</i>
882 <!-- _______________________________________________________________________ -->
883 </ul><a name="i_rem"><h4><hr size=0>'<tt>rem</tt>' Instruction</h4><ul>
887 <result> = rem <ty> <var1>, <var2> <i>; yields {ty}:result</i>
891 The '<tt>rem</tt>' instruction returns the remainder from the division of its two operands.<p>
894 The two arguments to the '<tt>rem</tt>' instruction must be either <a href="#t_integral">integral</a> or <a href="#t_floating">floating point</a> values. Both arguments must have identical types.<p>
897 TODO: remainder or modulus?<p>
902 <result> = rem int 4, %var <i>; yields {int}:result = 4 % %var</i>
906 <!-- _______________________________________________________________________ -->
907 </ul><a name="i_setcc"><h4><hr size=0>'<tt>set<i>cc</i></tt>' Instructions</h4><ul>
911 <result> = seteq <ty> <var1>, <var2> <i>; yields {bool}:result</i>
912 <result> = setne <ty> <var1>, <var2> <i>; yields {bool}:result</i>
913 <result> = setlt <ty> <var1>, <var2> <i>; yields {bool}:result</i>
914 <result> = setgt <ty> <var1>, <var2> <i>; yields {bool}:result</i>
915 <result> = setle <ty> <var1>, <var2> <i>; yields {bool}:result</i>
916 <result> = setge <ty> <var1>, <var2> <i>; yields {bool}:result</i>
920 The '<tt>set<i>cc</i></tt>' family of instructions returns a boolean value based on a comparison of their two operands.<p>
922 <h5>Arguments:</h5> The two arguments to the '<tt>set<i>cc</i></tt>'
923 instructions must be of <a href="#t_firstclass">first class</a> or <a
924 href="#t_pointer">pointer</a> type (it is not possible to compare
925 '<tt>label</tt>'s, '<tt>array</tt>'s, '<tt>structure</tt>' or '<tt>void</tt>'
926 values). Both arguments must have identical types.<p>
928 The '<tt>setlt</tt>', '<tt>setgt</tt>', '<tt>setle</tt>', and '<tt>setge</tt>' instructions do not operate on '<tt>bool</tt>' typed arguments.<p>
931 The '<tt>seteq</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if both operands are equal.<br>
932 The '<tt>setne</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if both operands are unequal.<br>
933 The '<tt>setlt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if the first operand is less than the second operand.<br>
934 The '<tt>setgt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if the first operand is greater than the second operand.<br>
935 The '<tt>setle</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if the first operand is less than or equal to the second operand.<br>
936 The '<tt>setge</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if the first operand is greater than or equal to the second operand.<p>
940 <result> = seteq int 4, 5 <i>; yields {bool}:result = false</i>
941 <result> = setne float 4, 5 <i>; yields {bool}:result = true</i>
942 <result> = setlt uint 4, 5 <i>; yields {bool}:result = true</i>
943 <result> = setgt sbyte 4, 5 <i>; yields {bool}:result = false</i>
944 <result> = setle sbyte 4, 5 <i>; yields {bool}:result = true</i>
945 <result> = setge sbyte 4, 5 <i>; yields {bool}:result = false</i>
950 <!-- ======================================================================= -->
951 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
952 <a name="bitwiseops">Bitwise Binary Operations
953 </b></font></td></tr></table><ul>
955 Bitwise binary operators are used to do various forms of bit-twiddling in a program. They are generally very efficient instructions, and can commonly be strength reduced from other instructions. They require two operands, execute an operation on them, and produce a single value. The resulting value of the bitwise binary operators is always the same type as its first operand.<p>
957 <!-- _______________________________________________________________________ -->
958 </ul><a name="i_and"><h4><hr size=0>'<tt>and</tt>' Instruction</h4><ul>
962 <result> = and <ty> <var1>, <var2> <i>; yields {ty}:result</i>
966 The '<tt>and</tt>' instruction returns the bitwise logical and of its two operands.<p>
969 The two arguments to the '<tt>and</tt>' instruction must be either <a href="#t_integral">integral</a> or <a href="#t_bool"><tt>bool</tt></a> values. Both arguments must have identical types.<p>
978 <result> = and int 4, %var <i>; yields {int}:result = 4 & %var</i>
979 <result> = and int 15, 40 <i>; yields {int}:result = 8</i>
980 <result> = and int 4, 8 <i>; yields {int}:result = 0</i>
985 <!-- _______________________________________________________________________ -->
986 </ul><a name="i_or"><h4><hr size=0>'<tt>or</tt>' Instruction</h4><ul>
990 <result> = or <ty> <var1>, <var2> <i>; yields {ty}:result</i>
993 <h5>Overview:</h5> The '<tt>or</tt>' instruction returns the bitwise logical
994 inclusive or of its two operands.<p>
998 The two arguments to the '<tt>or</tt>' instruction must be either <a
999 href="#t_integral">integral</a> or <a href="#t_bool"><tt>bool</tt></a> values.
1000 Both arguments must have identical types.<p>
1009 <result> = or int 4, %var <i>; yields {int}:result = 4 | %var</i>
1010 <result> = or int 15, 40 <i>; yields {int}:result = 47</i>
1011 <result> = or int 4, 8 <i>; yields {int}:result = 12</i>
1015 <!-- _______________________________________________________________________ -->
1016 </ul><a name="i_xor"><h4><hr size=0>'<tt>xor</tt>' Instruction</h4><ul>
1020 <result> = xor <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1025 The '<tt>xor</tt>' instruction returns the bitwise logical exclusive or of its
1030 The two arguments to the '<tt>xor</tt>' instruction must be either <a
1031 href="#t_integral">integral</a> or <a href="#t_bool"><tt>bool</tt></a> values.
1032 Both arguments must have identical types.<p>
1041 <result> = xor int 4, %var <i>; yields {int}:result = 4 ^ %var</i>
1042 <result> = xor int 15, 40 <i>; yields {int}:result = 39</i>
1043 <result> = xor int 4, 8 <i>; yields {int}:result = 12</i>
1047 <!-- _______________________________________________________________________ -->
1048 </ul><a name="i_shl"><h4><hr size=0>'<tt>shl</tt>' Instruction</h4><ul>
1052 <result> = shl <ty> <var1>, ubyte <var2> <i>; yields {ty}:result</i>
1057 The '<tt>shl</tt>' instruction returns the first operand shifted to the left a
1058 specified number of bits.
1062 The first argument to the '<tt>shl</tt>' instruction must be an <a
1063 href="#t_integral">integral</a> type. The second argument must be an
1064 '<tt>ubyte</tt>' type.<p>
1067 ... 0 bits are shifted into the emptied bit positions...<p>
1072 <result> = shl int 4, ubyte %var <i>; yields {int}:result = 4 << %var</i>
1073 <result> = shl int 4, ubyte 2 <i>; yields {int}:result = 16</i>
1074 <result> = shl int 1, ubyte 10 <i>; yields {int}:result = 1024</i>
1078 <!-- _______________________________________________________________________ -->
1079 </ul><a name="i_shr"><h4><hr size=0>'<tt>shr</tt>' Instruction</h4><ul>
1084 <result> = shr <ty> <var1>, ubyte <var2> <i>; yields {ty}:result</i>
1088 The '<tt>shr</tt>' instruction returns the first operand shifted to the right a specified number of bits.
1091 The first argument to the '<tt>shr</tt>' instruction must be an <a href="#t_integral">integral</a> type. The second argument must be an '<tt>ubyte</tt>' type.<p>
1094 ... if the first argument is a <a href="#t_signed">signed</a> type, the most significant bit is duplicated in the newly free'd bit positions. If the first argument is unsigned, zeros shall fill the empty positions...<p>
1098 <result> = shr int 4, ubyte %var <i>; yields {int}:result = 4 >> %var</i>
1099 <result> = shr int 4, ubyte 1 <i>; yields {int}:result = 2</i>
1100 <result> = shr int 4, ubyte 2 <i>; yields {int}:result = 1</i>
1101 <result> = shr int 4, ubyte 3 <i>; yields {int}:result = 0</i>
1108 <!-- ======================================================================= -->
1109 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
1110 <a name="memoryops">Memory Access Operations
1111 </b></font></td></tr></table><ul>
1113 Accessing memory in SSA form is, well, sticky at best. This section describes how to read and write memory in LLVM.<p>
1116 <!-- _______________________________________________________________________ -->
1117 </ul><a name="i_malloc"><h4><hr size=0>'<tt>malloc</tt>' Instruction</h4><ul>
1121 <result> = malloc <type>, uint <NumElements> <i>; yields {type*}:result</i>
1122 <result> = malloc <type> <i>; yields {type*}:result</i>
1126 The '<tt>malloc</tt>' instruction allocates memory from the system heap and returns a pointer to it.<p>
1130 The the '<tt>malloc</tt>' instruction allocates
1131 <tt>sizeof(<type>)*NumElements</tt> bytes of memory from the operating
1132 system, and returns a pointer of the appropriate type to the program. The
1133 second form of the instruction is a shorter version of the first instruction
1134 that defaults to allocating one element.<p>
1136 '<tt>type</tt>' must be a sized type<p>
1139 Memory is allocated, a pointer is returned.<p>
1143 %array = malloc [4 x ubyte ] <i>; yields {[%4 x ubyte]*}:array</i>
1145 %size = <a href="#i_add">add</a> uint 2, 2 <i>; yields {uint}:size = uint 4</i>
1146 %array1 = malloc ubyte, uint 4 <i>; yields {ubyte*}:array1</i>
1147 %array2 = malloc [12 x ubyte], uint %size <i>; yields {[12 x ubyte]*}:array2</i>
1151 <!-- _______________________________________________________________________ -->
1152 </ul><a name="i_free"><h4><hr size=0>'<tt>free</tt>' Instruction</h4><ul>
1156 free <type> <value> <i>; yields {void}</i>
1161 The '<tt>free</tt>' instruction returns memory back to the unused memory heap, to be reallocated in the future.<p>
1166 '<tt>value</tt>' shall be a pointer value that points to a value that was allocated with the '<tt><a href="#i_malloc">malloc</a></tt>' instruction.<p>
1170 Memory is available for use after this point. The contents of the '<tt>value</tt>' pointer are undefined after this instruction.<p>
1175 %array = <a href="#i_malloc">malloc</a> [4 x ubyte] <i>; yields {[4 x ubyte]*}:array</i>
1176 free [4 x ubyte]* %array
1180 <!-- _______________________________________________________________________ -->
1181 </ul><a name="i_alloca"><h4><hr size=0>'<tt>alloca</tt>' Instruction</h4><ul>
1185 <result> = alloca <type>, uint <NumElements> <i>; yields {type*}:result</i>
1186 <result> = alloca <type> <i>; yields {type*}:result</i>
1191 The '<tt>alloca</tt>' instruction allocates memory on the current stack frame of
1192 the procedure that is live until the current function returns to its caller.<p>
1196 The the '<tt>alloca</tt>' instruction allocates
1197 <tt>sizeof(<type>)*NumElements</tt> bytes of memory on the runtime stack,
1198 returning a pointer of the appropriate type to the program. The second form of
1199 the instruction is a shorter version of the first that defaults to allocating
1202 '<tt>type</tt>' may be any sized type.<p>
1206 Memory is allocated, a pointer is returned. '<tt>alloca</tt>'d memory is
1207 automatically released when the function returns. The '<tt>alloca</tt>'
1208 instruction is commonly used to represent automatic variables that must have an
1209 address available, as well as spilled variables.<p>
1213 %ptr = alloca int <i>; yields {int*}:ptr</i>
1214 %ptr = alloca int, uint 4 <i>; yields {int*}:ptr</i>
1218 <!-- _______________________________________________________________________ -->
1219 </ul><a name="i_getelementptr"><h4><hr size=0>'<tt>getelementptr</tt>' Instruction</h4><ul>
1223 <result> = getelementptr <ty>* <ptrval>{, uint <aidx>|, ubyte <sidx>}*
1228 The '<tt>getelementptr</tt>' instruction is used to get the address of a
1229 subelement of an aggregate data structure. In addition to being present as an
1230 explicit instruction, the '<tt>getelementptr</tt>' functionality is present in
1231 both the '<tt><a href="#i_load">load</a></tt>' and '<tt><a
1232 href="#i_store">store</a></tt>' instructions to allow more compact specification
1233 of common expressions.<p>
1237 This instruction takes a list of <tt>uint</tt> values and <tt>ubyte</tt>
1238 constants that indicate what form of addressing to perform. The actual types of
1239 the arguments provided depend on the type of the first pointer argument. The
1240 '<tt>getelementptr</tt>' instruction is used to index down through the type
1241 levels of a structure.<p>
1250 %aptr = getelementptr {int, [12 x ubyte]}* %sptr, 1 <i>; yields {[12 x ubyte]*}:aptr</i>
1251 %ub = load [12x ubyte]* %aptr, 4 <i>;yields {ubyte}:ub</i>
1256 <!-- _______________________________________________________________________ -->
1257 </ul><a name="i_load"><h4><hr size=0>'<tt>load</tt>' Instruction</h4><ul>
1261 <result> = load <ty>* <pointer>
1262 <result> = load <ty>* <pointer> <index list>
1266 The '<tt>load</tt>' instruction is used to read from memory.<p>
1270 There are three forms of the '<tt>load</tt>' instruction: one for reading from a general pointer, one for reading from a pointer to an array, and one for reading from a pointer to a structure.<p>
1272 In the first form, '<tt><ty></tt>' must be a pointer to a simple type (a primitive type or another pointer).<p>
1274 In the second form, '<tt><ty></tt>' must be a pointer to an array, and a list of one or more indices is provided as indexes into the (possibly multidimensional) array. No bounds checking is performed on array reads.<p>
1276 In the third form, the pointer must point to a (possibly nested) structure. There shall be one ubyte argument for each level of dereferencing involved.<p>
1283 %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1284 <a href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
1285 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1287 %array = <a href="#i_malloc">malloc</a> [4 x ubyte] <i>; yields {[4 x ubyte]*}:array</i>
1288 <a href="#i_store">store</a> ubyte 124, [4 x ubyte]* %array, uint 4
1289 %val = load [4 x ubyte]* %array, uint 4 <i>; yields {ubyte}:val = ubyte 124</i>
1290 %val = load {{int, float}}* %stptr, 0, 1 <i>; yields {float}:val</i>
1296 <!-- _______________________________________________________________________ -->
1297 </ul><a name="i_store"><h4><hr size=0>'<tt>store</tt>' Instruction</h4><ul>
1301 store <ty> <value>, <ty>* <pointer> <i>; yields {void}</i>
1302 store <ty> <value>, <ty>* <arrayptr>{, uint <idx>}+ <i>; yields {void}</i>
1303 store <ty> <value>, <ty>* <structptr>{, ubyte <idx>}+ <i>; yields {void}e</i>
1307 The '<tt>store</tt>' instruction is used to write to memory.<p>
1310 There are three forms of the '<tt>store</tt>' instruction: one for writing through a general pointer, one for writing through a pointer to a (possibly multidimensional) array, and one for writing to an element of a (potentially nested) structure.<p>
1312 The semantics of this instruction closely match that of the <a href="#i_load">load</a> instruction, except that memory is written to, not read from.
1319 %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1320 <a href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
1321 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1323 %array = <a href="#i_malloc">malloc</a> [4 x ubyte] <i>; yields {[4 x ubyte]*}:array</i>
1324 <a href="#i_store">store</a> ubyte 124, [4 x ubyte]* %array, uint 4
1325 %val = load [4 x ubyte]* %array, uint 4 <i>; yields {ubyte}:val = ubyte 124</i>
1326 %val = load {{int, float}}* %stptr, 0, 1 <i>; yields {float}:val</i>
1332 <!-- ======================================================================= -->
1333 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
1334 <a name="otherops">Other Operations
1335 </b></font></td></tr></table><ul>
1337 The instructions in this catagory are the "miscellaneous" functions, that defy better classification.<p>
1340 <!-- _______________________________________________________________________ -->
1341 </ul><a name="i_cast"><h4><hr size=0>'<tt>cast .. to</tt>' Instruction</h4><ul>
1345 <a name="logical_integrals">
1346 Talk about what is considered true or false for integrals.
1369 <!-- _______________________________________________________________________ -->
1370 </ul><a name="i_call"><h4><hr size=0>'<tt>call</tt>' Instruction</h4><ul>
1388 %retval = call int %test(int %argc)
1392 <!-- _______________________________________________________________________ --></ul><a name="i_icall"><h3><hr size=0>'<tt>icall</tt>' Instruction</h3><ul>
1394 Indirect calls are desperately needed to implement virtual function tables (C++, java) and function pointers (C, C++, ...).<p>
1396 A new instruction <tt>icall</tt> or similar should be introduced to represent an indirect call.<p>
1400 %retval = icall int %funcptr(int %arg1) <i>; yields {int}:%retval</i>
1405 <!-- _______________________________________________________________________ -->
1406 </ul><a name="i_phi"><h4><hr size=0>'<tt>phi</tt>' Instruction</h4><ul>
1426 <!-- ======================================================================= -->
1427 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
1428 <a name="builtinfunc">Builtin Functions
1429 </b></font></td></tr></table><ul>
1431 <b>Notice:</b> Preliminary idea!<p>
1433 Builtin functions are very similar to normal functions, except they are defined by the implementation. Invocations of these functions are very similar to function invocations, except that the syntax is a little less verbose.<p>
1435 Builtin functions are useful to implement semi-high level ideas like a '<tt>min</tt>' or '<tt>max</tt>' operation that can have important properties when doing program analysis. For example:
1438 <li>Some optimizations can make use of identities defined over the functions,
1439 for example a parrallelizing compiler could make use of '<tt>min</tt>'
1440 identities to parrellelize a loop.
1441 <li>Builtin functions would have polymorphic types, where normal function calls
1442 may only have a single type.
1443 <li>Builtin functions would be known to not have side effects, simplifying
1444 analysis over straight function calls.
1445 <li>The syntax of the builtin are cleaner than the syntax of the
1446 '<a href="#i_call"><tt>call</tt></a>' instruction (very minor point).
1449 Because these invocations are explicit in the representation, the runtime can choose to implement these builtin functions any way that they want, including:
1452 <li>Inlining the code directly into the invocation
1453 <li>Implementing the functions in some sort of Runtime class, convert invocation
1454 to a standard function call.
1455 <li>Implementing the functions in some sort of Runtime class, and perform
1456 standard inlining optimizations on it.
1459 Note that these builtins do not use quoted identifiers: the name of the builtin effectively becomes an identifier in the language.<p>
1463 ; Example of a normal function call
1464 %maximum = call int %maximum(int %arg1, int %arg2) <i>; yields {int}:%maximum</i>
1466 ; Examples of potential builtin functions
1467 %max = max(int %arg1, int %arg2) <i>; yields {int}:%max</i>
1468 %min = min(int %arg1, int %arg2) <i>; yields {int}:%min</i>
1469 %sin = sin(double %arg) <i>; yields {double}:%sin</i>
1470 %cos = cos(double %arg) <i>; yields {double}:%cos</i>
1472 ; Show that builtin's are polymorphic, like instructions
1473 %max = max(float %arg1, float %arg2) <i>; yields {float}:%max</i>
1474 %cos = cos(float %arg) <i>; yields {float}:%cos</i>
1477 The '<tt>maximum</tt>' vs '<tt>max</tt>' example illustrates the difference in calling semantics between a '<a href="#i_call"><tt>call</tt></a>' instruction and a builtin function invocation. Notice that the '<tt>maximum</tt>' example assumes that the function is defined local to the caller.<p>
1482 <!-- *********************************************************************** -->
1483 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1484 <a name="todo">TODO List
1485 </b></font></td></tr></table><ul>
1486 <!-- *********************************************************************** -->
1488 This list of random topics includes things that will <b>need</b> to be addressed before the llvm may be used to implement a java like langauge. Right now, it is pretty much useless for any language, given to unavailable of structure types<p>
1490 <!-- _______________________________________________________________________ -->
1491 </ul><a name="synchronization"><h3><hr size=0>Synchronization Instructions</h3><ul>
1493 We will need some type of synchronization instructions to be able to implement stuff in Java well. The way I currently envision doing this is to introduce a '<tt>lock</tt>' type, and then add two (builtin or instructions) operations to lock and unlock the lock.<p>
1496 <!-- *********************************************************************** -->
1497 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1498 <a name="extensions">Possible Extensions
1499 </b></font></td></tr></table><ul>
1500 <!-- *********************************************************************** -->
1502 These extensions are distinct from the TODO list, as they are mostly "interesting" ideas that could be implemented in the future by someone so motivated. They are not directly required to get <a href="#rw_java">Java</a> like languages working.<p>
1504 <!-- _______________________________________________________________________ -->
1505 </ul><a name="i_tailcall"><h3><hr size=0>'<tt>tailcall</tt>' Instruction</h3><ul>
1507 This could be useful. Who knows. '.net' does it, but is the optimization really worth the extra hassle? Using strong typing would make this trivial to implement and a runtime could always callback to using downconverting this to a normal '<a href="#i_call"><tt>call</tt></a>' instruction.<p>
1510 <!-- _______________________________________________________________________ -->
1511 </ul><a name="globalvars"><h3><hr size=0>Global Variables</h3><ul>
1513 In order to represent programs written in languages like C, we need to be able to support variables at the module (global) scope. Perhaps they should be written outside of the module definition even. Maybe global functions should be handled like this as well.<p>
1516 <!-- _______________________________________________________________________ -->
1517 </ul><a name="explicitparrellelism"><h3><hr size=0>Explicit Parrellelism</h3><ul>
1519 With the rise of massively parrellel architectures (like <a href="#rw_ia64">the IA64 architecture</a>, multithreaded CPU cores, and SIMD data sets) it is becoming increasingly more important to extract all of the ILP from a code stream possible. It would be interesting to research encoding functions that can explicitly represent this. One straightforward way to do this would be to introduce a "stop" instruction that is equilivent to the IA64 stop bit.<p>
1523 <!-- *********************************************************************** -->
1524 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1525 <a name="related">Related Work
1526 </b></font></td></tr></table><ul>
1527 <!-- *********************************************************************** -->
1530 Codesigned virtual machines.<p>
1533 <a name="rw_safetsa">
1535 <DD>Description here<p>
1538 <dt><a href="http://www.javasoft.com">Java</a>
1539 <DD>Desciption here<p>
1542 <dt><a href="http://www.microsoft.com/net">Microsoft .net</a>
1543 <DD>Desciption here<p>
1545 <a name="rw_gccrtl">
1546 <dt><a href="http://www.math.umn.edu/systems_guide/gcc-2.95.1/gcc_15.html">GNU RTL Intermediate Representation</a>
1547 <DD>Desciption here<p>
1550 <dt><a href="http://developer.intel.com/design/ia-64/index.htm">IA64 Architecture & Instruction Set</a>
1551 <DD>Desciption here<p>
1554 <dt><a href="http://www-cs-faculty.stanford.edu/~knuth/mmix-news.html">MMIX Instruction Set</a>
1555 <DD>Desciption here<p>
1557 <a name="rw_stroustrup">
1558 <dt><a href="http://www.research.att.com/~bs/devXinterview.html">"Interview With Bjarne Stroustrup"</a>
1559 <DD>This interview influenced the design and thought process behind LLVM in several ways, most notably the way that derived types are written in text format. See the question that starts with "you defined the C declarator syntax as an experiment that failed".<p>
1562 <!-- _______________________________________________________________________ -->
1563 </ul><a name="rw_vectorization"><h3><hr size=0>Vectorized Architectures</h3><ul>
1566 <a name="rw_intel_simd">
1567 <dt>Intel MMX, MMX2, SSE, SSE2
1568 <DD>Description here<p>
1570 <a name="rw_amd_simd">
1571 <dt><a href="http://www.nondot.org/~sabre/os/H1ChipFeatures/3DNow!TechnologyManual.pdf">AMD 3Dnow!, 3Dnow! 2</a>
1572 <DD>Desciption here<p>
1574 <a name="rw_sun_simd">
1575 <dt><a href="http://www.nondot.org/~sabre/os/H1ChipFeatures/VISInstructionSetUsersManual.pdf">Sun VIS ISA</a>
1576 <DD>Desciption here<p>
1583 <!-- *********************************************************************** -->
1585 <!-- *********************************************************************** -->
1590 <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
1591 <!-- Created: Tue Jan 23 15:19:28 CST 2001 -->
1592 <!-- hhmts start -->
1593 Last modified: Sun Apr 14 01:12:55 CDT 2002