System X Intermediate Language Reference

Intermediate File Structure

An intermediate code file consists of:

Syntax

intermediate‑code‑file compile‑unit‑header type‑declarations? data‑definitions? function‑definitions? metadata‑declarations?

Compile Unit Header

Syntax

compile‑unit‑header cu ( compile‑unit‑id , metadata‑ref )
compile‑unit‑id hex‑number | number | identifier
hex‑number hex‑digit+
hex‑digit [0-9A-Fa-f]
number sign? (fractional‑part | digit+) exponent‑part?
sign + | -
digit [0-9]
fractional‑part digit* . digit+ | digit+ .
exponent‑part (e | E) sign? digit+
identifier id‑start id‑cont*
id‑start 'Unicode characters having ID_Start Property' | _
id‑cont 'Unicode characters having ID_Continue Property' | @ | _
metadata‑ref ! digit+

Types

The types of the intermediate language can be grouped into fundamental and compound types.

Fundamental Types

The following table contains a summary of fundamental types:

type kind signed | unsigned size range
void - - - -
bool Boolean - 1 byte true | false
sbyte integer signed 1 byte -128...127
byte integer unsigned 1 byte 0...255
short integer signed 2 bytes -32768...32767
ushort integer unsigned 2 bytes 0...65535
int integer signed 4 bytes -2147483648...2147483647
uint integer unsigned 4 bytes 0...4294967295
long integer signed 8 bytes -9223372036854775808...9223372036854775807
ulong integer unsigned 8 bytes 0...18446744073709551615
float floating-point - 4 bytes IEEE single precision floating-point type range
double floating-point - 8 bytes IEEE double precision floating-point type range

The void keyword represents lack of type.

Other fundamental types can be divided into Boolean, integer and floating-point types.

The integer and floating-point types are together called the arithmetic types.

Compound Types

There are four kinds of compound types:

The structured, array and function types used in the intermediate file has to be declared in the type declaration part of the intermediate file.

Structured Types

A structured type consists of a non-empty sequence of fields. Each field has a type identified using a type-ref.

A structured type has to be declared using a structure‑type‑declaration.

Array Types

An array type consists of a non-empty sequence of elements of the same type. The type of the elements are identified using a a type-ref.

An array type has to be declared using a array‑type‑declaration.

Function Types

A function type consists of the return type and the types of the parameters of a function. The return type and the types of the parameters are identified using a type-ref.

An function type has to be declared using a function‑type‑declaration.

Pointer Types

A pointer type is identified using a type-ref that consists of a prefix that identifies a base type and a suffix consisting of one or more * symbols.

The base type may be fundamental type, a structured type, an array type or a function type.

The value of a pointer type object is a memory address of the pointed-to object.

Type Declarations

Syntax

type‑declarations types { type‑declaration* }
type‑declaration type‑id = type
( structure‑type‑declaration | array‑type‑declaration | function‑type‑declaration )
structure‑type‑declaration { type‑ref ( , type‑ref )* }
array‑type‑declaration [ array‑size x type‑ref ]
function‑type‑declaration function return‑type ( ( param‑type (, param‑type)* )? )
return‑type type‑ref
param‑type type‑ref
type‑ref base‑type **
base‑type type‑id | fundamental‑type
type‑id $T digit+
fundamental‑type void | bool | sbyte | byte | short | ushort | int | uint | long | ulong | float | double
array‑size digit+

Data

Data Definitions

Syntax

data‑definitions data { data‑definition* }
data‑definition type‑ref once? identifier ( ; | = initializer )
initializer constant

The once flag denotes that identical data definitions may occur many times in different translation units. The linker chooses one of them and throws the rest away.

Constants

Syntax

constant bool‑constant | sbyte‑constant | byte‑constant | short‑constant | ushort‑constant | int‑constant | uint‑constant | long‑constant | ulong‑constant | float‑constant | double‑constant | address‑constant | array‑constant | structure‑constant | string‑constant | string‑array‑constant | conversion‑constant | clsid‑constant | symbol‑constant
bool‑constant bool (true | false)
sbyte‑constant sbyte number
byte‑constant byte number
short‑constant short number
ushort‑constant ushort number
int‑constant int number
uint‑constant uint number
long‑constant long number
ulong‑constant ulong number
float‑constant float number
double‑constant double number
address‑constant ptr‑type ( null | identifier )
ptr‑type type‑ref
array‑constant [ (constant (, constant)* )? ]
structure‑constant { (constant (, constant)* )? }
string‑constant " [^\r\n]* "
string‑array‑constant string‑array‑prefix [ (constant (, constant)* )? ]
string‑array‑prefix u | w | b
conversion‑constant type‑ref conv ( constant )
clsid‑constant type‑ref clsid ( (hex‑digit | -)+ )
symbol‑constant type‑ref symbol‑value

Values

Syntax

value reg‑value | symbol‑value | literal‑value
reg‑value $ digit+
symbol‑value @ identifier
literal‑value true | false | null | number | identifier
ptr-value value

Functions

Function Definitions

Syntax

function‑definitions function‑definition*
function‑definition function‑header { basic‑block* }
function‑header function function‑type‑id once? identifier metadata‑ref?
function‑type‑id type‑id
basic‑block label instructions
label @ digit+

The once flag denotes that identical function definitions may occur many times in different translation units. The linker chooses one of them and throws the rest away.

Instructions

Syntax

instructions ( instruction metadata‑ref? )+
instruction store‑instruction | arg‑instruction | jmp‑instruction | branch‑instruction | procedure‑call‑instruction | ret‑instruction | switch‑instruction | no‑operation‑instruction | value‑instruction
value‑instruction type‑ref reg‑value operation
operation unary‑instruction | binary‑instruction | param‑instruction | local‑instruction | load‑instruction | elemaddr‑instruction | ptroffset‑instruction | ptrdiff‑instruction | function‑call‑instruction | trap‑instruction
unary‑instruction unary‑inst operand
unary‑inst not‑instruction | neg‑instruction | signextend‑instruction | zeroextend‑instruction | truncate‑instruction | bitcast‑instruction| inttofloat‑instruction | floattoint‑instruction | inttoptr‑instruction | ptrtoint‑instruction
operand type‑ref value
binary‑instruction binary‑inst operand , operand
binary‑inst add‑instruction sub‑instruction mul‑instruction div‑instruction mod‑instruction and‑instruction or‑instruction xor‑instruction shl‑instruction shr‑instruction equal‑instruction less‑instruction

Alphabetical List of Instructions

1 'add' instruction
2 'and' instruction
3 'arg' instruction
4 'bitcast' instruction
5 'branch' instruction
6 'call' instruction (Function Call)
7 'call' instruction (Procedure Call)
8 'div' instruction
9 'elemaddr' instruction
10 'equal' instruction
11 'floattoint' instruction
12 'inttofloat' instruction
13 'inttoptr' instruction
14 'jmp' instruction
15 'less' instruction
16 'load' instruction
17 'local' instruction
18 'mod' instruction
19 'mul' instruction
20 'neg' instruction
21 'nop' instruction
22 'not' instruction
23 'or' instruction
24 'param' instruction
25 'ptrdiff' instruction
26 'ptroffset' instruction
27 'ptrtoint' instruction
28 'ret' instruction
29 'shl' instruction
30 'shr' instruction
31 'signextend' instruction
32 'store' instruction
33 'sub' instruction
34 'switch' instruction
35 'trap' instruction
36 'truncate' instruction
37 'xor' instruction
38 'zeroextend' instruction

'add' Instruction

Syntax

add‑instruction add

Synopsis

<type> <result> = add <type> <left>, <type> <right> <meta>?

Description

The 'add' instruction sets the result to the sum of its operands.

Constraints

The type of the result and the types of the operands must be the same. The type must be an arithmetic type.

'and' Instruction

Syntax

and‑instruction and

Synopsis

<type> <result> = and <type> <left>, <type> <right> <meta>?

Description

The 'and' instruction sets the result to the bitwise AND of its left and right operands.

Constraints

The type of the result and the types of the operands must be the same. The type must be an integer or Boolean type.

'arg' Instruction

Syntax

arg‑instruction arg type‑ref value

Synopsis

arg <type> <argument> <meta>?

Description

The 'arg' instruction passes a function argument to a following function call, procedure call or trap.

Constraints

The type of the operand must be an aritmetic, pointer or Boolean type.

'bitcast' Instruction

Syntax

bitcast‑instruction bitcast

Synopsis

<type> <result> = bitcast <type> <operand> <meta>?

Description

The 'bitcast' instruction sets the result to its operand converted to a target type without changing the bits of the operand.

Constraints

The type of the operand must be an aritmetic, pointer, function or Boolean type. The type of the result must be an arithmetic, pointer, function or Boolean type.

'branch' Instruction

Syntax

branch‑instruction branch type‑ref value , label , label

Synopsis

branch <type> <condition>, <true-target>, <false-target> <meta>?

Description

The 'branch' instruction transfers control to the intermediate instruction that has the true-target label if condition is true and to the intermedite instruction that has the false-target label if condition is false.

Constraints

The type of the operand must be a Boolean type.

'call' Instruction (Function Call)

Syntax

function‑call‑instruction call function‑type‑id callee
function‑type‑id type‑id
callee value

Synopsis

<type> <result> = call <type> <callee> <meta>?

Description

The function call 'call' instruction calls the 'callee' function that returns a result.

Constraints

The type of the 'callee' must be a function or a pointer to function type. The type of the result must be the type returned by the 'callee' function.

'call' Instruction (Procedure Call)

Syntax

procedure‑call‑instruction call procedure‑type‑id callee
procedure‑type‑id type‑id
callee value

Synopsis

call <type> <callee> <meta>?

Description

The procedure call 'call' instruction calls the void 'callee' function.

Constraints

The type of the 'callee' must be a function or a pointer to function type.

'div' Instruction

Syntax

div‑instruction div

Synopsis

<type> <result> = div <type> <left>, <type> <right> <meta>?

Description

The 'div' instruction sets the result to the quotient of its left operand divided by its right operand.

Constraints

The type of the result and the types of the operands must be the same. The type must be an arithmetic type.

'elemaddr' Instruction

Syntax

elemaddr‑instruction elemaddr operand , operand

Synopsis

<type> <result> = elemaddr <type> <ptr>, <type> <index> <meta>?

Description

The 'elemaddr' instruction sets the result to an address of an index'th element from an aggregate pointed by ptr.

Constraints

The type of the 'ptr' must be a a pointer type. The type of the result must be a pointer to the element type.

'equal' Instruction

Syntax

equal‑instruction equal

Synopsis

<type> <result> = equal <type> <left>, <type> <right> <meta>?

Description

The 'equal' instruction sets the result to true if its left and right operands are equal and false otherwise.

Constraints

The type of the operands must be the same. The type of the operands must be an arithmetic, pointer of Boolean type. The type of the result must be a Boolean type.

'floattoint' Instruction

Syntax

floattoint‑instruction floattoint

Synopsis

<type> <result> = floattoint <type> <operand> <meta>?

Description

The 'floattoint' instruction sets the result to its floating-point type operand converted to an integer type.

Constraints

The type of the operand must be a floating-point type. The type of the result must be an integer type.

'inttofloat' Instruction

Syntax

inttofloat‑instruction inttofloat

Synopsis

<type> <result> = inttofloat <type> <operand> <meta>?

Description

The 'inttofloat' instruction sets the result to its integer type operand converted to a floating-point type.

Constraints

The type of the operand must be an integer type. The type of the result must be a floating-point type.

'inttoptr' Instruction

Syntax

inttoptr‑instruction inttoptr

Synopsis

<type> <result> = inttoptr <type> <operand> <meta>?

Description

The 'inttoptr' instruction sets the result to its integer type operand converted to a pointer type.

Constraints

The type of the operand must be an integer type. The type of the result must be a pointer type.

'jmp' Instruction

Syntax

jmp‑instruction jmp label

Synopsis

jmp <label> <meta>?

Description

The 'jmp' instruction transfers control to the intermediate instruction that has the given label.

'less' Instruction

Syntax

less‑instruction less

Synopsis

<type> <result> = less <type> <left>, <type> <right> <meta>?

Description

The 'less' instruction sets the result to true if its left operand is less than its right operand and false otherwise.

Constraints

The type of the operands must be the same. The type of the operands must be an arithmetic or pointer type. The type of the result must be a Boolean type.

'load' Instruction

Syntax

load‑instruction load operand

Synopsis

<type> <result> = load <type> <from> <meta>?

Description

The 'load' instruction reads from memory.

Constraints

The type of the operand must be a pointer type. The type of the result must be the type of the value pointed by the 'from' pointer.

'local' Instruction

Syntax

local‑instruction local type‑ref

Synopsis

<type> <var> = local <type> <meta>?

Description

The 'local' instruction allocates space for a local variable from a function frame.

Constraints

The type of the result must a pointer to a local variable type.

'mod' Instruction

Syntax

mod‑instruction mod

Synopsis

<type> <result> = mod <type> <left>, <type> <right> <meta>?

Description

The 'mod' instruction sets the result to the remainder of the division of its left and right operands.

Constraints

The type of the result and the types of the operands must be the same. The type must be an integer type.

'mul' Instruction

Syntax

mul‑instruction mul

Synopsis

<type> <result> = mul <type> <left>, <type> <right> <meta>?

Description

The 'mul' instruction sets the result to the product of its operands.

Constraints

The type of the result and the types of the operands must be the same. The type must be an arithmetic type.

'neg' Instruction

Syntax

neg‑instruction neg

Synopsis

<type> <result> = neg <type> <operand> <meta>?

Description

The 'neg' instruction sets the result to its operand subtracted from zero.

'nop' Instruction

Syntax

no‑operation‑instruction nop

Synopsis

nop <meta>?

Description

The 'nop' instruction is a no-operation.

'not' Instruction

Syntax

not‑instruction not

Synopsis

<type> <result> = not <type> <operand> <meta>?

Description

If the operand of the 'not' instruction is a Boolean value, the instruction sets a Boolean type result to true if its operand is false and false if its operand is true. If the operand of the 'not' instruction is a an integer value, the instruction sets an integer type result to the bitwise complement of its operand.

Constraints

The type of the result and the type of the operand must be the same. The type of the operand must be an integer or a Boolean type.

'or' Instruction

Syntax

or‑instruction or

Synopsis

<type> <result> = or <type> <left>, <type> <right> <meta>?

Description

The 'or' instruction sets the result to the bitwise OR of its left and right operands.

Constraints

The type of the result and the types of the operands must be the same. The type must be an integer or Boolean type.

'param' Instruction

Syntax

param‑instruction param

Synopsis

<type> <result> = param <meta>?

Description

The 'param' instruction receives a value of a function parameter of a given type.

'ptrdiff' Instruction

Syntax

ptrdiff‑instruction ptrdiff operand , operand

Synopsis

<type> <result> = ptrdiff <type> <left-ptr>, <type> <right-ptr> <meta>?

Description

The 'ptrdiff' instruction sets the result to the number of elements between pointers left-ptr and right-ptr.

Constraints

The type of the operands must be the same. The type of the operands must be a pointer type. The type of the result must be an integer type.

'ptroffset' Instruction

Syntax

ptroffset‑instruction ptroffset operand , operand

Synopsis

<type> <result> = prtoffset <type> <ptr>, <type> <offset> <meta>?

Description

The 'ptroffset' instruction sets the result to an address of an element having given offset added to pointer ptr.

Constraints

The type of the left operand must be a pointer type. The type of the 'offset' must be an integer type. The type of the result must be a pointer type.

'ptrtoint' Instruction

Syntax

ptrtoint‑instruction ptrtoint

Synopsis

<type> <result> = ptrtoint <type> <operand> <meta>?

Description

The 'ptrtoint' instruction converts its pointer type operand to an integer type.

Constraints

The type of the operand must be a pointer type. The type of the result must be an integer type.

'ret' instruction

Syntax

ret‑instruction ret (void | type‑ref value)

Synopsis

ret (void | <type> <value>) <meta>?

The 'ret' instruction has two forms: The first form is used for returning control from a void function, and the second form is used for returning a value from a non-void function.

'shl' Instruction

Syntax

shl‑instruction shl

Synopsis

<type> <result> = shl <type> <left>, <type> <right> <meta>?

Description

The 'shl' instruction sets the result to its left operand shifted left by the number of bit positions indicated by its right operand.

Constraints

The type of the result and the types of the operands must be the same. The type must be an integer type.

'shr' Instruction

Syntax

shr‑instruction shr

Synopsis

<type> <result> = shr <type> <left>, <type> <right> <meta>?

Description

The 'shr' instruction sets the result to its left operand shifted right by the number of bit positions indicated by its right operand.

Constraints

The type of the result and the types of the operands must be the same. The type must be an integer type.

'signextend' Instruction

Syntax

signextend‑instruction signextend

Synopsis

<type> <result> = signextend <type> <operand> <meta>?

Description

The 'signextend' instruction increases the number of bits of its operand while preserving the value of its operand. If the operand is non-negative the vacant most significant bit positions of the result are filled with zero bits, otherwise the vacant most significant bit positions of the result are fillled with one bits.

Constraints

The type of the result and the type of the operand must be the same. The type must be an aritmetic type.

'store' Instruction

Syntax

store‑instruction store type‑ref value , type‑ref ptr‑value

Synopsis

store <type> <value>, <type> <ptr> <meta>?

Description

The 'store' instruction writes a value to the memory location pointed by ptr.

Constraints

The type of the 'ptr' must be a pointer type.

'sub' Instruction

Syntax

sub‑instruction sub

Synopsis

<type> <result> = sub <type> <left>, <type> <right> <meta>?

Description

The 'sub' instruction sets the result to the difference of its operands.

Constraints

The type of the result and the types of the operands must be the same. The type must be an arithmetic type.

'switch' Instruction

Syntax

switch‑instruction switch type‑ref value label , [ type‑ref value , label (: type‑ref value , label)* ]

Synopsis

switch <type> <condition> <default-target> , [ ( <type> <value>, <case-target> ( : <type> <value>, <case-target> )* )? ] <meta>?

Description

The 'switch' instruction transfers control to an instruction having a label case-target if the condition is equal to the value associated with the case-target. If none of the values associated with case-targets match the condition, control is transfered to the instruction having label default-target.

Constraints

The type of the 'condition' must be an integer or Boolean type. The type of each case value must be the same as the type of the condition.

'trap' Instruction

Syntax

trap‑instruction trap type‑ref value , type‑ref value , type‑ref value

Synopsis

<type> <result> = trap <type> <op1>, <type> <op2>, <type> <op3> <meta>?

Description

The 'trap' instruction calls a virtual operating system trap. See System X Kernel Traps.

Constrains

The types of the operands shall be byte. The second byte operand shall contain the number of the virtual operating system trap to be called, the first and the third byte operands shall be zero. The arguments to the trap function shall be passed using the arg instruction.

'truncate' Instruction

Syntax

truncate‑instruction truncate

Synopsis

<type> <result> = truncate <type> <operand> <meta>?

Description

The 'truncate' instruction truncates its operand by throwing away the most significant bits of the operand so that it fits the type of the result.

Constraints

The type of the operand must be an aritmetic type. The type of the result must be an arithmetic or Boolean type.

'xor' Instruction

Syntax

xor‑instruction xor

Synopsis

<type> <result> = xor <type> <left>, <type> <right> <meta>?

Description

The 'xor' instruction sets the result to the bitwise exclusive OR of its left and right operands.

Constraints

The type of the result and the types of the operands must be the same. The type must be an integer or Boolean type.

'zeroextend' Instruction

Syntax

zeroextend‑instruction zeroextend

Synopsis

<type> <result> = zeroextend <type> <operand> <meta>?

Description

The 'zeroextend' instruction increases the number of bits of its operand by adding zero bits to the vacant most significant bit positions of the result.

Constraints

The type of the operand must be an aritmetic or Boolean type. The type of the result must be an arithmetic type.

Metadata

Metadata Declarations

Syntax

metada‑declarations metadata { mdstruct* }
mdstruct ! digit+ = { (mdfield (, mdfield)*)? }
mdfield identifier : mditem
mditem mdbool | mdlong | mdstring | metadata‑ref
mdbool true | false
mdlong number
mdstring string‑constant