Skip to content

Commit 96918bc

Browse files
committed
Fix the assembler to print a better relocatable expression error
diagnostic that includes location information. Currently if one has this assembly: .quad (0x1234 + (4 * SOME_VALUE)) where SOME_VALUE is undefined ones gets the less than useful error message with no location information: % clang -c x.s clang -cc1as: fatal error: error in backend: expected relocatable expression With this fix one now gets a more useful error message with location information: % clang -c x.s x.s:5:8: error: expected relocatable expression .quad (0x1234 + (4 * SOME_VALUE)) ^ To do this I plumbed the SMLoc through the MCObjectStreamer EmitValue() and EmitValueImpl() interfaces so it could be used when creating the MCFixup. rdar://12391022 llvm-svn: 206906
1 parent 5f1a001 commit 96918bc

14 files changed

+40
-19
lines changed

llvm/include/llvm/MC/MCELFStreamer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class MCELFStreamer : public MCObjectStreamer {
7272
uint64_t Size = 0, unsigned ByteAlignment = 0) override;
7373
void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
7474
uint64_t Size, unsigned ByteAlignment = 0) override;
75-
void EmitValueImpl(const MCExpr *Value, unsigned Size) override;
75+
void EmitValueImpl(const MCExpr *Value, unsigned Size,
76+
const SMLoc &Loc = SMLoc()) override;
7677

7778
void EmitFileDirective(StringRef Filename) override;
7879

llvm/include/llvm/MC/MCObjectStreamer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class MCObjectStreamer : public MCStreamer {
8181
void EmitLabel(MCSymbol *Symbol) override;
8282
void EmitDebugLabel(MCSymbol *Symbol) override;
8383
void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
84-
void EmitValueImpl(const MCExpr *Value, unsigned Size) override;
84+
void EmitValueImpl(const MCExpr *Value, unsigned Size,
85+
const SMLoc &Loc = SMLoc()) override;
8586
void EmitULEB128Value(const MCExpr *Value) override;
8687
void EmitSLEB128Value(const MCExpr *Value) override;
8788
void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;

llvm/include/llvm/MC/MCStreamer.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,12 @@ class MCStreamer {
529529
/// @param Value - The value to emit.
530530
/// @param Size - The size of the integer (in bytes) to emit. This must
531531
/// match a native machine width.
532-
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) = 0;
532+
/// @param Loc - The location of the expression for error reporting.
533+
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
534+
const SMLoc &Loc = SMLoc()) = 0;
533535

534-
void EmitValue(const MCExpr *Value, unsigned Size);
536+
void EmitValue(const MCExpr *Value, unsigned Size,
537+
const SMLoc &Loc = SMLoc());
535538

536539
/// EmitIntValue - Special case of EmitValue that avoids the client having
537540
/// to pass in a MCExpr for constant integers.

llvm/lib/LTO/LTOModule.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,8 @@ namespace {
698698
void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
699699
uint64_t Size, unsigned ByteAlignment) override {}
700700
void EmitBytes(StringRef Data) override {}
701-
void EmitValueImpl(const MCExpr *Value, unsigned Size) override {}
701+
void EmitValueImpl(const MCExpr *Value, unsigned Size,
702+
const SMLoc &Loc) override {}
702703
void EmitULEB128Value(const MCExpr *Value) override {}
703704
void EmitSLEB128Value(const MCExpr *Value) override {}
704705
void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,

llvm/lib/MC/MCAsmStreamer.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ class MCAsmStreamer : public MCStreamer {
175175

176176
void EmitBytes(StringRef Data) override;
177177

178-
void EmitValueImpl(const MCExpr *Value, unsigned Size) override;
178+
void EmitValueImpl(const MCExpr *Value, unsigned Size,
179+
const SMLoc &Loc = SMLoc()) override;
179180
void EmitIntValue(uint64_t Value, unsigned Size) override;
180181

181182
void EmitULEB128Value(const MCExpr *Value) override;
@@ -702,7 +703,8 @@ void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
702703
EmitValue(MCConstantExpr::Create(Value, getContext()), Size);
703704
}
704705

705-
void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
706+
void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
707+
const SMLoc &Loc) {
706708
assert(Size <= 8 && "Invalid size");
707709
assert(getCurrentSection().first &&
708710
"Cannot emit contents before setting section!");

llvm/lib/MC/MCELFStreamer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,12 @@ void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
275275
EmitCommonSymbol(Symbol, Size, ByteAlignment);
276276
}
277277

278-
void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
278+
void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
279+
const SMLoc &Loc) {
279280
if (getCurrentSectionData()->isBundleLocked())
280281
report_fatal_error("Emitting values inside a locked bundle is forbidden");
281282
fixSymbolsInTLSFixups(Value);
282-
MCObjectStreamer::EmitValueImpl(Value, Size);
283+
MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
283284
}
284285

285286
void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,

llvm/lib/MC/MCNullStreamer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ namespace {
7070
uint64_t Size, unsigned ByteAlignment) override {}
7171
void EmitBytes(StringRef Data) override {}
7272

73-
void EmitValueImpl(const MCExpr *Value, unsigned Size) override {}
73+
void EmitValueImpl(const MCExpr *Value, unsigned Size,
74+
const SMLoc &Loc = SMLoc()) override {}
7475
void EmitULEB128Value(const MCExpr *Value) override {}
7576
void EmitSLEB128Value(const MCExpr *Value) override {}
7677
void EmitGPRel32Value(const MCExpr *Value) override {}

llvm/lib/MC/MCObjectStreamer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) {
9797
return Value;
9898
}
9999

100-
void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
100+
void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
101+
const SMLoc &Loc) {
101102
MCDataFragment *DF = getOrCreateDataFragment();
102103

103104
MCLineEntry::Make(this, getCurrentSection().first);
@@ -110,7 +111,7 @@ void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
110111
}
111112
DF->getFixups().push_back(
112113
MCFixup::Create(DF->getContents().size(), Value,
113-
MCFixup::getKindForSize(Size, false)));
114+
MCFixup::getKindForSize(Size, false), Loc));
114115
DF->getContents().resize(DF->getContents().size() + Size, 0);
115116
}
116117

llvm/lib/MC/MCParser/AsmParser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2364,7 +2364,7 @@ bool AsmParser::parseDirectiveValue(unsigned Size) {
23642364
return Error(ExprLoc, "literal value out of range for directive");
23652365
getStreamer().EmitIntValue(IntValue, Size);
23662366
} else
2367-
getStreamer().EmitValue(Value, Size);
2367+
getStreamer().EmitValue(Value, Size, ExprLoc);
23682368

23692369
if (getLexer().is(AsmToken::EndOfStatement))
23702370
break;

llvm/lib/MC/MCStreamer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size) {
147147
}
148148

149149

150-
void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
151-
EmitValueImpl(Value, Size);
150+
void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size,
151+
const SMLoc &Loc) {
152+
EmitValueImpl(Value, Size, Loc);
152153
}
153154

154155
void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size) {

llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ class AArch64ELFStreamer : public MCELFStreamer {
9292
/// This is one of the functions used to emit data into an ELF section, so the
9393
/// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
9494
/// if necessary.
95-
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
95+
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
96+
const SMLoc &Loc) {
9697
EmitDataMappingSymbol();
97-
MCELFStreamer::EmitValueImpl(Value, Size);
98+
MCELFStreamer::EmitValueImpl(Value, Size, Loc);
9899
}
99100

100101
private:

llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ class ARMELFStreamer : public MCELFStreamer {
531531
/// This is one of the functions used to emit data into an ELF section, so the
532532
/// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
533533
/// necessary.
534-
void EmitValueImpl(const MCExpr *Value, unsigned Size) override {
534+
void EmitValueImpl(const MCExpr *Value, unsigned Size,
535+
const SMLoc &Loc) override {
535536
EmitDataMappingSymbol();
536537
MCELFStreamer::EmitValueImpl(Value, Size);
537538
}

llvm/lib/Target/ARM64/MCTargetDesc/ARM64ELFStreamer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class ARM64ELFStreamer : public MCELFStreamer {
9292
/// This is one of the functions used to emit data into an ELF section, so the
9393
/// ARM64 streamer overrides it to add the appropriate mapping symbol ($d)
9494
/// if necessary.
95-
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
95+
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
96+
const SMLoc &Loc) {
9697
EmitDataMappingSymbol();
9798
MCELFStreamer::EmitValueImpl(Value, Size);
9899
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: not llvm-mc -triple x86_64-apple-darwin10 %s -filetype=obj -o - 2> %t.err > %t
2+
// RUN: FileCheck --check-prefix=CHECK-ERROR < %t.err %s
3+
4+
.quad (0x1234 + (4 * SOME_VALUE))
5+
// CHECK-ERROR: error: expected relocatable expression
6+
// CHECK-ERROR: ^

0 commit comments

Comments
 (0)