T-code specification


T-code Instruction set

arithmetic/logic

binary:
add sub mul div mod
and or xor shl shr
eq ne lt le gt ge

unary:
not

immediate:
addi subi muli divi modi
andi ori xori shli shri
eqi nei lti lei gti gei
 

control transfer

call callt ret efor case jmp jt jf

data transfer

mov movi movd pass passi ldx stx lit ads

special

fun sys

Meaning of instructions

argument v1..v255 where v1..v63 locals, v64..v255 globals.  In immediate mode, argument is a small integer 0..255.  Locals and globals are accessed to M[v].

add a1 a2 a3     a1 = a2 + a3
addi a1 a2 n     a1 = a2 + n    n unsigned 8-bit 0..255
...
mov a1 a2        a1 = a2
movi a1 n        a1 = n         n 16-bit
movd a1 a2       a1 = M[a2]     ld/st indirect (defer)
ldx a1 a2 a3     a1 = M[a2+a3]  index
stx a1 a2 a3     M[a2+a3] = a1

jmp d            pc = d               d 24-bit
jt a d           if a != 0 pc += d    d 16-bit
jf a d           if a == 0 pc += d
efor a d         a++, if a <= adj(a) pc += d
call d           call
callt d          tail call, use old locals
callf d          forward call, refer to symbol table
case a d         case a in range lo..hi jump table, else pc += d

                 follows by range lit lo, lit hi, jmp table
fun arity nlocal function header, arity, nlocal 8-bit is no. of all locals
ret a nlocal     return, nlocal number of registers, a is return value
                 nlocal 8-bit
pass k a2        pass a2 as k th parameter of a func call
passi k n        pass n as k th param of a call, n 16-bit
lit n            v64 = n         n 24-bit
ads n            v64 = n         n 24-bit
sys a1 a2 num    syscall num with a1, a2 opt. input arg.

encoding

0 add sub mul div mod and or xor
8 eq ne lt le gt ge shl shr
16 addi subi muli divi modi andi ori xori
24 eqi nei lti lei gti gei shli shri
32 not mov movi movd pass passi lit ads
40 ldx stx call callt fun ret efor case
48 jmp jt jf sys end callf

note:  "array" is not an opcode anymore, it becomes "sys a 10" where a is the register storing the size of allocation.

decoding

To enable efficient decoding, the displacement (16-bit and 24-bit) will be left justified.  Decoding by shifting right will also achieve sign extension.  The format is as follows:

  a3:8 a2:3 a1:8 op:8

a3,a2  forms a 16-bit displacement
a3,a2,a1 forms a 24-bit number

the decoding is:

c = CS[ip]
op = c & 255
a = c >> 8    // a is signed 24-bit
d = a >> 8    // d is signed 16-bit
a1 = a & 255
a2 = d & 255
a3 = (d >> 8) & 255

a1, a2, a3 are 0..255

Argument of syscall

sysnum: a3 , like imm mode
input at most two:  a1, a2
output v64  if there is output, like func call

syscall number:
1 print          in: a1
2 printc         in: a1
3 getchar        in: -   out:v64
4 -
5 fopen          in: a1, a2       // fp, mode
6 fclose         in: a1           // fp
7 fprint         in: a1, a2       // fp, n
8 fprintc        in: a1, a2       // fp, c
9 fgetc          in: a1  out:v64  // fp
10 array         in: a1  out:v64
11 load          in: a1

25 December 2005