From 71580abeba71239e077478fdb5bdaf2b8cc7ef6a Mon Sep 17 00:00:00 2001 From: Douglas Katzman Date: Thu, 20 Aug 2015 16:16:16 +0000 Subject: [PATCH] [Sparc]: correct the 'set' synthetic instruction Differential Revision: http://reviews.llvm.org/D12194 llvm-svn: 245575 --- lib/Target/Sparc/AsmParser/SparcAsmParser.cpp | 42 +++++++++-- test/MC/Sparc/sparc-asm-errors.s | 8 +++ test/MC/Sparc/sparc-synthetic-instructions.s | 72 ++++++++++++++++++- 3 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 test/MC/Sparc/sparc-asm-errors.s diff --git a/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp b/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp index 286a5f545ae..d8f8d216ba5 100644 --- a/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp +++ b/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp @@ -437,7 +437,22 @@ void SparcAsmParser::expandSET(MCInst &Inst, SMLoc IDLoc, // the imm operand can be either an expression or an immediate. bool IsImm = Inst.getOperand(1).isImm(); - uint64_t ImmValue = IsImm ? MCValOp.getImm() : 0; + int64_t RawImmValue = IsImm ? MCValOp.getImm() : 0; + + // Allow either a signed or unsigned 32-bit immediate. + if (RawImmValue < -2147483648 || RawImmValue > 4294967295) { + Error(IDLoc, "set: argument must be between -2147483648 and 4294967295"); + return; + } + + // If the value was expressed as a large unsigned number, that's ok. + // We want to see if it "looks like" a small signed number. + int32_t ImmValue = RawImmValue; + // For 'set' you can't use 'or' with a negative operand on V9 because + // that would splat the sign bit across the upper half of the destination + // register, whereas 'set' is defined to zero the high 32 bits. + bool IsEffectivelyImm13 = + IsImm && ((is64Bit() ? 0 : -4096) <= ImmValue && ImmValue < 4096); const MCExpr *ValExpr; if (IsImm) ValExpr = MCConstantExpr::create(ImmValue, getContext()); @@ -446,7 +461,10 @@ void SparcAsmParser::expandSET(MCInst &Inst, SMLoc IDLoc, MCOperand PrevReg = MCOperand::createReg(Sparc::G0); - if (!IsImm || (ImmValue & ~0x1fff)) { + // If not just a signed imm13 value, then either we use a 'sethi' with a + // following 'or', or a 'sethi' by itself if there are no more 1 bits. + // In either case, start with the 'sethi'. + if (!IsEffectivelyImm13) { MCInst TmpInst; const MCExpr *Expr = SparcMCExpr::create(SparcMCExpr::VK_Sparc_HI, ValExpr, getContext()); @@ -458,10 +476,24 @@ void SparcAsmParser::expandSET(MCInst &Inst, SMLoc IDLoc, PrevReg = MCRegOp; } - if (!IsImm || ((ImmValue & 0x1fff) != 0 || ImmValue == 0)) { + // The low bits require touching in 3 cases: + // * A non-immediate value will always require both instructions. + // * An effectively imm13 value needs only an 'or' instruction. + // * Otherwise, an immediate that is not effectively imm13 requires the + // 'or' only if bits remain after clearing the 22 bits that 'sethi' set. + // If the low bits are known zeros, there's nothing to do. + // In the second case, and only in that case, must we NOT clear + // bits of the immediate value via the %lo() assembler function. + // Note also, the 'or' instruction doesn't mind a large value in the case + // where the operand to 'set' was 0xFFFFFzzz - it does exactly what you mean. + if (!IsImm || IsEffectivelyImm13 || (ImmValue & 0x3ff)) { MCInst TmpInst; - const MCExpr *Expr = - SparcMCExpr::create(SparcMCExpr::VK_Sparc_LO, ValExpr, getContext()); + const MCExpr *Expr; + if (IsEffectivelyImm13) + Expr = ValExpr; + else + Expr = + SparcMCExpr::create(SparcMCExpr::VK_Sparc_LO, ValExpr, getContext()); TmpInst.setLoc(IDLoc); TmpInst.setOpcode(SP::ORri); TmpInst.addOperand(MCRegOp); diff --git a/test/MC/Sparc/sparc-asm-errors.s b/test/MC/Sparc/sparc-asm-errors.s new file mode 100644 index 00000000000..6a4128f683f --- /dev/null +++ b/test/MC/Sparc/sparc-asm-errors.s @@ -0,0 +1,8 @@ +! RUN: not llvm-mc %s -arch=sparc -show-encoding 2>&1 | FileCheck %s +! RUN: not llvm-mc %s -arch=sparcv9 -show-encoding 2>&1 | FileCheck %s + +! Test the lower and upper bounds of 'set' + ! CHECK: argument must be between + set -2147483649, %o1 + ! CHECK: argument must be between + set 4294967296, %o1 diff --git a/test/MC/Sparc/sparc-synthetic-instructions.s b/test/MC/Sparc/sparc-synthetic-instructions.s index 09fd30c09e2..f83c8c2893a 100644 --- a/test/MC/Sparc/sparc-synthetic-instructions.s +++ b/test/MC/Sparc/sparc-synthetic-instructions.s @@ -27,13 +27,33 @@ ! CHECK: or %g1, %lo(40000), %g1 ! encoding: [0x82,0x10,0b011000AA,A] ! CHECK: ! fixup A - offset: 0, value: %lo(40000), kind: fixup_sparc_lo10 set 40000, %g1 - ! CHECK: mov %lo(1), %g1 ! encoding: [0x82,0x10,0b001000AA,A] - ! CHECK: ! fixup A - offset: 0, value: %lo(1), kind: fixup_sparc_lo10 + ! CHECK: mov 1, %g1 ! encoding: [0x82,0x10,0x20,0x01] set 1, %g1 ! CHECK: sethi %hi(32768), %g1 ! encoding: [0x03,0b00AAAAAA,A,A] ! CHECK: ! fixup A - offset: 0, value: %hi(32768), kind: fixup_sparc_hi22 set 32768, %g1 + ! Expect a 'sethi' without an 'or'. + ! CHECK: sethi %hi(268431360), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! CHECK: ! fixup A - offset: 0, value: %hi(268431360), kind: fixup_sparc_hi22 + set 0x0ffff000, %o1 + + ! CHECK: sethi %hi(268433408), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! CHECK: ! fixup A - offset: 0, value: %hi(268433408), kind: fixup_sparc_hi22 + set 0x0ffff800, %o1 + + ! This is the boundary case that uses the lowest of the 22 bits in sethi. + ! CHECK: sethi %hi(268434432), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! CHECK: ! fixup A - offset: 0, value: %hi(268434432), kind: fixup_sparc_hi22 + set 0x0ffffc00, %o1 + + ! Now the synthetic instruction becomes two instructions. + ! CHECK: sethi %hi(2147483647), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! CHECK: ! fixup A - offset: 0, value: %hi(2147483647), kind: fixup_sparc_hi22 + ! CHECK: or %o1, %lo(2147483647), %o1 ! encoding: [0x92,0x12,0b011000AA,A] + ! CHECK: ! fixup A - offset: 0, value: %lo(2147483647), kind: fixup_sparc_lo10 + set 2147483647, %o1 + ! CHECK: xnor %g1, %g0, %g2 ! encoding: [0x84,0x38,0x40,0x00] not %g1, %g2 ! CHECK: xnor %g1, %g0, %g1 ! encoding: [0x82,0x38,0x40,0x00] @@ -143,3 +163,51 @@ wr %i0, %tbr ! CHECK: wr %g0, 5, %tbr ! encoding: [0x81,0x98,0x20,0x05] wr 5, %tbr + +! The following tests exercise 'set' in such a way that its output differs +! depending on whether targeting V8 or V9. +! +! RUN: llvm-mc %s -arch=sparc -show-encoding | FileCheck %s --check-prefix=V8 +! RUN: llvm-mc %s -arch=sparcv9 -show-encoding | FileCheck %s --check-prefix=V9 + + ! V8: mov -1, %o1 ! encoding: [0x92,0x10,0x3f,0xff] + ! V9: sethi %hi(-1), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! V9: ! fixup A - offset: 0, value: %hi(-1), kind: fixup_sparc_hi22 + ! V9: or %o1, %lo(-1), %o1 ! encoding: [0x92,0x12,0b011000AA,A] + ! V9: ! fixup A - offset: 0, value: %lo(-1), kind: fixup_sparc_lo10 + set 0xffffffff, %o1 + + ! V8: mov -2, %o1 ! encoding: [0x92,0x10,0x3f,0xfe] + ! V9: sethi %hi(-2), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! V9: ! fixup A - offset: 0, value: %hi(-2), kind: fixup_sparc_hi22 + ! V9: or %o1, %lo(-2), %o1 ! encoding: [0x92,0x12,0b011000AA,A] + ! V9: ! fixup A - offset: 0, value: %lo(-2), kind: fixup_sparc_lo10 + set 0xfffffffe, %o1 + + ! V8: mov -16, %o1 ! encoding: [0x92,0x10,0x3f,0xf0] + ! V9: sethi %hi(-16), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! V9: ! fixup A - offset: 0, value: %hi(-16), kind: fixup_sparc_hi22 + ! V9: or %o1, %lo(-16), %o1 ! encoding: [0x92,0x12,0b011000AA,A] + ! V9: ! fixup A - offset: 0, value: %lo(-16), kind: fixup_sparc_lo10 + set 0xfffffff0, %o1 + + ! V8: mov -256, %o1 ! encoding: [0x92,0x10,0x3f,0x00] + ! V9: sethi %hi(-256), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! V9: ! fixup A - offset: 0, value: %hi(-256), kind: fixup_sparc_hi22 + ! V9: or %o1, %lo(-256), %o1 ! encoding: [0x92,0x12,0b011000AA,A] + ! V9: ! fixup A - offset: 0, value: %lo(-256), kind: fixup_sparc_lo10 + set 0xffffff00, %o1 + + ! V8: mov -4096, %o1 ! encoding: [0x92,0x10,0x30,0x00] + ! V9: sethi %hi(-4096), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! V9: ! fixup A - offset: 0, value: %hi(-4096), kind: fixup_sparc_hi22 + set 0xfffff000, %o1 + + ! These results are the same for V8 and V9, so this test could have + ! been with the others that weren't segregated by architecture, + ! but logically it belongs here as a boundary case. + ! V8: sethi %hi(-8192), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! V8: ! fixup A - offset: 0, value: %hi(-8192), kind: fixup_sparc_hi22 + ! V9: sethi %hi(-8192), %o1 ! encoding: [0x13,0b00AAAAAA,A,A] + ! V9: ! fixup A - offset: 0, value: %hi(-8192), kind: fixup_sparc_hi22 + set 0xffffe000, %o1