Skip to content

Commit

Permalink
MIPS/Asm/O32: Don't add another $ to PrivateGlobal symbol
Browse files Browse the repository at this point in the history
For asm code like:
xx:
	la	$2,$yy
$yy:
	nop

MIPS O32 use `$` as PrivateGlobalPrefix, while another
`$` is added for getOrCreateSymbol, thus:
  error: Undefined temporary symbol $$yy

We also set symbols that starts with `.L` as local for O32.

See: llvm#65020.
  • Loading branch information
wzssyqa authored and MaskRay committed Feb 14, 2024
1 parent 61c83e9 commit 43d59de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
7 changes: 6 additions & 1 deletion llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2920,6 +2920,11 @@ bool MipsAsmParser::loadAndAddSymbolAddress(const MCExpr *SymExpr,
(Res.getSymA()->getSymbol().isELF() &&
cast<MCSymbolELF>(Res.getSymA()->getSymbol()).getBinding() ==
ELF::STB_LOCAL);
if (!IsLocalSym && ABI.IsO32()) {
// PrivateGlobalPrefix for O32 is '$', while we support '.L' anyway.
if (Res.getSymA()->getSymbol().getName().starts_with(".L"))
IsLocalSym = true;
}
bool UseXGOT = STI->hasFeature(Mips::FeatureXGOT) && !IsLocalSym;

// The case where the result register is $25 is somewhat special. If the
Expand Down Expand Up @@ -6359,7 +6364,7 @@ bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
return true;

SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
MCSymbol *Sym = getContext().getOrCreateSymbol("$" + Identifier);
MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
// Otherwise create a symbol reference.
const MCExpr *SymRef =
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/Mips/hf1_body.ll
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ entry:
; ALL: .set reorder
; ALL: .reloc 0, R_MIPS_NONE, v_sf
; GAS: la $25, $__fn_local_v_sf
; IAS: lw $25, %got($$__fn_local_v_sf)($gp)
; IAS: addiu $25, $25, %lo($$__fn_local_v_sf)
; IAS: lw $25, %got($__fn_local_v_sf)($gp)
; IAS: addiu $25, $25, %lo($__fn_local_v_sf)
; ALL: mfc1 $4, $f12
; ALL: jr $25
; ALL: .end __fn_stub_v_sf
23 changes: 23 additions & 0 deletions llvm/test/MC/Mips/macro-la-local.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# RUN: llvm-mc %s -triple=mips-unknown-linux -show-encoding -mcpu=mips32r2 | \
# RUN: FileCheck %s --check-prefixes=CHECK
# RUN: llvm-mc %s -triple=mips-unknown-linux -show-encoding -mcpu=mips32r6 | \
# RUN: FileCheck %s --check-prefixes=CHECK

.text
.abicalls
.option pic2
xx:
la $2,.Lhello #CHECK: lw $2, %got(.Lhello)($gp) # encoding: [0x8f,0x82,A,A]
#CHECK: # fixup A - offset: 0, value: %got(.Lhello), kind: fixup_Mips_GOT
#CHECK: addiu $2, $2, %lo(.Lhello) # encoding: [0x24,0x42,A,A]
#CHECK: # fixup A - offset: 0, value: %lo(.Lhello), kind: fixup_Mips_LO16

la $2,$hello2 #CHECK: lw $2, %got($hello2)($gp) # encoding: [0x8f,0x82,A,A]
#CHECK: # fixup A - offset: 0, value: %got($hello2), kind: fixup_Mips_GOT
#CHECK: addiu $2, $2, %lo($hello2) # encoding: [0x24,0x42,A,A]
#CHECK: # fixup A - offset: 0, value: %lo($hello2), kind: fixup_Mips_LO16
.rdata
.Lhello:
.asciz "Hello world\n"
$hello2:
.asciz "Hello world\n"

0 comments on commit 43d59de

Please sign in to comment.