;; -*- Mode: Irken -*-

;; table of all opcodes for the vm.

;; NOTE: if you change this file, you need to re-run `vm/genopcodes` to
;;   regenerate the include file used by `vm/irkvm.c`.

(defmacro OI
  (OI name nargs varargs target?)
  -> {code=0 name=name nargs=nargs varargs=varargs target=target?})

;; XXX some bunches of these opcodes could be collapsed.
  e.g. vlen / vref / vset = = rlen / rref / rset ( and maybe slen / sref / sset with a quick TC_STRING check )

(define opcode-info
     name      target ? args
  #((OI 'lit     2      #f     #t)   ;; target index
    (OI 'litc    2      #f     #t)   ;; target index
    (OI 'ret     1      #f     #f)   ;; val
    (OI 'add     3      #f     #t)   ;; target a b
    (OI 'sub     3      #f     #t)   ;; target a b
    (OI 'mul     3      #f     #t)   ;; target a b
    (OI 'div     3      #f     #t)   ;; target a b
    (OI 'srem    3      #f     #t)   ;; target a b
    (OI 'shl     3      #f     #t)   ;; target a b
    (OI 'ashr    3      #f     #t)   ;; target a b
    (OI 'or      3      #f     #t)   ;; target a b
    (OI 'xor     3      #f     #t)   ;; target a b
    (OI 'and     3      #f     #t)   ;; target a b
    (OI 'eq      3      #f     #t)   ;; target a b
    (OI 'lt      3      #f     #t)   ;; target a b
    (OI 'gt      3      #f     #t)   ;; target a b
    (OI 'le      3      #f     #t)   ;; target a b
    (OI 'ge      3      #f     #t)   ;; target a b
    (OI 'cmp     3      #f     #t)   ;; target a b
    (OI 'tst     2      #f     #f)   ;; val offset
    (OI 'jmp     1      #f     #f)   ;; offset
    (OI 'fun     2      #f     #t)   ;; target offset
    (OI 'tail    2      #f     #f)   ;; closure args
    (OI 'tail0   1      #f     #f)   ;; closure
    (OI 'env     2      #f     #t)   ;; target size
    (OI 'stor    3      #f     #f)   ;; tuple arg index
    (OI 'ref     3      #f     #t)   ;; target depth index
    (OI 'mov     2      #f     #f)   ;; dst src
    (OI 'epush   1      #f     #f)   ;; args
    (OI 'trcall  3      #t     #f)   ;; offset depth nregs reg0 ...
    (OI 'trcall0 2      #f     #f)   ;; offset depth
    (OI 'ref0    2      #f     #t)   ;; target index
    (OI 'call    3      #f     #f)   ;; closure args nregs
    (OI 'call0   2      #f     #f)   ;; closure nregs
    (OI 'pop     1      #f     #t)   ;; target
    (OI 'printo  1      #f     #f)   ;; arg
    (OI 'prints  1      #f     #f)   ;; arg
    (OI 'topis   1      #f     #f)   ;; env
    (OI 'topref  2      #f     #t)   ;; target index
    (OI 'topset  2      #f     #f)   ;; index val
    (OI 'set     3      #f     #f)   ;; depth index val
    (OI 'set0    2      #f     #f)   ;; index val
    (OI 'pop0    0      #f     #f)   ;;
    (OI 'epop    0      #f     #f)   ;;
    (OI 'tron    0      #f     #f)   ;;
    (OI 'troff   0      #f     #f)   ;;
    (OI 'gc      0      #f     #f)   ;;
    (OI 'imm     2      #f     #t)   ;; target tag
  target tag nelem elem0 ...
    (OI 'makei   3      #f     #t)   ;; target tag payload
    (OI 'exit    1      #f     #f)   ;; arg
    (OI 'nvcase  5      #t     #f)   ;; ob elabel nalts tag0 off0 tag1 off1 ...
    (OI 'tupref  3      #f     #t)   ;; target ob index
  target vec
  target vec index - reg
    (OI 'vset    3      #f     #f)   ;; vec index-reg val
    (OI 'vmake   3      #f     #t)   ;; target size val
    (OI 'alloc   3      #f     #t)   ;; target tag size
    (OI 'rref    3      #f     #t)   ;; target rec label-code
    (OI 'rset    3      #f     #f)   ;; rec label-code val
    (OI 'getcc   1      #f     #t)   ;; target
    (OI 'putcc   3      #f     #t)   ;; target k val
  ( OI ' irk      3       # t      # t )    ; ; target closure nargs arg0 ...
    ;; (OI 'getc    1      #f     #t)   ;; target
( OI ' dlsym    2       # f      # t )    ; ; target name
    (OI 'ffi     4      #t     #t)   ;; target fun nargs arg0 ...
    (OI 'smake   2      #f     #t)   ;; target size
    (OI 'sfromc  3      #f     #t)   ;; target src len
    (OI 'slen    2      #f     #t)   ;; target string
    (OI 'sref    3      #f     #t)   ;; target string index
    (OI 'sset    3      #f     #f)   ;; string index char
    (OI 'scopy   5      #f     #f)   ;; src src-start n dst dst-start
    (OI 'unchar  2      #f     #t)   ;; target char
    (OI 'gist    1      #f     #t)   ;; target
    (OI 'argv    1      #f     #t)   ;; target
    (OI 'quiet   1      #f     #f)   ;; bool
    (OI 'heap    2      #f     #f)   ;; size nreg
    (OI 'readf   2      #f     #t)   ;; target path
    (OI 'malloc  3      #f     #t)   ;; target sindex size
    (OI 'halloc  3      #f     #t)   ;; target sindex size
    (OI 'cget    3      #f     #t)   ;; target src code
  src code 
    (OI 'free    1      #f     #f)   ;; src
    (OI 'sizeoff 2      #f     #f)   ;; index val
    (OI 'sgetp   2      #f     #t)   ;; dst src
    (OI 'caref   4      #f     #t)   ;; dst src sindex num
    (OI 'csref   3      #f     #t)   ;; dst src sindex
    (OI 'dlopen  2      #f     #t)   ;; target name
    (OI 'dlsym0  2      #f     #t)   ;; target name
    (OI 'dlsym   3      #f     #t)   ;; target handle name
    (OI 'csize   2      #f     #t)   ;; target sindex
    (OI 'cref2int 2     #f     #t)   ;; target src
    (OI 'int2cref 2     #f     #t)   ;; target src
    (OI 'ob2int  2      #f     #t)   ;; target src
    (OI 'obptr2int 2    #f     #t)   ;; target src
    (OI 'errno   1      #f     #t)   ;; target
    (OI 'meta    1      #f     #t)   ;; target
    (OI 'hash    5      #f     #t)   ;; target d k0 k1 size
   name      target ? args
    ))

(for-range i (vector-length opcode-info)
  (let ((info opcode-info[i]))
    (set! info.code i)))


 -*- Mode: Irken -*-
 table of all opcodes for the vm.
 NOTE: if you change this file, you need to re-run `vm/genopcodes` to
   regenerate the include file used by `vm/irkvm.c`.
 XXX some bunches of these opcodes could be collapsed.
 target index
 target index
 val
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 target a b
 val offset
 offset
 target offset
 closure args
 closure
 target size
 tuple arg index
 target depth index
 dst src
 args
 offset depth nregs reg0 ...
 offset depth
 target index
 closure args nregs
 closure nregs
 target
 arg
 arg
 env
 target index
 index val
 depth index val
 index val





 target tag
 target tag payload
 arg
 ob elabel nalts tag0 off0 tag1 off1 ...
 target ob index
 vec index-reg val
 target size val
 target tag size
 target rec label-code
 rec label-code val
 target
 target k val
 ; target closure nargs arg0 ...
 (OI 'getc    1      #f     #t)   ;; target
 ; target name
 target fun nargs arg0 ...
 target size
 target src len
 target string
 target string index
 string index char
 src src-start n dst dst-start
 target char
 target
 target
 bool
 size nreg
 target path
 target sindex size
 target sindex size
 target src code
 src
 index val
 dst src
 dst src sindex num
 dst src sindex
 target name
 target name
 target handle name
 target sindex
 target src
 target src
 target src
 target src
 target
 target
 target d k0 k1 size



(defmacro OI
  (OI name nargs varargs target?)
  -> {code=0 name=name nargs=nargs varargs=varargs target=target?})

  e.g. vlen / vref / vset = = rlen / rref / rset ( and maybe slen / sref / sset with a quick TC_STRING check )

(define opcode-info
     name      target ? args
  target tag nelem elem0 ...
  target vec
  target vec index - reg
  src code 
   name      target ? args
    ))

(for-range i (vector-length opcode-info)
  (let ((info opcode-info[i]))
    (set! info.code i)))

