blob: d4db857a22cc37dfa0b600d3066f75032d9c8387 [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 Dunbaraef87e32010-07-18 18:31:38 +000039/// \brief The concrete assembly parser instance.
40class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000041 friend class GenericAsmParser;
42
Daniel Dunbaraef87e32010-07-18 18:31:38 +000043 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
44 void operator=(const AsmParser &); // DO NOT IMPLEMENT
45private:
46 AsmLexer Lexer;
47 MCContext &Ctx;
48 MCStreamer &Out;
49 SourceMgr &SrcMgr;
50 MCAsmParserExtension *GenericParser;
51 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000052
Daniel Dunbaraef87e32010-07-18 18:31:38 +000053 /// This is the current buffer index we're lexing from as managed by the
54 /// SourceMgr object.
55 int CurBuffer;
56
57 AsmCond TheCondState;
58 std::vector<AsmCond> TheCondStack;
59
60 /// DirectiveMap - This is a table handlers for directives. Each handler is
61 /// invoked after the directive identifier is read and is responsible for
62 /// parsing and validating the rest of the directive. The handler is passed
63 /// in the directive name and the location of the directive keyword.
64 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000065
66 /// Boolean tracking whether macro substitution is enabled.
67 unsigned MacrosEnabled : 1;
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069public:
70 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
71 const MCAsmInfo &MAI);
72 ~AsmParser();
73
74 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
75
76 void AddDirectiveHandler(MCAsmParserExtension *Object,
77 StringRef Directive,
78 DirectiveHandler Handler) {
79 DirectiveMap[Directive] = std::make_pair(Object, Handler);
80 }
81
82public:
83 /// @name MCAsmParser Interface
84 /// {
85
86 virtual SourceMgr &getSourceManager() { return SrcMgr; }
87 virtual MCAsmLexer &getLexer() { return Lexer; }
88 virtual MCContext &getContext() { return Ctx; }
89 virtual MCStreamer &getStreamer() { return Out; }
90
91 virtual void Warning(SMLoc L, const Twine &Meg);
92 virtual bool Error(SMLoc L, const Twine &Msg);
93
94 const AsmToken &Lex();
95
96 bool ParseExpression(const MCExpr *&Res);
97 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
98 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
99 virtual bool ParseAbsoluteExpression(int64_t &Res);
100
101 /// }
102
103private:
104 bool ParseStatement();
105
106 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
107
108 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
109 bool EnterIncludeFile(const std::string &Filename);
110
111 void EatToEndOfStatement();
112
113 bool ParseAssignment(StringRef Name);
114
115 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
116 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
117 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
118
119 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
120 /// and set \arg Res to the identifier contents.
121 bool ParseIdentifier(StringRef &Res);
122
123 // Directive Parsing.
124 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
125 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
126 bool ParseDirectiveFill(); // ".fill"
127 bool ParseDirectiveSpace(); // ".space"
128 bool ParseDirectiveSet(); // ".set"
129 bool ParseDirectiveOrg(); // ".org"
130 // ".align{,32}", ".p2align{,w,l}"
131 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
132
133 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
134 /// accepts a single symbol (which should be a label or an external).
135 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
136 bool ParseDirectiveELFType(); // ELF specific ".type"
137
138 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
139
140 bool ParseDirectiveAbort(); // ".abort"
141 bool ParseDirectiveInclude(); // ".include"
142
143 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
144 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
145 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
146 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
147
148 /// ParseEscapedString - Parse the current token as a string which may include
149 /// escaped characters and return the string contents.
150 bool ParseEscapedString(std::string &Data);
151};
152
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000153/// \brief Generic implementations of directive handling, etc. which is shared
154/// (or the default, at least) for all assembler parser.
155class GenericAsmParser : public MCAsmParserExtension {
156public:
157 GenericAsmParser() {}
158
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000159 AsmParser &getParser() {
160 return (AsmParser&) this->MCAsmParserExtension::getParser();
161 }
162
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000163 virtual void Initialize(MCAsmParser &Parser) {
164 // Call the base implementation.
165 this->MCAsmParserExtension::Initialize(Parser);
166
167 // Debugging directives.
168 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
169 &GenericAsmParser::ParseDirectiveFile));
170 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
171 &GenericAsmParser::ParseDirectiveLine));
172 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
173 &GenericAsmParser::ParseDirectiveLoc));
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000174
175 // Macro directives.
176 Parser.AddDirectiveHandler(this, ".macros_on",
177 MCAsmParser::DirectiveHandler(
178 &GenericAsmParser::ParseDirectiveMacrosOnOff));
179 Parser.AddDirectiveHandler(this, ".macros_off",
180 MCAsmParser::DirectiveHandler(
181 &GenericAsmParser::ParseDirectiveMacrosOnOff));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000182 }
183
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000184 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
185 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
186 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
187 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000188};
189
190}
191
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000192namespace llvm {
193
194extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000195extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000196
197}
198
Chris Lattneraaec2052010-01-19 19:46:13 +0000199enum { DEFAULT_ADDRSPACE = 0 };
200
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000201AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
202 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000203 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000204 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000205 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000206 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000207
208 // Initialize the generic parser.
209 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000210
211 // Initialize the platform / file format parser.
212 //
213 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
214 // created.
215 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000216 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000217 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000218 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000219 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000220 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000221 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000222}
223
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000224AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000225 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000226 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000227}
228
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000229void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000230 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000231}
232
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000233bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000234 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000235 return true;
236}
237
Sean Callananbf2013e2010-01-20 23:19:55 +0000238void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
239 const char *Type) const {
240 SrcMgr.PrintMessage(Loc, Msg, Type);
241}
Sean Callananfd0b0282010-01-21 00:19:58 +0000242
243bool AsmParser::EnterIncludeFile(const std::string &Filename) {
244 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
245 if (NewBuf == -1)
246 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000247
Sean Callananfd0b0282010-01-21 00:19:58 +0000248 CurBuffer = NewBuf;
249
250 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
251
252 return false;
253}
254
255const AsmToken &AsmParser::Lex() {
256 const AsmToken *tok = &Lexer.Lex();
257
258 if (tok->is(AsmToken::Eof)) {
259 // If this is the end of an included file, pop the parent file off the
260 // include stack.
261 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
262 if (ParentIncludeLoc != SMLoc()) {
263 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
264 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
265 ParentIncludeLoc.getPointer());
266 tok = &Lexer.Lex();
267 }
268 }
269
270 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000271 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000272
Sean Callananfd0b0282010-01-21 00:19:58 +0000273 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000274}
275
Chris Lattner79180e22010-04-05 23:15:42 +0000276bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000277 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000278 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000279 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000280 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000281 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000282 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
283 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000284
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000285 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000286 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000287
Chris Lattnerb717fb02009-07-02 21:53:43 +0000288 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000289
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000290 AsmCond StartingCondState = TheCondState;
291
Chris Lattnerb717fb02009-07-02 21:53:43 +0000292 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000293 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000294 if (!ParseStatement()) continue;
295
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000296 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000297 HadError = true;
298 EatToEndOfStatement();
299 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000300
301 if (TheCondState.TheCond != StartingCondState.TheCond ||
302 TheCondState.Ignore != StartingCondState.Ignore)
303 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000304
Chris Lattner79180e22010-04-05 23:15:42 +0000305 // Finalize the output stream if there are no errors and if the client wants
306 // us to.
307 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000308 Out.Finish();
309
Chris Lattnerb717fb02009-07-02 21:53:43 +0000310 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000311}
312
Chris Lattner2cf5f142009-06-22 01:29:09 +0000313/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
314void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000315 while (Lexer.isNot(AsmToken::EndOfStatement) &&
316 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000317 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000318
319 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000321 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000322}
323
Chris Lattnerc4193832009-06-22 05:51:26 +0000324
Chris Lattner74ec1a32009-06-22 06:32:03 +0000325/// ParseParenExpr - Parse a paren expression and return it.
326/// NOTE: This assumes the leading '(' has already been consumed.
327///
328/// parenexpr ::= expr)
329///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000330bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000331 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000333 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000334 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000335 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000336 return false;
337}
Chris Lattnerc4193832009-06-22 05:51:26 +0000338
Chris Lattner74ec1a32009-06-22 06:32:03 +0000339/// ParsePrimaryExpr - Parse a primary expression and return it.
340/// primaryexpr ::= (parenexpr
341/// primaryexpr ::= symbol
342/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000343/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000344/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000345bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000346 switch (Lexer.getKind()) {
347 default:
348 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000350 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000351 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000353 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000354 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000355 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000356 case AsmToken::Identifier: {
357 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000358 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000359 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000360
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000361 // Mark the symbol as used in an expression.
362 Sym->setUsedInExpr(true);
363
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000364 // Lookup the symbol variant if used.
365 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
366 if (Split.first.size() != getTok().getIdentifier().size())
367 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
368
Chris Lattnerb4307b32010-01-15 19:28:38 +0000369 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000370 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000371
372 // If this is an absolute variable reference, substitute it now to preserve
373 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000374 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000375 if (Variant)
376 return Error(EndLoc, "unexpected modified on variable reference");
377
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000378 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000379 return false;
380 }
381
382 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000383 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000384 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000385 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000386 case AsmToken::Integer: {
387 SMLoc Loc = getTok().getLoc();
388 int64_t IntVal = getTok().getIntVal();
389 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000390 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000391 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000392 // Look for 'b' or 'f' following an Integer as a directional label
393 if (Lexer.getKind() == AsmToken::Identifier) {
394 StringRef IDVal = getTok().getString();
395 if (IDVal == "f" || IDVal == "b"){
396 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
397 IDVal == "f" ? 1 : 0);
398 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
399 getContext());
400 if(IDVal == "b" && Sym->isUndefined())
401 return Error(Loc, "invalid reference to undefined symbol");
402 EndLoc = Lexer.getLoc();
403 Lex(); // Eat identifier.
404 }
405 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000406 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000407 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000408 case AsmToken::Dot: {
409 // This is a '.' reference, which references the current PC. Emit a
410 // temporary label to the streamer and refer to it.
411 MCSymbol *Sym = Ctx.CreateTempSymbol();
412 Out.EmitLabel(Sym);
413 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
414 EndLoc = Lexer.getLoc();
415 Lex(); // Eat identifier.
416 return false;
417 }
418
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000420 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000421 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000423 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000424 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000425 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000426 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000427 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000429 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000430 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000431 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000432 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000433 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000434 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000435 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000436 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000437 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000438 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000439 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000440 }
441}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000442
Chris Lattnerb4307b32010-01-15 19:28:38 +0000443bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000444 SMLoc EndLoc;
445 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000446}
447
Chris Lattner74ec1a32009-06-22 06:32:03 +0000448/// ParseExpression - Parse an expression and return it.
449///
450/// expr ::= expr +,- expr -> lowest.
451/// expr ::= expr |,^,&,! expr -> middle.
452/// expr ::= expr *,/,%,<<,>> expr -> highest.
453/// expr ::= primaryexpr
454///
Chris Lattner54482b42010-01-15 19:39:23 +0000455bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000456 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000457 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000458 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
459 return true;
460
461 // Try to constant fold it up front, if possible.
462 int64_t Value;
463 if (Res->EvaluateAsAbsolute(Value))
464 Res = MCConstantExpr::Create(Value, getContext());
465
466 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000467}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000468
Chris Lattnerb4307b32010-01-15 19:28:38 +0000469bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000470 Res = 0;
471 return ParseParenExpr(Res, EndLoc) ||
472 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000473}
474
Daniel Dunbar475839e2009-06-29 20:37:27 +0000475bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000476 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000477
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000478 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000479 if (ParseExpression(Expr))
480 return true;
481
Daniel Dunbare00b0112009-10-16 01:57:52 +0000482 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000483 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000484
485 return false;
486}
487
Daniel Dunbar3f872332009-07-28 16:08:33 +0000488static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000489 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000490 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000491 default:
492 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000493
494 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000495 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000496 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000497 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000498 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000499 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000500 return 1;
501
502 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000503 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000504 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000505 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000506 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000507 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000508 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000509 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000510 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000511 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000512 case AsmToken::ExclaimEqual:
513 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000514 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000515 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000516 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000517 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000518 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000519 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000520 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000521 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000522 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000523 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000524 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000525 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000526 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000527 return 2;
528
529 // Intermediate Precedence: |, &, ^
530 //
531 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000532 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000533 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000534 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000535 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000536 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000537 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000538 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000539 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000540 return 3;
541
542 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000543 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000544 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000545 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000546 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000547 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000548 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000549 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000550 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000551 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000552 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000553 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000554 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000555 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000556 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000557 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000558 }
559}
560
561
562/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
563/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000564bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
565 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000566 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000567 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000568 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000569
570 // If the next token is lower precedence than we are allowed to eat, return
571 // successfully with what we ate already.
572 if (TokPrec < Precedence)
573 return false;
574
Sean Callanan79ed1a82010-01-19 20:22:31 +0000575 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000576
577 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000578 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000579 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000580
581 // If BinOp binds less tightly with RHS than the operator after RHS, let
582 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000583 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000584 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000585 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000586 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000587 }
588
Daniel Dunbar475839e2009-06-29 20:37:27 +0000589 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000590 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000591 }
592}
593
Chris Lattnerc4193832009-06-22 05:51:26 +0000594
595
596
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000597/// ParseStatement:
598/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000599/// ::= Label* Directive ...Operands... EndOfStatement
600/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000601bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000602 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000603 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000604 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000605 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000606 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000607
608 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000609 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000610 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000611 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000612 int64_t LocalLabelVal = -1;
613 // GUESS allow an integer followed by a ':' as a directional local label
614 if (Lexer.is(AsmToken::Integer)) {
615 LocalLabelVal = getTok().getIntVal();
616 if (LocalLabelVal < 0) {
617 if (!TheCondState.Ignore)
618 return TokError("unexpected token at start of statement");
619 IDVal = "";
620 }
621 else {
622 IDVal = getTok().getString();
623 Lex(); // Consume the integer token to be used as an identifier token.
624 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000625 if (!TheCondState.Ignore)
626 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000627 }
628 }
629 }
630 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000631 if (!TheCondState.Ignore)
632 return TokError("unexpected token at start of statement");
633 IDVal = "";
634 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000635
Chris Lattner7834fac2010-04-17 18:14:27 +0000636 // Handle conditional assembly here before checking for skipping. We
637 // have to do this so that .endif isn't skipped in a ".if 0" block for
638 // example.
639 if (IDVal == ".if")
640 return ParseDirectiveIf(IDLoc);
641 if (IDVal == ".elseif")
642 return ParseDirectiveElseIf(IDLoc);
643 if (IDVal == ".else")
644 return ParseDirectiveElse(IDLoc);
645 if (IDVal == ".endif")
646 return ParseDirectiveEndIf(IDLoc);
647
648 // If we are in a ".if 0" block, ignore this statement.
649 if (TheCondState.Ignore) {
650 EatToEndOfStatement();
651 return false;
652 }
653
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000654 // FIXME: Recurse on local labels?
655
656 // See what kind of statement we have.
657 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000658 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000659 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000660 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000661
662 // Diagnose attempt to use a variable as a label.
663 //
664 // FIXME: Diagnostics. Note the location of the definition as a label.
665 // FIXME: This doesn't diagnose assignment to a symbol which has been
666 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000667 MCSymbol *Sym;
668 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000669 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000670 else
671 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000672 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000673 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000674
Daniel Dunbar959fd882009-08-26 22:13:22 +0000675 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000676 Out.EmitLabel(Sym);
677
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000678 // Consume any end of statement token, if present, to avoid spurious
679 // AddBlankLine calls().
680 if (Lexer.is(AsmToken::EndOfStatement)) {
681 Lex();
682 if (Lexer.is(AsmToken::Eof))
683 return false;
684 }
685
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000686 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000687 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000688
Daniel Dunbar3f872332009-07-28 16:08:33 +0000689 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000690 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000691 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000692
Daniel Dunbare2ace502009-08-31 08:09:09 +0000693 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000694
695 default: // Normal instruction or directive.
696 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000697 }
698
699 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000700 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000701 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000703 return ParseDirectiveSet();
704
Daniel Dunbara0d14262009-06-24 23:30:00 +0000705 // Data directives
706
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000708 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000710 return ParseDirectiveAscii(true);
711
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000713 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000714 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000715 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000717 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000719 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000720
721 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000723 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000725 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000727 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000729 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000731 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000732 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000733 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000735 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000737 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
738
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000740 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000741
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000743 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000745 return ParseDirectiveSpace();
746
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000747 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000748
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000750 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000752 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000754 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000756 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000757 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000758 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000759 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000760 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000761 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000762 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000763 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000764 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000765 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000766 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000767 if (IDVal == ".type")
768 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000770 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000771 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000772 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000773 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000774 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000775 if (IDVal == ".weak_def_can_be_hidden")
776 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000777
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000778 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000779 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000780 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000781 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000782
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000783 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000784 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000785 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000786 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000787
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000788 // Look up the handler in the handler table.
789 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
790 DirectiveMap.lookup(IDVal);
791 if (Handler.first)
792 return (Handler.first->*Handler.second)(IDVal, IDLoc);
793
Kevin Enderby9c656452009-09-10 20:51:44 +0000794 // Target hook for parsing target specific directives.
795 if (!getTargetParser().ParseDirective(ID))
796 return false;
797
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000798 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000799 EatToEndOfStatement();
800 return false;
801 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000802
Chris Lattnera7f13542010-05-19 23:34:33 +0000803 // Canonicalize the opcode to lower case.
804 SmallString<128> Opcode;
805 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
806 Opcode.push_back(tolower(IDVal[i]));
807
Chris Lattner98986712010-01-14 22:21:20 +0000808 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000809 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000810 ParsedOperands);
811 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
812 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000813
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000814 // If parsing succeeded, match the instruction.
815 if (!HadError) {
816 MCInst Inst;
817 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
818 // Emit the instruction on success.
819 Out.EmitInstruction(Inst);
820 } else {
821 // Otherwise emit a diagnostic about the match failure and set the error
822 // flag.
823 //
824 // FIXME: We should give nicer diagnostics about the exact failure.
825 Error(IDLoc, "unrecognized instruction");
826 HadError = true;
827 }
828 }
Chris Lattner98986712010-01-14 22:21:20 +0000829
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000830 // If there was no error, consume the end-of-statement token. Otherwise this
831 // will be done by our caller.
832 if (!HadError)
833 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000834
835 // Free any parsed operands.
836 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
837 delete ParsedOperands[i];
838
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000839 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000840}
Chris Lattner9a023f72009-06-24 04:43:34 +0000841
Benjamin Kramer38e59892010-07-14 22:38:02 +0000842bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000843 // FIXME: Use better location, we should use proper tokens.
844 SMLoc EqualLoc = Lexer.getLoc();
845
Daniel Dunbar821e3332009-08-31 08:09:28 +0000846 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000847 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000848 return true;
849
Daniel Dunbar3f872332009-07-28 16:08:33 +0000850 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000851 return TokError("unexpected token in assignment");
852
853 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000854 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000855
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000856 // Validate that the LHS is allowed to be a variable (either it has not been
857 // used as a symbol, or it is an absolute symbol).
858 MCSymbol *Sym = getContext().LookupSymbol(Name);
859 if (Sym) {
860 // Diagnose assignment to a label.
861 //
862 // FIXME: Diagnostics. Note the location of the definition as a label.
863 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000864 if (Sym->isUndefined() && !Sym->isUsedInExpr())
865 ; // Allow redefinitions of undefined symbols only used in directives.
866 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000867 return Error(EqualLoc, "redefinition of '" + Name + "'");
868 else if (!Sym->isVariable())
869 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000870 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000871 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
872 Name + "'");
873 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000874 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000875
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000876 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000877
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000878 Sym->setUsedInExpr(true);
879
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000880 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000881 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000882
883 return false;
884}
885
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000886/// ParseIdentifier:
887/// ::= identifier
888/// ::= string
889bool AsmParser::ParseIdentifier(StringRef &Res) {
890 if (Lexer.isNot(AsmToken::Identifier) &&
891 Lexer.isNot(AsmToken::String))
892 return true;
893
Sean Callanan18b83232010-01-19 21:44:56 +0000894 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000895
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000897
898 return false;
899}
900
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901/// ParseDirectiveSet:
902/// ::= .set identifier ',' expression
903bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000904 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000905
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000906 if (ParseIdentifier(Name))
907 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000908
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000909 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000910 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000911 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000912
Daniel Dunbare2ace502009-08-31 08:09:09 +0000913 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000914}
915
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000916bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000917 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000918
919 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000920 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000921 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
922 if (Str[i] != '\\') {
923 Data += Str[i];
924 continue;
925 }
926
927 // Recognize escaped characters. Note that this escape semantics currently
928 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
929 ++i;
930 if (i == e)
931 return TokError("unexpected backslash at end of string");
932
933 // Recognize octal sequences.
934 if ((unsigned) (Str[i] - '0') <= 7) {
935 // Consume up to three octal characters.
936 unsigned Value = Str[i] - '0';
937
938 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
939 ++i;
940 Value = Value * 8 + (Str[i] - '0');
941
942 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
943 ++i;
944 Value = Value * 8 + (Str[i] - '0');
945 }
946 }
947
948 if (Value > 255)
949 return TokError("invalid octal escape sequence (out of range)");
950
951 Data += (unsigned char) Value;
952 continue;
953 }
954
955 // Otherwise recognize individual escapes.
956 switch (Str[i]) {
957 default:
958 // Just reject invalid escape sequences for now.
959 return TokError("invalid escape sequence (unrecognized character)");
960
961 case 'b': Data += '\b'; break;
962 case 'f': Data += '\f'; break;
963 case 'n': Data += '\n'; break;
964 case 'r': Data += '\r'; break;
965 case 't': Data += '\t'; break;
966 case '"': Data += '"'; break;
967 case '\\': Data += '\\'; break;
968 }
969 }
970
971 return false;
972}
973
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000975/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000977 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000978 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000979 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000981
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000982 std::string Data;
983 if (ParseEscapedString(Data))
984 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000985
986 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000987 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000988 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
989
Sean Callanan79ed1a82010-01-19 20:22:31 +0000990 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000991
992 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000993 break;
994
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000995 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000997 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998 }
999 }
1000
Sean Callanan79ed1a82010-01-19 20:22:31 +00001001 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002 return false;
1003}
1004
1005/// ParseDirectiveValue
1006/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1007bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001008 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001009 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001010 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001011 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001012 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013 return true;
1014
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001015 // Special case constant expressions to match code generator.
1016 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001017 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001018 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001019 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001021 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022 break;
1023
1024 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001025 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001027 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 }
1029 }
1030
Sean Callanan79ed1a82010-01-19 20:22:31 +00001031 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032 return false;
1033}
1034
1035/// ParseDirectiveSpace
1036/// ::= .space expression [ , expression ]
1037bool AsmParser::ParseDirectiveSpace() {
1038 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001039 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001040 return true;
1041
1042 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001043 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1044 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001046 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001047
Daniel Dunbar475839e2009-06-29 20:37:27 +00001048 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 return true;
1050
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001051 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052 return TokError("unexpected token in '.space' directive");
1053 }
1054
Sean Callanan79ed1a82010-01-19 20:22:31 +00001055 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001056
1057 if (NumBytes <= 0)
1058 return TokError("invalid number of bytes in '.space' directive");
1059
1060 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001061 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062
1063 return false;
1064}
1065
1066/// ParseDirectiveFill
1067/// ::= .fill expression , expression , expression
1068bool AsmParser::ParseDirectiveFill() {
1069 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001070 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return true;
1072
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001073 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001074 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001075 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076
1077 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001078 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079 return true;
1080
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001081 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001083 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084
1085 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001086 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087 return true;
1088
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001089 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001090 return TokError("unexpected token in '.fill' directive");
1091
Sean Callanan79ed1a82010-01-19 20:22:31 +00001092 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001094 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1095 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001096
1097 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001098 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001099
1100 return false;
1101}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001102
1103/// ParseDirectiveOrg
1104/// ::= .org expression [ , expression ]
1105bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001106 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001107 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001108 return true;
1109
1110 // Parse optional fill expression.
1111 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001112 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1113 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001114 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001115 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001116
Daniel Dunbar475839e2009-06-29 20:37:27 +00001117 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001118 return true;
1119
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001120 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001121 return TokError("unexpected token in '.org' directive");
1122 }
1123
Sean Callanan79ed1a82010-01-19 20:22:31 +00001124 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001125
1126 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1127 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001128 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001129
1130 return false;
1131}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001132
1133/// ParseDirectiveAlign
1134/// ::= {.align, ...} expression [ , expression [ , expression ]]
1135bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001136 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001137 int64_t Alignment;
1138 if (ParseAbsoluteExpression(Alignment))
1139 return true;
1140
1141 SMLoc MaxBytesLoc;
1142 bool HasFillExpr = false;
1143 int64_t FillExpr = 0;
1144 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001145 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1146 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001147 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001148 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001149
1150 // The fill expression can be omitted while specifying a maximum number of
1151 // alignment bytes, e.g:
1152 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001153 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001154 HasFillExpr = true;
1155 if (ParseAbsoluteExpression(FillExpr))
1156 return true;
1157 }
1158
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001159 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1160 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001161 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001162 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001163
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001164 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001165 if (ParseAbsoluteExpression(MaxBytesToFill))
1166 return true;
1167
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001168 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001169 return TokError("unexpected token in directive");
1170 }
1171 }
1172
Sean Callanan79ed1a82010-01-19 20:22:31 +00001173 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001174
Daniel Dunbar648ac512010-05-17 21:54:30 +00001175 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001176 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177
1178 // Compute alignment in bytes.
1179 if (IsPow2) {
1180 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001181 if (Alignment >= 32) {
1182 Error(AlignmentLoc, "invalid alignment value");
1183 Alignment = 31;
1184 }
1185
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001186 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001187 }
1188
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001189 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001190 if (MaxBytesLoc.isValid()) {
1191 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001192 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1193 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001194 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001195 }
1196
1197 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001198 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1199 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001200 MaxBytesToFill = 0;
1201 }
1202 }
1203
Daniel Dunbar648ac512010-05-17 21:54:30 +00001204 // Check whether we should use optimal code alignment for this .align
1205 // directive.
1206 //
1207 // FIXME: This should be using a target hook.
1208 bool UseCodeAlign = false;
1209 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001210 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001211 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001212 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1213 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001214 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001215 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001216 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001217 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1218 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001219 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001220
1221 return false;
1222}
1223
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001224/// ParseDirectiveSymbolAttribute
1225/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001226bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001227 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001228 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001229 StringRef Name;
1230
1231 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001232 return TokError("expected identifier in directive");
1233
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001234 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001235
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001236 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001237
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001238 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001239 break;
1240
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001241 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001242 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001243 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001244 }
1245 }
1246
Sean Callanan79ed1a82010-01-19 20:22:31 +00001247 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001248 return false;
1249}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001250
Matt Fleming924c5e52010-05-21 11:36:59 +00001251/// ParseDirectiveELFType
1252/// ::= .type identifier , @attribute
1253bool AsmParser::ParseDirectiveELFType() {
1254 StringRef Name;
1255 if (ParseIdentifier(Name))
1256 return TokError("expected identifier in directive");
1257
1258 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001259 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001260
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001261 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001262 return TokError("unexpected token in '.type' directive");
1263 Lex();
1264
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001265 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001266 return TokError("expected '@' before type");
1267 Lex();
1268
1269 StringRef Type;
1270 SMLoc TypeLoc;
1271
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001272 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001273 if (ParseIdentifier(Type))
1274 return TokError("expected symbol type in directive");
1275
1276 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1277 .Case("function", MCSA_ELF_TypeFunction)
1278 .Case("object", MCSA_ELF_TypeObject)
1279 .Case("tls_object", MCSA_ELF_TypeTLS)
1280 .Case("common", MCSA_ELF_TypeCommon)
1281 .Case("notype", MCSA_ELF_TypeNoType)
1282 .Default(MCSA_Invalid);
1283
1284 if (Attr == MCSA_Invalid)
1285 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1286
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001287 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001288 return TokError("unexpected token in '.type' directive");
1289
1290 Lex();
1291
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001292 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001293
1294 return false;
1295}
1296
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001297/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001298/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1299bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001300 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001301 StringRef Name;
1302 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001303 return TokError("expected identifier in directive");
1304
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001305 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001306 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001307
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001308 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001309 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001310 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001311
1312 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001313 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001314 if (ParseAbsoluteExpression(Size))
1315 return true;
1316
1317 int64_t Pow2Alignment = 0;
1318 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001319 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001320 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001321 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001322 if (ParseAbsoluteExpression(Pow2Alignment))
1323 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001324
1325 // If this target takes alignments in bytes (not log) validate and convert.
1326 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1327 if (!isPowerOf2_64(Pow2Alignment))
1328 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1329 Pow2Alignment = Log2_64(Pow2Alignment);
1330 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001331 }
1332
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001333 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001334 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001335
Sean Callanan79ed1a82010-01-19 20:22:31 +00001336 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001337
Chris Lattner1fc3d752009-07-09 17:25:12 +00001338 // NOTE: a size of zero for a .comm should create a undefined symbol
1339 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001340 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001341 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1342 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001343
Eric Christopherc260a3e2010-05-14 01:38:54 +00001344 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001345 // may internally end up wanting an alignment in bytes.
1346 // FIXME: Diagnose overflow.
1347 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001348 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1349 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001350
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001351 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001352 return Error(IDLoc, "invalid symbol redefinition");
1353
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001354 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001355 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001356 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001357 getStreamer().EmitZerofill(Ctx.getMachOSection(
1358 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1359 0, SectionKind::getBSS()),
1360 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001361 return false;
1362 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001363
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001364 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001365 return false;
1366}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001367
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001368/// ParseDirectiveAbort
1369/// ::= .abort [ "abort_string" ]
1370bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001371 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001373
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001374 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001375 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1376 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001377 return TokError("expected string in '.abort' directive");
1378
Sean Callanan18b83232010-01-19 21:44:56 +00001379 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001380
Sean Callanan79ed1a82010-01-19 20:22:31 +00001381 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001382 }
1383
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001384 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001385 return TokError("unexpected token in '.abort' directive");
1386
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001388
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001389 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001390 if (Str.empty())
1391 Error(Loc, ".abort detected. Assembly stopping.");
1392 else
1393 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001394
1395 return false;
1396}
Kevin Enderby71148242009-07-14 21:35:03 +00001397
Kevin Enderby1f049b22009-07-14 23:21:55 +00001398/// ParseDirectiveInclude
1399/// ::= .include "filename"
1400bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001401 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001402 return TokError("expected string in '.include' directive");
1403
Sean Callanan18b83232010-01-19 21:44:56 +00001404 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001405 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001406 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001407
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001408 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001409 return TokError("unexpected token in '.include' directive");
1410
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001411 // Strip the quotes.
1412 Filename = Filename.substr(1, Filename.size()-2);
1413
1414 // Attempt to switch the lexer to the included file before consuming the end
1415 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001416 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001417 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001418 return true;
1419 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001420
1421 return false;
1422}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001423
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001424/// ParseDirectiveIf
1425/// ::= .if expression
1426bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001427 TheCondStack.push_back(TheCondState);
1428 TheCondState.TheCond = AsmCond::IfCond;
1429 if(TheCondState.Ignore) {
1430 EatToEndOfStatement();
1431 }
1432 else {
1433 int64_t ExprValue;
1434 if (ParseAbsoluteExpression(ExprValue))
1435 return true;
1436
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001437 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001438 return TokError("unexpected token in '.if' directive");
1439
Sean Callanan79ed1a82010-01-19 20:22:31 +00001440 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001441
1442 TheCondState.CondMet = ExprValue;
1443 TheCondState.Ignore = !TheCondState.CondMet;
1444 }
1445
1446 return false;
1447}
1448
1449/// ParseDirectiveElseIf
1450/// ::= .elseif expression
1451bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1452 if (TheCondState.TheCond != AsmCond::IfCond &&
1453 TheCondState.TheCond != AsmCond::ElseIfCond)
1454 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1455 " an .elseif");
1456 TheCondState.TheCond = AsmCond::ElseIfCond;
1457
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001458 bool LastIgnoreState = false;
1459 if (!TheCondStack.empty())
1460 LastIgnoreState = TheCondStack.back().Ignore;
1461 if (LastIgnoreState || TheCondState.CondMet) {
1462 TheCondState.Ignore = true;
1463 EatToEndOfStatement();
1464 }
1465 else {
1466 int64_t ExprValue;
1467 if (ParseAbsoluteExpression(ExprValue))
1468 return true;
1469
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001470 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001471 return TokError("unexpected token in '.elseif' directive");
1472
Sean Callanan79ed1a82010-01-19 20:22:31 +00001473 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001474 TheCondState.CondMet = ExprValue;
1475 TheCondState.Ignore = !TheCondState.CondMet;
1476 }
1477
1478 return false;
1479}
1480
1481/// ParseDirectiveElse
1482/// ::= .else
1483bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001484 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001485 return TokError("unexpected token in '.else' directive");
1486
Sean Callanan79ed1a82010-01-19 20:22:31 +00001487 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001488
1489 if (TheCondState.TheCond != AsmCond::IfCond &&
1490 TheCondState.TheCond != AsmCond::ElseIfCond)
1491 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1492 ".elseif");
1493 TheCondState.TheCond = AsmCond::ElseCond;
1494 bool LastIgnoreState = false;
1495 if (!TheCondStack.empty())
1496 LastIgnoreState = TheCondStack.back().Ignore;
1497 if (LastIgnoreState || TheCondState.CondMet)
1498 TheCondState.Ignore = true;
1499 else
1500 TheCondState.Ignore = false;
1501
1502 return false;
1503}
1504
1505/// ParseDirectiveEndIf
1506/// ::= .endif
1507bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001508 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001509 return TokError("unexpected token in '.endif' directive");
1510
Sean Callanan79ed1a82010-01-19 20:22:31 +00001511 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001512
1513 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1514 TheCondStack.empty())
1515 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1516 ".else");
1517 if (!TheCondStack.empty()) {
1518 TheCondState = TheCondStack.back();
1519 TheCondStack.pop_back();
1520 }
1521
1522 return false;
1523}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001524
1525/// ParseDirectiveFile
1526/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001527bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001528 // FIXME: I'm not sure what this is.
1529 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001530 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001531 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001532 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001533
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001534 if (FileNumber < 1)
1535 return TokError("file number less than one");
1536 }
1537
Daniel Dunbareceec052010-07-12 17:45:27 +00001538 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001539 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001540
Chris Lattnerd32e8032010-01-25 19:02:58 +00001541 StringRef Filename = getTok().getString();
1542 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001543 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001544
Daniel Dunbareceec052010-07-12 17:45:27 +00001545 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001546 return TokError("unexpected token in '.file' directive");
1547
Chris Lattnerd32e8032010-01-25 19:02:58 +00001548 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001549 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001550 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001551 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1552
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001553 return false;
1554}
1555
1556/// ParseDirectiveLine
1557/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001558bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001559 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1560 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001561 return TokError("unexpected token in '.line' directive");
1562
Sean Callanan18b83232010-01-19 21:44:56 +00001563 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001564 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001565 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001566
1567 // FIXME: Do something with the .line.
1568 }
1569
Daniel Dunbareceec052010-07-12 17:45:27 +00001570 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001571 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001572
1573 return false;
1574}
1575
1576
1577/// ParseDirectiveLoc
1578/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001579bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001580 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001581 return TokError("unexpected token in '.loc' directive");
1582
1583 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001584 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001585 (void) FileNumber;
1586 // FIXME: Validate file.
1587
Sean Callanan79ed1a82010-01-19 20:22:31 +00001588 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001589 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1590 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001591 return TokError("unexpected token in '.loc' directive");
1592
Sean Callanan18b83232010-01-19 21:44:56 +00001593 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001594 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001595 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001596
Daniel Dunbareceec052010-07-12 17:45:27 +00001597 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1598 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001599 return TokError("unexpected token in '.loc' directive");
1600
Sean Callanan18b83232010-01-19 21:44:56 +00001601 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001602 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001603 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001604
1605 // FIXME: Do something with the .loc.
1606 }
1607 }
1608
Daniel Dunbareceec052010-07-12 17:45:27 +00001609 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001610 return TokError("unexpected token in '.file' directive");
1611
1612 return false;
1613}
1614
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001615/// ParseDirectiveMacrosOnOff
1616/// ::= .macros_on
1617/// ::= .macros_off
1618bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1619 SMLoc DirectiveLoc) {
1620 if (getLexer().isNot(AsmToken::EndOfStatement))
1621 return Error(getLexer().getLoc(),
1622 "unexpected token in '" + Directive + "' directive");
1623
1624 getParser().MacrosEnabled = Directive == ".macros_on";
1625
1626 return false;
1627}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001628
1629/// \brief Create an MCAsmParser instance.
1630MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1631 MCContext &C, MCStreamer &Out,
1632 const MCAsmInfo &MAI) {
1633 return new AsmParser(T, SM, C, Out, MAI);
1634}