instruction.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package common
  2. type Short = uint8
  3. type Long = uint16
  4. const ShortSize = 8
  5. const LongSize = 16
  6. const SumMaxBranches = 1 << ShortSize
  7. const ProductMaxSize = 1 << ShortSize
  8. const ClosureMaxSize = 1 << ShortSize
  9. const FunCodeMaxLength = 1 << LongSize
  10. const LocalSlotMaxSize = 1 << LongSize
  11. const GlobalSlotMaxSize = 1 << (ShortSize + LongSize)
  12. const ArrayMaxSize = 1 << (ShortSize + LongSize)
  13. type Instruction struct {
  14. OpCode OpType
  15. Arg0 Short
  16. Arg1 Long
  17. }
  18. type OpType Short
  19. const (
  20. NOP OpType = iota
  21. NIL // [ ___ ]: Load a nil value
  22. /* Data Transfer */
  23. GLOBAL // [ index ]: Load a global value (constant or function)
  24. LOAD // [ _, offset ]: Load a value from (frame base) + offset
  25. STORE // [ _, offset ]: Store current value to (frame base) + offset
  26. /* Sum Type Operations */
  27. SUM // [index, ___ ]: Create a value of a sum type
  28. JIF // [index, dest ]: Jump if Index matches the current value
  29. JMP // [____, dest ]: Jump unconditionally
  30. /* Product Type Operations */
  31. PROD // [size, _ ]: Create a value of a product type
  32. GET // [index, _ ]: Extract the value of a field
  33. SET // [index, _ ]: Perform a functional update on a field
  34. /* Function Type Operations */
  35. CTX // [rec, _ ]: Use the current value as the context of a closure
  36. CALL // [___, _ ]: Call a (native)function (pop func, pop arg, push ret)
  37. /* Array Operations */
  38. ARRAY // [ size ]: Create an empty array
  39. APPEND // [ ____ ]: Append an element to the created array
  40. )
  41. func (inst Instruction) GetGlobalIndex() uint {
  42. return (uint(inst.Arg0) << LongSize) + uint(inst.Arg1)
  43. }
  44. func (inst Instruction) GetArraySize() uint {
  45. return inst.GetGlobalIndex()
  46. }
  47. func GlobalIndex(i uint) (Short, Long) {
  48. return Short(i & ((1 << LongSize) - 1)), Long(i >> LongSize)
  49. }
  50. func ArraySize(n uint) (Short, Long) {
  51. return GlobalIndex(n)
  52. }
  53. func (inst Instruction) GetOffset() uint {
  54. return uint(inst.Arg1)
  55. }
  56. func (inst Instruction) GetDestAddr() uint {
  57. return uint(inst.Arg1)
  58. }
  59. func (inst Instruction) GetShortIndexOrSize() uint {
  60. return uint(inst.Arg0)
  61. }
  62. func (inst Instruction) GetRawShortIndexOrSize() Short {
  63. return inst.Arg0
  64. }