LIR

  • Revision slug: Nanojit/LIR
  • Revision title: LIR
  • Revision id: 100586
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment 15 words added, 15 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 indicates that the operand is interpreted as a floating-point value, or that the result is a 64-bit float, but the distinction between f and q does not impose a type constraint on LIR code.

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.

Non-pure operations

st

32-bit store.

stq

64-bit store.

sti

32-bit store.

stqi

64-bit store.

ld

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

ldq

64-bit load.

ldcb

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

ldcs

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

ldc

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

ldqc

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

live

Extend live range of reference.

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

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

2

cmov

qcmov

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

result = ov i1

cs

result = cs i1

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> indicates that the operand is interpreted as a floating-point value, or that the result is a 64-bit float, but the distinction between <em>f</em> and <em>q</em> does not impose a type constraint on LIR code.</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>
<h3>Non-pure operations</h3>
<h4>st</h4>
<p>32-bit store.</p>
<h4>stq</h4>
<p>64-bit store.</p>
<h4>sti</h4>
<p>32-bit store.</p>
<h4>stqi</h4>
<p>64-bit store.</p>
<h4>ld</h4>
<p>32-bit load. This instruction is never removed by common subexpression elimination.</p>
<h4>ldq</h4>
<p>64-bit load.</p>
<h4>ldcb</h4>
<p>8-bit load. This instruction may be removed by common subexpression elimination.</p>
<h4>ldcs</h4>
<p>16-bit load. This instruction may be removed by common subexpression elimination.</p>
<h4>ldc</h4>
<p>32-bit load. This instruction may be removed by common subexpression elimination.</p>
<h4>ldqc</h4>
<p>64-bit load. This instruction may be removed by common subexpression elimination.</p>
<h4>live</h4>
<p>Extend live range of reference.</p>
<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>
<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>
<h4>2</h4>
<h4>cmov</h4>
<h4>qcmov</h4>
<h3>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>
<pre><var>result</var> = ov <var>i1</var></pre>
<h4>cs</h4>
<pre><var>result</var> = cs <var>i1</var></pre><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