diff --git a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp --- a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp +++ b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp @@ -3655,17 +3655,25 @@ if (inPicMode()) { // FIXME: // a) Fix lw/sw $reg, symbol($reg) instruction expanding. - // b) If expression includes offset (sym + number), do not - // encode the offset into a relocation. Take it in account - // in the last load/store instruction. // c) Check that immediates of R_MIPS_GOT16/R_MIPS_LO16 relocations // do not exceed 16-bit. // d) Use R_MIPS_GOT_PAGE/R_MIPS_GOT_OFST relocations instead // of R_MIPS_GOT_DISP in appropriate cases to reduce number // of GOT entries. - loadAndAddSymbolAddress(OffsetOp.getExpr(), TmpReg, Mips::NoRegister, + MCValue Res; + if (!OffsetOp.getExpr()->evaluateAsRelocatable(Res, nullptr, nullptr)) { + Error(IDLoc, "expected relocatable expression"); + return; + } + if (Res.getSymB() != nullptr) { + Error(IDLoc, "expected relocatable expression with only one symbol"); + return; + } + + loadAndAddSymbolAddress(Res.getSymA(), TmpReg, Mips::NoRegister, !ABI.ArePtrs64bit(), IDLoc, Out, STI); - TOut.emitRRI(Inst.getOpcode(), DstReg, TmpReg, 0, IDLoc, STI); + TOut.emitRRI(Inst.getOpcode(), DstReg, TmpReg, Res.getConstant(), IDLoc, + STI); } else { const MCExpr *ExprOffset = OffsetOp.getExpr(); MCOperand LoOperand = MCOperand::createExpr( diff --git a/llvm/test/MC/Mips/mips-expansions.s b/llvm/test/MC/Mips/mips-expansions.s --- a/llvm/test/MC/Mips/mips-expansions.s +++ b/llvm/test/MC/Mips/mips-expansions.s @@ -122,17 +122,17 @@ # CHECK-LE: sw $10, 0($1) # encoding: [0x00,0x00,0x2a,0xac] lw $8, 1f+8 -# CHECK-LE: lw $8, %got(($tmp0)+8)($gp) # encoding: [A,A,0x88,0x8f] -# CHECK-LE: # fixup A - offset: 0, value: %got(($tmp0)+8), kind: fixup_Mips_GOT -# CHECK-LE: addiu $8, $8, %lo(($tmp0)+8) # encoding: [A,A,0x08,0x25] -# CHECK-LE: # fixup A - offset: 0, value: %lo(($tmp0)+8), kind: fixup_Mips_LO16 -# CHECK-LE: lw $8, 0($8) # encoding: [0x00,0x00,0x08,0x8d] +# CHECK-LE: lw $8, %got($tmp0)($gp) # encoding: [A,A,0x88,0x8f] +# CHECK-LE-NEXT: # fixup A - offset: 0, value: %got($tmp0), kind: fixup_Mips_GOT +# CHECK-LE-NEXT: addiu $8, $8, %lo($tmp0) # encoding: [A,A,0x08,0x25] +# CHECK-LE-NEXT: # fixup A - offset: 0, value: %lo($tmp0), kind: fixup_Mips_LO16 +# CHECK-LE-NEXT: lw $8, 8($8) # encoding: [0x08,0x00,0x08,0x8d] sw $8, 1f+8 -# CHECK-LE: lw $1, %got(($tmp0)+8)($gp) # encoding: [A,A,0x81,0x8f] -# CHECK-LE: # fixup A - offset: 0, value: %got(($tmp0)+8), kind: fixup_Mips_GOT -# CHECK-LE: addiu $1, $1, %lo(($tmp0)+8) # encoding: [A,A,0x21,0x24] -# CHECK-LE: # fixup A - offset: 0, value: %lo(($tmp0)+8), kind: fixup_Mips_LO16 -# CHECK-LE: sw $8, 0($1) # encoding: [0x00,0x00,0x28,0xac] +# CHECK-LE: lw $1, %got($tmp0)($gp) # encoding: [A,A,0x81,0x8f] +# CHECK-LE-NEXT: # fixup A - offset: 0, value: %got($tmp0), kind: fixup_Mips_GOT +# CHECK-LE-NEXT: addiu $1, $1, %lo($tmp0) # encoding: [A,A,0x21,0x24] +# CHECK-LE-NEXT: # fixup A - offset: 0, value: %lo($tmp0), kind: fixup_Mips_LO16 +# CHECK-LE-NEXT: sw $8, 8($1) # encoding: [0x08,0x00,0x28,0xac] lw $10, 655483($4) # CHECK-LE: lui $10, 10 # encoding: [0x0a,0x00,0x0a,0x3c] @@ -144,15 +144,13 @@ # CHECK-LE: sw $10, -7616($1) # encoding: [0x40,0xe2,0x2a,0xac] lw $8, symbol+8 -# CHECK-LE: lw $8, %got(symbol)($gp) # encoding: [A,A,0x88,0x8f] -# CHECK-LE: # fixup A - offset: 0, value: %got(symbol), kind: fixup_Mips_GOT -# CHECK-LE: addiu $8, $8, 8 # encoding: [0x08,0x00,0x08,0x25] -# CHECK-LE: lw $8, 0($8) # encoding: [0x00,0x00,0x08,0x8d] +# CHECK-LE: lw $8, %got(symbol)($gp) # encoding: [A,A,0x88,0x8f] +# CHECK-LE-NEXT: # fixup A - offset: 0, value: %got(symbol), kind: fixup_Mips_GOT +# CHECK-LE-NEXT: lw $8, 8($8) # encoding: [0x08,0x00,0x08,0x8d] sw $8, symbol+8 -# CHECK-LE: lw $1, %got(symbol)($gp) # encoding: [A,A,0x81,0x8f] -# CHECK-LE: # fixup A - offset: 0, value: %got(symbol), kind: fixup_Mips_GOT -# CHECK-LE: addiu $1, $1, 8 # encoding: [0x08,0x00,0x21,0x24] -# CHECK-LE: sw $8, 0($1) # encoding: [0x00,0x00,0x28,0xac] +# CHECK-LE: lw $1, %got(symbol)($gp) # encoding: [A,A,0x81,0x8f] +# CHECK-LE-NEXT: # fixup A - offset: 0, value: %got(symbol), kind: fixup_Mips_GOT +# CHECK-LE-NEXT: sw $8, 8($1) # encoding: [0x08,0x00,0x28,0xac] ldc1 $f0, symbol # CHECK-LE: lw $1, %got(symbol)($gp) # encoding: [A,A,0x81,0x8f] diff --git a/llvm/test/MC/Mips/mips64-expansions.s b/llvm/test/MC/Mips/mips64-expansions.s --- a/llvm/test/MC/Mips/mips64-expansions.s +++ b/llvm/test/MC/Mips/mips64-expansions.s @@ -481,15 +481,13 @@ # CHECK: sw $10, 0($1) # encoding: [0x00,0x00,0x2a,0xac] lw $8, sym+8 -# CHECK: ld $8, %got_disp(sym)($gp) # encoding: [A,A,0x88,0xdf] -# CHECK: # fixup A - offset: 0, value: %got_disp(sym), kind: fixup_Mips_GOT_DISP -# CHECK: daddiu $8, $8, 8 # encoding: [0x08,0x00,0x08,0x65] -# CHECK: lw $8, 0($8) # encoding: [0x00,0x00,0x08,0x8d] +# CHECK: ld $8, %got_disp(sym)($gp) # encoding: [A,A,0x88,0xdf] +# CHECK-NEXT: # fixup A - offset: 0, value: %got_disp(sym), kind: fixup_Mips_GOT_DISP +# CHECK-NEXT: lw $8, 8($8) # encoding: [0x08,0x00,0x08,0x8d] sw $8, sym+8 -# CHECK: ld $1, %got_disp(sym)($gp) # encoding: [A,A,0x81,0xdf] -# CHECK: # fixup A - offset: 0, value: %got_disp(sym), kind: fixup_Mips_GOT_DISP -# CHECK: daddiu $1, $1, 8 # encoding: [0x08,0x00,0x21,0x64] -# CHECK: sw $8, 0($1) # encoding: [0x00,0x00,0x28,0xac] +# CHECK: ld $1, %got_disp(sym)($gp) # encoding: [A,A,0x81,0xdf] +# CHECK-NEXT: # fixup A - offset: 0, value: %got_disp(sym), kind: fixup_Mips_GOT_DISP +# CHECK-NEXT: sw $8, 8($1) # encoding: [0x08,0x00,0x28,0xac] lw $10, 655483($4) # CHECK: lui $10, 10 # encoding: [0x0a,0x00,0x0a,0x3c] @@ -501,15 +499,13 @@ # CHECK: sw $10, -7616($1) # encoding: [0x40,0xe2,0x2a,0xac] lw $8, symbol+8 -# CHECK: ld $8, %got_disp(symbol)($gp) # encoding: [A,A,0x88,0xdf] -# CHECK: # fixup A - offset: 0, value: %got_disp(symbol), kind: fixup_Mips_GOT_DISP -# CHECK: daddiu $8, $8, 8 # encoding: [0x08,0x00,0x08,0x65] -# CHECK: lw $8, 0($8) # encoding: [0x00,0x00,0x08,0x8d] +# CHECK: ld $8, %got_disp(symbol)($gp) # encoding: [A,A,0x88,0xdf] +# CHECK-NEXT: # fixup A - offset: 0, value: %got_disp(symbol), kind: fixup_Mips_GOT_DISP +# CHECK-NEXT: lw $8, 8($8) # encoding: [0x08,0x00,0x08,0x8d] sw $8, symbol+8 -# CHECK: ld $1, %got_disp(symbol)($gp) # encoding: [A,A,0x81,0xdf] -# CHECK: # fixup A - offset: 0, value: %got_disp(symbol), kind: fixup_Mips_GOT_DISP -# CHECK: daddiu $1, $1, 8 # encoding: [0x08,0x00,0x21,0x64] -# CHECK: sw $8, 0($1) # encoding: [0x00,0x00,0x28,0xac] +# CHECK: ld $1, %got_disp(symbol)($gp) # encoding: [A,A,0x81,0xdf] +# CHECK-NEXT: # fixup A - offset: 0, value: %got_disp(symbol), kind: fixup_Mips_GOT_DISP +# CHECK-NEXT: sw $8, 8($1) # encoding: [0x08,0x00,0x28,0xac] ldc1 $f0, symbol # CHECK: ld $1, %got_disp(symbol)($gp) # encoding: [A,A,0x81,0xdf]