func (i insnO) op(code gen.OpCoder, reg regs.R) { if reg >= 8 { panic("register not supported by instruction") } code.WriteByte(i.opbase + byte(reg)) }
func writeSibTo(code gen.OpCoder, scale byte, index, base regs.R) { if scale >= 4 { panic("scale factor out of bounds") } code.WriteByte((scale << 6) | (byte(index&7) << 3) | byte(base&7)) }
func (i insnPrefixMI) opImm(code gen.OpCoder, t types.T, reg regs.R, value int32) { opcode, imm := i.immOpcode(value) i.prefix.writeTo(code, t, 0, 0, byte(reg)) code.WriteByte(opcode) writeModTo(code, ModReg, i.ro, byte(reg)) imm.writeTo(code) }
func writePrefixRegInsnTo(code gen.OpCoder, p prefix, t types.T, opcode []byte, ro, rm byte) { if opcode == nil { panic("instruction not supported") } p.writeTo(code, t, ro, 0, rm) code.Write(opcode) writeModTo(code, ModReg, ro, rm) }
func writePrefixStackInsnTo(code gen.OpCoder, p prefix, t types.T, opcode []byte, reg regs.R, disp int32) { mod, imm := dispMod(t, regStackPtr, disp) p.writeTo(code, t, byte(reg), 0, 0) code.Write(opcode) writeModTo(code, mod, byte(reg), MemSIB) writeSibTo(code, 0, regStackPtr, regStackPtr) imm.writeTo(code) }
func (i insnRexM) opStack(code gen.OpCoder, t types.T, disp int32) { mod, imm := dispMod(t, regStackPtr, disp) writeRexSizeTo(code, t, 0, 0, 0) code.Write(i.opcode) writeModTo(code, mod, i.ro, MemSIB) writeSibTo(code, 0, regStackPtr, regStackPtr) imm.writeTo(code) }
func (i insnAddr32) op(code gen.OpCoder, addr int32) { var offset int32 if addr != 0 { siteAddr := code.Len() + i.size() offset = addr - siteAddr } else { offset = -i.size() // infinite loop as placeholder } i.writeTo(code, offset) }
func writePrefixAddrInsnTo(code gen.OpCoder, p prefix, t types.T, opcode []byte, reg regs.R, scale uint8, index regs.R, addr int32) { if opcode == nil { panic("instruction not supported") } p.writeTo(code, t, byte(reg), 0, 0) code.Write(opcode) writeModTo(code, ModMem, byte(reg), MemSIB) writeSibTo(code, scale, index, NoBase) writeInt32To(code, addr) }
func (i insnPrefixMI) opImmToStack(code gen.OpCoder, t types.T, disp, value int32) { mod, immDisp := dispMod(t, regStackPtr, disp) opcode, immValue := i.immOpcode(value) i.prefix.writeTo(code, t, 0, 0, 0) code.WriteByte(opcode) writeModTo(code, mod, i.ro, MemSIB) writeSibTo(code, 0, regStackPtr, regStackPtr) immDisp.writeTo(code) immValue.writeTo(code) }
func (p *floatSizePrefix) writeTo(code gen.OpCoder, t types.T, ro, index, rmOrBase byte) { switch t.Size() { case types.Size32: code.Write(p.size32) case types.Size64: code.Write(p.size64) default: panic(t) } writeRexTo(code, 0, ro, index, rmOrBase) }
func (i insnRexM) opIndirect(code gen.OpCoder, t types.T, reg regs.R, disp int32) { mod, imm := dispMod(t, reg, disp) writeRexSizeTo(code, t, 0, 0, byte(reg)) code.Write(i.opcode) if reg != 12 { writeModTo(code, mod, i.ro, byte(reg)) } else { writeModTo(code, mod, i.ro, MemSIB) writeSibTo(code, 0, NoIndex, reg) } imm.writeTo(code) }
func writeRexTo(code gen.OpCoder, rex, ro, index, rmOrBase byte) { if ro >= 8 { rex |= rexR } if index >= 8 { rex |= rexX } if rmOrBase >= 8 { rex |= rexB } if rex != 0 { code.WriteByte(rex) } }
func (i insnPrefixMI) opImmToIndirect(code gen.OpCoder, t types.T, scale uint8, index, base regs.R, disp, value int32) { mod, immDisp := dispMod(t, base, disp) opcode, immValue := i.immOpcode(value) i.prefix.writeTo(code, t, 0, byte(index), byte(base)) code.WriteByte(opcode) if scale == 0 && index == NoIndex && base != 12 { writeModTo(code, mod, i.ro, byte(base)) } else { writeModTo(code, mod, i.ro, MemSIB) writeSibTo(code, scale, index, base) } immDisp.writeTo(code) immValue.writeTo(code) }
func writePrefixIndirectInsnTo(code gen.OpCoder, p prefix, t types.T, opcode []byte, reg regs.R, scale uint8, index, base regs.R, disp int32) { if opcode == nil { panic("instruction not supported") } mod, imm := dispMod(t, base, disp) p.writeTo(code, t, byte(reg), byte(index), byte(base)) code.Write(opcode) if scale == 0 && index == NoIndex && base != 12 { writeModTo(code, mod, byte(reg), byte(base)) } else { writeModTo(code, mod, byte(reg), MemSIB) writeSibTo(code, scale, index, base) } imm.writeTo(code) }
func (opcode insnAddr8) op(code gen.OpCoder, addr int32) (ok bool) { insnSize := int32(len(opcode)) + 1 siteAddr := code.Len() + insnSize offset := addr - siteAddr if offset >= -0x80 && offset < 0x80 { code.Write(opcode) code.WriteByte(uint8(offset)) ok = true } return }
// UpdateCalls modifies CallRel instructions, possibly while they are being // executed. func (mach X86) UpdateCalls(code gen.OpCoder, l *links.L) { funcAddr := l.FinalAddr() for _, retAddr := range l.Sites { mach.PutUint32(code.Bytes()[retAddr-4:retAddr], uint32(funcAddr-retAddr)) } }
func (i insnRexOM) opReg(code gen.OpCoder, reg regs.R) { writeRexTo(code, 0, 0, 0, byte(reg)) code.Write(i.opcode) writeModTo(code, ModReg, i.ro, byte(reg)) }
func (mach X86) updateAddr32(code gen.OpCoder, addr, value int32) { binary.LittleEndian.PutUint32(code.Bytes()[addr-4:addr], uint32(value)) }
func (mach X86) updateAddr8(code gen.OpCoder, addr, value int32) { if value < -0x80 || value >= 0x80 { panic(value) } code.Bytes()[addr-1] = uint8(value) }
func writeModTo(code gen.OpCoder, mod mod, ro, rm byte) { code.WriteByte(byte(mod) | ((ro & 7) << 3) | (rm & 7)) }
func (mach X86) OpInit(code gen.OpCoder, startAddr int32) (retAddr int32) { if code.Len() == 0 || code.Len() > functionAlignment { panic("inconsistency") } code.Align(functionAlignment, paddingByte) Add.opImm(code, types.I64, regStackLimit, gen.StackReserve) var notResume links.L Test.opFromReg(code, types.I64, regResult, regResult) Je.rel8.opStub(code) notResume.AddSite(code.Len()) Ret.op(code) // simulate return from snapshot function call notResume.Addr = code.Len() mach.updateBranches8(code, ¬Resume) CallRel.op(code, startAddr) return code.Len() }
func (i insnRexM) opReg(code gen.OpCoder, t types.T, reg regs.R) { writeRexSizeTo(code, t, 0, 0, byte(reg)) code.Write(i.opcode) writeModTo(code, ModReg, i.ro, byte(reg)) }
func (i insnSuffixRMI) opReg(code gen.OpCoder, t types.T, target, source regs.R, value int8) { code.Write(i.opcode) i.suffix.writeTo(code, t, byte(target), 0, byte(source)) writeModTo(code, ModReg, byte(target), byte(source)) code.WriteByte(uint8(value)) }
func (i insnPrefixMI) opImm8(code gen.OpCoder, t types.T, reg regs.R, value uint8) { i.prefix.writeTo(code, t, 0, 0, byte(reg)) code.WriteByte(i.opcode8) writeModTo(code, ModReg, i.ro, byte(reg)) code.WriteByte(value) }
func (opcode insnRex) op(code gen.OpCoder, t types.T) { writeRexSizeTo(code, t, 0, 0, 0) code.Write(opcode) }
func (i insnRexOI) op(code gen.OpCoder, t types.T, reg regs.R, imm imm) { writeRexSizeTo(code, t, 0, 0, byte(reg)) code.WriteByte(i.opbase + (byte(reg) & 7)) imm.writeTo(code) }
func (i insnPrefixRexRM) opReg(code gen.OpCoder, floatType, intType types.T, target, source regs.R) { i.prefix.writeTo(code, floatType, 0, 0, 0) writeRexSizeTo(code, intType, byte(target), 0, byte(source)) code.Write(i.opcode) writeModTo(code, ModReg, byte(target), byte(source)) }
func (i insnAddr8) opStub(code gen.OpCoder) { i.op(code, code.Len()) // infinite loop as placeholder }
func (opcode insnAddr32) writeTo(code gen.OpCoder, offset int32) { code.Write(opcode) writeInt32To(code, int32(offset)) }
func (i insnAddr32) opMissingFunction(code gen.OpCoder) { siteAddr := code.Len() + i.size() i.writeTo(code, -siteAddr) }