blob: 688827d2c76917c0f181e8757aca11e8940b69a7 [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 Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.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"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000030#include "llvm/Support/Compiler.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000031#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000032#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000033#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000036using namespace llvm;
37
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000038namespace {
39
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000040/// \brief Helper class for tracking macro definitions.
41struct Macro {
42 StringRef Name;
43 StringRef Body;
44
45public:
46 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
47};
48
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000049/// \brief Helper class for storing information about an active macro
50/// instantiation.
51struct MacroInstantiation {
52 /// The macro being instantiated.
53 const Macro *TheMacro;
54
55 /// The macro instantiation with substitutions.
56 MemoryBuffer *Instantiation;
57
58 /// The location of the instantiation.
59 SMLoc InstantiationLoc;
60
61 /// The location where parsing should resume upon instantiation completion.
62 SMLoc ExitLoc;
63
64public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000065 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
66 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000067};
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069/// \brief The concrete assembly parser instance.
70class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000071 friend class GenericAsmParser;
72
Daniel Dunbaraef87e32010-07-18 18:31:38 +000073 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
74 void operator=(const AsmParser &); // DO NOT IMPLEMENT
75private:
76 AsmLexer Lexer;
77 MCContext &Ctx;
78 MCStreamer &Out;
79 SourceMgr &SrcMgr;
80 MCAsmParserExtension *GenericParser;
81 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 /// This is the current buffer index we're lexing from as managed by the
84 /// SourceMgr object.
85 int CurBuffer;
86
87 AsmCond TheCondState;
88 std::vector<AsmCond> TheCondStack;
89
90 /// DirectiveMap - This is a table handlers for directives. Each handler is
91 /// invoked after the directive identifier is read and is responsible for
92 /// parsing and validating the rest of the directive. The handler is passed
93 /// in the directive name and the location of the directive keyword.
94 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000095
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000096 /// MacroMap - Map of currently defined macros.
97 StringMap<Macro*> MacroMap;
98
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000099 /// ActiveMacros - Stack of active macro instantiations.
100 std::vector<MacroInstantiation*> ActiveMacros;
101
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000102 /// Boolean tracking whether macro substitution is enabled.
103 unsigned MacrosEnabled : 1;
104
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000105 /// Flag tracking whether any errors have been encountered.
106 unsigned HadError : 1;
107
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000108public:
109 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
110 const MCAsmInfo &MAI);
111 ~AsmParser();
112
113 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
114
115 void AddDirectiveHandler(MCAsmParserExtension *Object,
116 StringRef Directive,
117 DirectiveHandler Handler) {
118 DirectiveMap[Directive] = std::make_pair(Object, Handler);
119 }
120
121public:
122 /// @name MCAsmParser Interface
123 /// {
124
125 virtual SourceMgr &getSourceManager() { return SrcMgr; }
126 virtual MCAsmLexer &getLexer() { return Lexer; }
127 virtual MCContext &getContext() { return Ctx; }
128 virtual MCStreamer &getStreamer() { return Out; }
129
130 virtual void Warning(SMLoc L, const Twine &Meg);
131 virtual bool Error(SMLoc L, const Twine &Msg);
132
133 const AsmToken &Lex();
134
135 bool ParseExpression(const MCExpr *&Res);
136 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseAbsoluteExpression(int64_t &Res);
139
140 /// }
141
142private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000143 void CheckForValidSection();
144
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 bool ParseStatement();
146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000147 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
148 void HandleMacroExit();
149
150 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000151 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
152 SrcMgr.PrintMessage(Loc, Msg, Type);
153 }
154
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000155 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
156 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000157
158 /// \brief Reset the current lexer position to that given by \arg Loc. The
159 /// current token is not set; clients should ensure Lex() is called
160 /// subsequently.
161 void JumpToLoc(SMLoc Loc);
162
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000164
165 /// \brief Parse up to the end of statement and a return the contents from the
166 /// current token until the end of the statement; the current token on exit
167 /// will be either the EndOfStatement or EOF.
168 StringRef ParseStringToEndOfStatement();
169
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000170 bool ParseAssignment(StringRef Name);
171
172 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
175
176 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
177 /// and set \arg Res to the identifier contents.
178 bool ParseIdentifier(StringRef &Res);
179
180 // Directive Parsing.
181 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
182 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000183 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseDirectiveFill(); // ".fill"
185 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000186 bool ParseDirectiveZero(); // ".zero"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187 bool ParseDirectiveSet(); // ".set"
188 bool ParseDirectiveOrg(); // ".org"
189 // ".align{,32}", ".p2align{,w,l}"
190 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
191
192 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
193 /// accepts a single symbol (which should be a label or an external).
194 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
195 bool ParseDirectiveELFType(); // ELF specific ".type"
196
197 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
198
199 bool ParseDirectiveAbort(); // ".abort"
200 bool ParseDirectiveInclude(); // ".include"
201
202 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
203 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
204 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
205 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
206
207 /// ParseEscapedString - Parse the current token as a string which may include
208 /// escaped characters and return the string contents.
209 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000210
211 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
212 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213};
214
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000215/// \brief Generic implementations of directive handling, etc. which is shared
216/// (or the default, at least) for all assembler parser.
217class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000218 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
219 void AddDirectiveHandler(StringRef Directive) {
220 getParser().AddDirectiveHandler(this, Directive,
221 HandleDirective<GenericAsmParser, Handler>);
222 }
223
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000224public:
225 GenericAsmParser() {}
226
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000227 AsmParser &getParser() {
228 return (AsmParser&) this->MCAsmParserExtension::getParser();
229 }
230
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000231 virtual void Initialize(MCAsmParser &Parser) {
232 // Call the base implementation.
233 this->MCAsmParserExtension::Initialize(Parser);
234
235 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000236 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000239
240 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000241 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
242 ".macros_on");
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
244 ".macros_off");
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000248
249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000251 }
252
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000253 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
254 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
255 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000256
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000258 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
259 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000260
261 void ParseUleb128(uint64_t Value);
262 void ParseSleb128(int64_t Value);
263 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000264};
265
266}
267
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000268namespace llvm {
269
270extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000271extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000272
273}
274
Chris Lattneraaec2052010-01-19 19:46:13 +0000275enum { DEFAULT_ADDRSPACE = 0 };
276
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000277AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
278 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000279 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000280 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000281 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000282 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000283
284 // Initialize the generic parser.
285 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000286
287 // Initialize the platform / file format parser.
288 //
289 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
290 // created.
291 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000292 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000293 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000294 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000295 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000296 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000297 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000298}
299
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000300AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000301 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
302
303 // Destroy any macros.
304 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
305 ie = MacroMap.end(); it != ie; ++it)
306 delete it->getValue();
307
Daniel Dunbare4749702010-07-12 18:12:02 +0000308 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000309 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000310}
311
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000312void AsmParser::PrintMacroInstantiations() {
313 // Print the active macro instantiation stack.
314 for (std::vector<MacroInstantiation*>::const_reverse_iterator
315 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
316 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
317 "note");
318}
319
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000320void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000321 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000322 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000323}
324
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000325bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000326 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000327 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000328 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000329 return true;
330}
331
Sean Callananfd0b0282010-01-21 00:19:58 +0000332bool AsmParser::EnterIncludeFile(const std::string &Filename) {
333 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
334 if (NewBuf == -1)
335 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000336
Sean Callananfd0b0282010-01-21 00:19:58 +0000337 CurBuffer = NewBuf;
338
339 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
340
341 return false;
342}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000343
344void AsmParser::JumpToLoc(SMLoc Loc) {
345 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
346 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
347}
348
Sean Callananfd0b0282010-01-21 00:19:58 +0000349const AsmToken &AsmParser::Lex() {
350 const AsmToken *tok = &Lexer.Lex();
351
352 if (tok->is(AsmToken::Eof)) {
353 // If this is the end of an included file, pop the parent file off the
354 // include stack.
355 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
356 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000357 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000358 tok = &Lexer.Lex();
359 }
360 }
361
362 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000363 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000364
Sean Callananfd0b0282010-01-21 00:19:58 +0000365 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000366}
367
Chris Lattner79180e22010-04-05 23:15:42 +0000368bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000369 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000370 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000371 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000372
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000373 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000374 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000375
376 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000377 AsmCond StartingCondState = TheCondState;
378
Chris Lattnerb717fb02009-07-02 21:53:43 +0000379 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000381 if (!ParseStatement()) continue;
382
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000383 // We had an error, validate that one was emitted and recover by skipping to
384 // the next line.
385 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000386 EatToEndOfStatement();
387 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000388
389 if (TheCondState.TheCond != StartingCondState.TheCond ||
390 TheCondState.Ignore != StartingCondState.Ignore)
391 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000392
393 // Check to see there are no empty DwarfFile slots.
394 const std::vector<MCDwarfFile *> &MCDwarfFiles =
395 getContext().getMCDwarfFiles();
396 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000397 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000398 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000399 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000400
Chris Lattner79180e22010-04-05 23:15:42 +0000401 // Finalize the output stream if there are no errors and if the client wants
402 // us to.
403 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000404 Out.Finish();
405
Chris Lattnerb717fb02009-07-02 21:53:43 +0000406 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000407}
408
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000409void AsmParser::CheckForValidSection() {
410 if (!getStreamer().getCurrentSection()) {
411 TokError("expected section directive before assembly directive");
412 Out.SwitchSection(Ctx.getMachOSection(
413 "__TEXT", "__text",
414 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
415 0, SectionKind::getText()));
416 }
417}
418
Chris Lattner2cf5f142009-06-22 01:29:09 +0000419/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
420void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000421 while (Lexer.isNot(AsmToken::EndOfStatement) &&
422 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000423 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000424
425 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000426 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000427 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000428}
429
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000430StringRef AsmParser::ParseStringToEndOfStatement() {
431 const char *Start = getTok().getLoc().getPointer();
432
433 while (Lexer.isNot(AsmToken::EndOfStatement) &&
434 Lexer.isNot(AsmToken::Eof))
435 Lex();
436
437 const char *End = getTok().getLoc().getPointer();
438 return StringRef(Start, End - Start);
439}
Chris Lattnerc4193832009-06-22 05:51:26 +0000440
Chris Lattner74ec1a32009-06-22 06:32:03 +0000441/// ParseParenExpr - Parse a paren expression and return it.
442/// NOTE: This assumes the leading '(' has already been consumed.
443///
444/// parenexpr ::= expr)
445///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000446bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000447 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000449 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000450 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000451 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000452 return false;
453}
Chris Lattnerc4193832009-06-22 05:51:26 +0000454
Chris Lattner74ec1a32009-06-22 06:32:03 +0000455/// ParsePrimaryExpr - Parse a primary expression and return it.
456/// primaryexpr ::= (parenexpr
457/// primaryexpr ::= symbol
458/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000459/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000460/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000461bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000462 switch (Lexer.getKind()) {
463 default:
464 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000465 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000466 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000467 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000468 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000469 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000470 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000471 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000472 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000473 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000474 EndLoc = Lexer.getLoc();
475
476 StringRef Identifier;
477 if (ParseIdentifier(Identifier))
478 return false;
479
Daniel Dunbarfffff912009-10-16 01:34:54 +0000480 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000481 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000482 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000483
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000484 // Mark the symbol as used in an expression.
485 Sym->setUsedInExpr(true);
486
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000487 // Lookup the symbol variant if used.
488 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000489 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000490 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000491 if (Variant == MCSymbolRefExpr::VK_Invalid) {
492 Variant = MCSymbolRefExpr::VK_None;
493 TokError("invalid variant '" + Split.second + "'");
494 }
495 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000496
Daniel Dunbarfffff912009-10-16 01:34:54 +0000497 // If this is an absolute variable reference, substitute it now to preserve
498 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000499 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000500 if (Variant)
501 return Error(EndLoc, "unexpected modified on variable reference");
502
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000503 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000504 return false;
505 }
506
507 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000508 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000509 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000510 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000511 case AsmToken::Integer: {
512 SMLoc Loc = getTok().getLoc();
513 int64_t IntVal = getTok().getIntVal();
514 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000515 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000516 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000517 // Look for 'b' or 'f' following an Integer as a directional label
518 if (Lexer.getKind() == AsmToken::Identifier) {
519 StringRef IDVal = getTok().getString();
520 if (IDVal == "f" || IDVal == "b"){
521 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
522 IDVal == "f" ? 1 : 0);
523 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
524 getContext());
525 if(IDVal == "b" && Sym->isUndefined())
526 return Error(Loc, "invalid reference to undefined symbol");
527 EndLoc = Lexer.getLoc();
528 Lex(); // Eat identifier.
529 }
530 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000531 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000532 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000533 case AsmToken::Dot: {
534 // This is a '.' reference, which references the current PC. Emit a
535 // temporary label to the streamer and refer to it.
536 MCSymbol *Sym = Ctx.CreateTempSymbol();
537 Out.EmitLabel(Sym);
538 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
539 EndLoc = Lexer.getLoc();
540 Lex(); // Eat identifier.
541 return false;
542 }
543
Daniel Dunbar3f872332009-07-28 16:08:33 +0000544 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000545 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000546 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000547 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000548 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000549 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000550 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000551 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000552 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000553 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000554 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000555 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000556 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000557 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000558 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000559 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000560 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000561 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000562 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000563 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000564 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000565 }
566}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000567
Chris Lattnerb4307b32010-01-15 19:28:38 +0000568bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000569 SMLoc EndLoc;
570 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000571}
572
Daniel Dunbarcceba832010-09-17 02:47:07 +0000573const MCExpr *
574AsmParser::ApplyModifierToExpr(const MCExpr *E,
575 MCSymbolRefExpr::VariantKind Variant) {
576 // Recurse over the given expression, rebuilding it to apply the given variant
577 // if there is exactly one symbol.
578 switch (E->getKind()) {
579 case MCExpr::Target:
580 case MCExpr::Constant:
581 return 0;
582
583 case MCExpr::SymbolRef: {
584 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
585
586 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
587 TokError("invalid variant on expression '" +
588 getTok().getIdentifier() + "' (already modified)");
589 return E;
590 }
591
592 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
593 }
594
595 case MCExpr::Unary: {
596 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
597 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
598 if (!Sub)
599 return 0;
600 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
601 }
602
603 case MCExpr::Binary: {
604 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
605 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
606 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
607
608 if (!LHS && !RHS)
609 return 0;
610
611 if (!LHS) LHS = BE->getLHS();
612 if (!RHS) RHS = BE->getRHS();
613
614 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
615 }
616 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000617
618 assert(0 && "Invalid expression kind!");
619 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000620}
621
Chris Lattner74ec1a32009-06-22 06:32:03 +0000622/// ParseExpression - Parse an expression and return it.
623///
624/// expr ::= expr +,- expr -> lowest.
625/// expr ::= expr |,^,&,! expr -> middle.
626/// expr ::= expr *,/,%,<<,>> expr -> highest.
627/// expr ::= primaryexpr
628///
Chris Lattner54482b42010-01-15 19:39:23 +0000629bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000630 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000631 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000632 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
633 return true;
634
Daniel Dunbarcceba832010-09-17 02:47:07 +0000635 // As a special case, we support 'a op b @ modifier' by rewriting the
636 // expression to include the modifier. This is inefficient, but in general we
637 // expect users to use 'a@modifier op b'.
638 if (Lexer.getKind() == AsmToken::At) {
639 Lex();
640
641 if (Lexer.isNot(AsmToken::Identifier))
642 return TokError("unexpected symbol modifier following '@'");
643
644 MCSymbolRefExpr::VariantKind Variant =
645 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
646 if (Variant == MCSymbolRefExpr::VK_Invalid)
647 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
648
649 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
650 if (!ModifiedRes) {
651 return TokError("invalid modifier '" + getTok().getIdentifier() +
652 "' (no symbols present)");
653 return true;
654 }
655
656 Res = ModifiedRes;
657 Lex();
658 }
659
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000660 // Try to constant fold it up front, if possible.
661 int64_t Value;
662 if (Res->EvaluateAsAbsolute(Value))
663 Res = MCConstantExpr::Create(Value, getContext());
664
665 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000666}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000667
Chris Lattnerb4307b32010-01-15 19:28:38 +0000668bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000669 Res = 0;
670 return ParseParenExpr(Res, EndLoc) ||
671 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000672}
673
Daniel Dunbar475839e2009-06-29 20:37:27 +0000674bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000675 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000677 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000678 if (ParseExpression(Expr))
679 return true;
680
Daniel Dunbare00b0112009-10-16 01:57:52 +0000681 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000682 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000683
684 return false;
685}
686
Daniel Dunbar3f872332009-07-28 16:08:33 +0000687static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000688 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000689 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000690 default:
691 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000692
Daniel Dunbarcceba832010-09-17 02:47:07 +0000693 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000694 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000695 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000696 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000697 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000698 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000699 return 1;
700
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000701
702 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000703 //
704 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000705 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000706 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000707 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000708 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000709 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000710 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000711 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000712 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000713 return 2;
714
715 // Intermediate Precedence: +, -, ==, !=, <>, <, <=, >, >=
716 case AsmToken::Plus:
717 Kind = MCBinaryExpr::Add;
718 return 3;
719 case AsmToken::Minus:
720 Kind = MCBinaryExpr::Sub;
721 return 3;
722 case AsmToken::EqualEqual:
723 Kind = MCBinaryExpr::EQ;
724 return 3;
725 case AsmToken::ExclaimEqual:
726 case AsmToken::LessGreater:
727 Kind = MCBinaryExpr::NE;
728 return 3;
729 case AsmToken::Less:
730 Kind = MCBinaryExpr::LT;
731 return 3;
732 case AsmToken::LessEqual:
733 Kind = MCBinaryExpr::LTE;
734 return 3;
735 case AsmToken::Greater:
736 Kind = MCBinaryExpr::GT;
737 return 3;
738 case AsmToken::GreaterEqual:
739 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000740 return 3;
741
742 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000743 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000744 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000745 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000746 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000747 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000748 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000749 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000750 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000751 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000752 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000753 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000754 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000755 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000756 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000757 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000758 }
759}
760
761
762/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
763/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000764bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
765 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000766 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000767 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000768 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000769
770 // If the next token is lower precedence than we are allowed to eat, return
771 // successfully with what we ate already.
772 if (TokPrec < Precedence)
773 return false;
774
Sean Callanan79ed1a82010-01-19 20:22:31 +0000775 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000776
777 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000778 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000779 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000780
781 // If BinOp binds less tightly with RHS than the operator after RHS, let
782 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000783 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000784 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000785 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000786 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000787 }
788
Daniel Dunbar475839e2009-06-29 20:37:27 +0000789 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000790 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000791 }
792}
793
Chris Lattnerc4193832009-06-22 05:51:26 +0000794
795
796
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000797/// ParseStatement:
798/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000799/// ::= Label* Directive ...Operands... EndOfStatement
800/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000801bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000802 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000803 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000804 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000805 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000806 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000807
808 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000809 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000810 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000811 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000812 int64_t LocalLabelVal = -1;
813 // GUESS allow an integer followed by a ':' as a directional local label
814 if (Lexer.is(AsmToken::Integer)) {
815 LocalLabelVal = getTok().getIntVal();
816 if (LocalLabelVal < 0) {
817 if (!TheCondState.Ignore)
818 return TokError("unexpected token at start of statement");
819 IDVal = "";
820 }
821 else {
822 IDVal = getTok().getString();
823 Lex(); // Consume the integer token to be used as an identifier token.
824 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000825 if (!TheCondState.Ignore)
826 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000827 }
828 }
829 }
830 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000831 if (!TheCondState.Ignore)
832 return TokError("unexpected token at start of statement");
833 IDVal = "";
834 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000835
Chris Lattner7834fac2010-04-17 18:14:27 +0000836 // Handle conditional assembly here before checking for skipping. We
837 // have to do this so that .endif isn't skipped in a ".if 0" block for
838 // example.
839 if (IDVal == ".if")
840 return ParseDirectiveIf(IDLoc);
841 if (IDVal == ".elseif")
842 return ParseDirectiveElseIf(IDLoc);
843 if (IDVal == ".else")
844 return ParseDirectiveElse(IDLoc);
845 if (IDVal == ".endif")
846 return ParseDirectiveEndIf(IDLoc);
847
848 // If we are in a ".if 0" block, ignore this statement.
849 if (TheCondState.Ignore) {
850 EatToEndOfStatement();
851 return false;
852 }
853
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000854 // FIXME: Recurse on local labels?
855
856 // See what kind of statement we have.
857 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000858 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000859 CheckForValidSection();
860
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000861 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000862 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000863
864 // Diagnose attempt to use a variable as a label.
865 //
866 // FIXME: Diagnostics. Note the location of the definition as a label.
867 // FIXME: This doesn't diagnose assignment to a symbol which has been
868 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000869 MCSymbol *Sym;
870 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000871 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000872 else
873 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000874 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000875 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000876
Daniel Dunbar959fd882009-08-26 22:13:22 +0000877 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000878 Out.EmitLabel(Sym);
879
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000880 // Consume any end of statement token, if present, to avoid spurious
881 // AddBlankLine calls().
882 if (Lexer.is(AsmToken::EndOfStatement)) {
883 Lex();
884 if (Lexer.is(AsmToken::Eof))
885 return false;
886 }
887
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000888 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000889 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000890
Daniel Dunbar3f872332009-07-28 16:08:33 +0000891 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000892 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000893 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000894
Daniel Dunbare2ace502009-08-31 08:09:09 +0000895 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000896
897 default: // Normal instruction or directive.
898 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000899 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000900
901 // If macros are enabled, check to see if this is a macro instantiation.
902 if (MacrosEnabled)
903 if (const Macro *M = MacroMap.lookup(IDVal))
904 return HandleMacroEntry(IDVal, IDLoc, M);
905
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000906 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000907 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000908 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000909 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000910 return ParseDirectiveSet();
911
Daniel Dunbara0d14262009-06-24 23:30:00 +0000912 // Data directives
913
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000914 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000915 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000916 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000917 return ParseDirectiveAscii(true);
918
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000919 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000920 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000921 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000922 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000923 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000924 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000925 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000926 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000927 if (IDVal == ".single")
928 return ParseDirectiveRealValue(APFloat::IEEEsingle);
929 if (IDVal == ".double")
930 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000931
Eli Friedman5d68ec22010-07-19 04:17:25 +0000932 if (IDVal == ".align") {
933 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
934 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
935 }
936 if (IDVal == ".align32") {
937 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
938 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
939 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000940 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000941 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000942 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000943 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000944 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000945 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000946 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000947 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000948 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000949 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000950 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000951 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
952
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000953 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000954 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000955
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000956 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000957 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000958 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000960 if (IDVal == ".zero")
961 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000962
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000963 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000964
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000965 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000966 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000967 // ELF only? Should it be here?
968 if (IDVal == ".local")
969 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000970 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000971 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000972 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000973 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000974 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000975 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000976 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000977 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000978 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000979 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000980 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000981 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000982 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000983 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000984 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000985 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000986 if (IDVal == ".type")
987 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000988 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000989 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000990 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000991 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000992 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000993 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000994 if (IDVal == ".weak_def_can_be_hidden")
995 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000996
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000997 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000998 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000999 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001000 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001001
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001002 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001003 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001004 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001005 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001006
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001007 // Look up the handler in the handler table.
1008 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1009 DirectiveMap.lookup(IDVal);
1010 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001011 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001012
Kevin Enderby9c656452009-09-10 20:51:44 +00001013 // Target hook for parsing target specific directives.
1014 if (!getTargetParser().ParseDirective(ID))
1015 return false;
1016
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001017 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001018 EatToEndOfStatement();
1019 return false;
1020 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001021
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001022 CheckForValidSection();
1023
Chris Lattnera7f13542010-05-19 23:34:33 +00001024 // Canonicalize the opcode to lower case.
1025 SmallString<128> Opcode;
1026 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1027 Opcode.push_back(tolower(IDVal[i]));
1028
Chris Lattner98986712010-01-14 22:21:20 +00001029 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001030 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001031 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001032
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001033 // Dump the parsed representation, if requested.
1034 if (getShowParsedOperands()) {
1035 SmallString<256> Str;
1036 raw_svector_ostream OS(Str);
1037 OS << "parsed instruction: [";
1038 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1039 if (i != 0)
1040 OS << ", ";
1041 ParsedOperands[i]->dump(OS);
1042 }
1043 OS << "]";
1044
1045 PrintMessage(IDLoc, OS.str(), "note");
1046 }
1047
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001048 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001049 if (!HadError)
1050 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1051 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001052
Chris Lattner98986712010-01-14 22:21:20 +00001053 // Free any parsed operands.
1054 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1055 delete ParsedOperands[i];
1056
Chris Lattnercbf8a982010-09-11 16:18:25 +00001057 // Don't skip the rest of the line, the instruction parser is responsible for
1058 // that.
1059 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001060}
Chris Lattner9a023f72009-06-24 04:43:34 +00001061
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001062MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1063 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001064 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1065{
1066 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1067 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001068 SmallString<256> Buf;
1069 raw_svector_ostream OS(Buf);
1070
1071 StringRef Body = M->Body;
1072 while (!Body.empty()) {
1073 // Scan for the next substitution.
1074 std::size_t End = Body.size(), Pos = 0;
1075 for (; Pos != End; ++Pos) {
1076 // Check for a substitution or escape.
1077 if (Body[Pos] != '$' || Pos + 1 == End)
1078 continue;
1079
1080 char Next = Body[Pos + 1];
1081 if (Next == '$' || Next == 'n' || isdigit(Next))
1082 break;
1083 }
1084
1085 // Add the prefix.
1086 OS << Body.slice(0, Pos);
1087
1088 // Check if we reached the end.
1089 if (Pos == End)
1090 break;
1091
1092 switch (Body[Pos+1]) {
1093 // $$ => $
1094 case '$':
1095 OS << '$';
1096 break;
1097
1098 // $n => number of arguments
1099 case 'n':
1100 OS << A.size();
1101 break;
1102
1103 // $[0-9] => argument
1104 default: {
1105 // Missing arguments are ignored.
1106 unsigned Index = Body[Pos+1] - '0';
1107 if (Index >= A.size())
1108 break;
1109
1110 // Otherwise substitute with the token values, with spaces eliminated.
1111 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1112 ie = A[Index].end(); it != ie; ++it)
1113 OS << it->getString();
1114 break;
1115 }
1116 }
1117
1118 // Update the scan point.
1119 Body = Body.substr(Pos + 2);
1120 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001121
1122 // We include the .endmacro in the buffer as our queue to exit the macro
1123 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001124 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001125
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001126 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001127}
1128
1129bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1130 const Macro *M) {
1131 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1132 // this, although we should protect against infinite loops.
1133 if (ActiveMacros.size() == 20)
1134 return TokError("macros cannot be nested more than 20 levels deep");
1135
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001136 // Parse the macro instantiation arguments.
1137 std::vector<std::vector<AsmToken> > MacroArguments;
1138 MacroArguments.push_back(std::vector<AsmToken>());
1139 unsigned ParenLevel = 0;
1140 for (;;) {
1141 if (Lexer.is(AsmToken::Eof))
1142 return TokError("unexpected token in macro instantiation");
1143 if (Lexer.is(AsmToken::EndOfStatement))
1144 break;
1145
1146 // If we aren't inside parentheses and this is a comma, start a new token
1147 // list.
1148 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1149 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001150 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001151 // Adjust the current parentheses level.
1152 if (Lexer.is(AsmToken::LParen))
1153 ++ParenLevel;
1154 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1155 --ParenLevel;
1156
1157 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001158 MacroArguments.back().push_back(getTok());
1159 }
1160 Lex();
1161 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001162
1163 // Create the macro instantiation object and add to the current macro
1164 // instantiation stack.
1165 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001166 getTok().getLoc(),
1167 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001168 ActiveMacros.push_back(MI);
1169
1170 // Jump to the macro instantiation and prime the lexer.
1171 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1172 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1173 Lex();
1174
1175 return false;
1176}
1177
1178void AsmParser::HandleMacroExit() {
1179 // Jump to the EndOfStatement we should return to, and consume it.
1180 JumpToLoc(ActiveMacros.back()->ExitLoc);
1181 Lex();
1182
1183 // Pop the instantiation entry.
1184 delete ActiveMacros.back();
1185 ActiveMacros.pop_back();
1186}
1187
Benjamin Kramer38e59892010-07-14 22:38:02 +00001188bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001189 // FIXME: Use better location, we should use proper tokens.
1190 SMLoc EqualLoc = Lexer.getLoc();
1191
Daniel Dunbar821e3332009-08-31 08:09:28 +00001192 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001193 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001194 return true;
1195
Daniel Dunbar3f872332009-07-28 16:08:33 +00001196 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001197 return TokError("unexpected token in assignment");
1198
1199 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001200 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001201
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001202 // Validate that the LHS is allowed to be a variable (either it has not been
1203 // used as a symbol, or it is an absolute symbol).
1204 MCSymbol *Sym = getContext().LookupSymbol(Name);
1205 if (Sym) {
1206 // Diagnose assignment to a label.
1207 //
1208 // FIXME: Diagnostics. Note the location of the definition as a label.
1209 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001210 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1211 ; // Allow redefinitions of undefined symbols only used in directives.
1212 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001213 return Error(EqualLoc, "redefinition of '" + Name + "'");
1214 else if (!Sym->isVariable())
1215 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001216 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001217 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1218 Name + "'");
1219 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001220 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001221
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001222 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001223
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001224 Sym->setUsedInExpr(true);
1225
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001226 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001227 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001228
1229 return false;
1230}
1231
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001232/// ParseIdentifier:
1233/// ::= identifier
1234/// ::= string
1235bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001236 // The assembler has relaxed rules for accepting identifiers, in particular we
1237 // allow things like '.globl $foo', which would normally be separate
1238 // tokens. At this level, we have already lexed so we cannot (currently)
1239 // handle this as a context dependent token, instead we detect adjacent tokens
1240 // and return the combined identifier.
1241 if (Lexer.is(AsmToken::Dollar)) {
1242 SMLoc DollarLoc = getLexer().getLoc();
1243
1244 // Consume the dollar sign, and check for a following identifier.
1245 Lex();
1246 if (Lexer.isNot(AsmToken::Identifier))
1247 return true;
1248
1249 // We have a '$' followed by an identifier, make sure they are adjacent.
1250 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1251 return true;
1252
1253 // Construct the joined identifier and consume the token.
1254 Res = StringRef(DollarLoc.getPointer(),
1255 getTok().getIdentifier().size() + 1);
1256 Lex();
1257 return false;
1258 }
1259
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001260 if (Lexer.isNot(AsmToken::Identifier) &&
1261 Lexer.isNot(AsmToken::String))
1262 return true;
1263
Sean Callanan18b83232010-01-19 21:44:56 +00001264 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001265
Sean Callanan79ed1a82010-01-19 20:22:31 +00001266 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001267
1268 return false;
1269}
1270
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001271/// ParseDirectiveSet:
1272/// ::= .set identifier ',' expression
1273bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001274 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001275
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001276 if (ParseIdentifier(Name))
1277 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001278
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001279 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001280 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001281 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001282
Daniel Dunbare2ace502009-08-31 08:09:09 +00001283 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001284}
1285
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001286bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001287 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001288
1289 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001290 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001291 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1292 if (Str[i] != '\\') {
1293 Data += Str[i];
1294 continue;
1295 }
1296
1297 // Recognize escaped characters. Note that this escape semantics currently
1298 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1299 ++i;
1300 if (i == e)
1301 return TokError("unexpected backslash at end of string");
1302
1303 // Recognize octal sequences.
1304 if ((unsigned) (Str[i] - '0') <= 7) {
1305 // Consume up to three octal characters.
1306 unsigned Value = Str[i] - '0';
1307
1308 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1309 ++i;
1310 Value = Value * 8 + (Str[i] - '0');
1311
1312 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1313 ++i;
1314 Value = Value * 8 + (Str[i] - '0');
1315 }
1316 }
1317
1318 if (Value > 255)
1319 return TokError("invalid octal escape sequence (out of range)");
1320
1321 Data += (unsigned char) Value;
1322 continue;
1323 }
1324
1325 // Otherwise recognize individual escapes.
1326 switch (Str[i]) {
1327 default:
1328 // Just reject invalid escape sequences for now.
1329 return TokError("invalid escape sequence (unrecognized character)");
1330
1331 case 'b': Data += '\b'; break;
1332 case 'f': Data += '\f'; break;
1333 case 'n': Data += '\n'; break;
1334 case 'r': Data += '\r'; break;
1335 case 't': Data += '\t'; break;
1336 case '"': Data += '"'; break;
1337 case '\\': Data += '\\'; break;
1338 }
1339 }
1340
1341 return false;
1342}
1343
Daniel Dunbara0d14262009-06-24 23:30:00 +00001344/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001345/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001346bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001347 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001348 CheckForValidSection();
1349
Daniel Dunbara0d14262009-06-24 23:30:00 +00001350 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001351 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001352 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001354 std::string Data;
1355 if (ParseEscapedString(Data))
1356 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001357
1358 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001359 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001360 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1361
Sean Callanan79ed1a82010-01-19 20:22:31 +00001362 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001363
1364 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001365 break;
1366
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001367 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001368 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001369 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001370 }
1371 }
1372
Sean Callanan79ed1a82010-01-19 20:22:31 +00001373 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001374 return false;
1375}
1376
1377/// ParseDirectiveValue
1378/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1379bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001380 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001381 CheckForValidSection();
1382
Daniel Dunbara0d14262009-06-24 23:30:00 +00001383 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001384 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001385 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001386 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001387 return true;
1388
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001389 // Special case constant expressions to match code generator.
1390 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001391 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001392 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001393 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001394
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001395 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001396 break;
1397
1398 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001400 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001401 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001402 }
1403 }
1404
Sean Callanan79ed1a82010-01-19 20:22:31 +00001405 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001406 return false;
1407}
1408
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001409/// ParseDirectiveRealValue
1410/// ::= (.single | .double) [ expression (, expression)* ]
1411bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1412 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1413 CheckForValidSection();
1414
1415 for (;;) {
1416 // We don't truly support arithmetic on floating point expressions, so we
1417 // have to manually parse unary prefixes.
1418 bool IsNeg = false;
1419 if (getLexer().is(AsmToken::Minus)) {
1420 Lex();
1421 IsNeg = true;
1422 } else if (getLexer().is(AsmToken::Plus))
1423 Lex();
1424
1425 if (getLexer().isNot(AsmToken::Integer) &&
1426 getLexer().isNot(AsmToken::Real))
1427 return TokError("unexpected token in directive");
1428
1429 // Convert to an APFloat.
1430 APFloat Value(Semantics);
1431 if (Value.convertFromString(getTok().getString(),
1432 APFloat::rmNearestTiesToEven) ==
1433 APFloat::opInvalidOp)
1434 return TokError("invalid floating point literal");
1435 if (IsNeg)
1436 Value.changeSign();
1437
1438 // Consume the numeric token.
1439 Lex();
1440
1441 // Emit the value as an integer.
1442 APInt AsInt = Value.bitcastToAPInt();
1443 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1444 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1445
1446 if (getLexer().is(AsmToken::EndOfStatement))
1447 break;
1448
1449 if (getLexer().isNot(AsmToken::Comma))
1450 return TokError("unexpected token in directive");
1451 Lex();
1452 }
1453 }
1454
1455 Lex();
1456 return false;
1457}
1458
Daniel Dunbara0d14262009-06-24 23:30:00 +00001459/// ParseDirectiveSpace
1460/// ::= .space expression [ , expression ]
1461bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001462 CheckForValidSection();
1463
Daniel Dunbara0d14262009-06-24 23:30:00 +00001464 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001465 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001466 return true;
1467
1468 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001469 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1470 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001471 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001472 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001473
Daniel Dunbar475839e2009-06-29 20:37:27 +00001474 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001475 return true;
1476
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001478 return TokError("unexpected token in '.space' directive");
1479 }
1480
Sean Callanan79ed1a82010-01-19 20:22:31 +00001481 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001482
1483 if (NumBytes <= 0)
1484 return TokError("invalid number of bytes in '.space' directive");
1485
1486 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001487 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001488
1489 return false;
1490}
1491
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001492/// ParseDirectiveZero
1493/// ::= .zero expression
1494bool AsmParser::ParseDirectiveZero() {
1495 CheckForValidSection();
1496
1497 int64_t NumBytes;
1498 if (ParseAbsoluteExpression(NumBytes))
1499 return true;
1500
1501 if (getLexer().isNot(AsmToken::EndOfStatement))
1502 return TokError("unexpected token in '.zero' directive");
1503
1504 Lex();
1505
1506 getStreamer().EmitFill(NumBytes, 0, DEFAULT_ADDRSPACE);
1507
1508 return false;
1509}
1510
Daniel Dunbara0d14262009-06-24 23:30:00 +00001511/// ParseDirectiveFill
1512/// ::= .fill expression , expression , expression
1513bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001514 CheckForValidSection();
1515
Daniel Dunbara0d14262009-06-24 23:30:00 +00001516 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001517 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001518 return true;
1519
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001520 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001521 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001522 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001523
1524 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001525 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001526 return true;
1527
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001528 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001529 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001530 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001531
1532 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001533 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001534 return true;
1535
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001536 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001537 return TokError("unexpected token in '.fill' directive");
1538
Sean Callanan79ed1a82010-01-19 20:22:31 +00001539 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001540
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001541 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1542 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001543
1544 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001545 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001546
1547 return false;
1548}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001549
1550/// ParseDirectiveOrg
1551/// ::= .org expression [ , expression ]
1552bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001553 CheckForValidSection();
1554
Daniel Dunbar821e3332009-08-31 08:09:28 +00001555 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001556 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001557 return true;
1558
1559 // Parse optional fill expression.
1560 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1562 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001563 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001564 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001565
Daniel Dunbar475839e2009-06-29 20:37:27 +00001566 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001567 return true;
1568
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001569 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001570 return TokError("unexpected token in '.org' directive");
1571 }
1572
Sean Callanan79ed1a82010-01-19 20:22:31 +00001573 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001574
1575 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1576 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001577 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001578
1579 return false;
1580}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001581
1582/// ParseDirectiveAlign
1583/// ::= {.align, ...} expression [ , expression [ , expression ]]
1584bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001585 CheckForValidSection();
1586
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001587 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001588 int64_t Alignment;
1589 if (ParseAbsoluteExpression(Alignment))
1590 return true;
1591
1592 SMLoc MaxBytesLoc;
1593 bool HasFillExpr = false;
1594 int64_t FillExpr = 0;
1595 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001596 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1597 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001598 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001599 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001600
1601 // The fill expression can be omitted while specifying a maximum number of
1602 // alignment bytes, e.g:
1603 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001604 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001605 HasFillExpr = true;
1606 if (ParseAbsoluteExpression(FillExpr))
1607 return true;
1608 }
1609
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1611 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001612 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001613 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001614
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001615 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001616 if (ParseAbsoluteExpression(MaxBytesToFill))
1617 return true;
1618
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001619 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001620 return TokError("unexpected token in directive");
1621 }
1622 }
1623
Sean Callanan79ed1a82010-01-19 20:22:31 +00001624 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001625
Daniel Dunbar648ac512010-05-17 21:54:30 +00001626 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001627 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001628
1629 // Compute alignment in bytes.
1630 if (IsPow2) {
1631 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001632 if (Alignment >= 32) {
1633 Error(AlignmentLoc, "invalid alignment value");
1634 Alignment = 31;
1635 }
1636
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001637 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001638 }
1639
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001640 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001641 if (MaxBytesLoc.isValid()) {
1642 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001643 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1644 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001645 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001646 }
1647
1648 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001649 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1650 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001651 MaxBytesToFill = 0;
1652 }
1653 }
1654
Daniel Dunbar648ac512010-05-17 21:54:30 +00001655 // Check whether we should use optimal code alignment for this .align
1656 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001657 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001658 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1659 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001660 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001661 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001662 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001663 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1664 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001665 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001666
1667 return false;
1668}
1669
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001670/// ParseDirectiveSymbolAttribute
1671/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001672bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001673 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001674 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001675 StringRef Name;
1676
1677 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001678 return TokError("expected identifier in directive");
1679
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001680 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001681
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001682 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001683
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001685 break;
1686
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001688 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001689 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001690 }
1691 }
1692
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001694 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001695}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001696
Matt Fleming924c5e52010-05-21 11:36:59 +00001697/// ParseDirectiveELFType
1698/// ::= .type identifier , @attribute
1699bool AsmParser::ParseDirectiveELFType() {
1700 StringRef Name;
1701 if (ParseIdentifier(Name))
1702 return TokError("expected identifier in directive");
1703
1704 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001705 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001706
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001707 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001708 return TokError("unexpected token in '.type' directive");
1709 Lex();
1710
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001711 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001712 return TokError("expected '@' before type");
1713 Lex();
1714
1715 StringRef Type;
1716 SMLoc TypeLoc;
1717
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001718 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001719 if (ParseIdentifier(Type))
1720 return TokError("expected symbol type in directive");
1721
1722 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1723 .Case("function", MCSA_ELF_TypeFunction)
1724 .Case("object", MCSA_ELF_TypeObject)
1725 .Case("tls_object", MCSA_ELF_TypeTLS)
1726 .Case("common", MCSA_ELF_TypeCommon)
1727 .Case("notype", MCSA_ELF_TypeNoType)
1728 .Default(MCSA_Invalid);
1729
1730 if (Attr == MCSA_Invalid)
1731 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1732
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001733 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001734 return TokError("unexpected token in '.type' directive");
1735
1736 Lex();
1737
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001738 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001739
1740 return false;
1741}
1742
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001743/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001744/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1745bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001746 CheckForValidSection();
1747
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001748 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001749 StringRef Name;
1750 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001751 return TokError("expected identifier in directive");
1752
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001753 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001754 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001755
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001757 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001758 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001759
1760 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001761 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001762 if (ParseAbsoluteExpression(Size))
1763 return true;
1764
1765 int64_t Pow2Alignment = 0;
1766 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001767 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001768 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001769 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001770 if (ParseAbsoluteExpression(Pow2Alignment))
1771 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001772
1773 // If this target takes alignments in bytes (not log) validate and convert.
1774 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1775 if (!isPowerOf2_64(Pow2Alignment))
1776 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1777 Pow2Alignment = Log2_64(Pow2Alignment);
1778 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001779 }
1780
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001782 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001783
Sean Callanan79ed1a82010-01-19 20:22:31 +00001784 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001785
Chris Lattner1fc3d752009-07-09 17:25:12 +00001786 // NOTE: a size of zero for a .comm should create a undefined symbol
1787 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001788 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001789 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1790 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001791
Eric Christopherc260a3e2010-05-14 01:38:54 +00001792 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001793 // may internally end up wanting an alignment in bytes.
1794 // FIXME: Diagnose overflow.
1795 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001796 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1797 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001798
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001799 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001800 return Error(IDLoc, "invalid symbol redefinition");
1801
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001802 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001803 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001804 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001805 getStreamer().EmitZerofill(Ctx.getMachOSection(
1806 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1807 0, SectionKind::getBSS()),
1808 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001809 return false;
1810 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001811
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001812 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001813 return false;
1814}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001815
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001816/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001817/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001818bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001819 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001820 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001821
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001822 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001824 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001825
Sean Callanan79ed1a82010-01-19 20:22:31 +00001826 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001827
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001828 if (Str.empty())
1829 Error(Loc, ".abort detected. Assembly stopping.");
1830 else
1831 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001832 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001833
1834 return false;
1835}
Kevin Enderby71148242009-07-14 21:35:03 +00001836
Kevin Enderby1f049b22009-07-14 23:21:55 +00001837/// ParseDirectiveInclude
1838/// ::= .include "filename"
1839bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001840 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001841 return TokError("expected string in '.include' directive");
1842
Sean Callanan18b83232010-01-19 21:44:56 +00001843 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001844 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001845 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001846
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001847 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001848 return TokError("unexpected token in '.include' directive");
1849
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001850 // Strip the quotes.
1851 Filename = Filename.substr(1, Filename.size()-2);
1852
1853 // Attempt to switch the lexer to the included file before consuming the end
1854 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001855 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001856 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001857 return true;
1858 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001859
1860 return false;
1861}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001862
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001863/// ParseDirectiveIf
1864/// ::= .if expression
1865bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001866 TheCondStack.push_back(TheCondState);
1867 TheCondState.TheCond = AsmCond::IfCond;
1868 if(TheCondState.Ignore) {
1869 EatToEndOfStatement();
1870 }
1871 else {
1872 int64_t ExprValue;
1873 if (ParseAbsoluteExpression(ExprValue))
1874 return true;
1875
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001876 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001877 return TokError("unexpected token in '.if' directive");
1878
Sean Callanan79ed1a82010-01-19 20:22:31 +00001879 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001880
1881 TheCondState.CondMet = ExprValue;
1882 TheCondState.Ignore = !TheCondState.CondMet;
1883 }
1884
1885 return false;
1886}
1887
1888/// ParseDirectiveElseIf
1889/// ::= .elseif expression
1890bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1891 if (TheCondState.TheCond != AsmCond::IfCond &&
1892 TheCondState.TheCond != AsmCond::ElseIfCond)
1893 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1894 " an .elseif");
1895 TheCondState.TheCond = AsmCond::ElseIfCond;
1896
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001897 bool LastIgnoreState = false;
1898 if (!TheCondStack.empty())
1899 LastIgnoreState = TheCondStack.back().Ignore;
1900 if (LastIgnoreState || TheCondState.CondMet) {
1901 TheCondState.Ignore = true;
1902 EatToEndOfStatement();
1903 }
1904 else {
1905 int64_t ExprValue;
1906 if (ParseAbsoluteExpression(ExprValue))
1907 return true;
1908
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001909 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001910 return TokError("unexpected token in '.elseif' directive");
1911
Sean Callanan79ed1a82010-01-19 20:22:31 +00001912 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001913 TheCondState.CondMet = ExprValue;
1914 TheCondState.Ignore = !TheCondState.CondMet;
1915 }
1916
1917 return false;
1918}
1919
1920/// ParseDirectiveElse
1921/// ::= .else
1922bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001923 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001924 return TokError("unexpected token in '.else' directive");
1925
Sean Callanan79ed1a82010-01-19 20:22:31 +00001926 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001927
1928 if (TheCondState.TheCond != AsmCond::IfCond &&
1929 TheCondState.TheCond != AsmCond::ElseIfCond)
1930 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1931 ".elseif");
1932 TheCondState.TheCond = AsmCond::ElseCond;
1933 bool LastIgnoreState = false;
1934 if (!TheCondStack.empty())
1935 LastIgnoreState = TheCondStack.back().Ignore;
1936 if (LastIgnoreState || TheCondState.CondMet)
1937 TheCondState.Ignore = true;
1938 else
1939 TheCondState.Ignore = false;
1940
1941 return false;
1942}
1943
1944/// ParseDirectiveEndIf
1945/// ::= .endif
1946bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001947 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001948 return TokError("unexpected token in '.endif' directive");
1949
Sean Callanan79ed1a82010-01-19 20:22:31 +00001950 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001951
1952 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1953 TheCondStack.empty())
1954 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1955 ".else");
1956 if (!TheCondStack.empty()) {
1957 TheCondState = TheCondStack.back();
1958 TheCondStack.pop_back();
1959 }
1960
1961 return false;
1962}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001963
1964/// ParseDirectiveFile
1965/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001966bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001967 // FIXME: I'm not sure what this is.
1968 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001969 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001970 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001971 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001972 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001973
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001974 if (FileNumber < 1)
1975 return TokError("file number less than one");
1976 }
1977
Daniel Dunbareceec052010-07-12 17:45:27 +00001978 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001979 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001980
Chris Lattnerd32e8032010-01-25 19:02:58 +00001981 StringRef Filename = getTok().getString();
1982 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001983 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001984
Daniel Dunbareceec052010-07-12 17:45:27 +00001985 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001986 return TokError("unexpected token in '.file' directive");
1987
Chris Lattnerd32e8032010-01-25 19:02:58 +00001988 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001989 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001990 else {
1991 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1992 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001993 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001994 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001995
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001996 return false;
1997}
1998
1999/// ParseDirectiveLine
2000/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002001bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002002 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2003 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002004 return TokError("unexpected token in '.line' directive");
2005
Sean Callanan18b83232010-01-19 21:44:56 +00002006 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002007 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002008 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002009
2010 // FIXME: Do something with the .line.
2011 }
2012
Daniel Dunbareceec052010-07-12 17:45:27 +00002013 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002014 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002015
2016 return false;
2017}
2018
2019
2020/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002021/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002022/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2023/// The first number is a file number, must have been previously assigned with
2024/// a .file directive, the second number is the line number and optionally the
2025/// third number is a column position (zero if not specified). The remaining
2026/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002027bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002028
Daniel Dunbareceec052010-07-12 17:45:27 +00002029 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002030 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002031 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002032 if (FileNumber < 1)
2033 return TokError("file number less than one in '.loc' directive");
2034 if (!getContext().ValidateDwarfFileNumber(FileNumber))
2035 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002036 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002037
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002038 int64_t LineNumber = 0;
2039 if (getLexer().is(AsmToken::Integer)) {
2040 LineNumber = getTok().getIntVal();
2041 if (LineNumber < 1)
2042 return TokError("line number less than one in '.loc' directive");
2043 Lex();
2044 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002045
2046 int64_t ColumnPos = 0;
2047 if (getLexer().is(AsmToken::Integer)) {
2048 ColumnPos = getTok().getIntVal();
2049 if (ColumnPos < 0)
2050 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002051 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002052 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002053
Kevin Enderbyc0957932010-09-30 16:52:03 +00002054 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002055 unsigned Isa = 0;
2056 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2057 for (;;) {
2058 if (getLexer().is(AsmToken::EndOfStatement))
2059 break;
2060
2061 StringRef Name;
2062 SMLoc Loc = getTok().getLoc();
2063 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002064 return TokError("unexpected token in '.loc' directive");
2065
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002066 if (Name == "basic_block")
2067 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2068 else if (Name == "prologue_end")
2069 Flags |= DWARF2_FLAG_PROLOGUE_END;
2070 else if (Name == "epilogue_begin")
2071 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2072 else if (Name == "is_stmt") {
2073 SMLoc Loc = getTok().getLoc();
2074 const MCExpr *Value;
2075 if (getParser().ParseExpression(Value))
2076 return true;
2077 // The expression must be the constant 0 or 1.
2078 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2079 int Value = MCE->getValue();
2080 if (Value == 0)
2081 Flags &= ~DWARF2_FLAG_IS_STMT;
2082 else if (Value == 1)
2083 Flags |= DWARF2_FLAG_IS_STMT;
2084 else
2085 return Error(Loc, "is_stmt value not 0 or 1");
2086 }
2087 else {
2088 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2089 }
2090 }
2091 else if (Name == "isa") {
2092 SMLoc Loc = getTok().getLoc();
2093 const MCExpr *Value;
2094 if (getParser().ParseExpression(Value))
2095 return true;
2096 // The expression must be a constant greater or equal to 0.
2097 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2098 int Value = MCE->getValue();
2099 if (Value < 0)
2100 return Error(Loc, "isa number less than zero");
2101 Isa = Value;
2102 }
2103 else {
2104 return Error(Loc, "isa number not a constant value");
2105 }
2106 }
2107 else {
2108 return Error(Loc, "unknown sub-directive in '.loc' directive");
2109 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002110
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002111 if (getLexer().is(AsmToken::EndOfStatement))
2112 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002113 }
2114 }
2115
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002116 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002117
2118 return false;
2119}
2120
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002121/// ParseDirectiveMacrosOnOff
2122/// ::= .macros_on
2123/// ::= .macros_off
2124bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2125 SMLoc DirectiveLoc) {
2126 if (getLexer().isNot(AsmToken::EndOfStatement))
2127 return Error(getLexer().getLoc(),
2128 "unexpected token in '" + Directive + "' directive");
2129
2130 getParser().MacrosEnabled = Directive == ".macros_on";
2131
2132 return false;
2133}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002134
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002135/// ParseDirectiveMacro
2136/// ::= .macro name
2137bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2138 SMLoc DirectiveLoc) {
2139 StringRef Name;
2140 if (getParser().ParseIdentifier(Name))
2141 return TokError("expected identifier in directive");
2142
2143 if (getLexer().isNot(AsmToken::EndOfStatement))
2144 return TokError("unexpected token in '.macro' directive");
2145
2146 // Eat the end of statement.
2147 Lex();
2148
2149 AsmToken EndToken, StartToken = getTok();
2150
2151 // Lex the macro definition.
2152 for (;;) {
2153 // Check whether we have reached the end of the file.
2154 if (getLexer().is(AsmToken::Eof))
2155 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2156
2157 // Otherwise, check whether we have reach the .endmacro.
2158 if (getLexer().is(AsmToken::Identifier) &&
2159 (getTok().getIdentifier() == ".endm" ||
2160 getTok().getIdentifier() == ".endmacro")) {
2161 EndToken = getTok();
2162 Lex();
2163 if (getLexer().isNot(AsmToken::EndOfStatement))
2164 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2165 "' directive");
2166 break;
2167 }
2168
2169 // Otherwise, scan til the end of the statement.
2170 getParser().EatToEndOfStatement();
2171 }
2172
2173 if (getParser().MacroMap.lookup(Name)) {
2174 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2175 }
2176
2177 const char *BodyStart = StartToken.getLoc().getPointer();
2178 const char *BodyEnd = EndToken.getLoc().getPointer();
2179 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2180 getParser().MacroMap[Name] = new Macro(Name, Body);
2181 return false;
2182}
2183
2184/// ParseDirectiveEndMacro
2185/// ::= .endm
2186/// ::= .endmacro
2187bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2188 SMLoc DirectiveLoc) {
2189 if (getLexer().isNot(AsmToken::EndOfStatement))
2190 return TokError("unexpected token in '" + Directive + "' directive");
2191
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002192 // If we are inside a macro instantiation, terminate the current
2193 // instantiation.
2194 if (!getParser().ActiveMacros.empty()) {
2195 getParser().HandleMacroExit();
2196 return false;
2197 }
2198
2199 // Otherwise, this .endmacro is a stray entry in the file; well formed
2200 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002201 return TokError("unexpected '" + Directive + "' in file, "
2202 "no current macro definition");
2203}
2204
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002205void GenericAsmParser::ParseUleb128(uint64_t Value) {
2206 const uint64_t Mask = (1 << 7) - 1;
2207 do {
2208 unsigned Byte = Value & Mask;
2209 Value >>= 7;
2210 if (Value) // Not the last one
2211 Byte |= (1 << 7);
2212 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2213 } while (Value);
2214}
2215
2216void GenericAsmParser::ParseSleb128(int64_t Value) {
2217 const int64_t Mask = (1 << 7) - 1;
2218 for(;;) {
2219 unsigned Byte = Value & Mask;
2220 Value >>= 7;
2221 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2222 (Value == -1 && (Byte & 0x40) != 0));
2223 if (!Done)
2224 Byte |= (1 << 7);
2225 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2226 if (Done)
2227 break;
2228 }
2229}
2230
2231bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2232 int64_t Value;
2233 if (getParser().ParseAbsoluteExpression(Value))
2234 return true;
2235
2236 if (getLexer().isNot(AsmToken::EndOfStatement))
2237 return TokError("unexpected token in directive");
2238
2239 if (DirName[1] == 's')
2240 ParseSleb128(Value);
2241 else
2242 ParseUleb128(Value);
2243 return false;
2244}
2245
2246
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002247/// \brief Create an MCAsmParser instance.
2248MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2249 MCContext &C, MCStreamer &Out,
2250 const MCAsmInfo &MAI) {
2251 return new AsmParser(T, SM, C, Out, MAI);
2252}