code.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package code
  2. // Author: simon
  3. // Author: ynwdlxm@163.com
  4. // Date: 2022/10/19 13:12
  5. // Desc: bytecode
  6. import (
  7. "bytes"
  8. "encoding/binary"
  9. "fmt"
  10. )
  11. type Instructions []byte
  12. // String MiniDisassembler
  13. func (ins Instructions) String() string {
  14. var out bytes.Buffer
  15. i := 0
  16. for i < len(ins) {
  17. def, err := Lookup(ins[i])
  18. if err != nil {
  19. _, err := fmt.Fprintf(&out, "ERROR: %s\n", err)
  20. if err != nil {
  21. return ""
  22. }
  23. continue
  24. }
  25. operands, read := ReadOperands(def, ins[i+1:])
  26. _, err = fmt.Fprintf(&out, "%04d %s\n", i, ins.fmtInstructions(def, operands))
  27. if err != nil {
  28. return ""
  29. }
  30. i += 1 + read
  31. }
  32. return out.String()
  33. }
  34. func (ins Instructions) fmtInstructions(def *Definition, operands []int) string {
  35. operandCount := len(def.OperandWidths)
  36. if len(operands) != operandCount {
  37. return fmt.Sprintf("ERROR: operand len %d does not match defined %d\n", len(operands), operandCount)
  38. }
  39. switch operandCount {
  40. case 1:
  41. return fmt.Sprintf("%s %d", def.Name, operands[0])
  42. }
  43. return fmt.Sprintf("ERROR: unhandled operandCount for %s\n", def.Name)
  44. }
  45. type Opcode byte
  46. const (
  47. OpConstant Opcode = iota
  48. )
  49. // Definition For debugging and testing purposes
  50. //
  51. // it’s handy being able to look up how many operands an opcode has and what its human-readable name is.
  52. // In order to achieve that, we’ll add proper definitions and some tooling
  53. //
  54. // Name helps to make an opcode readable
  55. // OperandWidths contains the number of bytes each operand takes up
  56. type Definition struct {
  57. Name string
  58. OperandWidths []int
  59. }
  60. var definitions = map[Opcode]*Definition{
  61. OpConstant: {Name: "OpConstant", OperandWidths: []int{2}},
  62. }
  63. func Lookup(op byte) (*Definition, error) {
  64. def, ok := definitions[Opcode(op)]
  65. if !ok {
  66. return nil, fmt.Errorf("opcode %d undefined", op)
  67. }
  68. return def, nil
  69. }
  70. func Make(op Opcode, operands ...int) []byte {
  71. def, ok := definitions[op]
  72. if !ok {
  73. return []byte{}
  74. }
  75. instructionLen := 1
  76. for _, w := range def.OperandWidths {
  77. instructionLen += w
  78. }
  79. instruction := make([]byte, instructionLen) // allocate the byte slice
  80. instruction[0] = byte(op)
  81. offset := 1
  82. for i, o := range operands {
  83. width := def.OperandWidths[i]
  84. switch width {
  85. case 2:
  86. binary.BigEndian.PutUint16(instruction[offset:], uint16(o))
  87. }
  88. offset += width
  89. }
  90. return instruction
  91. }
  92. // ReadOperands reverses everything Make did
  93. func ReadOperands(def *Definition, ins Instructions) ([]int, int) {
  94. operands := make([]int, len(def.OperandWidths))
  95. offset := 0
  96. for i, width := range def.OperandWidths {
  97. switch width {
  98. case 2:
  99. operands[i] = int(ReadUint16(ins[offset:]))
  100. }
  101. offset += width
  102. }
  103. return operands, offset
  104. }
  105. func ReadUint16(ins Instructions) uint16 {
  106. return binary.BigEndian.Uint16(ins)
  107. }