func (v *gen) genIntDiv(instr ir.Instr, signed, remainder bool, alloc *allocator) { numBytes := v.target.TypeSize(instr.Type()) if v.target.TypeSizeInBits(instr.Type())%8 != 0 { panic("unsupported int size for div") } alloc.valToReg(v, "@eax", instr.Operands()[0]) if numBytes == 4 { if signed { v.emitOp("cdq") } else { v.emitOp("xorl @edx, @edx") } } else if numBytes == 2 { if signed { v.emitOp("cwd") } else { v.emitOp("xorw @dx, @dx") } } else if numBytes == 1 { if signed { v.emitOp("cbw") } else { v.emitOp("xorb @ah, @ah") } } else { panic("unimplemented") } var prefix string if signed { prefix = "i" } v.emitOp("%sdiv%s %s", prefix, sizeSuffix(numBytes), alloc.valToReg(v, "@ebx", instr.Operands()[1])) returnReg := "@eax" if remainder { returnReg = "@edx" } alloc.regToVal(v, instr, returnReg) }
func (v *gen) genTwoOperandIntInstr(mnemonic string, instr ir.Instr, alloc *allocator) { numBytes := v.target.TypeSize(instr.Type()) v.emitOp("%s%s %s, %s", mnemonic, sizeSuffix(numBytes), alloc.valToReg(v, "@ebx", instr.Operands()[1]), alloc.valToReg(v, "@eax", instr.Operands()[0])) alloc.regToVal(v, instr, "@eax") }