diff options
author | Chris Lattner <sabre@nondot.org> | 2004-04-05 01:30:49 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2004-04-05 01:30:49 +0000 |
commit | f74d5c7d1d98b53d4f2522793c41fd628c4df481 (patch) | |
tree | a322dbfca2e4804d8b48ffbf614fa2c8d02906c6 | |
parent | 28977af72a11fcad5d1b54d7a96b3df02828f6fc (diff) |
Update getelementptr instruction description
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12654 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | docs/LangRef.html | 121 |
1 files changed, 83 insertions, 38 deletions
diff --git a/docs/LangRef.html b/docs/LangRef.html index ae389d1db5..60dba677ba 100644 --- a/docs/LangRef.html +++ b/docs/LangRef.html @@ -1423,58 +1423,103 @@ at the location specified by the '<tt><pointer></tt>' operand.</p> %val = load int* %ptr <i>; yields {int}:val = int 3</i> </pre> <!-- _______________________________________________________________________ --> -<div class="doc_subsubsection"> <a name="i_getelementptr">'<tt>getelementptr</tt>' -Instruction</a> </div> +<div class="doc_subsubsection"> + <a name="i_getelementptr">'<tt>getelementptr</tt>' Instruction</a> +</div> + <div class="doc_text"> <h5>Syntax:</h5> -<pre> <result> = getelementptr <ty>* <ptrval>{, long <aidx>|, ubyte <sidx>}*<br></pre> +<pre> + <result> = getelementptr <ty>* <ptrval>{, <ty> <idx>}* +</pre> + <h5>Overview:</h5> -<p>The '<tt>getelementptr</tt>' instruction is used to get the address -of a subelement of an aggregate data structure.</p> + +<p> +The '<tt>getelementptr</tt>' instruction is used to get the address of a +subelement of an aggregate data structure.</p> + <h5>Arguments:</h5> -<p>This instruction takes a list of <tt>long</tt> values and <tt>ubyte</tt> -constants that indicate what form of addressing to perform. The actual -types of the arguments provided depend on the type of the first pointer -argument. The '<tt>getelementptr</tt>' instruction is used to index -down through the type levels of a structure.</p> + +<p>This instruction takes a list of integer constants that indicate what +elements of the aggregate object to index to. The actual types of the arguments +provided depend on the type of the first pointer argument. The +'<tt>getelementptr</tt>' instruction is used to index down through the type +levels of a structure. When indexing into a structure, only <tt>uint</tt> +integer constants are allowed. When indexing into an array or pointer +<tt>int</tt> and <tt>long</tt> indexes are allowed of any sign.</p> + <p>For example, let's consider a C code fragment and how it gets compiled to LLVM:</p> -<pre>struct RT {<br> char A;<br> int B[10][20];<br> char C;<br>};<br>struct ST {<br> int X;<br> double Y;<br> struct RT Z;<br>};<br><br>int *foo(struct ST *s) {<br> return &s[1].Z.B[5][13];<br>}<br></pre> + +<pre> + struct RT { + char A; + int B[10][20]; + char C; + }; + struct ST { + int X; + double Y; + struct RT Z; + }; + + int *foo(struct ST *s) { + return &s[1].Z.B[5][13]; + } +</pre> + <p>The LLVM code generated by the GCC frontend is:</p> -<pre>%RT = type { sbyte, [10 x [20 x int]], sbyte }<br>%ST = type { int, double, %RT }<br><br>int* "foo"(%ST* %s) {<br> %reg = getelementptr %ST* %s, long 1, ubyte 2, ubyte 1, long 5, long 13<br> ret int* %reg<br>}<br></pre> + +<pre> + %RT = type { sbyte, [10 x [20 x int]], sbyte } + %ST = type { int, double, %RT } + + int* "foo"(%ST* %s) { + %reg = getelementptr %ST* %s, int 1, uint 2, uint 1, int 5, int 13<br> + ret int* %reg + } +</pre> + <h5>Semantics:</h5> -<p>The index types specified for the '<tt>getelementptr</tt>' -instruction depend on the pointer type that is being index into. <a - href="t_pointer">Pointer</a> and <a href="t_array">array</a> types -require '<tt>long</tt>' values, and <a href="t_struct">structure</a> -types require '<tt>ubyte</tt>' <b>constants</b>.</p> + +<p>The index types specified for the '<tt>getelementptr</tt>' instruction depend +on the pointer type that is being index into. <a href="t_pointer">Pointer</a> +and <a href="t_array">array</a> types require <tt>uint</tt>, <tt>int</tt>, +<tt>ulong</tt>, or <tt>long</tt> values, and <a href="t_struct">structure</a> +types require <tt>uint</tt> <b>constants</b>.</p> + <p>In the example above, the first index is indexing into the '<tt>%ST*</tt>' -type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int, -double, %RT }</tt>' type, a structure. The second index indexes into -the third element of the structure, yielding a '<tt>%RT</tt>' = '<tt>{ -sbyte, [10 x [20 x int]], sbyte }</tt>' type, another structure. The -third index indexes into the second element of the structure, yielding -a '<tt>[10 x [20 x int]]</tt>' type, an array. The two dimensions of -the array are subscripted into, yielding an '<tt>int</tt>' type. The '<tt>getelementptr</tt>' -instruction return a pointer to this element, thus yielding a '<tt>int*</tt>' -type.</p> +type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int, double, %RT +}</tt>' type, a structure. The second index indexes into the third element of +the structure, yielding a '<tt>%RT</tt>' = '<tt>{ sbyte, [10 x [20 x int]], +sbyte }</tt>' type, another structure. The third index indexes into the second +element of the structure, yielding a '<tt>[10 x [20 x int]]</tt>' type, an +array. The two dimensions of the array are subscripted into, yielding an +'<tt>int</tt>' type. The '<tt>getelementptr</tt>' instruction return a pointer +to this element, thus computing a value of '<tt>int*</tt>' type.</p> + <p>Note that it is perfectly legal to index partially through a structure, returning a pointer to an inner element. Because of this, the LLVM code for the given testcase is equivalent to:</p> -<pre>int* "foo"(%ST* %s) {<br> %t1 = getelementptr %ST* %s , long 1 <i>; yields %ST*:%t1</i> - %t2 = getelementptr %ST* %t1, long 0, ubyte 2 <i>; yields %RT*:%t2</i> - %t3 = getelementptr %RT* %t2, long 0, ubyte 1 <i>; yields [10 x [20 x int]]*:%t3</i> - %t4 = getelementptr [10 x [20 x int]]* %t3, long 0, long 5 <i>; yields [20 x int]*:%t4</i> - %t5 = getelementptr [20 x int]* %t4, long 0, long 13 <i>; yields int*:%t5</i> - ret int* %t5 -} + +<pre> + int* "foo"(%ST* %s) { + %t1 = getelementptr %ST* %s, int 1 <i>; yields %ST*:%t1</i> + %t2 = getelementptr %ST* %t1, int 0, uint 2 <i>; yields %RT*:%t2</i> + %t3 = getelementptr %RT* %t2, int 0, uint 1 <i>; yields [10 x [20 x int]]*:%t3</i> + %t4 = getelementptr [10 x [20 x int]]* %t3, int 0, int 5 <i>; yields [20 x int]*:%t4</i> + %t5 = getelementptr [20 x int]* %t4, int 0, int 13 <i>; yields int*:%t5</i> + ret int* %t5 + } </pre> <h5>Example:</h5> -<pre> <i>; yields [12 x ubyte]*:aptr</i> - %aptr = getelementptr {int, [12 x ubyte]}* %sptr, long 0, ubyte 1<br></pre> -<h5> Note To The Novice:</h5> -When using indexing into global arrays with the '<tt>getelementptr</tt>' -instruction, you must remember that the </div> +<pre> + <i>; yields [12 x ubyte]*:aptr</i> + %aptr = getelementptr {int, [12 x ubyte]}* %sptr, long 0, uint 1 +</pre> + +</div> <!-- ======================================================================= --> <div class="doc_subsection"> <a name="otherops">Other Operations</a> </div> <div class="doc_text"> |