Skip to content

Commit

Permalink
[Hexagon] Use addAliasForDirective for data directives
Browse files Browse the repository at this point in the history
Data directives such as .word, .half, .hword are currently parsed using 
HexagonAsmParser::ParseDirectiveValue which effectively duplicates logic from 
AsmParser::parseDirectiveValue. This patch deletes that duplicated logic in 
favour of using addAliasForDirective.

Differential Revision: https://reviews.llvm.org/D46999

llvm-svn: 332607
  • Loading branch information
asb committed May 17, 2018
1 parent 84a9c48 commit 5e41fc8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
43 changes: 4 additions & 39 deletions llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
Expand Up @@ -118,7 +118,6 @@ class HexagonAsmParser : public MCTargetAsmParser {

bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
bool ParseDirectiveSubsection(SMLoc L);
bool ParseDirectiveValue(unsigned Size, SMLoc L);
bool ParseDirectiveComm(bool IsLocal, SMLoc L);
bool RegisterMatchesArch(unsigned MatchNum) const;

Expand Down Expand Up @@ -165,6 +164,10 @@ class HexagonAsmParser : public MCTargetAsmParser {
MCB.setOpcode(Hexagon::BUNDLE);
setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));

Parser.addAliasForDirective(".half", ".2byte");
Parser.addAliasForDirective(".hword", ".2byte");
Parser.addAliasForDirective(".word", ".4byte");

MCAsmParserExtension::Initialize(_Parser);
}

Expand Down Expand Up @@ -651,11 +654,6 @@ bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
/// ParseDirective parses the Hexagon specific directives
bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
StringRef IDVal = DirectiveID.getIdentifier();
if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
return ParseDirectiveValue(4, DirectiveID.getLoc());
if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
IDVal.lower() == ".half")
return ParseDirectiveValue(2, DirectiveID.getLoc());
if (IDVal.lower() == ".falign")
return ParseDirectiveFalign(256, DirectiveID.getLoc());
if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
Expand Down Expand Up @@ -723,39 +721,6 @@ bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
return false;
}

/// ::= .word [ expression (, expression)* ]
bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
if (getLexer().isNot(AsmToken::EndOfStatement)) {
while (true) {
const MCExpr *Value;
SMLoc ExprLoc = L;
if (getParser().parseExpression(Value))
return true;

// Special case constant expressions to match code generator.
if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
assert(Size <= 8 && "Invalid size");
uint64_t IntValue = MCE->getValue();
if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
return Error(ExprLoc, "literal value out of range for directive");
getStreamer().EmitIntValue(IntValue, Size);
} else
getStreamer().EmitValue(Value, Size);

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

// FIXME: Improve diagnostic.
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lex();
}
}

Lex();
return false;
}

// This is largely a copy of AsmParser's ParseDirectiveComm extended to
// accept a 3rd argument, AccessAlignment which indicates the smallest
// memory access made to the symbol, expressed in bytes. If no
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/MC/Hexagon/data-directives-invalid.s
@@ -0,0 +1,20 @@
# RUN: not llvm-mc -triple hexagon < %s 2>&1 | FileCheck %s

# CHECK: [[@LINE+1]]:7: error: out of range literal value in '.byte' directive
.byte 0xffa
# CHECK: [[@LINE+1]]:7: error: out of range literal value in '.half' directive
.half 0xffffa
# CHECK: [[@LINE+1]]:8: error: out of range literal value in '.short' directive
.short 0xffffa
# CHECK: [[@LINE+1]]:8: error: out of range literal value in '.hword' directive
.hword 0xffffa
# CHECK: [[@LINE+1]]:8: error: out of range literal value in '.2byte' directive
.2byte 0xffffa
# CHECK: [[@LINE+1]]:7: error: out of range literal value in '.word' directive
.word 0xffffffffa
# CHECK: [[@LINE+1]]:7: error: out of range literal value in '.long' directive
.long 0xffffffffa
# CHECK: [[@LINE+1]]:8: error: out of range literal value in '.4byte' directive
.4byte 0xffffffffa
# CHECK: [[@LINE+1]]:8: error: literal value out of range for directive in '.8byte' directive
.8byte 0xffffffffffffffffa
25 changes: 25 additions & 0 deletions llvm/test/MC/Hexagon/data-directives-valid.s
@@ -0,0 +1,25 @@
# RUN: llvm-mc -filetype=obj -triple hexagon < %s \
# RUN: | llvm-objdump -s - | FileCheck %s

.data

# CHECK: Contents of section .data:
# CHECK-NEXT: 0000 deadbeef badcaf11 22334455 66778800
.byte 0xde
.half 0xbead
.word 0xafdcbaef
.8byte 0x8877665544332211
.byte 0

# CHECK-NEXT: 0010 deadbeef badcaf11 22334455 66778800
.byte 0xde
.2byte 0xbead
.4byte 0xafdcbaef
.8byte 0x8877665544332211
.byte 0

# CHECK-NEXT: 0020 deadbeef badcaf11 22
.byte 0xde
.short 0xbead
.long 0xafdcbaef
.hword 0x2211

0 comments on commit 5e41fc8

Please sign in to comment.