Skip to content

Commit

Permalink
Given target assembler parsers a chance to handle variant expressions
Browse files Browse the repository at this point in the history
first. Use this to turn the PPC modifiers into PPC specific expressions,
allowing them to work on constants.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189400 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
jsonn committed Aug 27, 2013
1 parent b0bb2d6 commit 66b7139
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/llvm/MC/MCTargetAsmParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define LLVM_MC_TARGETPARSER_H

#include "llvm/MC/MCParser/MCAsmParserExtension.h"
#include "llvm/MC/MCExpr.h"

namespace llvm {
class MCStreamer;
Expand Down Expand Up @@ -174,6 +175,12 @@ class MCTargetAsmParser : public MCAsmParserExtension {

virtual void convertToMapAndConstraints(unsigned Kind,
const SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;

virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
MCSymbolRefExpr::VariantKind,
MCContext &Ctx) {
return 0;
}
};

} // End llvm namespace
Expand Down
4 changes: 4 additions & 0 deletions lib/MC/MCParser/AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,10 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
const MCExpr *
AsmParser::ApplyModifierToExpr(const MCExpr *E,
MCSymbolRefExpr::VariantKind Variant) {
// Ask the target implementation about this expression first.
const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
if (NewE)
return NewE;
// Recurse over the given expression, rebuilding it to apply the given variant
// if there is exactly one symbol.
switch (E->getKind()) {
Expand Down
27 changes: 27 additions & 0 deletions lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ class PPCAsmParser : public MCTargetAsmParser {
virtual bool ParseDirective(AsmToken DirectiveID);

unsigned validateTargetOperandClass(MCParsedAsmOperand *Op, unsigned Kind);

virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
MCSymbolRefExpr::VariantKind,
MCContext &Ctx);
};

/// PPCOperand - Instances of this class represent a parsed PowerPC machine
Expand Down Expand Up @@ -1363,3 +1367,26 @@ unsigned PPCAsmParser::validateTargetOperandClass(MCParsedAsmOperand *AsmOp,
return Match_InvalidOperand;
}

const MCExpr *
PPCAsmParser::applyModifierToExpr(const MCExpr *E,
MCSymbolRefExpr::VariantKind Variant,
MCContext &Ctx) {
switch (Variant) {
case MCSymbolRefExpr::VK_PPC_LO:
return PPCMCExpr::Create(PPCMCExpr::VK_PPC_LO, E, false, Ctx);
case MCSymbolRefExpr::VK_PPC_HI:
return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HI, E, false, Ctx);
case MCSymbolRefExpr::VK_PPC_HA:
return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HA, E, false, Ctx);
case MCSymbolRefExpr::VK_PPC_HIGHER:
return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHER, E, false, Ctx);
case MCSymbolRefExpr::VK_PPC_HIGHERA:
return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHERA, E, false, Ctx);
case MCSymbolRefExpr::VK_PPC_HIGHEST:
return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHEST, E, false, Ctx);
case MCSymbolRefExpr::VK_PPC_HIGHESTA:
return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHESTA, E, false, Ctx);
default:
return 0;
}
}
7 changes: 4 additions & 3 deletions lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PPCMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
const MCAsmLayout *Layout) const {
MCValue Value;

if (!getSubExpr()->EvaluateAsRelocatable(Value, *Layout))
if (Layout && !getSubExpr()->EvaluateAsRelocatable(Value, *Layout))
return false;

if (Value.isAbsolute()) {
Expand Down Expand Up @@ -85,7 +85,7 @@ PPCMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
break;
}
Res = MCValue::get(Result);
} else {
} else if (Layout) {
MCContext &Context = Layout->getAssembler().getContext();
const MCSymbolRefExpr *Sym = Value.getSymA();
MCSymbolRefExpr::VariantKind Modifier = Sym->getKind();
Expand Down Expand Up @@ -118,7 +118,8 @@ PPCMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
}
Sym = MCSymbolRefExpr::Create(&Sym->getSymbol(), Modifier, Context);
Res = MCValue::get(Sym, Value.getSymB(), Value.getConstant());
}
} else
return false;

return true;
}
Expand Down
3 changes: 3 additions & 0 deletions test/MC/PowerPC/ppc64-errors.s
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@
# CHECK-NEXT: ld 1, 32768(2)
ld 1, 32768(2)

# CHECK: error: invalid modifier 'got' (no symbols present)
addi 4, 3, 123@got
# CHECK-NEXT: addi 4, 3, 123@got
4 changes: 4 additions & 0 deletions test/MC/PowerPC/ppc64-fixups.s
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,7 @@ base:
# CHECK-REL: 0x{{[0-9A-F]*[08]}} R_PPC64_DTPREL64 target 0x0
.quad target@dtprel

# Constant fixup
ori 1, 2, 131071@l
# CHECK: ori 1, 2, 131071@l # encoding: [0x60,0x41,A,A]
# CHECK-NEXT: # fixup A - offset: 2, value: 131071@l, kind: fixup_ppc_half16

0 comments on commit 66b7139

Please sign in to comment.