2010-04-07 20:29:34 +00:00
|
|
|
//===-- ARMAsmLexer.cpp - Tokenize ARM assembly to AsmTokens --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-26 00:24:13 +00:00
|
|
|
#include "MCTargetDesc/ARMBaseInfo.h"
|
2010-04-07 20:29:34 +00:00
|
|
|
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCParser/MCAsmLexer.h"
|
|
|
|
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
|
2011-07-25 20:53:02 +00:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2011-07-26 00:24:13 +00:00
|
|
|
#include "llvm/MC/MCTargetAsmLexer.h"
|
2010-04-07 20:29:34 +00:00
|
|
|
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2010-04-07 20:29:34 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
|
2010-04-07 20:29:34 +00:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-07-26 00:24:13 +00:00
|
|
|
class ARMBaseAsmLexer : public MCTargetAsmLexer {
|
2011-01-03 23:59:05 +00:00
|
|
|
const MCAsmInfo &AsmInfo;
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
const AsmToken &lexDefinite() {
|
|
|
|
return getLexer()->Lex();
|
|
|
|
}
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
AsmToken LexTokenUAL();
|
|
|
|
protected:
|
|
|
|
typedef std::map <std::string, unsigned> rmap_ty;
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
rmap_ty RegisterMap;
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-07-25 20:53:02 +00:00
|
|
|
void InitRegisterMap(const MCRegisterInfo *info) {
|
2011-01-03 23:59:05 +00:00
|
|
|
unsigned numRegs = info->getNumRegs();
|
2010-04-07 20:29:34 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
for (unsigned i = 0; i < numRegs; ++i) {
|
|
|
|
const char *regName = info->getName(i);
|
|
|
|
if (regName)
|
|
|
|
RegisterMap[regName] = i;
|
2010-04-07 20:29:34 +00:00
|
|
|
}
|
2011-01-03 23:59:05 +00:00
|
|
|
}
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
unsigned MatchRegisterName(StringRef Name) {
|
|
|
|
rmap_ty::iterator iter = RegisterMap.find(Name.str());
|
|
|
|
if (iter != RegisterMap.end())
|
|
|
|
return iter->second;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
AsmToken LexToken() {
|
|
|
|
if (!Lexer) {
|
|
|
|
SetError(SMLoc(), "No MCAsmLexer installed");
|
|
|
|
return AsmToken(AsmToken::Error, "", 0);
|
2010-04-07 20:29:34 +00:00
|
|
|
}
|
2011-01-03 23:59:05 +00:00
|
|
|
|
|
|
|
switch (AsmInfo.getAssemblerDialect()) {
|
|
|
|
default:
|
|
|
|
SetError(SMLoc(), "Unhandled dialect");
|
|
|
|
return AsmToken(AsmToken::Error, "", 0);
|
|
|
|
case 0:
|
|
|
|
return LexTokenUAL();
|
2010-04-07 20:29:34 +00:00
|
|
|
}
|
2011-01-03 23:59:05 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
ARMBaseAsmLexer(const Target &T, const MCAsmInfo &MAI)
|
2011-07-26 00:24:13 +00:00
|
|
|
: MCTargetAsmLexer(T), AsmInfo(MAI) {
|
2011-01-03 23:59:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ARMAsmLexer : public ARMBaseAsmLexer {
|
|
|
|
public:
|
2011-07-25 20:53:02 +00:00
|
|
|
ARMAsmLexer(const Target &T, const MCRegisterInfo &MRI, const MCAsmInfo &MAI)
|
2011-01-03 23:59:05 +00:00
|
|
|
: ARMBaseAsmLexer(T, MAI) {
|
2011-07-25 20:53:02 +00:00
|
|
|
InitRegisterMap(&MRI);
|
2011-01-03 23:59:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThumbAsmLexer : public ARMBaseAsmLexer {
|
|
|
|
public:
|
2011-07-25 20:53:02 +00:00
|
|
|
ThumbAsmLexer(const Target &T, const MCRegisterInfo &MRI,const MCAsmInfo &MAI)
|
2011-01-03 23:59:05 +00:00
|
|
|
: ARMBaseAsmLexer(T, MAI) {
|
2011-07-25 20:53:02 +00:00
|
|
|
InitRegisterMap(&MRI);
|
2011-01-03 23:59:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
2010-04-07 20:29:34 +00:00
|
|
|
|
|
|
|
AsmToken ARMBaseAsmLexer::LexTokenUAL() {
|
|
|
|
const AsmToken &lexedToken = lexDefinite();
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2010-04-07 20:29:34 +00:00
|
|
|
switch (lexedToken.getKind()) {
|
2011-01-03 23:59:05 +00:00
|
|
|
default: break;
|
2010-04-07 20:29:34 +00:00
|
|
|
case AsmToken::Error:
|
|
|
|
SetError(Lexer->getErrLoc(), Lexer->getErr());
|
2011-01-03 23:59:05 +00:00
|
|
|
break;
|
|
|
|
case AsmToken::Identifier: {
|
2010-04-07 20:29:34 +00:00
|
|
|
std::string upperCase = lexedToken.getString().str();
|
|
|
|
std::string lowerCase = LowercaseString(upperCase);
|
|
|
|
StringRef lowerRef(lowerCase);
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2010-04-07 20:29:34 +00:00
|
|
|
unsigned regID = MatchRegisterName(lowerRef);
|
2010-12-23 23:19:54 +00:00
|
|
|
// Check for register aliases.
|
|
|
|
// r13 -> sp
|
|
|
|
// r14 -> lr
|
|
|
|
// r15 -> pc
|
|
|
|
// ip -> r12
|
|
|
|
// FIXME: Some assemblers support lots of others. Do we want them all?
|
|
|
|
if (!regID) {
|
2010-12-24 00:03:39 +00:00
|
|
|
regID = StringSwitch<unsigned>(lowerCase)
|
|
|
|
.Case("r13", ARM::SP)
|
|
|
|
.Case("r14", ARM::LR)
|
|
|
|
.Case("r15", ARM::PC)
|
|
|
|
.Case("ip", ARM::R12)
|
|
|
|
.Default(0);
|
2010-12-23 23:19:54 +00:00
|
|
|
}
|
2010-12-22 23:26:02 +00:00
|
|
|
|
2011-01-03 23:59:05 +00:00
|
|
|
if (regID)
|
2010-04-07 20:29:34 +00:00
|
|
|
return AsmToken(AsmToken::Register,
|
|
|
|
lexedToken.getString(),
|
|
|
|
static_cast<int64_t>(regID));
|
|
|
|
}
|
|
|
|
}
|
2011-01-03 23:59:05 +00:00
|
|
|
|
|
|
|
return AsmToken(lexedToken);
|
2010-04-07 20:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void LLVMInitializeARMAsmLexer() {
|
2011-07-26 00:24:13 +00:00
|
|
|
RegisterMCAsmLexer<ARMAsmLexer> X(TheARMTarget);
|
|
|
|
RegisterMCAsmLexer<ThumbAsmLexer> Y(TheThumbTarget);
|
2010-04-07 20:29:34 +00:00
|
|
|
}
|