blob: eb39205336f65784b4f0ef8dc8ecf0a295209ddc [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000015#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000020#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000021#include "llvm/MC/MCInst.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000029#include "llvm/Support/Compiler.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000034#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000035using namespace llvm;
36
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000037namespace {
38
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000039/// \brief Helper class for tracking macro definitions.
40struct Macro {
41 StringRef Name;
42 StringRef Body;
43
44public:
45 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
46};
47
Daniel Dunbaraef87e32010-07-18 18:31:38 +000048/// \brief The concrete assembly parser instance.
49class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000050 friend class GenericAsmParser;
51
Daniel Dunbaraef87e32010-07-18 18:31:38 +000052 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
53 void operator=(const AsmParser &); // DO NOT IMPLEMENT
54private:
55 AsmLexer Lexer;
56 MCContext &Ctx;
57 MCStreamer &Out;
58 SourceMgr &SrcMgr;
59 MCAsmParserExtension *GenericParser;
60 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000061
Daniel Dunbaraef87e32010-07-18 18:31:38 +000062 /// This is the current buffer index we're lexing from as managed by the
63 /// SourceMgr object.
64 int CurBuffer;
65
66 AsmCond TheCondState;
67 std::vector<AsmCond> TheCondStack;
68
69 /// DirectiveMap - This is a table handlers for directives. Each handler is
70 /// invoked after the directive identifier is read and is responsible for
71 /// parsing and validating the rest of the directive. The handler is passed
72 /// in the directive name and the location of the directive keyword.
73 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000074
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000075 /// MacroMap - Map of currently defined macros.
76 StringMap<Macro*> MacroMap;
77
Daniel Dunbar3c802de2010-07-18 18:38:02 +000078 /// Boolean tracking whether macro substitution is enabled.
79 unsigned MacrosEnabled : 1;
80
Daniel Dunbaraef87e32010-07-18 18:31:38 +000081public:
82 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
83 const MCAsmInfo &MAI);
84 ~AsmParser();
85
86 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
87
88 void AddDirectiveHandler(MCAsmParserExtension *Object,
89 StringRef Directive,
90 DirectiveHandler Handler) {
91 DirectiveMap[Directive] = std::make_pair(Object, Handler);
92 }
93
94public:
95 /// @name MCAsmParser Interface
96 /// {
97
98 virtual SourceMgr &getSourceManager() { return SrcMgr; }
99 virtual MCAsmLexer &getLexer() { return Lexer; }
100 virtual MCContext &getContext() { return Ctx; }
101 virtual MCStreamer &getStreamer() { return Out; }
102
103 virtual void Warning(SMLoc L, const Twine &Meg);
104 virtual bool Error(SMLoc L, const Twine &Msg);
105
106 const AsmToken &Lex();
107
108 bool ParseExpression(const MCExpr *&Res);
109 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
110 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
111 virtual bool ParseAbsoluteExpression(int64_t &Res);
112
113 /// }
114
115private:
116 bool ParseStatement();
117
118 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
119
120 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
121 bool EnterIncludeFile(const std::string &Filename);
122
123 void EatToEndOfStatement();
124
125 bool ParseAssignment(StringRef Name);
126
127 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
128 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
129 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
130
131 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
132 /// and set \arg Res to the identifier contents.
133 bool ParseIdentifier(StringRef &Res);
134
135 // Directive Parsing.
136 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
137 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
138 bool ParseDirectiveFill(); // ".fill"
139 bool ParseDirectiveSpace(); // ".space"
140 bool ParseDirectiveSet(); // ".set"
141 bool ParseDirectiveOrg(); // ".org"
142 // ".align{,32}", ".p2align{,w,l}"
143 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
144
145 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
146 /// accepts a single symbol (which should be a label or an external).
147 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
148 bool ParseDirectiveELFType(); // ELF specific ".type"
149
150 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
151
152 bool ParseDirectiveAbort(); // ".abort"
153 bool ParseDirectiveInclude(); // ".include"
154
155 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
156 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
157 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
158 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
159
160 /// ParseEscapedString - Parse the current token as a string which may include
161 /// escaped characters and return the string contents.
162 bool ParseEscapedString(std::string &Data);
163};
164
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000165/// \brief Generic implementations of directive handling, etc. which is shared
166/// (or the default, at least) for all assembler parser.
167class GenericAsmParser : public MCAsmParserExtension {
168public:
169 GenericAsmParser() {}
170
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000171 AsmParser &getParser() {
172 return (AsmParser&) this->MCAsmParserExtension::getParser();
173 }
174
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000175 virtual void Initialize(MCAsmParser &Parser) {
176 // Call the base implementation.
177 this->MCAsmParserExtension::Initialize(Parser);
178
179 // Debugging directives.
180 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
181 &GenericAsmParser::ParseDirectiveFile));
182 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
183 &GenericAsmParser::ParseDirectiveLine));
184 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
185 &GenericAsmParser::ParseDirectiveLoc));
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000186
187 // Macro directives.
188 Parser.AddDirectiveHandler(this, ".macros_on",
189 MCAsmParser::DirectiveHandler(
190 &GenericAsmParser::ParseDirectiveMacrosOnOff));
191 Parser.AddDirectiveHandler(this, ".macros_off",
192 MCAsmParser::DirectiveHandler(
193 &GenericAsmParser::ParseDirectiveMacrosOnOff));
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000194 Parser.AddDirectiveHandler(this, ".macro", MCAsmParser::DirectiveHandler(
195 &GenericAsmParser::ParseDirectiveMacro));
196 Parser.AddDirectiveHandler(this, ".endm", MCAsmParser::DirectiveHandler(
197 &GenericAsmParser::ParseDirectiveEndMacro));
198 Parser.AddDirectiveHandler(this, ".endmacro", MCAsmParser::DirectiveHandler(
199 &GenericAsmParser::ParseDirectiveEndMacro));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000200 }
201
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000202 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
203 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
204 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000205
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000206 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000207 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
208 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000209};
210
211}
212
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000213namespace llvm {
214
215extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000216extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000217
218}
219
Chris Lattneraaec2052010-01-19 19:46:13 +0000220enum { DEFAULT_ADDRSPACE = 0 };
221
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000222AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
223 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000224 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000225 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000226 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000227 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000228
229 // Initialize the generic parser.
230 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000231
232 // Initialize the platform / file format parser.
233 //
234 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
235 // created.
236 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000237 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000238 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000239 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000240 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000241 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000242 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000243}
244
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000245AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000246 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000247 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000248}
249
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000250void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000251 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000252}
253
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000254bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000255 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000256 return true;
257}
258
Sean Callananbf2013e2010-01-20 23:19:55 +0000259void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
260 const char *Type) const {
261 SrcMgr.PrintMessage(Loc, Msg, Type);
262}
Sean Callananfd0b0282010-01-21 00:19:58 +0000263
264bool AsmParser::EnterIncludeFile(const std::string &Filename) {
265 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
266 if (NewBuf == -1)
267 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000268
Sean Callananfd0b0282010-01-21 00:19:58 +0000269 CurBuffer = NewBuf;
270
271 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
272
273 return false;
274}
275
276const AsmToken &AsmParser::Lex() {
277 const AsmToken *tok = &Lexer.Lex();
278
279 if (tok->is(AsmToken::Eof)) {
280 // If this is the end of an included file, pop the parent file off the
281 // include stack.
282 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
283 if (ParentIncludeLoc != SMLoc()) {
284 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
285 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
286 ParentIncludeLoc.getPointer());
287 tok = &Lexer.Lex();
288 }
289 }
290
291 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000292 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000293
Sean Callananfd0b0282010-01-21 00:19:58 +0000294 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000295}
296
Chris Lattner79180e22010-04-05 23:15:42 +0000297bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000298 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000299 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000300 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000301 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000302 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000303 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
304 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000305
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000306 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000307 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000308
Chris Lattnerb717fb02009-07-02 21:53:43 +0000309 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000310
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000311 AsmCond StartingCondState = TheCondState;
312
Chris Lattnerb717fb02009-07-02 21:53:43 +0000313 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000314 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000315 if (!ParseStatement()) continue;
316
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000317 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000318 HadError = true;
319 EatToEndOfStatement();
320 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000321
322 if (TheCondState.TheCond != StartingCondState.TheCond ||
323 TheCondState.Ignore != StartingCondState.Ignore)
324 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000325
Chris Lattner79180e22010-04-05 23:15:42 +0000326 // Finalize the output stream if there are no errors and if the client wants
327 // us to.
328 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000329 Out.Finish();
330
Chris Lattnerb717fb02009-07-02 21:53:43 +0000331 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000332}
333
Chris Lattner2cf5f142009-06-22 01:29:09 +0000334/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
335void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 while (Lexer.isNot(AsmToken::EndOfStatement) &&
337 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000338 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000339
340 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000341 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000342 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000343}
344
Chris Lattnerc4193832009-06-22 05:51:26 +0000345
Chris Lattner74ec1a32009-06-22 06:32:03 +0000346/// ParseParenExpr - Parse a paren expression and return it.
347/// NOTE: This assumes the leading '(' has already been consumed.
348///
349/// parenexpr ::= expr)
350///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000351bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000352 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000353 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000354 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000355 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000356 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000357 return false;
358}
Chris Lattnerc4193832009-06-22 05:51:26 +0000359
Chris Lattner74ec1a32009-06-22 06:32:03 +0000360/// ParsePrimaryExpr - Parse a primary expression and return it.
361/// primaryexpr ::= (parenexpr
362/// primaryexpr ::= symbol
363/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000364/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000365/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000366bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000367 switch (Lexer.getKind()) {
368 default:
369 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000371 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000372 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000373 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000374 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000375 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000376 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000377 case AsmToken::Identifier: {
378 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000379 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000380 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000381
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000382 // Mark the symbol as used in an expression.
383 Sym->setUsedInExpr(true);
384
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000385 // Lookup the symbol variant if used.
386 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
387 if (Split.first.size() != getTok().getIdentifier().size())
388 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
389
Chris Lattnerb4307b32010-01-15 19:28:38 +0000390 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000391 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000392
393 // If this is an absolute variable reference, substitute it now to preserve
394 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000395 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000396 if (Variant)
397 return Error(EndLoc, "unexpected modified on variable reference");
398
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000399 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000400 return false;
401 }
402
403 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000404 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000405 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000406 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000407 case AsmToken::Integer: {
408 SMLoc Loc = getTok().getLoc();
409 int64_t IntVal = getTok().getIntVal();
410 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000411 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000412 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000413 // Look for 'b' or 'f' following an Integer as a directional label
414 if (Lexer.getKind() == AsmToken::Identifier) {
415 StringRef IDVal = getTok().getString();
416 if (IDVal == "f" || IDVal == "b"){
417 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
418 IDVal == "f" ? 1 : 0);
419 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
420 getContext());
421 if(IDVal == "b" && Sym->isUndefined())
422 return Error(Loc, "invalid reference to undefined symbol");
423 EndLoc = Lexer.getLoc();
424 Lex(); // Eat identifier.
425 }
426 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000427 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000428 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000429 case AsmToken::Dot: {
430 // This is a '.' reference, which references the current PC. Emit a
431 // temporary label to the streamer and refer to it.
432 MCSymbol *Sym = Ctx.CreateTempSymbol();
433 Out.EmitLabel(Sym);
434 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
435 EndLoc = Lexer.getLoc();
436 Lex(); // Eat identifier.
437 return false;
438 }
439
Daniel Dunbar3f872332009-07-28 16:08:33 +0000440 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000441 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000442 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000443 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000444 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000445 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000446 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000447 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000448 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000449 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000450 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000451 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000452 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000453 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000454 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000455 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000456 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000457 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000458 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000459 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000460 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000461 }
462}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000463
Chris Lattnerb4307b32010-01-15 19:28:38 +0000464bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000465 SMLoc EndLoc;
466 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000467}
468
Chris Lattner74ec1a32009-06-22 06:32:03 +0000469/// ParseExpression - Parse an expression and return it.
470///
471/// expr ::= expr +,- expr -> lowest.
472/// expr ::= expr |,^,&,! expr -> middle.
473/// expr ::= expr *,/,%,<<,>> expr -> highest.
474/// expr ::= primaryexpr
475///
Chris Lattner54482b42010-01-15 19:39:23 +0000476bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000477 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000478 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000479 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
480 return true;
481
482 // Try to constant fold it up front, if possible.
483 int64_t Value;
484 if (Res->EvaluateAsAbsolute(Value))
485 Res = MCConstantExpr::Create(Value, getContext());
486
487 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000488}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000489
Chris Lattnerb4307b32010-01-15 19:28:38 +0000490bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000491 Res = 0;
492 return ParseParenExpr(Res, EndLoc) ||
493 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000494}
495
Daniel Dunbar475839e2009-06-29 20:37:27 +0000496bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000497 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000498
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000499 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000500 if (ParseExpression(Expr))
501 return true;
502
Daniel Dunbare00b0112009-10-16 01:57:52 +0000503 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000504 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000505
506 return false;
507}
508
Daniel Dunbar3f872332009-07-28 16:08:33 +0000509static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000510 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000511 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000512 default:
513 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000514
515 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000516 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000517 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000518 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000519 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000520 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000521 return 1;
522
523 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000524 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000525 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000526 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000527 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000528 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000529 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000530 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000531 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000532 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000533 case AsmToken::ExclaimEqual:
534 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000535 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000536 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000537 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000538 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000539 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000540 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000541 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000542 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000543 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000544 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000545 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000546 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000547 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000548 return 2;
549
550 // Intermediate Precedence: |, &, ^
551 //
552 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000553 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000554 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000555 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000556 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000557 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000558 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000559 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000560 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000561 return 3;
562
563 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000564 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000565 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000566 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000567 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000568 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000569 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000570 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000571 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000572 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000573 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000574 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000575 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000576 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000577 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000578 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000579 }
580}
581
582
583/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
584/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000585bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
586 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000587 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000588 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000589 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000590
591 // If the next token is lower precedence than we are allowed to eat, return
592 // successfully with what we ate already.
593 if (TokPrec < Precedence)
594 return false;
595
Sean Callanan79ed1a82010-01-19 20:22:31 +0000596 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000597
598 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000599 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000600 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000601
602 // If BinOp binds less tightly with RHS than the operator after RHS, let
603 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000604 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000605 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000606 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000607 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000608 }
609
Daniel Dunbar475839e2009-06-29 20:37:27 +0000610 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000611 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000612 }
613}
614
Chris Lattnerc4193832009-06-22 05:51:26 +0000615
616
617
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000618/// ParseStatement:
619/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000620/// ::= Label* Directive ...Operands... EndOfStatement
621/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000622bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000623 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000624 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000625 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000626 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000627 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000628
629 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000630 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000631 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000632 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000633 int64_t LocalLabelVal = -1;
634 // GUESS allow an integer followed by a ':' as a directional local label
635 if (Lexer.is(AsmToken::Integer)) {
636 LocalLabelVal = getTok().getIntVal();
637 if (LocalLabelVal < 0) {
638 if (!TheCondState.Ignore)
639 return TokError("unexpected token at start of statement");
640 IDVal = "";
641 }
642 else {
643 IDVal = getTok().getString();
644 Lex(); // Consume the integer token to be used as an identifier token.
645 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000646 if (!TheCondState.Ignore)
647 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000648 }
649 }
650 }
651 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000652 if (!TheCondState.Ignore)
653 return TokError("unexpected token at start of statement");
654 IDVal = "";
655 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000656
Chris Lattner7834fac2010-04-17 18:14:27 +0000657 // Handle conditional assembly here before checking for skipping. We
658 // have to do this so that .endif isn't skipped in a ".if 0" block for
659 // example.
660 if (IDVal == ".if")
661 return ParseDirectiveIf(IDLoc);
662 if (IDVal == ".elseif")
663 return ParseDirectiveElseIf(IDLoc);
664 if (IDVal == ".else")
665 return ParseDirectiveElse(IDLoc);
666 if (IDVal == ".endif")
667 return ParseDirectiveEndIf(IDLoc);
668
669 // If we are in a ".if 0" block, ignore this statement.
670 if (TheCondState.Ignore) {
671 EatToEndOfStatement();
672 return false;
673 }
674
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000675 // FIXME: Recurse on local labels?
676
677 // See what kind of statement we have.
678 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000679 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000680 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000681 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000682
683 // Diagnose attempt to use a variable as a label.
684 //
685 // FIXME: Diagnostics. Note the location of the definition as a label.
686 // FIXME: This doesn't diagnose assignment to a symbol which has been
687 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000688 MCSymbol *Sym;
689 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000690 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000691 else
692 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000693 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000694 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000695
Daniel Dunbar959fd882009-08-26 22:13:22 +0000696 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000697 Out.EmitLabel(Sym);
698
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000699 // Consume any end of statement token, if present, to avoid spurious
700 // AddBlankLine calls().
701 if (Lexer.is(AsmToken::EndOfStatement)) {
702 Lex();
703 if (Lexer.is(AsmToken::Eof))
704 return false;
705 }
706
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000707 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000708 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000709
Daniel Dunbar3f872332009-07-28 16:08:33 +0000710 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000711 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000712 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000713
Daniel Dunbare2ace502009-08-31 08:09:09 +0000714 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000715
716 default: // Normal instruction or directive.
717 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000718 }
719
720 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000721 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000722 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000724 return ParseDirectiveSet();
725
Daniel Dunbara0d14262009-06-24 23:30:00 +0000726 // Data directives
727
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000729 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000731 return ParseDirectiveAscii(true);
732
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000734 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000735 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000736 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000738 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000740 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000741
742 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000744 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000746 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000748 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000750 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000752 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000754 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000756 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000757 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000758 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
759
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000761 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000762
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000763 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000764 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000765 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000766 return ParseDirectiveSpace();
767
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000768 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000769
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000770 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000771 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000772 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000773 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000774 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000775 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000776 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000777 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000778 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000779 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000780 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000781 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000782 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000783 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000784 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000785 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000786 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000787 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000788 if (IDVal == ".type")
789 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000790 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000791 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000792 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000793 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000794 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000795 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000796 if (IDVal == ".weak_def_can_be_hidden")
797 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000798
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000799 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000800 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000801 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000802 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000803
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000805 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000806 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000807 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000808
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000809 // If macros are enabled, check to see if this is a macro instantiation.
810 if (MacrosEnabled) {
811 if (const Macro *M = MacroMap.lookup(IDVal)) {
812 (void) M;
813
814 Error(IDLoc, "macros are not yet supported");
815 EatToEndOfStatement();
816 return false;
817 }
818 }
819
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000820 // Look up the handler in the handler table.
821 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
822 DirectiveMap.lookup(IDVal);
823 if (Handler.first)
824 return (Handler.first->*Handler.second)(IDVal, IDLoc);
825
Kevin Enderby9c656452009-09-10 20:51:44 +0000826 // Target hook for parsing target specific directives.
827 if (!getTargetParser().ParseDirective(ID))
828 return false;
829
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000830 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000831 EatToEndOfStatement();
832 return false;
833 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000834
Chris Lattnera7f13542010-05-19 23:34:33 +0000835 // Canonicalize the opcode to lower case.
836 SmallString<128> Opcode;
837 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
838 Opcode.push_back(tolower(IDVal[i]));
839
Chris Lattner98986712010-01-14 22:21:20 +0000840 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000841 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000842 ParsedOperands);
843 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
844 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000845
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000846 // If parsing succeeded, match the instruction.
847 if (!HadError) {
848 MCInst Inst;
849 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
850 // Emit the instruction on success.
851 Out.EmitInstruction(Inst);
852 } else {
853 // Otherwise emit a diagnostic about the match failure and set the error
854 // flag.
855 //
856 // FIXME: We should give nicer diagnostics about the exact failure.
857 Error(IDLoc, "unrecognized instruction");
858 HadError = true;
859 }
860 }
Chris Lattner98986712010-01-14 22:21:20 +0000861
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000862 // If there was no error, consume the end-of-statement token. Otherwise this
863 // will be done by our caller.
864 if (!HadError)
865 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000866
867 // Free any parsed operands.
868 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
869 delete ParsedOperands[i];
870
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000871 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000872}
Chris Lattner9a023f72009-06-24 04:43:34 +0000873
Benjamin Kramer38e59892010-07-14 22:38:02 +0000874bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000875 // FIXME: Use better location, we should use proper tokens.
876 SMLoc EqualLoc = Lexer.getLoc();
877
Daniel Dunbar821e3332009-08-31 08:09:28 +0000878 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000879 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000880 return true;
881
Daniel Dunbar3f872332009-07-28 16:08:33 +0000882 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000883 return TokError("unexpected token in assignment");
884
885 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000886 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000887
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000888 // Validate that the LHS is allowed to be a variable (either it has not been
889 // used as a symbol, or it is an absolute symbol).
890 MCSymbol *Sym = getContext().LookupSymbol(Name);
891 if (Sym) {
892 // Diagnose assignment to a label.
893 //
894 // FIXME: Diagnostics. Note the location of the definition as a label.
895 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000896 if (Sym->isUndefined() && !Sym->isUsedInExpr())
897 ; // Allow redefinitions of undefined symbols only used in directives.
898 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000899 return Error(EqualLoc, "redefinition of '" + Name + "'");
900 else if (!Sym->isVariable())
901 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000902 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000903 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
904 Name + "'");
905 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000906 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000907
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000908 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000909
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000910 Sym->setUsedInExpr(true);
911
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000912 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000913 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000914
915 return false;
916}
917
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000918/// ParseIdentifier:
919/// ::= identifier
920/// ::= string
921bool AsmParser::ParseIdentifier(StringRef &Res) {
922 if (Lexer.isNot(AsmToken::Identifier) &&
923 Lexer.isNot(AsmToken::String))
924 return true;
925
Sean Callanan18b83232010-01-19 21:44:56 +0000926 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000927
Sean Callanan79ed1a82010-01-19 20:22:31 +0000928 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000929
930 return false;
931}
932
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000933/// ParseDirectiveSet:
934/// ::= .set identifier ',' expression
935bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000936 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000937
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000938 if (ParseIdentifier(Name))
939 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000940
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000941 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000942 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000943 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000944
Daniel Dunbare2ace502009-08-31 08:09:09 +0000945 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000946}
947
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000948bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000949 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000950
951 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000952 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000953 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
954 if (Str[i] != '\\') {
955 Data += Str[i];
956 continue;
957 }
958
959 // Recognize escaped characters. Note that this escape semantics currently
960 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
961 ++i;
962 if (i == e)
963 return TokError("unexpected backslash at end of string");
964
965 // Recognize octal sequences.
966 if ((unsigned) (Str[i] - '0') <= 7) {
967 // Consume up to three octal characters.
968 unsigned Value = Str[i] - '0';
969
970 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
971 ++i;
972 Value = Value * 8 + (Str[i] - '0');
973
974 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
975 ++i;
976 Value = Value * 8 + (Str[i] - '0');
977 }
978 }
979
980 if (Value > 255)
981 return TokError("invalid octal escape sequence (out of range)");
982
983 Data += (unsigned char) Value;
984 continue;
985 }
986
987 // Otherwise recognize individual escapes.
988 switch (Str[i]) {
989 default:
990 // Just reject invalid escape sequences for now.
991 return TokError("invalid escape sequence (unrecognized character)");
992
993 case 'b': Data += '\b'; break;
994 case 'f': Data += '\f'; break;
995 case 'n': Data += '\n'; break;
996 case 'r': Data += '\r'; break;
997 case 't': Data += '\t'; break;
998 case '"': Data += '"'; break;
999 case '\\': Data += '\\'; break;
1000 }
1001 }
1002
1003 return false;
1004}
1005
Daniel Dunbara0d14262009-06-24 23:30:00 +00001006/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001007/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001008bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001009 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001010 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001011 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001012 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001013
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001014 std::string Data;
1015 if (ParseEscapedString(Data))
1016 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001017
1018 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001019 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001020 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1021
Sean Callanan79ed1a82010-01-19 20:22:31 +00001022 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001023
1024 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001025 break;
1026
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001027 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001029 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 }
1031 }
1032
Sean Callanan79ed1a82010-01-19 20:22:31 +00001033 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001034 return false;
1035}
1036
1037/// ParseDirectiveValue
1038/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1039bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001040 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001041 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001042 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001043 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001044 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045 return true;
1046
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001047 // Special case constant expressions to match code generator.
1048 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001049 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001050 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001051 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001053 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054 break;
1055
1056 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001057 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001058 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001059 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060 }
1061 }
1062
Sean Callanan79ed1a82010-01-19 20:22:31 +00001063 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064 return false;
1065}
1066
1067/// ParseDirectiveSpace
1068/// ::= .space expression [ , expression ]
1069bool AsmParser::ParseDirectiveSpace() {
1070 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001071 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001072 return true;
1073
1074 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001075 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1076 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001078 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079
Daniel Dunbar475839e2009-06-29 20:37:27 +00001080 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081 return true;
1082
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001083 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return TokError("unexpected token in '.space' directive");
1085 }
1086
Sean Callanan79ed1a82010-01-19 20:22:31 +00001087 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088
1089 if (NumBytes <= 0)
1090 return TokError("invalid number of bytes in '.space' directive");
1091
1092 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001093 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001094
1095 return false;
1096}
1097
1098/// ParseDirectiveFill
1099/// ::= .fill expression , expression , expression
1100bool AsmParser::ParseDirectiveFill() {
1101 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001102 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001103 return true;
1104
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001105 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001106 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001107 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001108
1109 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001110 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001111 return true;
1112
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001113 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001115 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001116
1117 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001118 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119 return true;
1120
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001121 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122 return TokError("unexpected token in '.fill' directive");
1123
Sean Callanan79ed1a82010-01-19 20:22:31 +00001124 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001125
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001126 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1127 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001128
1129 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001130 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131
1132 return false;
1133}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001134
1135/// ParseDirectiveOrg
1136/// ::= .org expression [ , expression ]
1137bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001138 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001139 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001140 return true;
1141
1142 // Parse optional fill expression.
1143 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001144 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1145 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001146 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001147 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001148
Daniel Dunbar475839e2009-06-29 20:37:27 +00001149 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001150 return true;
1151
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001152 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001153 return TokError("unexpected token in '.org' directive");
1154 }
1155
Sean Callanan79ed1a82010-01-19 20:22:31 +00001156 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001157
1158 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1159 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001160 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001161
1162 return false;
1163}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001164
1165/// ParseDirectiveAlign
1166/// ::= {.align, ...} expression [ , expression [ , expression ]]
1167bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001168 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001169 int64_t Alignment;
1170 if (ParseAbsoluteExpression(Alignment))
1171 return true;
1172
1173 SMLoc MaxBytesLoc;
1174 bool HasFillExpr = false;
1175 int64_t FillExpr = 0;
1176 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001177 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1178 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001180 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181
1182 // The fill expression can be omitted while specifying a maximum number of
1183 // alignment bytes, e.g:
1184 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001185 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001186 HasFillExpr = true;
1187 if (ParseAbsoluteExpression(FillExpr))
1188 return true;
1189 }
1190
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001191 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1192 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001193 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001194 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001195
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001196 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001197 if (ParseAbsoluteExpression(MaxBytesToFill))
1198 return true;
1199
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001200 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001201 return TokError("unexpected token in directive");
1202 }
1203 }
1204
Sean Callanan79ed1a82010-01-19 20:22:31 +00001205 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001206
Daniel Dunbar648ac512010-05-17 21:54:30 +00001207 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001208 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001209
1210 // Compute alignment in bytes.
1211 if (IsPow2) {
1212 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001213 if (Alignment >= 32) {
1214 Error(AlignmentLoc, "invalid alignment value");
1215 Alignment = 31;
1216 }
1217
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001218 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001219 }
1220
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001221 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001222 if (MaxBytesLoc.isValid()) {
1223 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001224 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1225 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001226 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001227 }
1228
1229 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001230 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1231 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001232 MaxBytesToFill = 0;
1233 }
1234 }
1235
Daniel Dunbar648ac512010-05-17 21:54:30 +00001236 // Check whether we should use optimal code alignment for this .align
1237 // directive.
1238 //
1239 // FIXME: This should be using a target hook.
1240 bool UseCodeAlign = false;
1241 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001242 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001243 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001244 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1245 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001246 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001247 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001248 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001249 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1250 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001251 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001252
1253 return false;
1254}
1255
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001256/// ParseDirectiveSymbolAttribute
1257/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001258bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001259 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001260 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001261 StringRef Name;
1262
1263 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001264 return TokError("expected identifier in directive");
1265
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001266 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001267
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001268 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001269
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001270 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001271 break;
1272
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001273 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001274 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001275 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001276 }
1277 }
1278
Sean Callanan79ed1a82010-01-19 20:22:31 +00001279 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001280 return false;
1281}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001282
Matt Fleming924c5e52010-05-21 11:36:59 +00001283/// ParseDirectiveELFType
1284/// ::= .type identifier , @attribute
1285bool AsmParser::ParseDirectiveELFType() {
1286 StringRef Name;
1287 if (ParseIdentifier(Name))
1288 return TokError("expected identifier in directive");
1289
1290 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001291 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001292
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001293 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001294 return TokError("unexpected token in '.type' directive");
1295 Lex();
1296
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001297 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001298 return TokError("expected '@' before type");
1299 Lex();
1300
1301 StringRef Type;
1302 SMLoc TypeLoc;
1303
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001304 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001305 if (ParseIdentifier(Type))
1306 return TokError("expected symbol type in directive");
1307
1308 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1309 .Case("function", MCSA_ELF_TypeFunction)
1310 .Case("object", MCSA_ELF_TypeObject)
1311 .Case("tls_object", MCSA_ELF_TypeTLS)
1312 .Case("common", MCSA_ELF_TypeCommon)
1313 .Case("notype", MCSA_ELF_TypeNoType)
1314 .Default(MCSA_Invalid);
1315
1316 if (Attr == MCSA_Invalid)
1317 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1318
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001319 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001320 return TokError("unexpected token in '.type' directive");
1321
1322 Lex();
1323
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001324 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001325
1326 return false;
1327}
1328
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001329/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001330/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1331bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001332 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001333 StringRef Name;
1334 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001335 return TokError("expected identifier in directive");
1336
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001337 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001338 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001339
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001340 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001341 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001342 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001343
1344 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001345 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001346 if (ParseAbsoluteExpression(Size))
1347 return true;
1348
1349 int64_t Pow2Alignment = 0;
1350 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001351 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001352 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001354 if (ParseAbsoluteExpression(Pow2Alignment))
1355 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001356
1357 // If this target takes alignments in bytes (not log) validate and convert.
1358 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1359 if (!isPowerOf2_64(Pow2Alignment))
1360 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1361 Pow2Alignment = Log2_64(Pow2Alignment);
1362 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001363 }
1364
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001365 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001366 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001367
Sean Callanan79ed1a82010-01-19 20:22:31 +00001368 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001369
Chris Lattner1fc3d752009-07-09 17:25:12 +00001370 // NOTE: a size of zero for a .comm should create a undefined symbol
1371 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001372 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001373 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1374 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001375
Eric Christopherc260a3e2010-05-14 01:38:54 +00001376 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001377 // may internally end up wanting an alignment in bytes.
1378 // FIXME: Diagnose overflow.
1379 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001380 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1381 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001382
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001383 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001384 return Error(IDLoc, "invalid symbol redefinition");
1385
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001386 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001387 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001388 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001389 getStreamer().EmitZerofill(Ctx.getMachOSection(
1390 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1391 0, SectionKind::getBSS()),
1392 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001393 return false;
1394 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001395
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001396 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001397 return false;
1398}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001399
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001400/// ParseDirectiveAbort
1401/// ::= .abort [ "abort_string" ]
1402bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001403 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001404 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001405
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001406 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001407 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1408 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001409 return TokError("expected string in '.abort' directive");
1410
Sean Callanan18b83232010-01-19 21:44:56 +00001411 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001412
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001414 }
1415
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001416 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001417 return TokError("unexpected token in '.abort' directive");
1418
Sean Callanan79ed1a82010-01-19 20:22:31 +00001419 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001420
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001421 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001422 if (Str.empty())
1423 Error(Loc, ".abort detected. Assembly stopping.");
1424 else
1425 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001426
1427 return false;
1428}
Kevin Enderby71148242009-07-14 21:35:03 +00001429
Kevin Enderby1f049b22009-07-14 23:21:55 +00001430/// ParseDirectiveInclude
1431/// ::= .include "filename"
1432bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001433 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001434 return TokError("expected string in '.include' directive");
1435
Sean Callanan18b83232010-01-19 21:44:56 +00001436 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001437 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001438 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001439
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001440 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001441 return TokError("unexpected token in '.include' directive");
1442
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001443 // Strip the quotes.
1444 Filename = Filename.substr(1, Filename.size()-2);
1445
1446 // Attempt to switch the lexer to the included file before consuming the end
1447 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001448 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001449 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001450 return true;
1451 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001452
1453 return false;
1454}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001455
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001456/// ParseDirectiveIf
1457/// ::= .if expression
1458bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001459 TheCondStack.push_back(TheCondState);
1460 TheCondState.TheCond = AsmCond::IfCond;
1461 if(TheCondState.Ignore) {
1462 EatToEndOfStatement();
1463 }
1464 else {
1465 int64_t ExprValue;
1466 if (ParseAbsoluteExpression(ExprValue))
1467 return true;
1468
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001469 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001470 return TokError("unexpected token in '.if' directive");
1471
Sean Callanan79ed1a82010-01-19 20:22:31 +00001472 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001473
1474 TheCondState.CondMet = ExprValue;
1475 TheCondState.Ignore = !TheCondState.CondMet;
1476 }
1477
1478 return false;
1479}
1480
1481/// ParseDirectiveElseIf
1482/// ::= .elseif expression
1483bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1484 if (TheCondState.TheCond != AsmCond::IfCond &&
1485 TheCondState.TheCond != AsmCond::ElseIfCond)
1486 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1487 " an .elseif");
1488 TheCondState.TheCond = AsmCond::ElseIfCond;
1489
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001490 bool LastIgnoreState = false;
1491 if (!TheCondStack.empty())
1492 LastIgnoreState = TheCondStack.back().Ignore;
1493 if (LastIgnoreState || TheCondState.CondMet) {
1494 TheCondState.Ignore = true;
1495 EatToEndOfStatement();
1496 }
1497 else {
1498 int64_t ExprValue;
1499 if (ParseAbsoluteExpression(ExprValue))
1500 return true;
1501
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001502 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001503 return TokError("unexpected token in '.elseif' directive");
1504
Sean Callanan79ed1a82010-01-19 20:22:31 +00001505 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001506 TheCondState.CondMet = ExprValue;
1507 TheCondState.Ignore = !TheCondState.CondMet;
1508 }
1509
1510 return false;
1511}
1512
1513/// ParseDirectiveElse
1514/// ::= .else
1515bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001516 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001517 return TokError("unexpected token in '.else' directive");
1518
Sean Callanan79ed1a82010-01-19 20:22:31 +00001519 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001520
1521 if (TheCondState.TheCond != AsmCond::IfCond &&
1522 TheCondState.TheCond != AsmCond::ElseIfCond)
1523 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1524 ".elseif");
1525 TheCondState.TheCond = AsmCond::ElseCond;
1526 bool LastIgnoreState = false;
1527 if (!TheCondStack.empty())
1528 LastIgnoreState = TheCondStack.back().Ignore;
1529 if (LastIgnoreState || TheCondState.CondMet)
1530 TheCondState.Ignore = true;
1531 else
1532 TheCondState.Ignore = false;
1533
1534 return false;
1535}
1536
1537/// ParseDirectiveEndIf
1538/// ::= .endif
1539bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001540 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001541 return TokError("unexpected token in '.endif' directive");
1542
Sean Callanan79ed1a82010-01-19 20:22:31 +00001543 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001544
1545 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1546 TheCondStack.empty())
1547 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1548 ".else");
1549 if (!TheCondStack.empty()) {
1550 TheCondState = TheCondStack.back();
1551 TheCondStack.pop_back();
1552 }
1553
1554 return false;
1555}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001556
1557/// ParseDirectiveFile
1558/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001559bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001560 // FIXME: I'm not sure what this is.
1561 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001562 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001563 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001564 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001565
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001566 if (FileNumber < 1)
1567 return TokError("file number less than one");
1568 }
1569
Daniel Dunbareceec052010-07-12 17:45:27 +00001570 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001571 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001572
Chris Lattnerd32e8032010-01-25 19:02:58 +00001573 StringRef Filename = getTok().getString();
1574 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001575 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001576
Daniel Dunbareceec052010-07-12 17:45:27 +00001577 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001578 return TokError("unexpected token in '.file' directive");
1579
Chris Lattnerd32e8032010-01-25 19:02:58 +00001580 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001581 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001582 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001583 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1584
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001585 return false;
1586}
1587
1588/// ParseDirectiveLine
1589/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001590bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001591 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1592 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001593 return TokError("unexpected token in '.line' directive");
1594
Sean Callanan18b83232010-01-19 21:44:56 +00001595 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001596 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001597 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001598
1599 // FIXME: Do something with the .line.
1600 }
1601
Daniel Dunbareceec052010-07-12 17:45:27 +00001602 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001603 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001604
1605 return false;
1606}
1607
1608
1609/// ParseDirectiveLoc
1610/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001611bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001612 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001613 return TokError("unexpected token in '.loc' directive");
1614
1615 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001616 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001617 (void) FileNumber;
1618 // FIXME: Validate file.
1619
Sean Callanan79ed1a82010-01-19 20:22:31 +00001620 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001621 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1622 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001623 return TokError("unexpected token in '.loc' directive");
1624
Sean Callanan18b83232010-01-19 21:44:56 +00001625 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001626 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001627 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001628
Daniel Dunbareceec052010-07-12 17:45:27 +00001629 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1630 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001631 return TokError("unexpected token in '.loc' directive");
1632
Sean Callanan18b83232010-01-19 21:44:56 +00001633 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001634 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001635 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001636
1637 // FIXME: Do something with the .loc.
1638 }
1639 }
1640
Daniel Dunbareceec052010-07-12 17:45:27 +00001641 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001642 return TokError("unexpected token in '.file' directive");
1643
1644 return false;
1645}
1646
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001647/// ParseDirectiveMacrosOnOff
1648/// ::= .macros_on
1649/// ::= .macros_off
1650bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1651 SMLoc DirectiveLoc) {
1652 if (getLexer().isNot(AsmToken::EndOfStatement))
1653 return Error(getLexer().getLoc(),
1654 "unexpected token in '" + Directive + "' directive");
1655
1656 getParser().MacrosEnabled = Directive == ".macros_on";
1657
1658 return false;
1659}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001660
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001661/// ParseDirectiveMacro
1662/// ::= .macro name
1663bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1664 SMLoc DirectiveLoc) {
1665 StringRef Name;
1666 if (getParser().ParseIdentifier(Name))
1667 return TokError("expected identifier in directive");
1668
1669 if (getLexer().isNot(AsmToken::EndOfStatement))
1670 return TokError("unexpected token in '.macro' directive");
1671
1672 // Eat the end of statement.
1673 Lex();
1674
1675 AsmToken EndToken, StartToken = getTok();
1676
1677 // Lex the macro definition.
1678 for (;;) {
1679 // Check whether we have reached the end of the file.
1680 if (getLexer().is(AsmToken::Eof))
1681 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1682
1683 // Otherwise, check whether we have reach the .endmacro.
1684 if (getLexer().is(AsmToken::Identifier) &&
1685 (getTok().getIdentifier() == ".endm" ||
1686 getTok().getIdentifier() == ".endmacro")) {
1687 EndToken = getTok();
1688 Lex();
1689 if (getLexer().isNot(AsmToken::EndOfStatement))
1690 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1691 "' directive");
1692 break;
1693 }
1694
1695 // Otherwise, scan til the end of the statement.
1696 getParser().EatToEndOfStatement();
1697 }
1698
1699 if (getParser().MacroMap.lookup(Name)) {
1700 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1701 }
1702
1703 const char *BodyStart = StartToken.getLoc().getPointer();
1704 const char *BodyEnd = EndToken.getLoc().getPointer();
1705 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1706 getParser().MacroMap[Name] = new Macro(Name, Body);
1707 return false;
1708}
1709
1710/// ParseDirectiveEndMacro
1711/// ::= .endm
1712/// ::= .endmacro
1713bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
1714 SMLoc DirectiveLoc) {
1715 if (getLexer().isNot(AsmToken::EndOfStatement))
1716 return TokError("unexpected token in '" + Directive + "' directive");
1717
1718 // If we see a .endmacro directly, it is a stray entry in the file; well
1719 // formed .endmacro directives are handled during the macro definition
1720 // parsing.
1721 return TokError("unexpected '" + Directive + "' in file, "
1722 "no current macro definition");
1723}
1724
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001725/// \brief Create an MCAsmParser instance.
1726MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1727 MCContext &C, MCStreamer &Out,
1728 const MCAsmInfo &MAI) {
1729 return new AsmParser(T, SM, C, Out, MAI);
1730}