blob: 5ef6f682bfa8a2586d2fb0450f154a9c300ac9b5 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000015#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000020#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000021#include "llvm/MC/MCInst.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
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();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000151 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
152
153 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
154 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000155
156 /// \brief Reset the current lexer position to that given by \arg Loc. The
157 /// current token is not set; clients should ensure Lex() is called
158 /// subsequently.
159 void JumpToLoc(SMLoc Loc);
160
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000161 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000162
163 /// \brief Parse up to the end of statement and a return the contents from the
164 /// current token until the end of the statement; the current token on exit
165 /// will be either the EndOfStatement or EOF.
166 StringRef ParseStringToEndOfStatement();
167
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000168 bool ParseAssignment(StringRef Name);
169
170 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
171 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
172 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
173
174 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
175 /// and set \arg Res to the identifier contents.
176 bool ParseIdentifier(StringRef &Res);
177
178 // Directive Parsing.
179 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
180 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
181 bool ParseDirectiveFill(); // ".fill"
182 bool ParseDirectiveSpace(); // ".space"
183 bool ParseDirectiveSet(); // ".set"
184 bool ParseDirectiveOrg(); // ".org"
185 // ".align{,32}", ".p2align{,w,l}"
186 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
187
188 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
189 /// accepts a single symbol (which should be a label or an external).
190 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
191 bool ParseDirectiveELFType(); // ELF specific ".type"
192
193 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
194
195 bool ParseDirectiveAbort(); // ".abort"
196 bool ParseDirectiveInclude(); // ".include"
197
198 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
199 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
200 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
201 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
202
203 /// ParseEscapedString - Parse the current token as a string which may include
204 /// escaped characters and return the string contents.
205 bool ParseEscapedString(std::string &Data);
206};
207
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000208/// \brief Generic implementations of directive handling, etc. which is shared
209/// (or the default, at least) for all assembler parser.
210class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000211 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
212 void AddDirectiveHandler(StringRef Directive) {
213 getParser().AddDirectiveHandler(this, Directive,
214 HandleDirective<GenericAsmParser, Handler>);
215 }
216
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000217public:
218 GenericAsmParser() {}
219
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000220 AsmParser &getParser() {
221 return (AsmParser&) this->MCAsmParserExtension::getParser();
222 }
223
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000224 virtual void Initialize(MCAsmParser &Parser) {
225 // Call the base implementation.
226 this->MCAsmParserExtension::Initialize(Parser);
227
228 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000229 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
230 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
231 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000232
233 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000234 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
235 ".macros_on");
236 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
237 ".macros_off");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000241
242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000244 }
245
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000246 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
247 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
248 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000249
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000250 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000251 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
252 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000253
254 void ParseUleb128(uint64_t Value);
255 void ParseSleb128(int64_t Value);
256 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000257};
258
259}
260
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000261namespace llvm {
262
263extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000264extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000265
266}
267
Chris Lattneraaec2052010-01-19 19:46:13 +0000268enum { DEFAULT_ADDRSPACE = 0 };
269
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000270AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
271 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000272 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000273 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000274 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000275 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000276
277 // Initialize the generic parser.
278 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000279
280 // Initialize the platform / file format parser.
281 //
282 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
283 // created.
284 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000285 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000286 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000287 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000288 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000289 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000290 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000291}
292
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000293AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000294 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
295
296 // Destroy any macros.
297 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
298 ie = MacroMap.end(); it != ie; ++it)
299 delete it->getValue();
300
Daniel Dunbare4749702010-07-12 18:12:02 +0000301 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000302 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000303}
304
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000305void AsmParser::PrintMacroInstantiations() {
306 // Print the active macro instantiation stack.
307 for (std::vector<MacroInstantiation*>::const_reverse_iterator
308 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
309 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
310 "note");
311}
312
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000313void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000314 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000315 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000316}
317
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000318bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000319 HadError = true;
Sean Callananbf2013e2010-01-20 23:19:55 +0000320 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000321 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000322 return true;
323}
324
Sean Callananbf2013e2010-01-20 23:19:55 +0000325void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
326 const char *Type) const {
327 SrcMgr.PrintMessage(Loc, Msg, Type);
328}
Sean Callananfd0b0282010-01-21 00:19:58 +0000329
330bool AsmParser::EnterIncludeFile(const std::string &Filename) {
331 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
332 if (NewBuf == -1)
333 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000334
Sean Callananfd0b0282010-01-21 00:19:58 +0000335 CurBuffer = NewBuf;
336
337 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
338
339 return false;
340}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000341
342void AsmParser::JumpToLoc(SMLoc Loc) {
343 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
344 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
345}
346
Sean Callananfd0b0282010-01-21 00:19:58 +0000347const AsmToken &AsmParser::Lex() {
348 const AsmToken *tok = &Lexer.Lex();
349
350 if (tok->is(AsmToken::Eof)) {
351 // If this is the end of an included file, pop the parent file off the
352 // include stack.
353 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
354 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000355 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000356 tok = &Lexer.Lex();
357 }
358 }
359
360 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000361 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000362
Sean Callananfd0b0282010-01-21 00:19:58 +0000363 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000364}
365
Chris Lattner79180e22010-04-05 23:15:42 +0000366bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000367 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000368 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000369 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000370 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000371 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000372 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
373 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000374
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000375 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000376 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000377
378 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000379 AsmCond StartingCondState = TheCondState;
380
Chris Lattnerb717fb02009-07-02 21:53:43 +0000381 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000382 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000383 if (!ParseStatement()) continue;
384
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000385 // We had an error, validate that one was emitted and recover by skipping to
386 // the next line.
387 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000388 EatToEndOfStatement();
389 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000390
391 if (TheCondState.TheCond != StartingCondState.TheCond ||
392 TheCondState.Ignore != StartingCondState.Ignore)
393 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000394
395 // Check to see there are no empty DwarfFile slots.
396 const std::vector<MCDwarfFile *> &MCDwarfFiles =
397 getContext().getMCDwarfFiles();
398 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000399 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000400 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000401 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000402
Chris Lattner79180e22010-04-05 23:15:42 +0000403 // Finalize the output stream if there are no errors and if the client wants
404 // us to.
405 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000406 Out.Finish();
407
Chris Lattnerb717fb02009-07-02 21:53:43 +0000408 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000409}
410
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000411void AsmParser::CheckForValidSection() {
412 if (!getStreamer().getCurrentSection()) {
413 TokError("expected section directive before assembly directive");
414 Out.SwitchSection(Ctx.getMachOSection(
415 "__TEXT", "__text",
416 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
417 0, SectionKind::getText()));
418 }
419}
420
Chris Lattner2cf5f142009-06-22 01:29:09 +0000421/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
422void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000423 while (Lexer.isNot(AsmToken::EndOfStatement) &&
424 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000425 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000426
427 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000429 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000430}
431
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000432StringRef AsmParser::ParseStringToEndOfStatement() {
433 const char *Start = getTok().getLoc().getPointer();
434
435 while (Lexer.isNot(AsmToken::EndOfStatement) &&
436 Lexer.isNot(AsmToken::Eof))
437 Lex();
438
439 const char *End = getTok().getLoc().getPointer();
440 return StringRef(Start, End - Start);
441}
Chris Lattnerc4193832009-06-22 05:51:26 +0000442
Chris Lattner74ec1a32009-06-22 06:32:03 +0000443/// ParseParenExpr - Parse a paren expression and return it.
444/// NOTE: This assumes the leading '(' has already been consumed.
445///
446/// parenexpr ::= expr)
447///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000448bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000449 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000451 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000452 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000453 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000454 return false;
455}
Chris Lattnerc4193832009-06-22 05:51:26 +0000456
Chris Lattner74ec1a32009-06-22 06:32:03 +0000457/// ParsePrimaryExpr - Parse a primary expression and return it.
458/// primaryexpr ::= (parenexpr
459/// primaryexpr ::= symbol
460/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000461/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000462/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000463bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000464 switch (Lexer.getKind()) {
465 default:
466 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000467 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000468 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000469 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000470 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000471 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000472 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000473 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000474 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000475 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000476 EndLoc = Lexer.getLoc();
477
478 StringRef Identifier;
479 if (ParseIdentifier(Identifier))
480 return false;
481
Daniel Dunbarfffff912009-10-16 01:34:54 +0000482 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000483 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000484 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000485
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000486 // Mark the symbol as used in an expression.
487 Sym->setUsedInExpr(true);
488
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000489 // Lookup the symbol variant if used.
490 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000491 if (Split.first.size() != Identifier.size())
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000492 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
493
Daniel Dunbarfffff912009-10-16 01:34:54 +0000494 // If this is an absolute variable reference, substitute it now to preserve
495 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000496 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000497 if (Variant)
498 return Error(EndLoc, "unexpected modified on variable reference");
499
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000500 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000501 return false;
502 }
503
504 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000505 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000506 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000507 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000508 case AsmToken::Integer: {
509 SMLoc Loc = getTok().getLoc();
510 int64_t IntVal = getTok().getIntVal();
511 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000512 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000513 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000514 // Look for 'b' or 'f' following an Integer as a directional label
515 if (Lexer.getKind() == AsmToken::Identifier) {
516 StringRef IDVal = getTok().getString();
517 if (IDVal == "f" || IDVal == "b"){
518 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
519 IDVal == "f" ? 1 : 0);
520 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
521 getContext());
522 if(IDVal == "b" && Sym->isUndefined())
523 return Error(Loc, "invalid reference to undefined symbol");
524 EndLoc = Lexer.getLoc();
525 Lex(); // Eat identifier.
526 }
527 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000528 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000529 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000530 case AsmToken::Dot: {
531 // This is a '.' reference, which references the current PC. Emit a
532 // temporary label to the streamer and refer to it.
533 MCSymbol *Sym = Ctx.CreateTempSymbol();
534 Out.EmitLabel(Sym);
535 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
536 EndLoc = Lexer.getLoc();
537 Lex(); // Eat identifier.
538 return false;
539 }
540
Daniel Dunbar3f872332009-07-28 16:08:33 +0000541 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000542 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000543 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000544 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000545 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000546 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000547 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000548 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000549 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000550 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000551 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000552 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000553 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000554 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000555 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000556 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000557 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000558 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000559 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000560 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000561 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000562 }
563}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000564
Chris Lattnerb4307b32010-01-15 19:28:38 +0000565bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000566 SMLoc EndLoc;
567 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000568}
569
Chris Lattner74ec1a32009-06-22 06:32:03 +0000570/// ParseExpression - Parse an expression and return it.
571///
572/// expr ::= expr +,- expr -> lowest.
573/// expr ::= expr |,^,&,! expr -> middle.
574/// expr ::= expr *,/,%,<<,>> expr -> highest.
575/// expr ::= primaryexpr
576///
Chris Lattner54482b42010-01-15 19:39:23 +0000577bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000578 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000579 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000580 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
581 return true;
582
583 // Try to constant fold it up front, if possible.
584 int64_t Value;
585 if (Res->EvaluateAsAbsolute(Value))
586 Res = MCConstantExpr::Create(Value, getContext());
587
588 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000589}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000590
Chris Lattnerb4307b32010-01-15 19:28:38 +0000591bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000592 Res = 0;
593 return ParseParenExpr(Res, EndLoc) ||
594 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000595}
596
Daniel Dunbar475839e2009-06-29 20:37:27 +0000597bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000598 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000599
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000600 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000601 if (ParseExpression(Expr))
602 return true;
603
Daniel Dunbare00b0112009-10-16 01:57:52 +0000604 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000605 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000606
607 return false;
608}
609
Daniel Dunbar3f872332009-07-28 16:08:33 +0000610static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000611 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000612 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000613 default:
614 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000615
616 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000617 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000618 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000619 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000620 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000621 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000622 return 1;
623
624 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000625 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000626 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000627 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000629 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000630 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000632 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 case AsmToken::ExclaimEqual:
635 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000636 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000637 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000638 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000639 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000640 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000641 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000642 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000644 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000645 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000646 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000647 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000648 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 return 2;
650
651 // Intermediate Precedence: |, &, ^
652 //
653 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000654 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000655 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000656 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000657 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000658 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000659 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000660 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000661 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000662 return 3;
663
664 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000665 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000666 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000667 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000668 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000669 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000670 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000671 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000672 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000673 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000674 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000675 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000677 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000678 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000680 }
681}
682
683
684/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
685/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000686bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
687 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000688 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000689 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000690 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000691
692 // If the next token is lower precedence than we are allowed to eat, return
693 // successfully with what we ate already.
694 if (TokPrec < Precedence)
695 return false;
696
Sean Callanan79ed1a82010-01-19 20:22:31 +0000697 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000698
699 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000700 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000701 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000702
703 // If BinOp binds less tightly with RHS than the operator after RHS, let
704 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000705 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000706 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000707 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000708 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000709 }
710
Daniel Dunbar475839e2009-06-29 20:37:27 +0000711 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000712 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000713 }
714}
715
Chris Lattnerc4193832009-06-22 05:51:26 +0000716
717
718
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000719/// ParseStatement:
720/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000721/// ::= Label* Directive ...Operands... EndOfStatement
722/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000723bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000724 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000725 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000726 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000727 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000728 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000729
730 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000731 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000732 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000733 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000734 int64_t LocalLabelVal = -1;
735 // GUESS allow an integer followed by a ':' as a directional local label
736 if (Lexer.is(AsmToken::Integer)) {
737 LocalLabelVal = getTok().getIntVal();
738 if (LocalLabelVal < 0) {
739 if (!TheCondState.Ignore)
740 return TokError("unexpected token at start of statement");
741 IDVal = "";
742 }
743 else {
744 IDVal = getTok().getString();
745 Lex(); // Consume the integer token to be used as an identifier token.
746 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000747 if (!TheCondState.Ignore)
748 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000749 }
750 }
751 }
752 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000753 if (!TheCondState.Ignore)
754 return TokError("unexpected token at start of statement");
755 IDVal = "";
756 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000757
Chris Lattner7834fac2010-04-17 18:14:27 +0000758 // Handle conditional assembly here before checking for skipping. We
759 // have to do this so that .endif isn't skipped in a ".if 0" block for
760 // example.
761 if (IDVal == ".if")
762 return ParseDirectiveIf(IDLoc);
763 if (IDVal == ".elseif")
764 return ParseDirectiveElseIf(IDLoc);
765 if (IDVal == ".else")
766 return ParseDirectiveElse(IDLoc);
767 if (IDVal == ".endif")
768 return ParseDirectiveEndIf(IDLoc);
769
770 // If we are in a ".if 0" block, ignore this statement.
771 if (TheCondState.Ignore) {
772 EatToEndOfStatement();
773 return false;
774 }
775
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000776 // FIXME: Recurse on local labels?
777
778 // See what kind of statement we have.
779 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000780 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000781 CheckForValidSection();
782
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000783 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000784 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000785
786 // Diagnose attempt to use a variable as a label.
787 //
788 // FIXME: Diagnostics. Note the location of the definition as a label.
789 // FIXME: This doesn't diagnose assignment to a symbol which has been
790 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000791 MCSymbol *Sym;
792 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000793 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000794 else
795 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000796 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000797 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000798
Daniel Dunbar959fd882009-08-26 22:13:22 +0000799 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000800 Out.EmitLabel(Sym);
801
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000802 // Consume any end of statement token, if present, to avoid spurious
803 // AddBlankLine calls().
804 if (Lexer.is(AsmToken::EndOfStatement)) {
805 Lex();
806 if (Lexer.is(AsmToken::Eof))
807 return false;
808 }
809
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000810 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000811 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000812
Daniel Dunbar3f872332009-07-28 16:08:33 +0000813 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000814 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000815 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000816
Daniel Dunbare2ace502009-08-31 08:09:09 +0000817 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000818
819 default: // Normal instruction or directive.
820 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000821 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000822
823 // If macros are enabled, check to see if this is a macro instantiation.
824 if (MacrosEnabled)
825 if (const Macro *M = MacroMap.lookup(IDVal))
826 return HandleMacroEntry(IDVal, IDLoc, M);
827
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000828 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000829 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000830 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000832 return ParseDirectiveSet();
833
Daniel Dunbara0d14262009-06-24 23:30:00 +0000834 // Data directives
835
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000836 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000837 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000838 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000839 return ParseDirectiveAscii(true);
840
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000842 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000843 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000844 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000845 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000846 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000847 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000848 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000849
Eli Friedman5d68ec22010-07-19 04:17:25 +0000850 if (IDVal == ".align") {
851 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
852 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
853 }
854 if (IDVal == ".align32") {
855 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
856 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
857 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000858 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000859 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000860 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000861 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000862 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000863 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000865 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000866 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000867 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000868 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000869 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
870
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000871 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000872 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000873
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000874 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000875 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000876 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000877 return ParseDirectiveSpace();
878
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000879 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000880
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000881 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000882 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000883 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000884 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000885 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000886 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000887 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000888 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000889 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000890 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000891 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000892 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000893 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000894 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000895 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000896 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000897 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000898 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000899 if (IDVal == ".type")
900 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000901 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000902 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000903 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000904 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000905 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000906 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000907 if (IDVal == ".weak_def_can_be_hidden")
908 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000909
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000910 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000911 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000912 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000913 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000914
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000915 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000916 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000917 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000918 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000919
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000920 // Look up the handler in the handler table.
921 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
922 DirectiveMap.lookup(IDVal);
923 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000924 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000925
Kevin Enderby9c656452009-09-10 20:51:44 +0000926 // Target hook for parsing target specific directives.
927 if (!getTargetParser().ParseDirective(ID))
928 return false;
929
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000930 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000931 EatToEndOfStatement();
932 return false;
933 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000934
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000935 CheckForValidSection();
936
Chris Lattnera7f13542010-05-19 23:34:33 +0000937 // Canonicalize the opcode to lower case.
938 SmallString<128> Opcode;
939 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
940 Opcode.push_back(tolower(IDVal[i]));
941
Chris Lattner98986712010-01-14 22:21:20 +0000942 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000943 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000944 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000945
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000946 // Dump the parsed representation, if requested.
947 if (getShowParsedOperands()) {
948 SmallString<256> Str;
949 raw_svector_ostream OS(Str);
950 OS << "parsed instruction: [";
951 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
952 if (i != 0)
953 OS << ", ";
954 ParsedOperands[i]->dump(OS);
955 }
956 OS << "]";
957
958 PrintMessage(IDLoc, OS.str(), "note");
959 }
960
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000961 // If parsing succeeded, match the instruction.
962 if (!HadError) {
963 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000964 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000965 // Emit the instruction on success.
966 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000967 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000968 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000969 }
Chris Lattner98986712010-01-14 22:21:20 +0000970
Chris Lattner98986712010-01-14 22:21:20 +0000971 // Free any parsed operands.
972 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
973 delete ParsedOperands[i];
974
Chris Lattnercbf8a982010-09-11 16:18:25 +0000975 // Don't skip the rest of the line, the instruction parser is responsible for
976 // that.
977 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000978}
Chris Lattner9a023f72009-06-24 04:43:34 +0000979
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000980MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
981 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000982 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
983{
984 // Macro instantiation is lexical, unfortunately. We construct a new buffer
985 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000986 SmallString<256> Buf;
987 raw_svector_ostream OS(Buf);
988
989 StringRef Body = M->Body;
990 while (!Body.empty()) {
991 // Scan for the next substitution.
992 std::size_t End = Body.size(), Pos = 0;
993 for (; Pos != End; ++Pos) {
994 // Check for a substitution or escape.
995 if (Body[Pos] != '$' || Pos + 1 == End)
996 continue;
997
998 char Next = Body[Pos + 1];
999 if (Next == '$' || Next == 'n' || isdigit(Next))
1000 break;
1001 }
1002
1003 // Add the prefix.
1004 OS << Body.slice(0, Pos);
1005
1006 // Check if we reached the end.
1007 if (Pos == End)
1008 break;
1009
1010 switch (Body[Pos+1]) {
1011 // $$ => $
1012 case '$':
1013 OS << '$';
1014 break;
1015
1016 // $n => number of arguments
1017 case 'n':
1018 OS << A.size();
1019 break;
1020
1021 // $[0-9] => argument
1022 default: {
1023 // Missing arguments are ignored.
1024 unsigned Index = Body[Pos+1] - '0';
1025 if (Index >= A.size())
1026 break;
1027
1028 // Otherwise substitute with the token values, with spaces eliminated.
1029 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1030 ie = A[Index].end(); it != ie; ++it)
1031 OS << it->getString();
1032 break;
1033 }
1034 }
1035
1036 // Update the scan point.
1037 Body = Body.substr(Pos + 2);
1038 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001039
1040 // We include the .endmacro in the buffer as our queue to exit the macro
1041 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001042 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001043
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001044 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001045}
1046
1047bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1048 const Macro *M) {
1049 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1050 // this, although we should protect against infinite loops.
1051 if (ActiveMacros.size() == 20)
1052 return TokError("macros cannot be nested more than 20 levels deep");
1053
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001054 // Parse the macro instantiation arguments.
1055 std::vector<std::vector<AsmToken> > MacroArguments;
1056 MacroArguments.push_back(std::vector<AsmToken>());
1057 unsigned ParenLevel = 0;
1058 for (;;) {
1059 if (Lexer.is(AsmToken::Eof))
1060 return TokError("unexpected token in macro instantiation");
1061 if (Lexer.is(AsmToken::EndOfStatement))
1062 break;
1063
1064 // If we aren't inside parentheses and this is a comma, start a new token
1065 // list.
1066 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1067 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001068 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001069 // Adjust the current parentheses level.
1070 if (Lexer.is(AsmToken::LParen))
1071 ++ParenLevel;
1072 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1073 --ParenLevel;
1074
1075 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001076 MacroArguments.back().push_back(getTok());
1077 }
1078 Lex();
1079 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001080
1081 // Create the macro instantiation object and add to the current macro
1082 // instantiation stack.
1083 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001084 getTok().getLoc(),
1085 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001086 ActiveMacros.push_back(MI);
1087
1088 // Jump to the macro instantiation and prime the lexer.
1089 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1090 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1091 Lex();
1092
1093 return false;
1094}
1095
1096void AsmParser::HandleMacroExit() {
1097 // Jump to the EndOfStatement we should return to, and consume it.
1098 JumpToLoc(ActiveMacros.back()->ExitLoc);
1099 Lex();
1100
1101 // Pop the instantiation entry.
1102 delete ActiveMacros.back();
1103 ActiveMacros.pop_back();
1104}
1105
Benjamin Kramer38e59892010-07-14 22:38:02 +00001106bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001107 // FIXME: Use better location, we should use proper tokens.
1108 SMLoc EqualLoc = Lexer.getLoc();
1109
Daniel Dunbar821e3332009-08-31 08:09:28 +00001110 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001111 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001112 return true;
1113
Daniel Dunbar3f872332009-07-28 16:08:33 +00001114 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001115 return TokError("unexpected token in assignment");
1116
1117 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001119
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001120 // Validate that the LHS is allowed to be a variable (either it has not been
1121 // used as a symbol, or it is an absolute symbol).
1122 MCSymbol *Sym = getContext().LookupSymbol(Name);
1123 if (Sym) {
1124 // Diagnose assignment to a label.
1125 //
1126 // FIXME: Diagnostics. Note the location of the definition as a label.
1127 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001128 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1129 ; // Allow redefinitions of undefined symbols only used in directives.
1130 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001131 return Error(EqualLoc, "redefinition of '" + Name + "'");
1132 else if (!Sym->isVariable())
1133 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001134 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001135 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1136 Name + "'");
1137 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001138 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001139
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001140 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001141
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001142 Sym->setUsedInExpr(true);
1143
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001144 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001145 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001146
1147 return false;
1148}
1149
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001150/// ParseIdentifier:
1151/// ::= identifier
1152/// ::= string
1153bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001154 // The assembler has relaxed rules for accepting identifiers, in particular we
1155 // allow things like '.globl $foo', which would normally be separate
1156 // tokens. At this level, we have already lexed so we cannot (currently)
1157 // handle this as a context dependent token, instead we detect adjacent tokens
1158 // and return the combined identifier.
1159 if (Lexer.is(AsmToken::Dollar)) {
1160 SMLoc DollarLoc = getLexer().getLoc();
1161
1162 // Consume the dollar sign, and check for a following identifier.
1163 Lex();
1164 if (Lexer.isNot(AsmToken::Identifier))
1165 return true;
1166
1167 // We have a '$' followed by an identifier, make sure they are adjacent.
1168 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1169 return true;
1170
1171 // Construct the joined identifier and consume the token.
1172 Res = StringRef(DollarLoc.getPointer(),
1173 getTok().getIdentifier().size() + 1);
1174 Lex();
1175 return false;
1176 }
1177
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001178 if (Lexer.isNot(AsmToken::Identifier) &&
1179 Lexer.isNot(AsmToken::String))
1180 return true;
1181
Sean Callanan18b83232010-01-19 21:44:56 +00001182 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001183
Sean Callanan79ed1a82010-01-19 20:22:31 +00001184 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001185
1186 return false;
1187}
1188
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001189/// ParseDirectiveSet:
1190/// ::= .set identifier ',' expression
1191bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001192 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001193
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001194 if (ParseIdentifier(Name))
1195 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001196
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001197 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001198 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001199 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001200
Daniel Dunbare2ace502009-08-31 08:09:09 +00001201 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001202}
1203
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001204bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001205 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001206
1207 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001208 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001209 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1210 if (Str[i] != '\\') {
1211 Data += Str[i];
1212 continue;
1213 }
1214
1215 // Recognize escaped characters. Note that this escape semantics currently
1216 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1217 ++i;
1218 if (i == e)
1219 return TokError("unexpected backslash at end of string");
1220
1221 // Recognize octal sequences.
1222 if ((unsigned) (Str[i] - '0') <= 7) {
1223 // Consume up to three octal characters.
1224 unsigned Value = Str[i] - '0';
1225
1226 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1227 ++i;
1228 Value = Value * 8 + (Str[i] - '0');
1229
1230 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1231 ++i;
1232 Value = Value * 8 + (Str[i] - '0');
1233 }
1234 }
1235
1236 if (Value > 255)
1237 return TokError("invalid octal escape sequence (out of range)");
1238
1239 Data += (unsigned char) Value;
1240 continue;
1241 }
1242
1243 // Otherwise recognize individual escapes.
1244 switch (Str[i]) {
1245 default:
1246 // Just reject invalid escape sequences for now.
1247 return TokError("invalid escape sequence (unrecognized character)");
1248
1249 case 'b': Data += '\b'; break;
1250 case 'f': Data += '\f'; break;
1251 case 'n': Data += '\n'; break;
1252 case 'r': Data += '\r'; break;
1253 case 't': Data += '\t'; break;
1254 case '"': Data += '"'; break;
1255 case '\\': Data += '\\'; break;
1256 }
1257 }
1258
1259 return false;
1260}
1261
Daniel Dunbara0d14262009-06-24 23:30:00 +00001262/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001263/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001264bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001265 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001266 CheckForValidSection();
1267
Daniel Dunbara0d14262009-06-24 23:30:00 +00001268 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001269 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001270 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001271
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001272 std::string Data;
1273 if (ParseEscapedString(Data))
1274 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001275
1276 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001277 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001278 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1279
Sean Callanan79ed1a82010-01-19 20:22:31 +00001280 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001281
1282 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001283 break;
1284
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001285 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001286 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001287 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001288 }
1289 }
1290
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001292 return false;
1293}
1294
1295/// ParseDirectiveValue
1296/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1297bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001298 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001299 CheckForValidSection();
1300
Daniel Dunbara0d14262009-06-24 23:30:00 +00001301 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001302 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001303 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001304 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001305 return true;
1306
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001307 // Special case constant expressions to match code generator.
1308 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001309 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001310 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001311 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001312
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001313 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001314 break;
1315
1316 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001317 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001318 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001319 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001320 }
1321 }
1322
Sean Callanan79ed1a82010-01-19 20:22:31 +00001323 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001324 return false;
1325}
1326
1327/// ParseDirectiveSpace
1328/// ::= .space expression [ , expression ]
1329bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001330 CheckForValidSection();
1331
Daniel Dunbara0d14262009-06-24 23:30:00 +00001332 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001333 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001334 return true;
1335
1336 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001337 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1338 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001339 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001340 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001341
Daniel Dunbar475839e2009-06-29 20:37:27 +00001342 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001343 return true;
1344
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001345 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001346 return TokError("unexpected token in '.space' directive");
1347 }
1348
Sean Callanan79ed1a82010-01-19 20:22:31 +00001349 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001350
1351 if (NumBytes <= 0)
1352 return TokError("invalid number of bytes in '.space' directive");
1353
1354 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001355 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001356
1357 return false;
1358}
1359
1360/// ParseDirectiveFill
1361/// ::= .fill expression , expression , expression
1362bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001363 CheckForValidSection();
1364
Daniel Dunbara0d14262009-06-24 23:30:00 +00001365 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001366 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001367 return true;
1368
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001369 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001370 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001371 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001372
1373 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001374 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001375 return true;
1376
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001377 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001378 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001379 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001380
1381 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001382 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001383 return true;
1384
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001385 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001386 return TokError("unexpected token in '.fill' directive");
1387
Sean Callanan79ed1a82010-01-19 20:22:31 +00001388 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001389
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001390 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1391 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001392
1393 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001394 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001395
1396 return false;
1397}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001398
1399/// ParseDirectiveOrg
1400/// ::= .org expression [ , expression ]
1401bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001402 CheckForValidSection();
1403
Daniel Dunbar821e3332009-08-31 08:09:28 +00001404 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001405 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001406 return true;
1407
1408 // Parse optional fill expression.
1409 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001410 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1411 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001412 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001414
Daniel Dunbar475839e2009-06-29 20:37:27 +00001415 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001416 return true;
1417
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001418 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001419 return TokError("unexpected token in '.org' directive");
1420 }
1421
Sean Callanan79ed1a82010-01-19 20:22:31 +00001422 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001423
1424 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1425 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001426 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001427
1428 return false;
1429}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001430
1431/// ParseDirectiveAlign
1432/// ::= {.align, ...} expression [ , expression [ , expression ]]
1433bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001434 CheckForValidSection();
1435
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001436 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001437 int64_t Alignment;
1438 if (ParseAbsoluteExpression(Alignment))
1439 return true;
1440
1441 SMLoc MaxBytesLoc;
1442 bool HasFillExpr = false;
1443 int64_t FillExpr = 0;
1444 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001445 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1446 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001447 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001448 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001449
1450 // The fill expression can be omitted while specifying a maximum number of
1451 // alignment bytes, e.g:
1452 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001453 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001454 HasFillExpr = true;
1455 if (ParseAbsoluteExpression(FillExpr))
1456 return true;
1457 }
1458
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001459 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1460 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001461 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001462 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001463
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001464 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001465 if (ParseAbsoluteExpression(MaxBytesToFill))
1466 return true;
1467
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001468 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001469 return TokError("unexpected token in directive");
1470 }
1471 }
1472
Sean Callanan79ed1a82010-01-19 20:22:31 +00001473 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001474
Daniel Dunbar648ac512010-05-17 21:54:30 +00001475 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001476 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001477
1478 // Compute alignment in bytes.
1479 if (IsPow2) {
1480 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001481 if (Alignment >= 32) {
1482 Error(AlignmentLoc, "invalid alignment value");
1483 Alignment = 31;
1484 }
1485
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001486 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001487 }
1488
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001489 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001490 if (MaxBytesLoc.isValid()) {
1491 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001492 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1493 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001494 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001495 }
1496
1497 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001498 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1499 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001500 MaxBytesToFill = 0;
1501 }
1502 }
1503
Daniel Dunbar648ac512010-05-17 21:54:30 +00001504 // Check whether we should use optimal code alignment for this .align
1505 // directive.
1506 //
1507 // FIXME: This should be using a target hook.
1508 bool UseCodeAlign = false;
1509 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001510 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001511 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001512 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1513 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001514 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001515 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001516 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001517 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1518 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001519 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001520
1521 return false;
1522}
1523
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001524/// ParseDirectiveSymbolAttribute
1525/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001526bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001527 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001528 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001529 StringRef Name;
1530
1531 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001532 return TokError("expected identifier in directive");
1533
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001534 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001535
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001536 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001537
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001538 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001539 break;
1540
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001541 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001542 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001543 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001544 }
1545 }
1546
Sean Callanan79ed1a82010-01-19 20:22:31 +00001547 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001548 return false;
1549}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001550
Matt Fleming924c5e52010-05-21 11:36:59 +00001551/// ParseDirectiveELFType
1552/// ::= .type identifier , @attribute
1553bool AsmParser::ParseDirectiveELFType() {
1554 StringRef Name;
1555 if (ParseIdentifier(Name))
1556 return TokError("expected identifier in directive");
1557
1558 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001559 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001560
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001562 return TokError("unexpected token in '.type' directive");
1563 Lex();
1564
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001565 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001566 return TokError("expected '@' before type");
1567 Lex();
1568
1569 StringRef Type;
1570 SMLoc TypeLoc;
1571
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001572 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001573 if (ParseIdentifier(Type))
1574 return TokError("expected symbol type in directive");
1575
1576 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1577 .Case("function", MCSA_ELF_TypeFunction)
1578 .Case("object", MCSA_ELF_TypeObject)
1579 .Case("tls_object", MCSA_ELF_TypeTLS)
1580 .Case("common", MCSA_ELF_TypeCommon)
1581 .Case("notype", MCSA_ELF_TypeNoType)
1582 .Default(MCSA_Invalid);
1583
1584 if (Attr == MCSA_Invalid)
1585 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1586
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001587 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001588 return TokError("unexpected token in '.type' directive");
1589
1590 Lex();
1591
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001592 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001593
1594 return false;
1595}
1596
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001597/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001598/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1599bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001600 CheckForValidSection();
1601
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001602 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001603 StringRef Name;
1604 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001605 return TokError("expected identifier in directive");
1606
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001607 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001608 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001609
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001611 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001612 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001613
1614 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001615 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001616 if (ParseAbsoluteExpression(Size))
1617 return true;
1618
1619 int64_t Pow2Alignment = 0;
1620 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001621 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001622 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001623 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001624 if (ParseAbsoluteExpression(Pow2Alignment))
1625 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001626
1627 // If this target takes alignments in bytes (not log) validate and convert.
1628 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1629 if (!isPowerOf2_64(Pow2Alignment))
1630 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1631 Pow2Alignment = Log2_64(Pow2Alignment);
1632 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001633 }
1634
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001635 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001636 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001637
Sean Callanan79ed1a82010-01-19 20:22:31 +00001638 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001639
Chris Lattner1fc3d752009-07-09 17:25:12 +00001640 // NOTE: a size of zero for a .comm should create a undefined symbol
1641 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001642 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001643 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1644 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001645
Eric Christopherc260a3e2010-05-14 01:38:54 +00001646 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001647 // may internally end up wanting an alignment in bytes.
1648 // FIXME: Diagnose overflow.
1649 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001650 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1651 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001652
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001653 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001654 return Error(IDLoc, "invalid symbol redefinition");
1655
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001656 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001657 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001658 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001659 getStreamer().EmitZerofill(Ctx.getMachOSection(
1660 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1661 0, SectionKind::getBSS()),
1662 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001663 return false;
1664 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001665
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001666 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001667 return false;
1668}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001669
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001670/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001671/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001672bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001673 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001674 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001675
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001676 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001677 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001678 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001679
Sean Callanan79ed1a82010-01-19 20:22:31 +00001680 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001681
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001682 if (Str.empty())
1683 Error(Loc, ".abort detected. Assembly stopping.");
1684 else
1685 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001686 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001687
1688 return false;
1689}
Kevin Enderby71148242009-07-14 21:35:03 +00001690
Kevin Enderby1f049b22009-07-14 23:21:55 +00001691/// ParseDirectiveInclude
1692/// ::= .include "filename"
1693bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001695 return TokError("expected string in '.include' directive");
1696
Sean Callanan18b83232010-01-19 21:44:56 +00001697 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001698 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001699 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001700
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001702 return TokError("unexpected token in '.include' directive");
1703
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001704 // Strip the quotes.
1705 Filename = Filename.substr(1, Filename.size()-2);
1706
1707 // Attempt to switch the lexer to the included file before consuming the end
1708 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001709 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001710 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001711 return true;
1712 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001713
1714 return false;
1715}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001716
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001717/// ParseDirectiveIf
1718/// ::= .if expression
1719bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001720 TheCondStack.push_back(TheCondState);
1721 TheCondState.TheCond = AsmCond::IfCond;
1722 if(TheCondState.Ignore) {
1723 EatToEndOfStatement();
1724 }
1725 else {
1726 int64_t ExprValue;
1727 if (ParseAbsoluteExpression(ExprValue))
1728 return true;
1729
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001730 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001731 return TokError("unexpected token in '.if' directive");
1732
Sean Callanan79ed1a82010-01-19 20:22:31 +00001733 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001734
1735 TheCondState.CondMet = ExprValue;
1736 TheCondState.Ignore = !TheCondState.CondMet;
1737 }
1738
1739 return false;
1740}
1741
1742/// ParseDirectiveElseIf
1743/// ::= .elseif expression
1744bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1745 if (TheCondState.TheCond != AsmCond::IfCond &&
1746 TheCondState.TheCond != AsmCond::ElseIfCond)
1747 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1748 " an .elseif");
1749 TheCondState.TheCond = AsmCond::ElseIfCond;
1750
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001751 bool LastIgnoreState = false;
1752 if (!TheCondStack.empty())
1753 LastIgnoreState = TheCondStack.back().Ignore;
1754 if (LastIgnoreState || TheCondState.CondMet) {
1755 TheCondState.Ignore = true;
1756 EatToEndOfStatement();
1757 }
1758 else {
1759 int64_t ExprValue;
1760 if (ParseAbsoluteExpression(ExprValue))
1761 return true;
1762
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001763 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001764 return TokError("unexpected token in '.elseif' directive");
1765
Sean Callanan79ed1a82010-01-19 20:22:31 +00001766 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001767 TheCondState.CondMet = ExprValue;
1768 TheCondState.Ignore = !TheCondState.CondMet;
1769 }
1770
1771 return false;
1772}
1773
1774/// ParseDirectiveElse
1775/// ::= .else
1776bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001777 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001778 return TokError("unexpected token in '.else' directive");
1779
Sean Callanan79ed1a82010-01-19 20:22:31 +00001780 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001781
1782 if (TheCondState.TheCond != AsmCond::IfCond &&
1783 TheCondState.TheCond != AsmCond::ElseIfCond)
1784 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1785 ".elseif");
1786 TheCondState.TheCond = AsmCond::ElseCond;
1787 bool LastIgnoreState = false;
1788 if (!TheCondStack.empty())
1789 LastIgnoreState = TheCondStack.back().Ignore;
1790 if (LastIgnoreState || TheCondState.CondMet)
1791 TheCondState.Ignore = true;
1792 else
1793 TheCondState.Ignore = false;
1794
1795 return false;
1796}
1797
1798/// ParseDirectiveEndIf
1799/// ::= .endif
1800bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001802 return TokError("unexpected token in '.endif' directive");
1803
Sean Callanan79ed1a82010-01-19 20:22:31 +00001804 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001805
1806 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1807 TheCondStack.empty())
1808 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1809 ".else");
1810 if (!TheCondStack.empty()) {
1811 TheCondState = TheCondStack.back();
1812 TheCondStack.pop_back();
1813 }
1814
1815 return false;
1816}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001817
1818/// ParseDirectiveFile
1819/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001820bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001821 // FIXME: I'm not sure what this is.
1822 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001823 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001824 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001825 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001826 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001827
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001828 if (FileNumber < 1)
1829 return TokError("file number less than one");
1830 }
1831
Daniel Dunbareceec052010-07-12 17:45:27 +00001832 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001833 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001834
Chris Lattnerd32e8032010-01-25 19:02:58 +00001835 StringRef Filename = getTok().getString();
1836 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001837 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001838
Daniel Dunbareceec052010-07-12 17:45:27 +00001839 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001840 return TokError("unexpected token in '.file' directive");
1841
Chris Lattnerd32e8032010-01-25 19:02:58 +00001842 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001843 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001844 else {
1845 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1846 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001847 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001848 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001849
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001850 return false;
1851}
1852
1853/// ParseDirectiveLine
1854/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001855bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001856 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1857 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001858 return TokError("unexpected token in '.line' directive");
1859
Sean Callanan18b83232010-01-19 21:44:56 +00001860 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001861 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001862 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001863
1864 // FIXME: Do something with the .line.
1865 }
1866
Daniel Dunbareceec052010-07-12 17:45:27 +00001867 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001868 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001869
1870 return false;
1871}
1872
1873
1874/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001875/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001876/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1877/// The first number is a file number, must have been previously assigned with
1878/// a .file directive, the second number is the line number and optionally the
1879/// third number is a column position (zero if not specified). The remaining
1880/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001881bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001882
Daniel Dunbareceec052010-07-12 17:45:27 +00001883 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001884 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001885 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001886 if (FileNumber < 1)
1887 return TokError("file number less than one in '.loc' directive");
1888 if (!getContext().ValidateDwarfFileNumber(FileNumber))
1889 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001890 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001891
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001892 int64_t LineNumber = 0;
1893 if (getLexer().is(AsmToken::Integer)) {
1894 LineNumber = getTok().getIntVal();
1895 if (LineNumber < 1)
1896 return TokError("line number less than one in '.loc' directive");
1897 Lex();
1898 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001899
1900 int64_t ColumnPos = 0;
1901 if (getLexer().is(AsmToken::Integer)) {
1902 ColumnPos = getTok().getIntVal();
1903 if (ColumnPos < 0)
1904 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001905 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001906 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001907
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001908 unsigned Flags = 0;
1909 unsigned Isa = 0;
1910 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1911 for (;;) {
1912 if (getLexer().is(AsmToken::EndOfStatement))
1913 break;
1914
1915 StringRef Name;
1916 SMLoc Loc = getTok().getLoc();
1917 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001918 return TokError("unexpected token in '.loc' directive");
1919
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001920 if (Name == "basic_block")
1921 Flags |= DWARF2_FLAG_BASIC_BLOCK;
1922 else if (Name == "prologue_end")
1923 Flags |= DWARF2_FLAG_PROLOGUE_END;
1924 else if (Name == "epilogue_begin")
1925 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
1926 else if (Name == "is_stmt") {
1927 SMLoc Loc = getTok().getLoc();
1928 const MCExpr *Value;
1929 if (getParser().ParseExpression(Value))
1930 return true;
1931 // The expression must be the constant 0 or 1.
1932 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1933 int Value = MCE->getValue();
1934 if (Value == 0)
1935 Flags &= ~DWARF2_FLAG_IS_STMT;
1936 else if (Value == 1)
1937 Flags |= DWARF2_FLAG_IS_STMT;
1938 else
1939 return Error(Loc, "is_stmt value not 0 or 1");
1940 }
1941 else {
1942 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
1943 }
1944 }
1945 else if (Name == "isa") {
1946 SMLoc Loc = getTok().getLoc();
1947 const MCExpr *Value;
1948 if (getParser().ParseExpression(Value))
1949 return true;
1950 // The expression must be a constant greater or equal to 0.
1951 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1952 int Value = MCE->getValue();
1953 if (Value < 0)
1954 return Error(Loc, "isa number less than zero");
1955 Isa = Value;
1956 }
1957 else {
1958 return Error(Loc, "isa number not a constant value");
1959 }
1960 }
1961 else {
1962 return Error(Loc, "unknown sub-directive in '.loc' directive");
1963 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001964
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001965 if (getLexer().is(AsmToken::EndOfStatement))
1966 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001967 }
1968 }
1969
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001970 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001971
1972 return false;
1973}
1974
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001975/// ParseDirectiveMacrosOnOff
1976/// ::= .macros_on
1977/// ::= .macros_off
1978bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1979 SMLoc DirectiveLoc) {
1980 if (getLexer().isNot(AsmToken::EndOfStatement))
1981 return Error(getLexer().getLoc(),
1982 "unexpected token in '" + Directive + "' directive");
1983
1984 getParser().MacrosEnabled = Directive == ".macros_on";
1985
1986 return false;
1987}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001988
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001989/// ParseDirectiveMacro
1990/// ::= .macro name
1991bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1992 SMLoc DirectiveLoc) {
1993 StringRef Name;
1994 if (getParser().ParseIdentifier(Name))
1995 return TokError("expected identifier in directive");
1996
1997 if (getLexer().isNot(AsmToken::EndOfStatement))
1998 return TokError("unexpected token in '.macro' directive");
1999
2000 // Eat the end of statement.
2001 Lex();
2002
2003 AsmToken EndToken, StartToken = getTok();
2004
2005 // Lex the macro definition.
2006 for (;;) {
2007 // Check whether we have reached the end of the file.
2008 if (getLexer().is(AsmToken::Eof))
2009 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2010
2011 // Otherwise, check whether we have reach the .endmacro.
2012 if (getLexer().is(AsmToken::Identifier) &&
2013 (getTok().getIdentifier() == ".endm" ||
2014 getTok().getIdentifier() == ".endmacro")) {
2015 EndToken = getTok();
2016 Lex();
2017 if (getLexer().isNot(AsmToken::EndOfStatement))
2018 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2019 "' directive");
2020 break;
2021 }
2022
2023 // Otherwise, scan til the end of the statement.
2024 getParser().EatToEndOfStatement();
2025 }
2026
2027 if (getParser().MacroMap.lookup(Name)) {
2028 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2029 }
2030
2031 const char *BodyStart = StartToken.getLoc().getPointer();
2032 const char *BodyEnd = EndToken.getLoc().getPointer();
2033 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2034 getParser().MacroMap[Name] = new Macro(Name, Body);
2035 return false;
2036}
2037
2038/// ParseDirectiveEndMacro
2039/// ::= .endm
2040/// ::= .endmacro
2041bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2042 SMLoc DirectiveLoc) {
2043 if (getLexer().isNot(AsmToken::EndOfStatement))
2044 return TokError("unexpected token in '" + Directive + "' directive");
2045
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002046 // If we are inside a macro instantiation, terminate the current
2047 // instantiation.
2048 if (!getParser().ActiveMacros.empty()) {
2049 getParser().HandleMacroExit();
2050 return false;
2051 }
2052
2053 // Otherwise, this .endmacro is a stray entry in the file; well formed
2054 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002055 return TokError("unexpected '" + Directive + "' in file, "
2056 "no current macro definition");
2057}
2058
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002059void GenericAsmParser::ParseUleb128(uint64_t Value) {
2060 const uint64_t Mask = (1 << 7) - 1;
2061 do {
2062 unsigned Byte = Value & Mask;
2063 Value >>= 7;
2064 if (Value) // Not the last one
2065 Byte |= (1 << 7);
2066 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2067 } while (Value);
2068}
2069
2070void GenericAsmParser::ParseSleb128(int64_t Value) {
2071 const int64_t Mask = (1 << 7) - 1;
2072 for(;;) {
2073 unsigned Byte = Value & Mask;
2074 Value >>= 7;
2075 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2076 (Value == -1 && (Byte & 0x40) != 0));
2077 if (!Done)
2078 Byte |= (1 << 7);
2079 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2080 if (Done)
2081 break;
2082 }
2083}
2084
2085bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2086 int64_t Value;
2087 if (getParser().ParseAbsoluteExpression(Value))
2088 return true;
2089
2090 if (getLexer().isNot(AsmToken::EndOfStatement))
2091 return TokError("unexpected token in directive");
2092
2093 if (DirName[1] == 's')
2094 ParseSleb128(Value);
2095 else
2096 ParseUleb128(Value);
2097 return false;
2098}
2099
2100
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002101/// \brief Create an MCAsmParser instance.
2102MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2103 MCContext &C, MCStreamer &Out,
2104 const MCAsmInfo &MAI) {
2105 return new AsmParser(T, SM, C, Out, MAI);
2106}