github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/code/reg.go (about) 1 package code 2 3 import "fmt" 4 5 // RegType represents the type of a register 6 type RegType uint8 7 8 // ValueRegType types 9 const ( 10 ValueRegType RegType = 0 11 CellRegType = 1 12 ) 13 14 // Reg is a register 15 type Reg struct { 16 tp RegType 17 idx uint8 18 } 19 20 // ValueReg returns a value register. 21 func ValueReg(idx uint8) Reg { 22 return Reg{ 23 idx: idx, 24 tp: ValueRegType, 25 } 26 } 27 28 // CellReg returns a cell register. 29 func CellReg(idx uint8) Reg { 30 return Reg{ 31 idx: idx, 32 tp: CellRegType, 33 } 34 } 35 36 // Idx returns the index of the register 37 func (r Reg) Idx() uint8 { 38 return r.idx 39 } 40 41 // RegType returns the type of the register 42 func (r Reg) RegType() RegType { 43 return r.tp 44 } 45 46 // IsCell returns true if r is a cell. 47 func (r Reg) IsCell() bool { 48 return r.tp == CellRegType 49 } 50 51 func (r Reg) toA() Opcode { 52 return Opcode(r.Idx())<<16 | Opcode(r.RegType())<<26 53 } 54 55 func (r Reg) toB() Opcode { 56 return Opcode(r.Idx())<<8 | Opcode(r.RegType())<<25 57 } 58 59 func (r Reg) toC() Opcode { 60 return Opcode(r.Idx()) | Opcode(r.RegType())<<24 61 } 62 63 func (r Reg) String() string { 64 switch r.RegType() { 65 case ValueRegType: 66 return fmt.Sprintf("r%d", r.Idx()) 67 case CellRegType: 68 return fmt.Sprintf("u%d", r.Idx()) 69 } 70 return "??" 71 }