Skip to content

Commit 5acdd8d

Browse files
committed
AVR: Rename AVRMCExpr::VK_ to AVR::S_
Prepare for removing AVRMCExpr. Adopt the new naming convention (S_ instead of VK_; the relocation specifier was previously named `VariantKind`)) used by most other targets. Make AVRMCAsmInfo.h include AVRMCExpr.h and change .cpp files to include AVRMCAsmInfo.h. We will eventually remove AVRMCExpr.h.
1 parent ec32d88 commit 5acdd8d

File tree

11 files changed

+260
-253
lines changed

11 files changed

+260
-253
lines changed

llvm/lib/Target/AVR/AVRAsmPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "AVRSubtarget.h"
1717
#include "AVRTargetMachine.h"
1818
#include "MCTargetDesc/AVRInstPrinter.h"
19-
#include "MCTargetDesc/AVRMCExpr.h"
19+
#include "MCTargetDesc/AVRMCAsmInfo.h"
2020
#include "TargetInfo/AVRTargetInfo.h"
2121

2222
#include "llvm/CodeGen/AsmPrinter.h"
@@ -215,7 +215,7 @@ const MCExpr *AVRAsmPrinter::lowerConstant(const Constant *CV,
215215
bool IsProgMem = GV->getAddressSpace() == AVR::ProgramMemory;
216216
if (IsProgMem) {
217217
const MCExpr *Expr = MCSymbolRefExpr::create(getSymbol(GV), Ctx);
218-
return AVRMCExpr::create(AVRMCExpr::VK_PM, Expr, false, Ctx);
218+
return AVRMCExpr::create(AVR::S_PM, Expr, false, Ctx);
219219
}
220220
}
221221

llvm/lib/Target/AVR/AVRMCInstLower.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "AVRMCInstLower.h"
1515
#include "AVRInstrInfo.h"
16-
#include "MCTargetDesc/AVRMCExpr.h"
16+
#include "MCTargetDesc/AVRMCAsmInfo.h"
1717

1818
#include "llvm/CodeGen/AsmPrinter.h"
1919
#include "llvm/IR/Mangler.h"
@@ -42,19 +42,19 @@ AVRMCInstLower::lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
4242

4343
if (TF & AVRII::MO_LO) {
4444
if (IsFunction) {
45-
Expr = AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVRMCExpr::VK_LO8_GS
46-
: AVRMCExpr::VK_PM_LO8,
45+
Expr = AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVR::S_LO8_GS
46+
: AVR::S_PM_LO8,
4747
Expr, IsNegated, Ctx);
4848
} else {
49-
Expr = AVRMCExpr::create(AVRMCExpr::VK_LO8, Expr, IsNegated, Ctx);
49+
Expr = AVRMCExpr::create(AVR::S_LO8, Expr, IsNegated, Ctx);
5050
}
5151
} else if (TF & AVRII::MO_HI) {
5252
if (IsFunction) {
53-
Expr = AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVRMCExpr::VK_HI8_GS
54-
: AVRMCExpr::VK_PM_HI8,
53+
Expr = AVRMCExpr::create(Subtarget.hasEIJMPCALL() ? AVR::S_HI8_GS
54+
: AVR::S_PM_HI8,
5555
Expr, IsNegated, Ctx);
5656
} else {
57-
Expr = AVRMCExpr::create(AVRMCExpr::VK_HI8, Expr, IsNegated, Ctx);
57+
Expr = AVRMCExpr::create(AVR::S_HI8, Expr, IsNegated, Ctx);
5858
}
5959
} else if (TF != 0) {
6060
llvm_unreachable("Unknown target flag on symbol operand");

llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AVRRegisterInfo.h"
10+
#include "MCTargetDesc/AVRMCAsmInfo.h"
1011
#include "MCTargetDesc/AVRMCELFStreamer.h"
11-
#include "MCTargetDesc/AVRMCExpr.h"
1212
#include "MCTargetDesc/AVRMCTargetDesc.h"
1313
#include "TargetInfo/AVRTargetInfo.h"
1414

@@ -447,7 +447,7 @@ bool AVRAsmParser::tryParseExpression(OperandVector &Operands, int64_t offset) {
447447

448448
bool AVRAsmParser::tryParseRelocExpression(OperandVector &Operands) {
449449
bool isNegated = false;
450-
AVRMCExpr::Specifier ModifierKind = AVRMCExpr::VK_AVR_NONE;
450+
AVR::Specifier ModifierKind = AVR::S_AVR_NONE;
451451

452452
SMLoc S = Parser.getTok().getLoc();
453453

@@ -473,14 +473,14 @@ bool AVRAsmParser::tryParseRelocExpression(OperandVector &Operands) {
473473
StringRef ModifierName = Parser.getTok().getString();
474474
ModifierKind = AVRMCExpr::parseSpecifier(ModifierName);
475475

476-
if (ModifierKind != AVRMCExpr::VK_AVR_NONE) {
476+
if (ModifierKind != AVR::S_AVR_NONE) {
477477
Parser.Lex();
478478
Parser.Lex(); // Eat modifier name and parenthesis
479479
if (Parser.getTok().getString() == GENERATE_STUBS &&
480480
Parser.getTok().getKind() == AsmToken::Identifier) {
481481
std::string GSModName = ModifierName.str() + "_" + GENERATE_STUBS;
482482
ModifierKind = AVRMCExpr::parseSpecifier(GSModName);
483-
if (ModifierKind != AVRMCExpr::VK_AVR_NONE)
483+
if (ModifierKind != AVR::S_AVR_NONE)
484484
Parser.Lex(); // Eat gs modifier name
485485
}
486486
} else {
@@ -698,15 +698,15 @@ ParseStatus AVRAsmParser::parseLiteralValues(unsigned SizeInBytes, SMLoc L) {
698698
Tokens[1].getKind() == AsmToken::Identifier) {
699699
MCSymbol *Symbol = getContext().getOrCreateSymbol(".text");
700700
AVRStreamer.emitValueForModiferKind(Symbol, SizeInBytes, L,
701-
AVRMCExpr::VK_AVR_NONE);
701+
AVR::S_AVR_NONE);
702702
return ParseStatus::NoMatch;
703703
}
704704

705705
if (Parser.getTok().getKind() == AsmToken::Identifier &&
706706
Parser.getLexer().peekTok().getKind() == AsmToken::LParen) {
707707
StringRef ModifierName = Parser.getTok().getString();
708-
AVRMCExpr::Specifier Spec = AVRMCExpr::parseSpecifier(ModifierName);
709-
if (Spec != AVRMCExpr::VK_AVR_NONE) {
708+
AVR::Specifier Spec = AVRMCExpr::parseSpecifier(ModifierName);
709+
if (Spec != AVR::S_AVR_NONE) {
710710
Parser.Lex();
711711
Parser.Lex(); // Eat the modifier and parenthesis
712712
} else {

llvm/lib/Target/AVR/MCTargetDesc/AVRELFObjectWriter.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "MCTargetDesc/AVRFixupKinds.h"
10-
#include "MCTargetDesc/AVRMCExpr.h"
10+
#include "MCTargetDesc/AVRMCAsmInfo.h"
1111
#include "MCTargetDesc/AVRMCTargetDesc.h"
1212

1313
#include "llvm/MC/MCAssembler.h"
@@ -36,42 +36,42 @@ AVRELFObjectWriter::AVRELFObjectWriter(uint8_t OSABI)
3636
unsigned AVRELFObjectWriter::getRelocType(const MCFixup &Fixup,
3737
const MCValue &Target,
3838
bool IsPCRel) const {
39-
auto Modifier = AVRMCExpr::Specifier(Target.getSpecifier());
39+
auto Spec = Target.getSpecifier();
4040
switch ((unsigned)Fixup.getKind()) {
4141
case FK_Data_1:
42-
switch (Modifier) {
42+
switch (Spec) {
4343
default:
4444
llvm_unreachable("Unsupported Modifier");
45-
case AVRMCExpr::VK_None:
45+
case AVR::S_None:
4646
return ELF::R_AVR_8;
47-
case AVRMCExpr::VK_DIFF8:
47+
case AVR::S_DIFF8:
4848
return ELF::R_AVR_DIFF8;
49-
case AVRMCExpr::VK_LO8:
49+
case AVR::S_LO8:
5050
return ELF::R_AVR_8_LO8;
51-
case AVRMCExpr::VK_HI8:
51+
case AVR::S_HI8:
5252
return ELF::R_AVR_8_HI8;
53-
case AVRMCExpr::VK_HH8:
53+
case AVR::S_HH8:
5454
return ELF::R_AVR_8_HLO8;
5555
}
5656
case FK_Data_4:
57-
switch (Modifier) {
57+
switch (Spec) {
5858
default:
5959
llvm_unreachable("Unsupported Modifier");
60-
case AVRMCExpr::VK_None:
60+
case AVR::S_None:
6161
return ELF::R_AVR_32;
62-
case AVRMCExpr::VK_DIFF32:
62+
case AVR::S_DIFF32:
6363
return ELF::R_AVR_DIFF32;
6464
}
6565
case FK_Data_2:
66-
switch (Modifier) {
66+
switch (Spec) {
6767
default:
6868
llvm_unreachable("Unsupported Modifier");
69-
case AVRMCExpr::VK_None:
69+
case AVR::S_None:
7070
return ELF::R_AVR_16;
71-
case AVRMCExpr::VK_AVR_NONE:
72-
case AVRMCExpr::VK_PM:
71+
case AVR::S_AVR_NONE:
72+
case AVR::S_PM:
7373
return ELF::R_AVR_16_PM;
74-
case AVRMCExpr::VK_DIFF16:
74+
case AVR::S_DIFF16:
7575
return ELF::R_AVR_DIFF16;
7676
}
7777
case AVR::fixup_32:

llvm/lib/Target/AVR/MCTargetDesc/AVRMCAsmInfo.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "AVRMCAsmInfo.h"
14+
#include "llvm/MC/MCAssembler.h"
15+
#include "llvm/MC/MCContext.h"
1416
#include "llvm/MC/MCExpr.h"
17+
#include "llvm/MC/MCValue.h"
1518
#include "llvm/TargetParser/Triple.h"
1619

1720
using namespace llvm;
@@ -26,3 +29,179 @@ AVRMCAsmInfo::AVRMCAsmInfo(const Triple &TT, const MCTargetOptions &Options) {
2629
UsesELFSectionDirectiveForBSS = true;
2730
SupportsDebugInformation = true;
2831
}
32+
33+
namespace {
34+
const struct ModifierEntry {
35+
const char *const Spelling;
36+
AVRMCExpr::Specifier specifier;
37+
} ModifierNames[] = {
38+
{"lo8", AVR::S_LO8}, {"hi8", AVR::S_HI8},
39+
{"hh8", AVR::S_HH8}, // synonym with hlo8
40+
{"hlo8", AVR::S_HH8}, {"hhi8", AVR::S_HHI8},
41+
42+
{"pm", AVR::S_PM}, {"pm_lo8", AVR::S_PM_LO8},
43+
{"pm_hi8", AVR::S_PM_HI8}, {"pm_hh8", AVR::S_PM_HH8},
44+
45+
{"lo8_gs", AVR::S_LO8_GS}, {"hi8_gs", AVR::S_HI8_GS},
46+
{"gs", AVR::S_GS},
47+
};
48+
49+
} // end of anonymous namespace
50+
51+
AVRMCExpr::Specifier AVRMCExpr::parseSpecifier(StringRef Name) {
52+
const auto &Modifier =
53+
llvm::find_if(ModifierNames, [&Name](ModifierEntry const &Mod) {
54+
return Mod.Spelling == Name;
55+
});
56+
57+
if (Modifier != std::end(ModifierNames)) {
58+
return Modifier->specifier;
59+
}
60+
return AVR::S_AVR_NONE;
61+
}
62+
63+
const char *AVRMCExpr::getName() const {
64+
const auto &Modifier =
65+
llvm::find_if(ModifierNames, [this](ModifierEntry const &Mod) {
66+
return Mod.specifier == specifier;
67+
});
68+
69+
if (Modifier != std::end(ModifierNames)) {
70+
return Modifier->Spelling;
71+
}
72+
return nullptr;
73+
}
74+
75+
AVR::Fixups AVRMCExpr::getFixupKind() const {
76+
AVR::Fixups Kind = AVR::Fixups::LastTargetFixupKind;
77+
78+
switch (specifier) {
79+
case AVR::S_LO8:
80+
Kind = isNegated() ? AVR::fixup_lo8_ldi_neg : AVR::fixup_lo8_ldi;
81+
break;
82+
case AVR::S_HI8:
83+
Kind = isNegated() ? AVR::fixup_hi8_ldi_neg : AVR::fixup_hi8_ldi;
84+
break;
85+
case AVR::S_HH8:
86+
Kind = isNegated() ? AVR::fixup_hh8_ldi_neg : AVR::fixup_hh8_ldi;
87+
break;
88+
case AVR::S_HHI8:
89+
Kind = isNegated() ? AVR::fixup_ms8_ldi_neg : AVR::fixup_ms8_ldi;
90+
break;
91+
92+
case AVR::S_PM_LO8:
93+
Kind = isNegated() ? AVR::fixup_lo8_ldi_pm_neg : AVR::fixup_lo8_ldi_pm;
94+
break;
95+
case AVR::S_PM_HI8:
96+
Kind = isNegated() ? AVR::fixup_hi8_ldi_pm_neg : AVR::fixup_hi8_ldi_pm;
97+
break;
98+
case AVR::S_PM_HH8:
99+
Kind = isNegated() ? AVR::fixup_hh8_ldi_pm_neg : AVR::fixup_hh8_ldi_pm;
100+
break;
101+
case AVR::S_PM:
102+
case AVR::S_GS:
103+
Kind = AVR::fixup_16_pm;
104+
break;
105+
case AVR::S_LO8_GS:
106+
Kind = AVR::fixup_lo8_ldi_gs;
107+
break;
108+
case AVR::S_HI8_GS:
109+
Kind = AVR::fixup_hi8_ldi_gs;
110+
break;
111+
112+
default:
113+
llvm_unreachable("Uninitialized expression");
114+
}
115+
116+
return Kind;
117+
}
118+
119+
int64_t AVRMCExpr::evaluateAsInt64(int64_t Value) const {
120+
if (Negated)
121+
Value *= -1;
122+
123+
switch (specifier) {
124+
case AVR::S_LO8:
125+
Value &= 0xff;
126+
break;
127+
case AVR::S_HI8:
128+
Value &= 0xff00;
129+
Value >>= 8;
130+
break;
131+
case AVR::S_HH8:
132+
Value &= 0xff0000;
133+
Value >>= 16;
134+
break;
135+
case AVR::S_HHI8:
136+
Value &= 0xff000000;
137+
Value >>= 24;
138+
break;
139+
case AVR::S_PM_LO8:
140+
case AVR::S_LO8_GS:
141+
Value >>= 1; // Program memory addresses must always be shifted by one.
142+
Value &= 0xff;
143+
break;
144+
case AVR::S_PM_HI8:
145+
case AVR::S_HI8_GS:
146+
Value >>= 1; // Program memory addresses must always be shifted by one.
147+
Value &= 0xff00;
148+
Value >>= 8;
149+
break;
150+
case AVR::S_PM_HH8:
151+
Value >>= 1; // Program memory addresses must always be shifted by one.
152+
Value &= 0xff0000;
153+
Value >>= 16;
154+
break;
155+
case AVR::S_PM:
156+
case AVR::S_GS:
157+
Value >>= 1; // Program memory addresses must always be shifted by one.
158+
break;
159+
160+
case AVR::S_AVR_NONE:
161+
default:
162+
llvm_unreachable("Uninitialized expression.");
163+
}
164+
return static_cast<uint64_t>(Value) & 0xff;
165+
}
166+
167+
bool AVRMCExpr::evaluateAsRelocatableImpl(MCValue &Result,
168+
const MCAssembler *Asm) const {
169+
MCValue Value;
170+
bool isRelocatable = getSubExpr()->evaluateAsRelocatable(Value, Asm);
171+
if (!isRelocatable)
172+
return false;
173+
174+
if (Value.isAbsolute()) {
175+
Result = MCValue::get(evaluateAsInt64(Value.getConstant()));
176+
} else {
177+
if (!Asm || !Asm->hasLayout())
178+
return false;
179+
180+
auto Spec = AVR::S_None;
181+
if (Value.getSpecifier() != MCSymbolRefExpr::VK_None)
182+
return false;
183+
assert(!Value.getSubSym());
184+
if (specifier == AVR::S_PM)
185+
Spec = AVR::S_PM;
186+
187+
// TODO: don't attach specifier to MCSymbolRefExpr.
188+
Result =
189+
MCValue::get(Value.getAddSym(), nullptr, Value.getConstant(), Spec);
190+
}
191+
192+
return true;
193+
}
194+
195+
bool AVRMCExpr::evaluateAsConstant(int64_t &Result) const {
196+
MCValue Value;
197+
bool isRelocatable = getSubExpr()->evaluateAsRelocatable(Value, nullptr);
198+
if (!isRelocatable)
199+
return false;
200+
201+
if (Value.isAbsolute()) {
202+
Result = evaluateAsInt64(Value.getConstant());
203+
return true;
204+
}
205+
206+
return false;
207+
}

0 commit comments

Comments
 (0)