Skip to content

Commit 3fd0d22

Browse files
committed
AArch64AsmParser: Restore Lsym@page-offset support
#134202 removed support for `sym@page-offset` in instruction operands. This change is generally reasonable since subtracting an offset from a symbol typically doesn’t make sense for Mach-O due to its .subsections_via_symbols mechanism, which treats them as separate atoms. However, BoringSSL relies on a temporary symbol with a negative offset, which can be meaningful when the symbol and the referenced location are within the same atom. ``` ../../third_party/boringssl/src/gen/bcm/p256-armv8-asm-apple.S:1160:25: error: unexpected token in argument list adrp x23,Lone_mont@PAGE-64 ``` It's worth noting that expressions involving @ can be complex and brittle in MCParser, and much of the Mach-O @ offsets remains under-tested. * Allow default argument for parsePrimaryExpr. The argument, used by the niche llvm-ml, should not require other targets to adapt.
1 parent f819f46 commit 3fd0d22

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

llvm/include/llvm/MC/MCParser/MCAsmParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class MCAsmParser {
308308
/// on error.
309309
/// \return - False on success.
310310
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
311-
AsmTypeInfo *TypeInfo) = 0;
311+
AsmTypeInfo *TypeInfo = nullptr) = 0;
312312

313313
/// Parse an arbitrary expression, assuming that an initial '(' has
314314
/// already been consumed.

llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4483,12 +4483,16 @@ bool AArch64AsmParser::parseSymbolicImmVal(const MCExpr *&ImmVal) {
44834483
if (getParser().parseAtSpecifier(ImmVal, EndLoc))
44844484
return true;
44854485
const MCExpr *Term;
4486-
if (parseOptionalToken(AsmToken::Plus)) {
4487-
if (getParser().parseExpression(Term, EndLoc))
4488-
return true;
4489-
ImmVal =
4490-
MCBinaryExpr::create(MCBinaryExpr::Add, ImmVal, Term, getContext());
4491-
}
4486+
MCBinaryExpr::Opcode Opcode;
4487+
if (parseOptionalToken(AsmToken::Plus))
4488+
Opcode = MCBinaryExpr::Add;
4489+
else if (parseOptionalToken(AsmToken::Minus))
4490+
Opcode = MCBinaryExpr::Sub;
4491+
else
4492+
return false;
4493+
if (getParser().parsePrimaryExpr(Term, EndLoc))
4494+
return true;
4495+
ImmVal = MCBinaryExpr::create(Opcode, ImmVal, Term, getContext());
44924496
}
44934497

44944498
return false;
@@ -5026,11 +5030,15 @@ bool AArch64AsmParser::parseOperand(OperandVector &Operands, bool isCondCode,
50265030
return true;
50275031
if (getParser().parseAtSpecifier(IdVal, E))
50285032
return true;
5029-
if (parseOptionalToken(AsmToken::Plus)) {
5030-
if (getParser().parseExpression(Term, E))
5033+
std::optional<MCBinaryExpr::Opcode> Opcode;
5034+
if (parseOptionalToken(AsmToken::Plus))
5035+
Opcode = MCBinaryExpr::Add;
5036+
else if (parseOptionalToken(AsmToken::Minus))
5037+
Opcode = MCBinaryExpr::Sub;
5038+
if (Opcode) {
5039+
if (getParser().parsePrimaryExpr(Term, E))
50315040
return true;
5032-
IdVal =
5033-
MCBinaryExpr::create(MCBinaryExpr::Add, IdVal, Term, getContext());
5041+
IdVal = MCBinaryExpr::create(*Opcode, IdVal, Term, getContext());
50345042
}
50355043
Operands.push_back(AArch64Operand::CreateImm(IdVal, S, E, getContext()));
50365044

llvm/test/MC/AArch64/arm64-diags.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ subs x20, x30, sym@PAGEOFF
366366
; CHECK-ERRORS: subs x20, x30, sym@PAGEOFF
367367
; CHECK-ERRORS: ^
368368

369+
add w3, w5, sym@PAGEOFF - 3-2
370+
; CHECK-ERRORS: [[#@LINE-1]]:28: error: unexpected token in argument list
371+
369372
tbl v0.8b, { v1 }, v0.8b
370373
tbl v0.16b, { v1.8b, v2.8b, v3.8b }, v0.16b
371374
tbx v3.16b, { v12.8b, v13.8b, v14.8b }, v6.8b

llvm/test/MC/MachO/AArch64/darwin-ARM64-reloc.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ _fred:
1717
ldr w2, [x3, _data_ext@gotpageoff]
1818
adrp x0, L_.str@PAGE
1919

20+
adrp x3, L1@PAGE - 5
21+
add x3, x3, L1@pageoff - (4+1)
22+
2023
.data
2124
_data:
2225
.quad _foo
26+
L1:
2327
.quad _foo + 4
2428
.quad _foo - _bar
2529
.quad _foo - _bar + 4
@@ -36,6 +40,34 @@ L_.str:
3640

3741
; CHECK: Relocations [
3842
; CHECK-NEXT: Section __text {
43+
; CHECK-NEXT: Relocation {
44+
; CHECK-NEXT: Offset: 0x2C
45+
; CHECK-NEXT: PCRel: 0
46+
; CHECK-NEXT: Length: 2
47+
; CHECK-NEXT: Type: ARM64_RELOC_ADDEND (10)
48+
; CHECK-NEXT: Section: __cstring (3)
49+
; CHECK-NEXT: }
50+
; CHECK-NEXT: Relocation {
51+
; CHECK-NEXT: Offset: 0x2C
52+
; CHECK-NEXT: PCRel: 0
53+
; CHECK-NEXT: Length: 2
54+
; CHECK-NEXT: Type: ARM64_RELOC_PAGEOFF12 (4)
55+
; CHECK-NEXT: Symbol: _data (2)
56+
; CHECK-NEXT: }
57+
; CHECK-NEXT: Relocation {
58+
; CHECK-NEXT: Offset: 0x28
59+
; CHECK-NEXT: PCRel: 0
60+
; CHECK-NEXT: Length: 2
61+
; CHECK-NEXT: Type: ARM64_RELOC_ADDEND (10)
62+
; CHECK-NEXT: Section: __cstring (3)
63+
; CHECK-NEXT: }
64+
; CHECK-NEXT: Relocation {
65+
; CHECK-NEXT: Offset: 0x28
66+
; CHECK-NEXT: PCRel: 1
67+
; CHECK-NEXT: Length: 2
68+
; CHECK-NEXT: Type: ARM64_RELOC_PAGE21 (3)
69+
; CHECK-NEXT: Symbol: _data (2)
70+
; CHECK-NEXT: }
3971
; CHECK-NEXT: Relocation {
4072
; CHECK-NEXT: Offset: 0x24
4173
; CHECK-NEXT: PCRel: 1

0 commit comments

Comments
 (0)