blob: c9ac95a78339048dd4f74c7c45ef2ce1f4bb3ab1 [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 {
41 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
42 void operator=(const AsmParser &); // DO NOT IMPLEMENT
43private:
44 AsmLexer Lexer;
45 MCContext &Ctx;
46 MCStreamer &Out;
47 SourceMgr &SrcMgr;
48 MCAsmParserExtension *GenericParser;
49 MCAsmParserExtension *PlatformParser;
50
51 /// This is the current buffer index we're lexing from as managed by the
52 /// SourceMgr object.
53 int CurBuffer;
54
55 AsmCond TheCondState;
56 std::vector<AsmCond> TheCondStack;
57
58 /// DirectiveMap - This is a table handlers for directives. Each handler is
59 /// invoked after the directive identifier is read and is responsible for
60 /// parsing and validating the rest of the directive. The handler is passed
61 /// in the directive name and the location of the directive keyword.
62 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
63public:
64 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
65 const MCAsmInfo &MAI);
66 ~AsmParser();
67
68 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
69
70 void AddDirectiveHandler(MCAsmParserExtension *Object,
71 StringRef Directive,
72 DirectiveHandler Handler) {
73 DirectiveMap[Directive] = std::make_pair(Object, Handler);
74 }
75
76public:
77 /// @name MCAsmParser Interface
78 /// {
79
80 virtual SourceMgr &getSourceManager() { return SrcMgr; }
81 virtual MCAsmLexer &getLexer() { return Lexer; }
82 virtual MCContext &getContext() { return Ctx; }
83 virtual MCStreamer &getStreamer() { return Out; }
84
85 virtual void Warning(SMLoc L, const Twine &Meg);
86 virtual bool Error(SMLoc L, const Twine &Msg);
87
88 const AsmToken &Lex();
89
90 bool ParseExpression(const MCExpr *&Res);
91 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
92 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
93 virtual bool ParseAbsoluteExpression(int64_t &Res);
94
95 /// }
96
97private:
98 bool ParseStatement();
99
100 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
101
102 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
103 bool EnterIncludeFile(const std::string &Filename);
104
105 void EatToEndOfStatement();
106
107 bool ParseAssignment(StringRef Name);
108
109 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
110 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
111 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
112
113 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
114 /// and set \arg Res to the identifier contents.
115 bool ParseIdentifier(StringRef &Res);
116
117 // Directive Parsing.
118 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
119 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
120 bool ParseDirectiveFill(); // ".fill"
121 bool ParseDirectiveSpace(); // ".space"
122 bool ParseDirectiveSet(); // ".set"
123 bool ParseDirectiveOrg(); // ".org"
124 // ".align{,32}", ".p2align{,w,l}"
125 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
126
127 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
128 /// accepts a single symbol (which should be a label or an external).
129 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
130 bool ParseDirectiveELFType(); // ELF specific ".type"
131
132 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
133
134 bool ParseDirectiveAbort(); // ".abort"
135 bool ParseDirectiveInclude(); // ".include"
136
137 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
138 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
139 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
140 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
141
142 /// ParseEscapedString - Parse the current token as a string which may include
143 /// escaped characters and return the string contents.
144 bool ParseEscapedString(std::string &Data);
145};
146
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000147/// \brief Generic implementations of directive handling, etc. which is shared
148/// (or the default, at least) for all assembler parser.
149class GenericAsmParser : public MCAsmParserExtension {
150public:
151 GenericAsmParser() {}
152
153 virtual void Initialize(MCAsmParser &Parser) {
154 // Call the base implementation.
155 this->MCAsmParserExtension::Initialize(Parser);
156
157 // Debugging directives.
158 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
159 &GenericAsmParser::ParseDirectiveFile));
160 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
161 &GenericAsmParser::ParseDirectiveLine));
162 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
163 &GenericAsmParser::ParseDirectiveLoc));
164 }
165
166 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
167 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
168 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
169};
170
171}
172
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000173namespace llvm {
174
175extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000176extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000177
178}
179
Chris Lattneraaec2052010-01-19 19:46:13 +0000180enum { DEFAULT_ADDRSPACE = 0 };
181
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000182AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
183 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000184 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000185 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbard1e3b442010-07-17 02:26:10 +0000186 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000187 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000188
189 // Initialize the generic parser.
190 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000191
192 // Initialize the platform / file format parser.
193 //
194 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
195 // created.
196 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000197 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000198 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000199 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000200 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000201 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000202 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000203}
204
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000205AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000206 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000207 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000208}
209
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000210void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000211 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000212}
213
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000214bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000215 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000216 return true;
217}
218
Sean Callananbf2013e2010-01-20 23:19:55 +0000219void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
220 const char *Type) const {
221 SrcMgr.PrintMessage(Loc, Msg, Type);
222}
Sean Callananfd0b0282010-01-21 00:19:58 +0000223
224bool AsmParser::EnterIncludeFile(const std::string &Filename) {
225 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
226 if (NewBuf == -1)
227 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000228
Sean Callananfd0b0282010-01-21 00:19:58 +0000229 CurBuffer = NewBuf;
230
231 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
232
233 return false;
234}
235
236const AsmToken &AsmParser::Lex() {
237 const AsmToken *tok = &Lexer.Lex();
238
239 if (tok->is(AsmToken::Eof)) {
240 // If this is the end of an included file, pop the parent file off the
241 // include stack.
242 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
243 if (ParentIncludeLoc != SMLoc()) {
244 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
245 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
246 ParentIncludeLoc.getPointer());
247 tok = &Lexer.Lex();
248 }
249 }
250
251 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000252 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000253
Sean Callananfd0b0282010-01-21 00:19:58 +0000254 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000255}
256
Chris Lattner79180e22010-04-05 23:15:42 +0000257bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000258 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000259 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000260 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000261 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000262 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000263 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
264 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000265
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000266 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000267 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000268
Chris Lattnerb717fb02009-07-02 21:53:43 +0000269 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000270
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000271 AsmCond StartingCondState = TheCondState;
272
Chris Lattnerb717fb02009-07-02 21:53:43 +0000273 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000274 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000275 if (!ParseStatement()) continue;
276
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000277 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000278 HadError = true;
279 EatToEndOfStatement();
280 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000281
282 if (TheCondState.TheCond != StartingCondState.TheCond ||
283 TheCondState.Ignore != StartingCondState.Ignore)
284 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000285
Chris Lattner79180e22010-04-05 23:15:42 +0000286 // Finalize the output stream if there are no errors and if the client wants
287 // us to.
288 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000289 Out.Finish();
290
Chris Lattnerb717fb02009-07-02 21:53:43 +0000291 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000292}
293
Chris Lattner2cf5f142009-06-22 01:29:09 +0000294/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
295void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000296 while (Lexer.isNot(AsmToken::EndOfStatement) &&
297 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000298 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000299
300 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000302 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000303}
304
Chris Lattnerc4193832009-06-22 05:51:26 +0000305
Chris Lattner74ec1a32009-06-22 06:32:03 +0000306/// ParseParenExpr - Parse a paren expression and return it.
307/// NOTE: This assumes the leading '(' has already been consumed.
308///
309/// parenexpr ::= expr)
310///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000311bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000312 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000313 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000314 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000315 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000316 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000317 return false;
318}
Chris Lattnerc4193832009-06-22 05:51:26 +0000319
Chris Lattner74ec1a32009-06-22 06:32:03 +0000320/// ParsePrimaryExpr - Parse a primary expression and return it.
321/// primaryexpr ::= (parenexpr
322/// primaryexpr ::= symbol
323/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000324/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000325/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000326bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000327 switch (Lexer.getKind()) {
328 default:
329 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000331 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000332 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000334 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000336 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000337 case AsmToken::Identifier: {
338 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000339 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000340 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000341
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000342 // Mark the symbol as used in an expression.
343 Sym->setUsedInExpr(true);
344
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000345 // Lookup the symbol variant if used.
346 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
347 if (Split.first.size() != getTok().getIdentifier().size())
348 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
349
Chris Lattnerb4307b32010-01-15 19:28:38 +0000350 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000351 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000352
353 // If this is an absolute variable reference, substitute it now to preserve
354 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000355 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000356 if (Variant)
357 return Error(EndLoc, "unexpected modified on variable reference");
358
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000359 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000360 return false;
361 }
362
363 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000364 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000365 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000366 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000367 case AsmToken::Integer: {
368 SMLoc Loc = getTok().getLoc();
369 int64_t IntVal = getTok().getIntVal();
370 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000371 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000372 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000373 // Look for 'b' or 'f' following an Integer as a directional label
374 if (Lexer.getKind() == AsmToken::Identifier) {
375 StringRef IDVal = getTok().getString();
376 if (IDVal == "f" || IDVal == "b"){
377 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
378 IDVal == "f" ? 1 : 0);
379 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
380 getContext());
381 if(IDVal == "b" && Sym->isUndefined())
382 return Error(Loc, "invalid reference to undefined symbol");
383 EndLoc = Lexer.getLoc();
384 Lex(); // Eat identifier.
385 }
386 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000387 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000388 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000389 case AsmToken::Dot: {
390 // This is a '.' reference, which references the current PC. Emit a
391 // temporary label to the streamer and refer to it.
392 MCSymbol *Sym = Ctx.CreateTempSymbol();
393 Out.EmitLabel(Sym);
394 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
395 EndLoc = Lexer.getLoc();
396 Lex(); // Eat identifier.
397 return false;
398 }
399
Daniel Dunbar3f872332009-07-28 16:08:33 +0000400 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000401 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000402 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000403 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000404 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000405 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000406 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000407 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000408 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000409 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000410 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000411 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000412 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000413 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000414 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000415 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000416 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000417 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000418 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000419 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000420 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000421 }
422}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000423
Chris Lattnerb4307b32010-01-15 19:28:38 +0000424bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000425 SMLoc EndLoc;
426 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000427}
428
Chris Lattner74ec1a32009-06-22 06:32:03 +0000429/// ParseExpression - Parse an expression and return it.
430///
431/// expr ::= expr +,- expr -> lowest.
432/// expr ::= expr |,^,&,! expr -> middle.
433/// expr ::= expr *,/,%,<<,>> expr -> highest.
434/// expr ::= primaryexpr
435///
Chris Lattner54482b42010-01-15 19:39:23 +0000436bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000437 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000438 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000439 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
440 return true;
441
442 // Try to constant fold it up front, if possible.
443 int64_t Value;
444 if (Res->EvaluateAsAbsolute(Value))
445 Res = MCConstantExpr::Create(Value, getContext());
446
447 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000448}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000449
Chris Lattnerb4307b32010-01-15 19:28:38 +0000450bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000451 Res = 0;
452 return ParseParenExpr(Res, EndLoc) ||
453 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000454}
455
Daniel Dunbar475839e2009-06-29 20:37:27 +0000456bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000457 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000458
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000459 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000460 if (ParseExpression(Expr))
461 return true;
462
Daniel Dunbare00b0112009-10-16 01:57:52 +0000463 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000464 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000465
466 return false;
467}
468
Daniel Dunbar3f872332009-07-28 16:08:33 +0000469static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000470 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000471 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000472 default:
473 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000474
475 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000476 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000477 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000478 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000479 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000480 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000481 return 1;
482
483 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000484 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000485 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000486 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000487 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000488 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000489 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000490 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000491 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000492 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000493 case AsmToken::ExclaimEqual:
494 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000495 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000496 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000497 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000498 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000499 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000500 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000501 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000502 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000503 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000504 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000505 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000506 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000507 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000508 return 2;
509
510 // Intermediate Precedence: |, &, ^
511 //
512 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000513 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000514 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000515 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000516 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000517 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000518 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000519 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000520 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000521 return 3;
522
523 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000524 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000525 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000526 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000527 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000528 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000529 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000530 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000531 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000532 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000533 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000534 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000535 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000536 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000537 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000538 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000539 }
540}
541
542
543/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
544/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000545bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
546 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000547 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000548 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000549 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000550
551 // If the next token is lower precedence than we are allowed to eat, return
552 // successfully with what we ate already.
553 if (TokPrec < Precedence)
554 return false;
555
Sean Callanan79ed1a82010-01-19 20:22:31 +0000556 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000557
558 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000559 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000560 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000561
562 // If BinOp binds less tightly with RHS than the operator after RHS, let
563 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000564 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000565 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000566 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000567 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000568 }
569
Daniel Dunbar475839e2009-06-29 20:37:27 +0000570 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000571 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000572 }
573}
574
Chris Lattnerc4193832009-06-22 05:51:26 +0000575
576
577
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000578/// ParseStatement:
579/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000580/// ::= Label* Directive ...Operands... EndOfStatement
581/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000582bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000583 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000584 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000585 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000586 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000587 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000588
589 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000590 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000591 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000592 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000593 int64_t LocalLabelVal = -1;
594 // GUESS allow an integer followed by a ':' as a directional local label
595 if (Lexer.is(AsmToken::Integer)) {
596 LocalLabelVal = getTok().getIntVal();
597 if (LocalLabelVal < 0) {
598 if (!TheCondState.Ignore)
599 return TokError("unexpected token at start of statement");
600 IDVal = "";
601 }
602 else {
603 IDVal = getTok().getString();
604 Lex(); // Consume the integer token to be used as an identifier token.
605 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000606 if (!TheCondState.Ignore)
607 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000608 }
609 }
610 }
611 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000612 if (!TheCondState.Ignore)
613 return TokError("unexpected token at start of statement");
614 IDVal = "";
615 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000616
Chris Lattner7834fac2010-04-17 18:14:27 +0000617 // Handle conditional assembly here before checking for skipping. We
618 // have to do this so that .endif isn't skipped in a ".if 0" block for
619 // example.
620 if (IDVal == ".if")
621 return ParseDirectiveIf(IDLoc);
622 if (IDVal == ".elseif")
623 return ParseDirectiveElseIf(IDLoc);
624 if (IDVal == ".else")
625 return ParseDirectiveElse(IDLoc);
626 if (IDVal == ".endif")
627 return ParseDirectiveEndIf(IDLoc);
628
629 // If we are in a ".if 0" block, ignore this statement.
630 if (TheCondState.Ignore) {
631 EatToEndOfStatement();
632 return false;
633 }
634
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000635 // FIXME: Recurse on local labels?
636
637 // See what kind of statement we have.
638 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000639 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000640 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000641 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000642
643 // Diagnose attempt to use a variable as a label.
644 //
645 // FIXME: Diagnostics. Note the location of the definition as a label.
646 // FIXME: This doesn't diagnose assignment to a symbol which has been
647 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000648 MCSymbol *Sym;
649 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000650 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000651 else
652 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000653 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000654 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000655
Daniel Dunbar959fd882009-08-26 22:13:22 +0000656 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000657 Out.EmitLabel(Sym);
658
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000659 // Consume any end of statement token, if present, to avoid spurious
660 // AddBlankLine calls().
661 if (Lexer.is(AsmToken::EndOfStatement)) {
662 Lex();
663 if (Lexer.is(AsmToken::Eof))
664 return false;
665 }
666
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000667 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000668 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000669
Daniel Dunbar3f872332009-07-28 16:08:33 +0000670 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000671 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000672 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000673
Daniel Dunbare2ace502009-08-31 08:09:09 +0000674 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000675
676 default: // Normal instruction or directive.
677 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000678 }
679
680 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000681 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000682 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000684 return ParseDirectiveSet();
685
Daniel Dunbara0d14262009-06-24 23:30:00 +0000686 // Data directives
687
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000689 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000691 return ParseDirectiveAscii(true);
692
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000694 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000695 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000696 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000698 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000700 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000701
702 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000704 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000706 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000708 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000710 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000712 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000714 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000716 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000718 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
719
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000721 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000722
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000724 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000726 return ParseDirectiveSpace();
727
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000728 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000729
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000731 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000732 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000733 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000735 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000737 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000738 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000739 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000741 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000743 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000745 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000747 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000748 if (IDVal == ".type")
749 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000750 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000751 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000752 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000753 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000754 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000755 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000756 if (IDVal == ".weak_def_can_be_hidden")
757 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000758
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000759 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000760 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000761 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000762 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000763
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000765 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000767 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000768
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000769 // Look up the handler in the handler table.
770 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
771 DirectiveMap.lookup(IDVal);
772 if (Handler.first)
773 return (Handler.first->*Handler.second)(IDVal, IDLoc);
774
Kevin Enderby9c656452009-09-10 20:51:44 +0000775 // Target hook for parsing target specific directives.
776 if (!getTargetParser().ParseDirective(ID))
777 return false;
778
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000779 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000780 EatToEndOfStatement();
781 return false;
782 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000783
Chris Lattnera7f13542010-05-19 23:34:33 +0000784 // Canonicalize the opcode to lower case.
785 SmallString<128> Opcode;
786 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
787 Opcode.push_back(tolower(IDVal[i]));
788
Chris Lattner98986712010-01-14 22:21:20 +0000789 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000790 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000791 ParsedOperands);
792 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
793 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000794
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000795 // If parsing succeeded, match the instruction.
796 if (!HadError) {
797 MCInst Inst;
798 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
799 // Emit the instruction on success.
800 Out.EmitInstruction(Inst);
801 } else {
802 // Otherwise emit a diagnostic about the match failure and set the error
803 // flag.
804 //
805 // FIXME: We should give nicer diagnostics about the exact failure.
806 Error(IDLoc, "unrecognized instruction");
807 HadError = true;
808 }
809 }
Chris Lattner98986712010-01-14 22:21:20 +0000810
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000811 // If there was no error, consume the end-of-statement token. Otherwise this
812 // will be done by our caller.
813 if (!HadError)
814 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000815
816 // Free any parsed operands.
817 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
818 delete ParsedOperands[i];
819
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000820 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000821}
Chris Lattner9a023f72009-06-24 04:43:34 +0000822
Benjamin Kramer38e59892010-07-14 22:38:02 +0000823bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000824 // FIXME: Use better location, we should use proper tokens.
825 SMLoc EqualLoc = Lexer.getLoc();
826
Daniel Dunbar821e3332009-08-31 08:09:28 +0000827 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000828 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000829 return true;
830
Daniel Dunbar3f872332009-07-28 16:08:33 +0000831 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000832 return TokError("unexpected token in assignment");
833
834 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000835 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000836
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000837 // Validate that the LHS is allowed to be a variable (either it has not been
838 // used as a symbol, or it is an absolute symbol).
839 MCSymbol *Sym = getContext().LookupSymbol(Name);
840 if (Sym) {
841 // Diagnose assignment to a label.
842 //
843 // FIXME: Diagnostics. Note the location of the definition as a label.
844 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000845 if (Sym->isUndefined() && !Sym->isUsedInExpr())
846 ; // Allow redefinitions of undefined symbols only used in directives.
847 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000848 return Error(EqualLoc, "redefinition of '" + Name + "'");
849 else if (!Sym->isVariable())
850 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000851 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000852 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
853 Name + "'");
854 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000855 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000856
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000857 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000858
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000859 Sym->setUsedInExpr(true);
860
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000861 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000862 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000863
864 return false;
865}
866
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000867/// ParseIdentifier:
868/// ::= identifier
869/// ::= string
870bool AsmParser::ParseIdentifier(StringRef &Res) {
871 if (Lexer.isNot(AsmToken::Identifier) &&
872 Lexer.isNot(AsmToken::String))
873 return true;
874
Sean Callanan18b83232010-01-19 21:44:56 +0000875 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000876
Sean Callanan79ed1a82010-01-19 20:22:31 +0000877 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000878
879 return false;
880}
881
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000882/// ParseDirectiveSet:
883/// ::= .set identifier ',' expression
884bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000885 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000886
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000887 if (ParseIdentifier(Name))
888 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000889
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000890 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000891 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000892 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000893
Daniel Dunbare2ace502009-08-31 08:09:09 +0000894 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000895}
896
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000897bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000898 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000899
900 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000901 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000902 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
903 if (Str[i] != '\\') {
904 Data += Str[i];
905 continue;
906 }
907
908 // Recognize escaped characters. Note that this escape semantics currently
909 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
910 ++i;
911 if (i == e)
912 return TokError("unexpected backslash at end of string");
913
914 // Recognize octal sequences.
915 if ((unsigned) (Str[i] - '0') <= 7) {
916 // Consume up to three octal characters.
917 unsigned Value = Str[i] - '0';
918
919 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
920 ++i;
921 Value = Value * 8 + (Str[i] - '0');
922
923 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
924 ++i;
925 Value = Value * 8 + (Str[i] - '0');
926 }
927 }
928
929 if (Value > 255)
930 return TokError("invalid octal escape sequence (out of range)");
931
932 Data += (unsigned char) Value;
933 continue;
934 }
935
936 // Otherwise recognize individual escapes.
937 switch (Str[i]) {
938 default:
939 // Just reject invalid escape sequences for now.
940 return TokError("invalid escape sequence (unrecognized character)");
941
942 case 'b': Data += '\b'; break;
943 case 'f': Data += '\f'; break;
944 case 'n': Data += '\n'; break;
945 case 'r': Data += '\r'; break;
946 case 't': Data += '\t'; break;
947 case '"': Data += '"'; break;
948 case '\\': Data += '\\'; break;
949 }
950 }
951
952 return false;
953}
954
Daniel Dunbara0d14262009-06-24 23:30:00 +0000955/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000956/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000957bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000958 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000960 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000961 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000962
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000963 std::string Data;
964 if (ParseEscapedString(Data))
965 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000966
967 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000968 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000969 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
970
Sean Callanan79ed1a82010-01-19 20:22:31 +0000971 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000972
973 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974 break;
975
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000976 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000978 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000979 }
980 }
981
Sean Callanan79ed1a82010-01-19 20:22:31 +0000982 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000983 return false;
984}
985
986/// ParseDirectiveValue
987/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
988bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000989 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000990 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000991 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000992 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000993 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000994 return true;
995
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000996 // Special case constant expressions to match code generator.
997 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000998 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000999 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001000 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001001
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001002 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001003 break;
1004
1005 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001006 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001008 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001009 }
1010 }
1011
Sean Callanan79ed1a82010-01-19 20:22:31 +00001012 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013 return false;
1014}
1015
1016/// ParseDirectiveSpace
1017/// ::= .space expression [ , expression ]
1018bool AsmParser::ParseDirectiveSpace() {
1019 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001020 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 return true;
1022
1023 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001024 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1025 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001027 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028
Daniel Dunbar475839e2009-06-29 20:37:27 +00001029 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 return true;
1031
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001032 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033 return TokError("unexpected token in '.space' directive");
1034 }
1035
Sean Callanan79ed1a82010-01-19 20:22:31 +00001036 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037
1038 if (NumBytes <= 0)
1039 return TokError("invalid number of bytes in '.space' directive");
1040
1041 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001042 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043
1044 return false;
1045}
1046
1047/// ParseDirectiveFill
1048/// ::= .fill expression , expression , expression
1049bool AsmParser::ParseDirectiveFill() {
1050 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001051 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052 return true;
1053
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001054 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001056 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057
1058 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001059 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060 return true;
1061
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001062 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001064 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001065
1066 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001067 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return true;
1069
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001070 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return TokError("unexpected token in '.fill' directive");
1072
Sean Callanan79ed1a82010-01-19 20:22:31 +00001073 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001074
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001075 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1076 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077
1078 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001079 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080
1081 return false;
1082}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001083
1084/// ParseDirectiveOrg
1085/// ::= .org expression [ , expression ]
1086bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001087 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001088 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001089 return true;
1090
1091 // Parse optional fill expression.
1092 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001093 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1094 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001095 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001096 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001097
Daniel Dunbar475839e2009-06-29 20:37:27 +00001098 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001099 return true;
1100
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001101 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001102 return TokError("unexpected token in '.org' directive");
1103 }
1104
Sean Callanan79ed1a82010-01-19 20:22:31 +00001105 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001106
1107 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1108 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001109 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001110
1111 return false;
1112}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001113
1114/// ParseDirectiveAlign
1115/// ::= {.align, ...} expression [ , expression [ , expression ]]
1116bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001117 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001118 int64_t Alignment;
1119 if (ParseAbsoluteExpression(Alignment))
1120 return true;
1121
1122 SMLoc MaxBytesLoc;
1123 bool HasFillExpr = false;
1124 int64_t FillExpr = 0;
1125 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001126 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1127 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001128 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001129 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001130
1131 // The fill expression can be omitted while specifying a maximum number of
1132 // alignment bytes, e.g:
1133 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001134 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001135 HasFillExpr = true;
1136 if (ParseAbsoluteExpression(FillExpr))
1137 return true;
1138 }
1139
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001140 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1141 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001142 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001143 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001144
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001145 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001146 if (ParseAbsoluteExpression(MaxBytesToFill))
1147 return true;
1148
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001149 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001150 return TokError("unexpected token in directive");
1151 }
1152 }
1153
Sean Callanan79ed1a82010-01-19 20:22:31 +00001154 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001155
Daniel Dunbar648ac512010-05-17 21:54:30 +00001156 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001157 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001158
1159 // Compute alignment in bytes.
1160 if (IsPow2) {
1161 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001162 if (Alignment >= 32) {
1163 Error(AlignmentLoc, "invalid alignment value");
1164 Alignment = 31;
1165 }
1166
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001167 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001168 }
1169
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001170 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001171 if (MaxBytesLoc.isValid()) {
1172 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001173 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1174 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001175 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001176 }
1177
1178 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001179 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1180 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181 MaxBytesToFill = 0;
1182 }
1183 }
1184
Daniel Dunbar648ac512010-05-17 21:54:30 +00001185 // Check whether we should use optimal code alignment for this .align
1186 // directive.
1187 //
1188 // FIXME: This should be using a target hook.
1189 bool UseCodeAlign = false;
1190 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001191 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001192 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001193 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1194 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001195 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001196 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001197 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001198 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1199 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001200 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001201
1202 return false;
1203}
1204
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001205/// ParseDirectiveSymbolAttribute
1206/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001207bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001208 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001209 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001210 StringRef Name;
1211
1212 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001213 return TokError("expected identifier in directive");
1214
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001215 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001216
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001217 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001218
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001219 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001220 break;
1221
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001222 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001223 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001224 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001225 }
1226 }
1227
Sean Callanan79ed1a82010-01-19 20:22:31 +00001228 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001229 return false;
1230}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001231
Matt Fleming924c5e52010-05-21 11:36:59 +00001232/// ParseDirectiveELFType
1233/// ::= .type identifier , @attribute
1234bool AsmParser::ParseDirectiveELFType() {
1235 StringRef Name;
1236 if (ParseIdentifier(Name))
1237 return TokError("expected identifier in directive");
1238
1239 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001240 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001241
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001242 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001243 return TokError("unexpected token in '.type' directive");
1244 Lex();
1245
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001246 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001247 return TokError("expected '@' before type");
1248 Lex();
1249
1250 StringRef Type;
1251 SMLoc TypeLoc;
1252
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001253 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001254 if (ParseIdentifier(Type))
1255 return TokError("expected symbol type in directive");
1256
1257 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1258 .Case("function", MCSA_ELF_TypeFunction)
1259 .Case("object", MCSA_ELF_TypeObject)
1260 .Case("tls_object", MCSA_ELF_TypeTLS)
1261 .Case("common", MCSA_ELF_TypeCommon)
1262 .Case("notype", MCSA_ELF_TypeNoType)
1263 .Default(MCSA_Invalid);
1264
1265 if (Attr == MCSA_Invalid)
1266 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1267
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001268 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001269 return TokError("unexpected token in '.type' directive");
1270
1271 Lex();
1272
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001273 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001274
1275 return false;
1276}
1277
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001278/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001279/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1280bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001281 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001282 StringRef Name;
1283 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001284 return TokError("expected identifier in directive");
1285
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001286 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001287 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001288
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001289 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001290 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001292
1293 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001294 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001295 if (ParseAbsoluteExpression(Size))
1296 return true;
1297
1298 int64_t Pow2Alignment = 0;
1299 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001300 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001301 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001302 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001303 if (ParseAbsoluteExpression(Pow2Alignment))
1304 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001305
1306 // If this target takes alignments in bytes (not log) validate and convert.
1307 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1308 if (!isPowerOf2_64(Pow2Alignment))
1309 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1310 Pow2Alignment = Log2_64(Pow2Alignment);
1311 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001312 }
1313
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001314 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001315 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001316
Sean Callanan79ed1a82010-01-19 20:22:31 +00001317 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001318
Chris Lattner1fc3d752009-07-09 17:25:12 +00001319 // NOTE: a size of zero for a .comm should create a undefined symbol
1320 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001321 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001322 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1323 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001324
Eric Christopherc260a3e2010-05-14 01:38:54 +00001325 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001326 // may internally end up wanting an alignment in bytes.
1327 // FIXME: Diagnose overflow.
1328 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001329 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1330 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001331
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001332 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001333 return Error(IDLoc, "invalid symbol redefinition");
1334
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001335 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001336 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001337 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001338 getStreamer().EmitZerofill(Ctx.getMachOSection(
1339 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1340 0, SectionKind::getBSS()),
1341 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001342 return false;
1343 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001344
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001345 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001346 return false;
1347}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001348
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001349/// ParseDirectiveAbort
1350/// ::= .abort [ "abort_string" ]
1351bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001352 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001354
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001355 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1357 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001358 return TokError("expected string in '.abort' directive");
1359
Sean Callanan18b83232010-01-19 21:44:56 +00001360 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001361
Sean Callanan79ed1a82010-01-19 20:22:31 +00001362 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001363 }
1364
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001365 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001366 return TokError("unexpected token in '.abort' directive");
1367
Sean Callanan79ed1a82010-01-19 20:22:31 +00001368 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001369
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001370 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001371 if (Str.empty())
1372 Error(Loc, ".abort detected. Assembly stopping.");
1373 else
1374 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001375
1376 return false;
1377}
Kevin Enderby71148242009-07-14 21:35:03 +00001378
Kevin Enderby1f049b22009-07-14 23:21:55 +00001379/// ParseDirectiveInclude
1380/// ::= .include "filename"
1381bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001382 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001383 return TokError("expected string in '.include' directive");
1384
Sean Callanan18b83232010-01-19 21:44:56 +00001385 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001386 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001388
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001389 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001390 return TokError("unexpected token in '.include' directive");
1391
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001392 // Strip the quotes.
1393 Filename = Filename.substr(1, Filename.size()-2);
1394
1395 // Attempt to switch the lexer to the included file before consuming the end
1396 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001397 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001398 PrintMessage(IncludeLoc,
1399 "Could not find include file '" + Filename + "'",
1400 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001401 return true;
1402 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001403
1404 return false;
1405}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001406
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001407/// ParseDirectiveIf
1408/// ::= .if expression
1409bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001410 TheCondStack.push_back(TheCondState);
1411 TheCondState.TheCond = AsmCond::IfCond;
1412 if(TheCondState.Ignore) {
1413 EatToEndOfStatement();
1414 }
1415 else {
1416 int64_t ExprValue;
1417 if (ParseAbsoluteExpression(ExprValue))
1418 return true;
1419
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001420 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001421 return TokError("unexpected token in '.if' directive");
1422
Sean Callanan79ed1a82010-01-19 20:22:31 +00001423 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001424
1425 TheCondState.CondMet = ExprValue;
1426 TheCondState.Ignore = !TheCondState.CondMet;
1427 }
1428
1429 return false;
1430}
1431
1432/// ParseDirectiveElseIf
1433/// ::= .elseif expression
1434bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1435 if (TheCondState.TheCond != AsmCond::IfCond &&
1436 TheCondState.TheCond != AsmCond::ElseIfCond)
1437 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1438 " an .elseif");
1439 TheCondState.TheCond = AsmCond::ElseIfCond;
1440
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001441 bool LastIgnoreState = false;
1442 if (!TheCondStack.empty())
1443 LastIgnoreState = TheCondStack.back().Ignore;
1444 if (LastIgnoreState || TheCondState.CondMet) {
1445 TheCondState.Ignore = true;
1446 EatToEndOfStatement();
1447 }
1448 else {
1449 int64_t ExprValue;
1450 if (ParseAbsoluteExpression(ExprValue))
1451 return true;
1452
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001453 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001454 return TokError("unexpected token in '.elseif' directive");
1455
Sean Callanan79ed1a82010-01-19 20:22:31 +00001456 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001457 TheCondState.CondMet = ExprValue;
1458 TheCondState.Ignore = !TheCondState.CondMet;
1459 }
1460
1461 return false;
1462}
1463
1464/// ParseDirectiveElse
1465/// ::= .else
1466bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001467 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001468 return TokError("unexpected token in '.else' directive");
1469
Sean Callanan79ed1a82010-01-19 20:22:31 +00001470 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001471
1472 if (TheCondState.TheCond != AsmCond::IfCond &&
1473 TheCondState.TheCond != AsmCond::ElseIfCond)
1474 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1475 ".elseif");
1476 TheCondState.TheCond = AsmCond::ElseCond;
1477 bool LastIgnoreState = false;
1478 if (!TheCondStack.empty())
1479 LastIgnoreState = TheCondStack.back().Ignore;
1480 if (LastIgnoreState || TheCondState.CondMet)
1481 TheCondState.Ignore = true;
1482 else
1483 TheCondState.Ignore = false;
1484
1485 return false;
1486}
1487
1488/// ParseDirectiveEndIf
1489/// ::= .endif
1490bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001491 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001492 return TokError("unexpected token in '.endif' directive");
1493
Sean Callanan79ed1a82010-01-19 20:22:31 +00001494 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001495
1496 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1497 TheCondStack.empty())
1498 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1499 ".else");
1500 if (!TheCondStack.empty()) {
1501 TheCondState = TheCondStack.back();
1502 TheCondStack.pop_back();
1503 }
1504
1505 return false;
1506}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001507
1508/// ParseDirectiveFile
1509/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001510bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001511 // FIXME: I'm not sure what this is.
1512 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001513 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001514 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001515 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001516
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001517 if (FileNumber < 1)
1518 return TokError("file number less than one");
1519 }
1520
Daniel Dunbareceec052010-07-12 17:45:27 +00001521 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001522 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001523
Chris Lattnerd32e8032010-01-25 19:02:58 +00001524 StringRef Filename = getTok().getString();
1525 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001526 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001527
Daniel Dunbareceec052010-07-12 17:45:27 +00001528 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001529 return TokError("unexpected token in '.file' directive");
1530
Chris Lattnerd32e8032010-01-25 19:02:58 +00001531 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001532 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001533 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001534 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1535
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001536 return false;
1537}
1538
1539/// ParseDirectiveLine
1540/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001541bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001542 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1543 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001544 return TokError("unexpected token in '.line' directive");
1545
Sean Callanan18b83232010-01-19 21:44:56 +00001546 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001547 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001548 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001549
1550 // FIXME: Do something with the .line.
1551 }
1552
Daniel Dunbareceec052010-07-12 17:45:27 +00001553 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001554 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001555
1556 return false;
1557}
1558
1559
1560/// ParseDirectiveLoc
1561/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001562bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001563 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001564 return TokError("unexpected token in '.loc' directive");
1565
1566 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001567 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001568 (void) FileNumber;
1569 // FIXME: Validate file.
1570
Sean Callanan79ed1a82010-01-19 20:22:31 +00001571 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001572 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1573 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001574 return TokError("unexpected token in '.loc' directive");
1575
Sean Callanan18b83232010-01-19 21:44:56 +00001576 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001577 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001578 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001579
Daniel Dunbareceec052010-07-12 17:45:27 +00001580 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1581 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001582 return TokError("unexpected token in '.loc' directive");
1583
Sean Callanan18b83232010-01-19 21:44:56 +00001584 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001585 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001586 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001587
1588 // FIXME: Do something with the .loc.
1589 }
1590 }
1591
Daniel Dunbareceec052010-07-12 17:45:27 +00001592 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001593 return TokError("unexpected token in '.file' directive");
1594
1595 return false;
1596}
1597
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001598
1599/// \brief Create an MCAsmParser instance.
1600MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1601 MCContext &C, MCStreamer &Out,
1602 const MCAsmInfo &MAI) {
1603 return new AsmParser(T, SM, C, Out, MAI);
1604}