LIR

  • Revision slug: Nanojit/LIR
  • Revision title: LIR
  • Revision id: 100590
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment 100 words added, 3 words removed

Revision Content

In Nanojit, LIR is the source language for compilation to machine code. LIR stands for low-level intermediate representation.

The LIR instruction set

Types in LIR. Values in LIR have a type: either 32-bit or 64-bit. A 32-bit value, additionally, may be a condition or not. Each instruction requires operands of a specific type and produces a result of a specific type.

LIR makes no distinction at all between pointers and integers of the same size, between signed and unsigned integer values, or between 64-bit floating-point and 64-bit integer values. It is acceptable to load a 64-bit value from memory using ldq and then use the result with a floating-point arithmetic operation such as fadd.

The names of the operands and results below indicate the types required by LIR. Names starting with i indicate 32-bit values. Results starting with b are 32-bit integers and additionally are conditions. Operand names starting with b must be conditions. Names starting with q or f indicate 64-bit values. (f and q indicate the same type as far as LIR is concerned, but f is used here for floating-point operations.) Operand names starting with p must be pointer-sized integers. That is, they must be 64 bits on a 64-bit platform and 32 bits otherwise.

Special operations

start

nearskip

skip

neartramp

tramp

Operations that are weird but don't count as special

addp

param

file

line

alloc

Allocate stack space, as though with the C standard library function alloca.

live

Extend live range of reference.

Loads and stores

st

32-bit store.

st p1[offset] = i2

stq

64-bit store.

stq p1[offset] = q2

sti

32-bit store.

sti p1[offset] = i2

stqi

64-bit store.

stqi p1[offset] = q2

ld

32-bit load. This instruction is never removed by common subexpression elimination.

i = ld p1[p2]

ldq

64-bit load. This instruction is never removed by common subexpression elimination.

q = ldq p1[offset]

ldcb

8-bit load. This instruction may be removed by common subexpression elimination.

i = ldcb p1[offset]

ldcs

16-bit load. This instruction may be removed by common subexpression elimination.

i = ldcs p1[offset]

ldc

32-bit load. This instruction may be removed by common subexpression elimination.

i = ldc p1[offset]

ldqc

64-bit load. This instruction may be removed by common subexpression elimination.

q = ldqc p1[offset]

Subroutine calls

calli

Indirect subroutine call returning a 32-bit integer value.

call

Subroutine call returning a 32-bit integer value.

callh

fcalli

Indirect subroutine call returning a 64-bit value.

fcall

Subroutine call returning a 64-bit value.

ret

fret

Branches

loop

Loop fragment.

x

Exit unconditionally.

xt

Exit if true.

xf

Exit if false.

xbarrier

Do not exit but emit writes to flush all values to the stack.

j

Jump unconditionally.

jt

Jump if true.

jf

Jump if false.

label

A jump target.

ji

Indirect jump.

Comparisons

eq

32-bit integer equality test.

result = eq i1, i2

There is no not-equal instruction. Instead, flip the instruction that uses the result, or add a not instruction.

lt

Signed 32-bit integer less-than test.

b = lt i1, i2

gt

Signed 32-bit integer greater-than test.

b = gt i1, i2

le

Signed 32-bit integer less-than-or-equals test.

b = le i1, i2

ge

Signed 32-bit integer greater-than-or-equals test.

b = ge i1, i2

ult

Unsigned 32-bit integer less-than test.

b = ult i1, i2

ugt

Unsigned 32-bit integer greater-than test.

b = ugt i1, i2

ule

Unsigned 32-bit integer less-than-or-equals test.

b = ule i1, i2

uge

Unsigned 32-bit integer greater-than-or-equals test.

b = uge i1, i2

feq

Floating-point equality test.

b = feq f1, f2

flt

Floating-point less-than test.

b = flt f1, f2

fgt

Floating-point greater-than test.

b = fgt f1, f2

fle

Floating-point less-than-or-equals test.

b = fle f1, f2

fge

Floating-point greater-than-or-equals test.

b = fge f1, f2

Constants

Note: These instructions are currently rendered in VerboseWriter output as lines containing only the symbolic name of the immediate value being loaded. The instruction name and numeric value are not displayed for short and int instructions. This is arguably a bug.

short

An immediate 32-bit integer that fits in a signed 16-bit integer.

i = short <number>

<number> must be an integer that fits in the range of a signed 16-bit integer (-32768 to 32767). The result of a short instruction is a 32-bit integer with the same value (sign-extended).

int

An immediate 32-bit integer.

i = int <number>

quad

An immediate 64-bit value.

q = quad <number>

Conditional moves

Note: These two instructions can be written using the idiom lirwriter->ins2(LIR_cmov, b1, lirwriter->ins2(LIR_2, i2, i3)). The LIR_2 instruction serves only to group the second and third operands, since LirWriter has no ins3 method.

The LirWriter::ins_choose() convenience method can be used instead. It uses the above idiom.

cmov

Choice of two 32-bit values.

i = cmov b1, i2, i3

qcmov

Choice of two 64-bit values.

Note: This instruction currently does not work on 32-bit Intel platforms.

q = qcmov b1, q2, q3

Integer arithmetic

neg

32-bit integer negation.

i = neg i1

add

32-bit integer addition.

i = add i1, i2

qiadd

64-bit integer addition.

q = qiadd q1, q2

sub

32-bit integer subtraction.

i = sub i1, i2

mul

32-bit integer multiplication.

i = mul i1, i2

and

32-bit bitwise AND.

i = and i1, i2

qiand

64-bit bitwise AND.

q = qiand q1, q2

or

32-bit bitwise OR.

i = or i1, i2

qior

64-bit bitwise OR.

q = qior q1, q2

xor

32-bit bitwise XOR.

i = or i1, i2

not

32-bit bitwise NOT.

i = not i1

lsh

32-bit left shift.

i = lsh i1, i2

qilsh

64-bit left shift.

q = qilsh q1, q2

rsh

32-bit right shift with sign extend.

i = rsh i1, i2

The two most significant bits of the result value are the same.

ush

32-bit unsigned right shift.

i = ush i1, i2

The most significant bit of the result value is 0.

ov

Test for overflow.

b = ov i1

On Intel, this reads the overflow condition flag. Other platforms have to emulate this behavior.

The result is 1 if i1 is the result of an add that changed the value of the most significant bit, for example.

cs

Test for carry.

b = cs i1

On Intel, this reads the carry condition flag. Other platforms have to emulate this behavior.

The result is 1 if i1 is the result of an add that overflowed the range of an unsigned 32-bit integer, for example.

Floating-point arithmetic

fneg

Floating-point negation.

result = fneg f1

fadd

Floating-point addition.

result = fadd f1, f2

fsub

Floating-point subtraction.

result = fsub f1, f2

fmul

Floating-point multiplication.

result = mul f1, f2

fdiv

Floating-point division.

f = div f1, f2

Numeric conversions

qlo

Get the low 32 bits of a 64-bit value.

i = qlo q

qhi

Get the high 32 bits of a 64-bit value.

i = qhi q

qjoin

Join two 32-bit values to form a 64-bit value.

q = qjoin i1, i2

i2f

Convert signed 32-bit integer to floating-point number.

f = i2f i1

u2f

Convert unsigned 32-bit integer to floating-point number.

f = u2f i1

 

Revision Source

<p>In Nanojit, <strong>LIR</strong> is the source language for compilation to machine code. LIR stands for <em>low-level intermediate representation</em>.</p>
<h2>The LIR instruction set</h2>
<p><strong>Types in LIR.</strong> Values in LIR have a type: either 32-bit or 64-bit. A 32-bit value, additionally, may be a condition or not. Each instruction requires operands of a specific type and produces a result of a specific type.</p>
<p>LIR makes no distinction at all between pointers and integers of the same size, between signed and unsigned integer values, or between 64-bit floating-point and 64-bit integer values. It is acceptable to load a 64-bit value from memory using <code>ldq</code> and then use the result with a floating-point arithmetic operation such as <code>fadd</code>.</p>
<p>The names of the operands and results below indicate the types required by LIR. Names starting with <em>i</em> indicate 32-bit values. Results starting with <em>b</em> are 32-bit integers and additionally are conditions. Operand names starting with <em>b</em> must be conditions. Names starting with <em>q</em> or <em>f</em> indicate 64-bit values. (<em>f</em> and <em>q</em> indicate the same type as far as LIR is concerned, but <em>f</em> is used here for floating-point operations.) Operand names starting with <em>p</em> must be pointer-sized integers. That is, they must be 64 bits on a 64-bit platform and 32 bits otherwise.</p>
<h3>Special operations</h3>
<h4>start</h4>
<h4>nearskip</h4>
<h4>skip</h4>
<h4>neartramp</h4>
<h4>tramp</h4>
<h3>Operations that are weird but don't count as special</h3>
<h4>addp</h4>
<h4>param</h4>
<h4>file</h4>
<h4>line</h4>
<h4>alloc</h4>
<p>Allocate stack space, as though with the C standard library function <code>alloca</code>.</p>
<h4>live</h4>
<p>Extend live range of reference.</p>
<h3>Loads and stores</h3>
<h4>st</h4>
<p>32-bit store.</p>
<pre>st <var>p1</var>[<var>offset</var>] = <var>i2</var></pre>
<h4>stq</h4>
<p>64-bit store.</p>
<pre>stq <var>p1</var>[<var>offset</var>] = <var>q2</var></pre>
<h4>sti</h4>
<p>32-bit store.</p>
<pre>sti <var>p1</var>[<var>offset</var>] = <var>i2</var></pre>
<h4>stqi</h4>
<p>64-bit store.</p>
<pre>stqi <var>p1</var>[<var>offset</var>] = <var>q2</var></pre>
<h4>ld</h4>
<p>32-bit load. This instruction is never removed by common subexpression elimination.</p>
<pre><var>i</var> = ld <var>p1</var>[<var>p2</var>]</pre>
<h4>ldq</h4>
<p>64-bit load. This instruction is never removed by common subexpression elimination.</p>
<pre><var>q</var> = ldq <var>p1</var>[<var>offset</var>]</pre>
<h4>ldcb</h4>
<p>8-bit load. This instruction may be removed by common subexpression elimination.</p>
<pre><var>i</var> = ldcb <var>p1</var>[<var>offset</var>]</pre>
<h4>ldcs</h4>
<p>16-bit load. This instruction may be removed by common subexpression elimination.</p>
<pre><var>i</var> = ldcs <var>p1</var>[<var>offset</var>]</pre>
<h4>ldc</h4>
<p>32-bit load. This instruction may be removed by common subexpression elimination.</p>
<pre><var>i</var> = ldc <var>p1</var>[<var>offset</var>]</pre>
<h4>ldqc</h4>
<p>64-bit load. This instruction may be removed by common subexpression elimination.</p>
<pre><var>q</var> = ldqc <var>p1</var>[<var>offset</var>]</pre>
<h3>Subroutine calls</h3>
<h4>calli</h4>
<p>Indirect subroutine call returning a 32-bit integer value.</p>
<h4>call</h4>
<p>Subroutine call returning a 32-bit integer value.</p>
<h4>callh</h4>
<h4>fcalli</h4>
<p>Indirect subroutine call returning a 64-bit value.</p>
<h4>fcall</h4>
<p>Subroutine call returning a 64-bit value.</p>
<h4>ret</h4>
<h4>fret</h4>
<h3>Branches</h3>
<h4>loop</h4>
<p>Loop fragment.</p>
<h4>x</h4>
<p>Exit unconditionally.</p>
<h4>xt</h4>
<p>Exit if true.</p>
<h4>xf</h4>
<p>Exit if false.</p>
<h4>xbarrier</h4>
<p>Do not exit but emit writes to flush all values to the stack.</p>
<h4>j</h4>
<p>Jump unconditionally.</p>
<h4>jt</h4>
<p>Jump if true.</p>
<h4>jf</h4>
<p>Jump if false.</p>
<h4>label</h4>
<p>A jump target.</p>
<h4>ji</h4>
<p>Indirect jump.</p>
<h3>Comparisons</h3>
<h4>eq</h4>
<p>32-bit integer equality test.</p>
<pre><var>result</var> = eq <var>i1</var>, <var>i2</var></pre>
<p>There is no not-equal instruction. Instead, flip the instruction that uses the result, or add a <code>not</code> instruction.</p>
<h4>lt</h4>
<p>Signed 32-bit integer less-than test.</p>
<pre><var>b</var> = lt <var>i1</var>, <var>i2</var></pre>
<h4>gt</h4>
<p>Signed 32-bit integer greater-than test.</p>
<pre><var>b</var> = gt <var>i1</var>, <var>i2</var></pre>
<h4>le</h4>
<p>Signed 32-bit integer less-than-or-equals test.</p>
<pre><var>b</var> = le <var>i1</var>, <var>i2</var></pre>
<h4>ge</h4>
<p>Signed 32-bit integer greater-than-or-equals test.</p>
<pre><var>b</var> = ge <var>i1</var>, <var>i2</var></pre>
<h4>ult</h4>
<p>Unsigned 32-bit integer less-than test.</p>
<pre><var>b</var> = ult <var>i1</var>, <var>i2</var></pre>
<h4>ugt</h4>
<p>Unsigned 32-bit integer greater-than test.</p>
<pre><var>b</var> = ugt <var>i1</var>, <var>i2</var></pre>
<h4>ule</h4>
<p>Unsigned 32-bit integer less-than-or-equals test.</p>
<pre><var>b</var> = ule <var>i1</var>, <var>i2</var></pre>
<h4>uge</h4>
<p>Unsigned 32-bit integer greater-than-or-equals test.</p>
<pre><var>b</var> = uge <var>i1</var>, <var>i2</var></pre>
<h4>feq</h4>
<p>Floating-point equality test.</p>
<pre><var>b</var> = feq <var>f1</var>, <var>f2</var></pre>
<h4>flt</h4>
<p>Floating-point less-than test.</p>
<pre><var>b</var> = flt <var>f1</var>, <var>f2</var></pre>
<h4>fgt</h4>
<p>Floating-point greater-than test.</p>
<pre><var>b</var> = fgt <var>f1</var>, <var>f2</var></pre>
<h4>fle</h4>
<p>Floating-point less-than-or-equals test.</p>
<pre><var>b</var> = fle <var>f1</var>, <var>f2</var></pre>
<h4>fge</h4>
<p>Floating-point greater-than-or-equals test.</p>
<pre><var>b</var> = fge <var>f1</var>, <var>f2</var></pre>
<h3>Constants</h3>
<p>Note: These instructions are currently rendered in <code>VerboseWriter</code> output as lines containing only the symbolic name of the immediate value being loaded. The instruction name and numeric value are not displayed for <code>short</code> and <code>int</code> instructions. This is arguably a bug.</p>
<h4>short</h4>
<p>An immediate 32-bit integer that fits in a signed 16-bit integer.</p>
<pre><var>i</var> = short <var>&lt;number&gt;</var></pre>
<p><code><var>&lt;number&gt;</var></code> must be an integer that fits in the range of a signed 16-bit integer (-32768 to 32767). The result of a <code>short</code> instruction is a 32-bit integer with the same value (sign-extended).</p>
<h4>int</h4>
<p>An immediate 32-bit integer.</p>
<pre><var>i</var> = int <var>&lt;number&gt;</var></pre>
<h4>quad</h4>
<p>An immediate 64-bit value.</p>
<pre><var>q</var> = quad <var>&lt;number&gt;</var></pre><h3>Conditional moves</h3>
<p>Note: These two instructions can be written using the idiom <code>lirwriter-&gt;ins2(LIR_cmov, b1, lirwriter-&gt;ins2(LIR_2, i2, i3))</code>. The <code>LIR_2</code> instruction serves only to group the second and third operands, since <code>LirWriter</code> has no <code>ins3</code> method.</p>
<p>The <code>LirWriter::ins_choose()</code> convenience method can be used instead. It uses the above idiom.</p>
<h4>cmov</h4>
<p>Choice of two 32-bit values.</p>
<pre><var>i</var> = cmov <var>b1</var>, <var>i2</var>, <var>i3</var></pre>
<h4>qcmov</h4>
<p>Choice of two 64-bit values.</p>
<p>Note: This instruction currently does not work on 32-bit Intel platforms.</p>
<pre><var>q</var> = qcmov <var>b1</var>, <var>q2</var>, <var>q3</var></pre><h3>Integer arithmetic</h3>
<h4>neg</h4>
<p>32-bit integer negation.</p>
<pre><var>i</var> = neg <var>i1</var></pre>
<h4>add</h4>
<p>32-bit integer addition.</p>
<pre><var>i</var> = add <var>i1</var>, <var>i2</var></pre>
<h4>qiadd</h4>
<p>64-bit integer addition.</p>
<pre><var>q</var> = qiadd <var>q1</var>, <var>q2</var></pre>
<h4>sub</h4>
<p>32-bit integer subtraction.</p>
<pre><var>i</var> = sub <var>i1</var>, <var>i2</var></pre>
<h4>mul</h4>
<p>32-bit integer multiplication.</p>
<pre><var>i</var> = mul <var>i1</var>, <var>i2</var></pre>
<h4>and</h4>
<p>32-bit bitwise AND.</p>
<pre><var>i</var> = and <var>i1</var>, <var>i2</var></pre>
<h4>qiand</h4>
<p>64-bit bitwise AND.</p>
<pre><var>q</var> = qiand <var>q1</var>, <var>q2</var></pre>
<h4>or</h4>
<p>32-bit bitwise OR.</p>
<pre><var>i</var> = or <var>i1</var>, <var>i2</var></pre>
<h4>qior</h4>
<p>64-bit bitwise OR.</p>
<pre><var>q</var> = qior <var>q1</var>, <var>q2</var></pre>
<h4>xor</h4>
<p>32-bit bitwise XOR.</p>
<pre><var>i</var> = or <var>i1</var>, <var>i2</var></pre>
<h4>not</h4>
<p>32-bit bitwise NOT.</p>
<pre><var>i</var> = not <var>i1</var></pre>
<h4>lsh</h4>
<p>32-bit left shift.</p>
<pre><var>i</var> = lsh <var>i1</var>, <var>i2</var></pre>
<h4>qilsh</h4>
<p>64-bit left shift.</p>
<pre><var>q</var> = qilsh <var>q1</var>, <var>q2</var></pre>
<h4>rsh</h4>
<p>32-bit right shift with sign extend.</p>
<pre><var>i</var> = rsh <var>i1</var>, <var>i2</var></pre>
<p>The two most significant bits of the result value are the same.</p>
<h4>ush</h4>
<p>32-bit unsigned right shift.</p>
<pre><var>i</var> = ush <var>i1</var>, <var>i2</var></pre>
<p>The most significant bit of the result value is 0.</p>
<h4>ov</h4>
<p>Test for overflow.</p>
<pre><var>b</var> = ov <var>i1</var></pre>
<p>On Intel, this reads the overflow condition flag. Other platforms have to emulate this behavior.</p>
<p>The result is <code>1</code> if <code><var>i1</var></code> is the result of an <code>add</code> that changed the value of the most significant bit, for example.</p>
<h4>cs</h4>
<p>Test for carry.</p>
<pre><var>b</var> = cs <var>i1</var></pre>
<p>On Intel, this reads the carry condition flag. Other platforms have to emulate this behavior.</p>
<p>The result is <code>1</code> if <code><var>i1</var></code> is the result of an <code>add</code> that overflowed the range of an unsigned 32-bit integer, for example.</p><h3>Floating-point arithmetic</h3>
<h4>fneg</h4>
<p>Floating-point negation.</p>
<pre><var>result</var> = fneg <var>f1</var></pre>
<h4>fadd</h4>
<p>Floating-point addition.</p>
<pre><var>result</var> = fadd <var>f1</var>, <var>f2</var></pre>
<h4>fsub</h4>
<p>Floating-point subtraction.</p>
<pre><var>result</var> = fsub <var>f1</var>, <var>f2</var></pre>
<h4>fmul</h4>
<p>Floating-point multiplication.</p>
<pre><var>result</var> = mul <var>f1</var>, <var>f2</var></pre>
<h4>fdiv</h4>
<p>Floating-point division.</p>
<pre><var>f</var> = div <var>f1</var>, <var>f2</var></pre>
<h3>Numeric conversions</h3>
<h4>qlo</h4>
<p>Get the low 32 bits of a 64-bit value.</p>
<pre><var>i</var> = qlo <var>q</var></pre>
<h4>qhi</h4>
<p>Get the high 32 bits of a 64-bit value.</p>
<pre><var>i</var> = qhi <var>q</var></pre>
<h4>qjoin</h4>
<p>Join two 32-bit values to form a 64-bit value.</p>
<pre><var>q</var> = qjoin <var>i1</var>, <var>i2</var></pre>
<h4>i2f</h4>
<p>Convert signed 32-bit integer to floating-point number.</p>
<pre><var>f</var> = i2f <var>i1</var></pre>
<h4>u2f</h4>
<p>Convert unsigned 32-bit integer to floating-point number.</p>
<pre><var>f</var> = u2f <var>i1</var></pre>
<p> </p>
Revert to this revision