2019-01-09 10:17:42 +03:00
|
|
|
package compiler
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// ReadOperands reads operands from the bytecode.
|
2019-01-09 10:17:42 +03:00
|
|
|
func ReadOperands(def *Definition, ins []byte) (operands []int, offset int) {
|
|
|
|
for _, width := range def.Operands {
|
|
|
|
switch width {
|
|
|
|
case 1:
|
|
|
|
operands = append(operands, int(ReadUint8(ins[offset:])))
|
|
|
|
case 2:
|
|
|
|
operands = append(operands, int(ReadUint16(ins[offset:])))
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += width
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// ReadUint16 reads uint16 from the byte slice.
|
2019-01-09 10:17:42 +03:00
|
|
|
func ReadUint16(b []byte) uint16 {
|
|
|
|
return uint16(b[1]) | uint16(b[0])<<8
|
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// ReadUint8 reads uint8 from the byte slice.
|
2019-01-09 10:17:42 +03:00
|
|
|
func ReadUint8(b []byte) uint8 {
|
|
|
|
return uint8(b[0])
|
|
|
|
}
|