instructions.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package tengo
  2. import (
  3. "fmt"
  4. "github.com/d5/tengo/v2/parser"
  5. )
  6. // MakeInstruction returns a bytecode for an opcode and the operands.
  7. func MakeInstruction(opcode parser.Opcode, operands ...int) []byte {
  8. numOperands := parser.OpcodeOperands[opcode]
  9. totalLen := 1
  10. for _, w := range numOperands {
  11. totalLen += w
  12. }
  13. instruction := make([]byte, totalLen)
  14. instruction[0] = opcode
  15. offset := 1
  16. for i, o := range operands {
  17. width := numOperands[i]
  18. switch width {
  19. case 1:
  20. instruction[offset] = byte(o)
  21. case 2:
  22. n := uint16(o)
  23. instruction[offset] = byte(n >> 8)
  24. instruction[offset+1] = byte(n)
  25. case 4:
  26. n := uint32(o)
  27. instruction[offset] = byte(n >> 24)
  28. instruction[offset+1] = byte(n >> 16)
  29. instruction[offset+2] = byte(n >> 8)
  30. instruction[offset+3] = byte(n)
  31. }
  32. offset += width
  33. }
  34. return instruction
  35. }
  36. // FormatInstructions returns string representation of bytecode instructions.
  37. func FormatInstructions(b []byte, posOffset int) []string {
  38. var out []string
  39. i := 0
  40. for i < len(b) {
  41. numOperands := parser.OpcodeOperands[b[i]]
  42. operands, read := parser.ReadOperands(numOperands, b[i+1:])
  43. switch len(numOperands) {
  44. case 0:
  45. out = append(out, fmt.Sprintf("%04d %-7s",
  46. posOffset+i, parser.OpcodeNames[b[i]]))
  47. case 1:
  48. out = append(out, fmt.Sprintf("%04d %-7s %-5d",
  49. posOffset+i, parser.OpcodeNames[b[i]], operands[0]))
  50. case 2:
  51. out = append(out, fmt.Sprintf("%04d %-7s %-5d %-5d",
  52. posOffset+i, parser.OpcodeNames[b[i]],
  53. operands[0], operands[1]))
  54. }
  55. i += 1 + read
  56. }
  57. return out
  58. }