blob: 310574fe27be5acd3b4e322b2476c61e04f34904 [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 Dunbaraef87e32010-07-18 18:31:38 +0000105public:
106 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
107 const MCAsmInfo &MAI);
108 ~AsmParser();
109
110 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
111
112 void AddDirectiveHandler(MCAsmParserExtension *Object,
113 StringRef Directive,
114 DirectiveHandler Handler) {
115 DirectiveMap[Directive] = std::make_pair(Object, Handler);
116 }
117
118public:
119 /// @name MCAsmParser Interface
120 /// {
121
122 virtual SourceMgr &getSourceManager() { return SrcMgr; }
123 virtual MCAsmLexer &getLexer() { return Lexer; }
124 virtual MCContext &getContext() { return Ctx; }
125 virtual MCStreamer &getStreamer() { return Out; }
126
127 virtual void Warning(SMLoc L, const Twine &Meg);
128 virtual bool Error(SMLoc L, const Twine &Msg);
129
130 const AsmToken &Lex();
131
132 bool ParseExpression(const MCExpr *&Res);
133 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
134 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
135 virtual bool ParseAbsoluteExpression(int64_t &Res);
136
137 /// }
138
139private:
140 bool ParseStatement();
141
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000142 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
143 void HandleMacroExit();
144
145 void PrintMacroInstantiations();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
147
148 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
149 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000150
151 /// \brief Reset the current lexer position to that given by \arg Loc. The
152 /// current token is not set; clients should ensure Lex() is called
153 /// subsequently.
154 void JumpToLoc(SMLoc Loc);
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000157
158 /// \brief Parse up to the end of statement and a return the contents from the
159 /// current token until the end of the statement; the current token on exit
160 /// will be either the EndOfStatement or EOF.
161 StringRef ParseStringToEndOfStatement();
162
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163 bool ParseAssignment(StringRef Name);
164
165 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
166 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
167 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
168
169 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
170 /// and set \arg Res to the identifier contents.
171 bool ParseIdentifier(StringRef &Res);
172
173 // Directive Parsing.
174 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
175 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
176 bool ParseDirectiveFill(); // ".fill"
177 bool ParseDirectiveSpace(); // ".space"
178 bool ParseDirectiveSet(); // ".set"
179 bool ParseDirectiveOrg(); // ".org"
180 // ".align{,32}", ".p2align{,w,l}"
181 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
182
183 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
184 /// accepts a single symbol (which should be a label or an external).
185 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
186 bool ParseDirectiveELFType(); // ELF specific ".type"
187
188 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
189
190 bool ParseDirectiveAbort(); // ".abort"
191 bool ParseDirectiveInclude(); // ".include"
192
193 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
194 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
195 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
196 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
197
198 /// ParseEscapedString - Parse the current token as a string which may include
199 /// escaped characters and return the string contents.
200 bool ParseEscapedString(std::string &Data);
201};
202
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000203/// \brief Generic implementations of directive handling, etc. which is shared
204/// (or the default, at least) for all assembler parser.
205class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000206 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
207 void AddDirectiveHandler(StringRef Directive) {
208 getParser().AddDirectiveHandler(this, Directive,
209 HandleDirective<GenericAsmParser, Handler>);
210 }
211
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000212public:
213 GenericAsmParser() {}
214
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000215 AsmParser &getParser() {
216 return (AsmParser&) this->MCAsmParserExtension::getParser();
217 }
218
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000219 virtual void Initialize(MCAsmParser &Parser) {
220 // Call the base implementation.
221 this->MCAsmParserExtension::Initialize(Parser);
222
223 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000224 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
225 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
226 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000227
228 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000229 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
230 ".macros_on");
231 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
232 ".macros_off");
233 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
234 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
235 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000236 }
237
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000238 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
239 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
240 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000241
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000242 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000243 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
244 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000245};
246
247}
248
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000249namespace llvm {
250
251extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000252extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000253
254}
255
Chris Lattneraaec2052010-01-19 19:46:13 +0000256enum { DEFAULT_ADDRSPACE = 0 };
257
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000258AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
259 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000260 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000261 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000262 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000263 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000264
265 // Initialize the generic parser.
266 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000267
268 // Initialize the platform / file format parser.
269 //
270 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
271 // created.
272 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000273 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000274 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000275 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000276 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000277 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000278 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000279}
280
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000281AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000282 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
283
284 // Destroy any macros.
285 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
286 ie = MacroMap.end(); it != ie; ++it)
287 delete it->getValue();
288
Daniel Dunbare4749702010-07-12 18:12:02 +0000289 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000290 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000291}
292
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000293void AsmParser::PrintMacroInstantiations() {
294 // Print the active macro instantiation stack.
295 for (std::vector<MacroInstantiation*>::const_reverse_iterator
296 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
297 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
298 "note");
299}
300
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000301void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000302 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000303 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000304}
305
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000306bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000307 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000308 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000309 return true;
310}
311
Sean Callananbf2013e2010-01-20 23:19:55 +0000312void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
313 const char *Type) const {
314 SrcMgr.PrintMessage(Loc, Msg, Type);
315}
Sean Callananfd0b0282010-01-21 00:19:58 +0000316
317bool AsmParser::EnterIncludeFile(const std::string &Filename) {
318 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
319 if (NewBuf == -1)
320 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000321
Sean Callananfd0b0282010-01-21 00:19:58 +0000322 CurBuffer = NewBuf;
323
324 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
325
326 return false;
327}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000328
329void AsmParser::JumpToLoc(SMLoc Loc) {
330 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
331 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
332}
333
Sean Callananfd0b0282010-01-21 00:19:58 +0000334const AsmToken &AsmParser::Lex() {
335 const AsmToken *tok = &Lexer.Lex();
336
337 if (tok->is(AsmToken::Eof)) {
338 // If this is the end of an included file, pop the parent file off the
339 // include stack.
340 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
341 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000342 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000343 tok = &Lexer.Lex();
344 }
345 }
346
347 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000348 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000349
Sean Callananfd0b0282010-01-21 00:19:58 +0000350 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000351}
352
Chris Lattner79180e22010-04-05 23:15:42 +0000353bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000354 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000355 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000356 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000357 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000358 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000359 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
360 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000361
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000362 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000363 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000364
Chris Lattnerb717fb02009-07-02 21:53:43 +0000365 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000366
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000367 AsmCond StartingCondState = TheCondState;
368
Chris Lattnerb717fb02009-07-02 21:53:43 +0000369 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000371 if (!ParseStatement()) continue;
372
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000373 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000374 HadError = true;
375 EatToEndOfStatement();
376 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000377
378 if (TheCondState.TheCond != StartingCondState.TheCond ||
379 TheCondState.Ignore != StartingCondState.Ignore)
380 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000381
382 // Check to see there are no empty DwarfFile slots.
383 const std::vector<MCDwarfFile *> &MCDwarfFiles =
384 getContext().getMCDwarfFiles();
385 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
386 if (!MCDwarfFiles[i]){
387 TokError("unassigned file number: " + Twine(i) + " for .file directives");
388 HadError = true;
389 }
390 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000391
Chris Lattner79180e22010-04-05 23:15:42 +0000392 // Finalize the output stream if there are no errors and if the client wants
393 // us to.
394 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000395 Out.Finish();
396
Chris Lattnerb717fb02009-07-02 21:53:43 +0000397 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000398}
399
Chris Lattner2cf5f142009-06-22 01:29:09 +0000400/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
401void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000402 while (Lexer.isNot(AsmToken::EndOfStatement) &&
403 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000404 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000405
406 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000407 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000408 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000409}
410
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000411StringRef AsmParser::ParseStringToEndOfStatement() {
412 const char *Start = getTok().getLoc().getPointer();
413
414 while (Lexer.isNot(AsmToken::EndOfStatement) &&
415 Lexer.isNot(AsmToken::Eof))
416 Lex();
417
418 const char *End = getTok().getLoc().getPointer();
419 return StringRef(Start, End - Start);
420}
Chris Lattnerc4193832009-06-22 05:51:26 +0000421
Chris Lattner74ec1a32009-06-22 06:32:03 +0000422/// ParseParenExpr - Parse a paren expression and return it.
423/// NOTE: This assumes the leading '(' has already been consumed.
424///
425/// parenexpr ::= expr)
426///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000427bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000428 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000429 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000430 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000431 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000432 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000433 return false;
434}
Chris Lattnerc4193832009-06-22 05:51:26 +0000435
Chris Lattner74ec1a32009-06-22 06:32:03 +0000436/// ParsePrimaryExpr - Parse a primary expression and return it.
437/// primaryexpr ::= (parenexpr
438/// primaryexpr ::= symbol
439/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000440/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000441/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000442bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000443 switch (Lexer.getKind()) {
444 default:
445 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000446 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000447 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000448 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000449 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000450 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000451 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000452 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000453 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000454 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000455 EndLoc = Lexer.getLoc();
456
457 StringRef Identifier;
458 if (ParseIdentifier(Identifier))
459 return false;
460
Daniel Dunbarfffff912009-10-16 01:34:54 +0000461 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000462 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000463 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000464
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000465 // Mark the symbol as used in an expression.
466 Sym->setUsedInExpr(true);
467
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000468 // Lookup the symbol variant if used.
469 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000470 if (Split.first.size() != Identifier.size())
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000471 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
472
Daniel Dunbarfffff912009-10-16 01:34:54 +0000473 // If this is an absolute variable reference, substitute it now to preserve
474 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000475 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000476 if (Variant)
477 return Error(EndLoc, "unexpected modified on variable reference");
478
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000479 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000480 return false;
481 }
482
483 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000484 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000485 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000486 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000487 case AsmToken::Integer: {
488 SMLoc Loc = getTok().getLoc();
489 int64_t IntVal = getTok().getIntVal();
490 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000491 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000492 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000493 // Look for 'b' or 'f' following an Integer as a directional label
494 if (Lexer.getKind() == AsmToken::Identifier) {
495 StringRef IDVal = getTok().getString();
496 if (IDVal == "f" || IDVal == "b"){
497 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
498 IDVal == "f" ? 1 : 0);
499 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
500 getContext());
501 if(IDVal == "b" && Sym->isUndefined())
502 return Error(Loc, "invalid reference to undefined symbol");
503 EndLoc = Lexer.getLoc();
504 Lex(); // Eat identifier.
505 }
506 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000507 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000508 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000509 case AsmToken::Dot: {
510 // This is a '.' reference, which references the current PC. Emit a
511 // temporary label to the streamer and refer to it.
512 MCSymbol *Sym = Ctx.CreateTempSymbol();
513 Out.EmitLabel(Sym);
514 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
515 EndLoc = Lexer.getLoc();
516 Lex(); // Eat identifier.
517 return false;
518 }
519
Daniel Dunbar3f872332009-07-28 16:08:33 +0000520 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000521 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000522 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000523 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000524 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000525 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000526 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000527 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000528 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000529 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000530 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000531 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000532 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000533 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000534 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000535 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000536 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000537 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000538 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000539 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000540 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000541 }
542}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000543
Chris Lattnerb4307b32010-01-15 19:28:38 +0000544bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000545 SMLoc EndLoc;
546 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000547}
548
Chris Lattner74ec1a32009-06-22 06:32:03 +0000549/// ParseExpression - Parse an expression and return it.
550///
551/// expr ::= expr +,- expr -> lowest.
552/// expr ::= expr |,^,&,! expr -> middle.
553/// expr ::= expr *,/,%,<<,>> expr -> highest.
554/// expr ::= primaryexpr
555///
Chris Lattner54482b42010-01-15 19:39:23 +0000556bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000557 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000558 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000559 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
560 return true;
561
562 // Try to constant fold it up front, if possible.
563 int64_t Value;
564 if (Res->EvaluateAsAbsolute(Value))
565 Res = MCConstantExpr::Create(Value, getContext());
566
567 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000568}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000569
Chris Lattnerb4307b32010-01-15 19:28:38 +0000570bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000571 Res = 0;
572 return ParseParenExpr(Res, EndLoc) ||
573 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000574}
575
Daniel Dunbar475839e2009-06-29 20:37:27 +0000576bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000577 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000578
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000579 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580 if (ParseExpression(Expr))
581 return true;
582
Daniel Dunbare00b0112009-10-16 01:57:52 +0000583 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000584 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000585
586 return false;
587}
588
Daniel Dunbar3f872332009-07-28 16:08:33 +0000589static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000590 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000591 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000592 default:
593 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000594
595 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000596 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000597 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000598 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000599 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000600 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000601 return 1;
602
603 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000604 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000605 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000606 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000607 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000608 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000609 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000610 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000611 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000612 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000613 case AsmToken::ExclaimEqual:
614 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000615 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000616 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000617 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000618 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000619 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000620 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000621 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000622 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000623 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000624 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000625 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000626 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000627 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000628 return 2;
629
630 // Intermediate Precedence: |, &, ^
631 //
632 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000633 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000634 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000635 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000636 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000637 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000638 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000639 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000640 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000641 return 3;
642
643 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000644 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000645 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000646 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000647 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000648 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000650 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000651 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000652 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000653 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000654 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000655 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000656 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000657 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000658 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000659 }
660}
661
662
663/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
664/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000665bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
666 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000667 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000668 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000669 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000670
671 // If the next token is lower precedence than we are allowed to eat, return
672 // successfully with what we ate already.
673 if (TokPrec < Precedence)
674 return false;
675
Sean Callanan79ed1a82010-01-19 20:22:31 +0000676 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000677
678 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000679 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000680 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000681
682 // If BinOp binds less tightly with RHS than the operator after RHS, let
683 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000684 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000685 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000686 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000687 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000688 }
689
Daniel Dunbar475839e2009-06-29 20:37:27 +0000690 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000691 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000692 }
693}
694
Chris Lattnerc4193832009-06-22 05:51:26 +0000695
696
697
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000698/// ParseStatement:
699/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000700/// ::= Label* Directive ...Operands... EndOfStatement
701/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000702bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000703 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000704 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000705 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000706 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000707 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000708
709 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000710 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000711 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000712 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000713 int64_t LocalLabelVal = -1;
714 // GUESS allow an integer followed by a ':' as a directional local label
715 if (Lexer.is(AsmToken::Integer)) {
716 LocalLabelVal = getTok().getIntVal();
717 if (LocalLabelVal < 0) {
718 if (!TheCondState.Ignore)
719 return TokError("unexpected token at start of statement");
720 IDVal = "";
721 }
722 else {
723 IDVal = getTok().getString();
724 Lex(); // Consume the integer token to be used as an identifier token.
725 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000726 if (!TheCondState.Ignore)
727 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000728 }
729 }
730 }
731 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000732 if (!TheCondState.Ignore)
733 return TokError("unexpected token at start of statement");
734 IDVal = "";
735 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000736
Chris Lattner7834fac2010-04-17 18:14:27 +0000737 // Handle conditional assembly here before checking for skipping. We
738 // have to do this so that .endif isn't skipped in a ".if 0" block for
739 // example.
740 if (IDVal == ".if")
741 return ParseDirectiveIf(IDLoc);
742 if (IDVal == ".elseif")
743 return ParseDirectiveElseIf(IDLoc);
744 if (IDVal == ".else")
745 return ParseDirectiveElse(IDLoc);
746 if (IDVal == ".endif")
747 return ParseDirectiveEndIf(IDLoc);
748
749 // If we are in a ".if 0" block, ignore this statement.
750 if (TheCondState.Ignore) {
751 EatToEndOfStatement();
752 return false;
753 }
754
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000755 // FIXME: Recurse on local labels?
756
757 // See what kind of statement we have.
758 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000759 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000760 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000761 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000762
763 // Diagnose attempt to use a variable as a label.
764 //
765 // FIXME: Diagnostics. Note the location of the definition as a label.
766 // FIXME: This doesn't diagnose assignment to a symbol which has been
767 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000768 MCSymbol *Sym;
769 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000770 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000771 else
772 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000773 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000774 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000775
Daniel Dunbar959fd882009-08-26 22:13:22 +0000776 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000777 Out.EmitLabel(Sym);
778
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000779 // Consume any end of statement token, if present, to avoid spurious
780 // AddBlankLine calls().
781 if (Lexer.is(AsmToken::EndOfStatement)) {
782 Lex();
783 if (Lexer.is(AsmToken::Eof))
784 return false;
785 }
786
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000787 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000788 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000789
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000791 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000792 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000793
Daniel Dunbare2ace502009-08-31 08:09:09 +0000794 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000795
796 default: // Normal instruction or directive.
797 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000798 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000799
800 // If macros are enabled, check to see if this is a macro instantiation.
801 if (MacrosEnabled)
802 if (const Macro *M = MacroMap.lookup(IDVal))
803 return HandleMacroEntry(IDVal, IDLoc, M);
804
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000805 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000806 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000807 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000808 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000809 return ParseDirectiveSet();
810
Daniel Dunbara0d14262009-06-24 23:30:00 +0000811 // Data directives
812
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000813 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000814 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000815 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000816 return ParseDirectiveAscii(true);
817
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000818 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000819 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000820 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000821 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000822 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000823 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000824 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000825 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000826
Eli Friedman5d68ec22010-07-19 04:17:25 +0000827 if (IDVal == ".align") {
828 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
829 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
830 }
831 if (IDVal == ".align32") {
832 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
833 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
834 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000835 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000836 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000838 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000840 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000842 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000843 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000844 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000845 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000846 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
847
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000848 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000849 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000850
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000851 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000852 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000853 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000854 return ParseDirectiveSpace();
855
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000856 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000857
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000858 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000859 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000860 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000861 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000862 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000863 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000865 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000866 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000867 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000868 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000869 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000870 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000871 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000872 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000873 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000874 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000875 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000876 if (IDVal == ".type")
877 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000878 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000879 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000880 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000881 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000882 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000883 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000884 if (IDVal == ".weak_def_can_be_hidden")
885 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000886
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000887 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000888 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000889 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000890 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000891
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000892 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000893 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000894 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000895 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000896
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000897 // Look up the handler in the handler table.
898 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
899 DirectiveMap.lookup(IDVal);
900 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000901 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000902
Kevin Enderby9c656452009-09-10 20:51:44 +0000903 // Target hook for parsing target specific directives.
904 if (!getTargetParser().ParseDirective(ID))
905 return false;
906
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000907 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000908 EatToEndOfStatement();
909 return false;
910 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000911
Chris Lattnera7f13542010-05-19 23:34:33 +0000912 // Canonicalize the opcode to lower case.
913 SmallString<128> Opcode;
914 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
915 Opcode.push_back(tolower(IDVal[i]));
916
Chris Lattner98986712010-01-14 22:21:20 +0000917 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000918 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000919 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000920
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000921 // Dump the parsed representation, if requested.
922 if (getShowParsedOperands()) {
923 SmallString<256> Str;
924 raw_svector_ostream OS(Str);
925 OS << "parsed instruction: [";
926 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
927 if (i != 0)
928 OS << ", ";
929 ParsedOperands[i]->dump(OS);
930 }
931 OS << "]";
932
933 PrintMessage(IDLoc, OS.str(), "note");
934 }
935
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000936 // If parsing succeeded, match the instruction.
937 if (!HadError) {
938 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000939 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000940 // Emit the instruction on success.
941 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000942 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000943 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000944 }
Chris Lattner98986712010-01-14 22:21:20 +0000945
Chris Lattner98986712010-01-14 22:21:20 +0000946 // Free any parsed operands.
947 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
948 delete ParsedOperands[i];
949
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000950 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000951}
Chris Lattner9a023f72009-06-24 04:43:34 +0000952
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000953MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
954 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000955 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
956{
957 // Macro instantiation is lexical, unfortunately. We construct a new buffer
958 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000959 SmallString<256> Buf;
960 raw_svector_ostream OS(Buf);
961
962 StringRef Body = M->Body;
963 while (!Body.empty()) {
964 // Scan for the next substitution.
965 std::size_t End = Body.size(), Pos = 0;
966 for (; Pos != End; ++Pos) {
967 // Check for a substitution or escape.
968 if (Body[Pos] != '$' || Pos + 1 == End)
969 continue;
970
971 char Next = Body[Pos + 1];
972 if (Next == '$' || Next == 'n' || isdigit(Next))
973 break;
974 }
975
976 // Add the prefix.
977 OS << Body.slice(0, Pos);
978
979 // Check if we reached the end.
980 if (Pos == End)
981 break;
982
983 switch (Body[Pos+1]) {
984 // $$ => $
985 case '$':
986 OS << '$';
987 break;
988
989 // $n => number of arguments
990 case 'n':
991 OS << A.size();
992 break;
993
994 // $[0-9] => argument
995 default: {
996 // Missing arguments are ignored.
997 unsigned Index = Body[Pos+1] - '0';
998 if (Index >= A.size())
999 break;
1000
1001 // Otherwise substitute with the token values, with spaces eliminated.
1002 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1003 ie = A[Index].end(); it != ie; ++it)
1004 OS << it->getString();
1005 break;
1006 }
1007 }
1008
1009 // Update the scan point.
1010 Body = Body.substr(Pos + 2);
1011 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001012
1013 // We include the .endmacro in the buffer as our queue to exit the macro
1014 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001015 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001016
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001017 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001018}
1019
1020bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1021 const Macro *M) {
1022 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1023 // this, although we should protect against infinite loops.
1024 if (ActiveMacros.size() == 20)
1025 return TokError("macros cannot be nested more than 20 levels deep");
1026
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001027 // Parse the macro instantiation arguments.
1028 std::vector<std::vector<AsmToken> > MacroArguments;
1029 MacroArguments.push_back(std::vector<AsmToken>());
1030 unsigned ParenLevel = 0;
1031 for (;;) {
1032 if (Lexer.is(AsmToken::Eof))
1033 return TokError("unexpected token in macro instantiation");
1034 if (Lexer.is(AsmToken::EndOfStatement))
1035 break;
1036
1037 // If we aren't inside parentheses and this is a comma, start a new token
1038 // list.
1039 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1040 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001041 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001042 // Adjust the current parentheses level.
1043 if (Lexer.is(AsmToken::LParen))
1044 ++ParenLevel;
1045 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1046 --ParenLevel;
1047
1048 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001049 MacroArguments.back().push_back(getTok());
1050 }
1051 Lex();
1052 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001053
1054 // Create the macro instantiation object and add to the current macro
1055 // instantiation stack.
1056 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001057 getTok().getLoc(),
1058 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001059 ActiveMacros.push_back(MI);
1060
1061 // Jump to the macro instantiation and prime the lexer.
1062 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1063 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1064 Lex();
1065
1066 return false;
1067}
1068
1069void AsmParser::HandleMacroExit() {
1070 // Jump to the EndOfStatement we should return to, and consume it.
1071 JumpToLoc(ActiveMacros.back()->ExitLoc);
1072 Lex();
1073
1074 // Pop the instantiation entry.
1075 delete ActiveMacros.back();
1076 ActiveMacros.pop_back();
1077}
1078
Benjamin Kramer38e59892010-07-14 22:38:02 +00001079bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001080 // FIXME: Use better location, we should use proper tokens.
1081 SMLoc EqualLoc = Lexer.getLoc();
1082
Daniel Dunbar821e3332009-08-31 08:09:28 +00001083 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001084 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001085 return true;
1086
Daniel Dunbar3f872332009-07-28 16:08:33 +00001087 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001088 return TokError("unexpected token in assignment");
1089
1090 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001091 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001092
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001093 // Validate that the LHS is allowed to be a variable (either it has not been
1094 // used as a symbol, or it is an absolute symbol).
1095 MCSymbol *Sym = getContext().LookupSymbol(Name);
1096 if (Sym) {
1097 // Diagnose assignment to a label.
1098 //
1099 // FIXME: Diagnostics. Note the location of the definition as a label.
1100 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001101 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1102 ; // Allow redefinitions of undefined symbols only used in directives.
1103 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001104 return Error(EqualLoc, "redefinition of '" + Name + "'");
1105 else if (!Sym->isVariable())
1106 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001107 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001108 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1109 Name + "'");
1110 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001111 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001112
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001113 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001114
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001115 Sym->setUsedInExpr(true);
1116
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001117 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001118 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001119
1120 return false;
1121}
1122
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001123/// ParseIdentifier:
1124/// ::= identifier
1125/// ::= string
1126bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001127 // The assembler has relaxed rules for accepting identifiers, in particular we
1128 // allow things like '.globl $foo', which would normally be separate
1129 // tokens. At this level, we have already lexed so we cannot (currently)
1130 // handle this as a context dependent token, instead we detect adjacent tokens
1131 // and return the combined identifier.
1132 if (Lexer.is(AsmToken::Dollar)) {
1133 SMLoc DollarLoc = getLexer().getLoc();
1134
1135 // Consume the dollar sign, and check for a following identifier.
1136 Lex();
1137 if (Lexer.isNot(AsmToken::Identifier))
1138 return true;
1139
1140 // We have a '$' followed by an identifier, make sure they are adjacent.
1141 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1142 return true;
1143
1144 // Construct the joined identifier and consume the token.
1145 Res = StringRef(DollarLoc.getPointer(),
1146 getTok().getIdentifier().size() + 1);
1147 Lex();
1148 return false;
1149 }
1150
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001151 if (Lexer.isNot(AsmToken::Identifier) &&
1152 Lexer.isNot(AsmToken::String))
1153 return true;
1154
Sean Callanan18b83232010-01-19 21:44:56 +00001155 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001156
Sean Callanan79ed1a82010-01-19 20:22:31 +00001157 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001158
1159 return false;
1160}
1161
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001162/// ParseDirectiveSet:
1163/// ::= .set identifier ',' expression
1164bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001165 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001166
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001167 if (ParseIdentifier(Name))
1168 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001169
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001170 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001171 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001172 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001173
Daniel Dunbare2ace502009-08-31 08:09:09 +00001174 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001175}
1176
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001177bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001178 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001179
1180 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001181 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001182 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1183 if (Str[i] != '\\') {
1184 Data += Str[i];
1185 continue;
1186 }
1187
1188 // Recognize escaped characters. Note that this escape semantics currently
1189 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1190 ++i;
1191 if (i == e)
1192 return TokError("unexpected backslash at end of string");
1193
1194 // Recognize octal sequences.
1195 if ((unsigned) (Str[i] - '0') <= 7) {
1196 // Consume up to three octal characters.
1197 unsigned Value = Str[i] - '0';
1198
1199 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1200 ++i;
1201 Value = Value * 8 + (Str[i] - '0');
1202
1203 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1204 ++i;
1205 Value = Value * 8 + (Str[i] - '0');
1206 }
1207 }
1208
1209 if (Value > 255)
1210 return TokError("invalid octal escape sequence (out of range)");
1211
1212 Data += (unsigned char) Value;
1213 continue;
1214 }
1215
1216 // Otherwise recognize individual escapes.
1217 switch (Str[i]) {
1218 default:
1219 // Just reject invalid escape sequences for now.
1220 return TokError("invalid escape sequence (unrecognized character)");
1221
1222 case 'b': Data += '\b'; break;
1223 case 'f': Data += '\f'; break;
1224 case 'n': Data += '\n'; break;
1225 case 'r': Data += '\r'; break;
1226 case 't': Data += '\t'; break;
1227 case '"': Data += '"'; break;
1228 case '\\': Data += '\\'; break;
1229 }
1230 }
1231
1232 return false;
1233}
1234
Daniel Dunbara0d14262009-06-24 23:30:00 +00001235/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001236/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001237bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001238 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001239 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001240 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001241 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001242
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001243 std::string Data;
1244 if (ParseEscapedString(Data))
1245 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001246
1247 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001248 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001249 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1250
Sean Callanan79ed1a82010-01-19 20:22:31 +00001251 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001252
1253 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001254 break;
1255
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001256 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001257 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001258 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001259 }
1260 }
1261
Sean Callanan79ed1a82010-01-19 20:22:31 +00001262 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001263 return false;
1264}
1265
1266/// ParseDirectiveValue
1267/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1268bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001269 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001270 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001271 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001272 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001273 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001274 return true;
1275
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001276 // Special case constant expressions to match code generator.
1277 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001278 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001279 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001280 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001281
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001282 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001283 break;
1284
1285 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001286 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001287 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001288 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001289 }
1290 }
1291
Sean Callanan79ed1a82010-01-19 20:22:31 +00001292 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001293 return false;
1294}
1295
1296/// ParseDirectiveSpace
1297/// ::= .space expression [ , expression ]
1298bool AsmParser::ParseDirectiveSpace() {
1299 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001300 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001301 return true;
1302
1303 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001304 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1305 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001306 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001307 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001308
Daniel Dunbar475839e2009-06-29 20:37:27 +00001309 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001310 return true;
1311
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001312 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001313 return TokError("unexpected token in '.space' directive");
1314 }
1315
Sean Callanan79ed1a82010-01-19 20:22:31 +00001316 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001317
1318 if (NumBytes <= 0)
1319 return TokError("invalid number of bytes in '.space' directive");
1320
1321 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001322 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001323
1324 return false;
1325}
1326
1327/// ParseDirectiveFill
1328/// ::= .fill expression , expression , expression
1329bool AsmParser::ParseDirectiveFill() {
1330 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001331 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001332 return true;
1333
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001334 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001335 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001336 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001337
1338 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001339 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001340 return true;
1341
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001342 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001343 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001344 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001345
1346 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001347 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001348 return true;
1349
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001350 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001351 return TokError("unexpected token in '.fill' directive");
1352
Sean Callanan79ed1a82010-01-19 20:22:31 +00001353 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001354
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001355 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1356 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001357
1358 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001359 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001360
1361 return false;
1362}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001363
1364/// ParseDirectiveOrg
1365/// ::= .org expression [ , expression ]
1366bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001367 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001368 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001369 return true;
1370
1371 // Parse optional fill expression.
1372 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001373 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1374 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001375 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001376 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001377
Daniel Dunbar475839e2009-06-29 20:37:27 +00001378 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001379 return true;
1380
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001381 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001382 return TokError("unexpected token in '.org' directive");
1383 }
1384
Sean Callanan79ed1a82010-01-19 20:22:31 +00001385 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001386
1387 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1388 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001389 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001390
1391 return false;
1392}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001393
1394/// ParseDirectiveAlign
1395/// ::= {.align, ...} expression [ , expression [ , expression ]]
1396bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001397 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001398 int64_t Alignment;
1399 if (ParseAbsoluteExpression(Alignment))
1400 return true;
1401
1402 SMLoc MaxBytesLoc;
1403 bool HasFillExpr = false;
1404 int64_t FillExpr = 0;
1405 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001406 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1407 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001408 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001409 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001410
1411 // The fill expression can be omitted while specifying a maximum number of
1412 // alignment bytes, e.g:
1413 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001414 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001415 HasFillExpr = true;
1416 if (ParseAbsoluteExpression(FillExpr))
1417 return true;
1418 }
1419
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001420 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1421 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001422 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001423 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001424
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001425 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001426 if (ParseAbsoluteExpression(MaxBytesToFill))
1427 return true;
1428
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001429 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001430 return TokError("unexpected token in directive");
1431 }
1432 }
1433
Sean Callanan79ed1a82010-01-19 20:22:31 +00001434 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001435
Daniel Dunbar648ac512010-05-17 21:54:30 +00001436 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001437 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001438
1439 // Compute alignment in bytes.
1440 if (IsPow2) {
1441 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001442 if (Alignment >= 32) {
1443 Error(AlignmentLoc, "invalid alignment value");
1444 Alignment = 31;
1445 }
1446
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001447 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001448 }
1449
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001450 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001451 if (MaxBytesLoc.isValid()) {
1452 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001453 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1454 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001455 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001456 }
1457
1458 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001459 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1460 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001461 MaxBytesToFill = 0;
1462 }
1463 }
1464
Daniel Dunbar648ac512010-05-17 21:54:30 +00001465 // Check whether we should use optimal code alignment for this .align
1466 // directive.
1467 //
1468 // FIXME: This should be using a target hook.
1469 bool UseCodeAlign = false;
1470 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001471 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001472 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001473 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1474 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001475 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001476 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001477 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001478 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1479 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001480 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001481
1482 return false;
1483}
1484
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001485/// ParseDirectiveSymbolAttribute
1486/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001487bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001488 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001489 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001490 StringRef Name;
1491
1492 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001493 return TokError("expected identifier in directive");
1494
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001495 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001496
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001497 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001498
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001499 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001500 break;
1501
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001502 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001503 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001504 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001505 }
1506 }
1507
Sean Callanan79ed1a82010-01-19 20:22:31 +00001508 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001509 return false;
1510}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001511
Matt Fleming924c5e52010-05-21 11:36:59 +00001512/// ParseDirectiveELFType
1513/// ::= .type identifier , @attribute
1514bool AsmParser::ParseDirectiveELFType() {
1515 StringRef Name;
1516 if (ParseIdentifier(Name))
1517 return TokError("expected identifier in directive");
1518
1519 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001520 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001521
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001522 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001523 return TokError("unexpected token in '.type' directive");
1524 Lex();
1525
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001526 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001527 return TokError("expected '@' before type");
1528 Lex();
1529
1530 StringRef Type;
1531 SMLoc TypeLoc;
1532
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001533 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001534 if (ParseIdentifier(Type))
1535 return TokError("expected symbol type in directive");
1536
1537 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1538 .Case("function", MCSA_ELF_TypeFunction)
1539 .Case("object", MCSA_ELF_TypeObject)
1540 .Case("tls_object", MCSA_ELF_TypeTLS)
1541 .Case("common", MCSA_ELF_TypeCommon)
1542 .Case("notype", MCSA_ELF_TypeNoType)
1543 .Default(MCSA_Invalid);
1544
1545 if (Attr == MCSA_Invalid)
1546 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1547
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001548 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001549 return TokError("unexpected token in '.type' directive");
1550
1551 Lex();
1552
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001553 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001554
1555 return false;
1556}
1557
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001558/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001559/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1560bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001562 StringRef Name;
1563 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001564 return TokError("expected identifier in directive");
1565
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001566 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001567 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001568
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001569 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001570 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001571 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001572
1573 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001574 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001575 if (ParseAbsoluteExpression(Size))
1576 return true;
1577
1578 int64_t Pow2Alignment = 0;
1579 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001580 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001581 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001582 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001583 if (ParseAbsoluteExpression(Pow2Alignment))
1584 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001585
1586 // If this target takes alignments in bytes (not log) validate and convert.
1587 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1588 if (!isPowerOf2_64(Pow2Alignment))
1589 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1590 Pow2Alignment = Log2_64(Pow2Alignment);
1591 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001592 }
1593
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001594 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001595 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001596
Sean Callanan79ed1a82010-01-19 20:22:31 +00001597 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001598
Chris Lattner1fc3d752009-07-09 17:25:12 +00001599 // NOTE: a size of zero for a .comm should create a undefined symbol
1600 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001601 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001602 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1603 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001604
Eric Christopherc260a3e2010-05-14 01:38:54 +00001605 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001606 // may internally end up wanting an alignment in bytes.
1607 // FIXME: Diagnose overflow.
1608 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001609 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1610 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001611
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001612 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001613 return Error(IDLoc, "invalid symbol redefinition");
1614
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001615 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001616 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001617 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001618 getStreamer().EmitZerofill(Ctx.getMachOSection(
1619 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1620 0, SectionKind::getBSS()),
1621 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001622 return false;
1623 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001624
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001625 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001626 return false;
1627}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001628
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001629/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001630/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001631bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001632 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001633 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001634
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001635 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001637 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001638
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001640
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001641 if (Str.empty())
1642 Error(Loc, ".abort detected. Assembly stopping.");
1643 else
1644 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001645 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001646
1647 return false;
1648}
Kevin Enderby71148242009-07-14 21:35:03 +00001649
Kevin Enderby1f049b22009-07-14 23:21:55 +00001650/// ParseDirectiveInclude
1651/// ::= .include "filename"
1652bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001653 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001654 return TokError("expected string in '.include' directive");
1655
Sean Callanan18b83232010-01-19 21:44:56 +00001656 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001657 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001658 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001659
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001660 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001661 return TokError("unexpected token in '.include' directive");
1662
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001663 // Strip the quotes.
1664 Filename = Filename.substr(1, Filename.size()-2);
1665
1666 // Attempt to switch the lexer to the included file before consuming the end
1667 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001668 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001669 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001670 return true;
1671 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001672
1673 return false;
1674}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001675
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001676/// ParseDirectiveIf
1677/// ::= .if expression
1678bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001679 TheCondStack.push_back(TheCondState);
1680 TheCondState.TheCond = AsmCond::IfCond;
1681 if(TheCondState.Ignore) {
1682 EatToEndOfStatement();
1683 }
1684 else {
1685 int64_t ExprValue;
1686 if (ParseAbsoluteExpression(ExprValue))
1687 return true;
1688
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001689 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001690 return TokError("unexpected token in '.if' directive");
1691
Sean Callanan79ed1a82010-01-19 20:22:31 +00001692 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001693
1694 TheCondState.CondMet = ExprValue;
1695 TheCondState.Ignore = !TheCondState.CondMet;
1696 }
1697
1698 return false;
1699}
1700
1701/// ParseDirectiveElseIf
1702/// ::= .elseif expression
1703bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1704 if (TheCondState.TheCond != AsmCond::IfCond &&
1705 TheCondState.TheCond != AsmCond::ElseIfCond)
1706 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1707 " an .elseif");
1708 TheCondState.TheCond = AsmCond::ElseIfCond;
1709
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001710 bool LastIgnoreState = false;
1711 if (!TheCondStack.empty())
1712 LastIgnoreState = TheCondStack.back().Ignore;
1713 if (LastIgnoreState || TheCondState.CondMet) {
1714 TheCondState.Ignore = true;
1715 EatToEndOfStatement();
1716 }
1717 else {
1718 int64_t ExprValue;
1719 if (ParseAbsoluteExpression(ExprValue))
1720 return true;
1721
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001722 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001723 return TokError("unexpected token in '.elseif' directive");
1724
Sean Callanan79ed1a82010-01-19 20:22:31 +00001725 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001726 TheCondState.CondMet = ExprValue;
1727 TheCondState.Ignore = !TheCondState.CondMet;
1728 }
1729
1730 return false;
1731}
1732
1733/// ParseDirectiveElse
1734/// ::= .else
1735bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001736 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001737 return TokError("unexpected token in '.else' directive");
1738
Sean Callanan79ed1a82010-01-19 20:22:31 +00001739 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001740
1741 if (TheCondState.TheCond != AsmCond::IfCond &&
1742 TheCondState.TheCond != AsmCond::ElseIfCond)
1743 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1744 ".elseif");
1745 TheCondState.TheCond = AsmCond::ElseCond;
1746 bool LastIgnoreState = false;
1747 if (!TheCondStack.empty())
1748 LastIgnoreState = TheCondStack.back().Ignore;
1749 if (LastIgnoreState || TheCondState.CondMet)
1750 TheCondState.Ignore = true;
1751 else
1752 TheCondState.Ignore = false;
1753
1754 return false;
1755}
1756
1757/// ParseDirectiveEndIf
1758/// ::= .endif
1759bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001760 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001761 return TokError("unexpected token in '.endif' directive");
1762
Sean Callanan79ed1a82010-01-19 20:22:31 +00001763 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001764
1765 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1766 TheCondStack.empty())
1767 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1768 ".else");
1769 if (!TheCondStack.empty()) {
1770 TheCondState = TheCondStack.back();
1771 TheCondStack.pop_back();
1772 }
1773
1774 return false;
1775}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001776
1777/// ParseDirectiveFile
1778/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001779bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001780 // FIXME: I'm not sure what this is.
1781 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001782 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001783 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001784 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001786
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001787 if (FileNumber < 1)
1788 return TokError("file number less than one");
1789 }
1790
Daniel Dunbareceec052010-07-12 17:45:27 +00001791 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001792 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001793
Chris Lattnerd32e8032010-01-25 19:02:58 +00001794 StringRef Filename = getTok().getString();
1795 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001796 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001797
Daniel Dunbareceec052010-07-12 17:45:27 +00001798 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001799 return TokError("unexpected token in '.file' directive");
1800
Chris Lattnerd32e8032010-01-25 19:02:58 +00001801 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001802 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001803 else {
1804 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1805 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001806 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001807 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001808
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001809 return false;
1810}
1811
1812/// ParseDirectiveLine
1813/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001814bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001815 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1816 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001817 return TokError("unexpected token in '.line' directive");
1818
Sean Callanan18b83232010-01-19 21:44:56 +00001819 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001820 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001821 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001822
1823 // FIXME: Do something with the .line.
1824 }
1825
Daniel Dunbareceec052010-07-12 17:45:27 +00001826 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001827 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001828
1829 return false;
1830}
1831
1832
1833/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001834/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001835/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1836/// The first number is a file number, must have been previously assigned with
1837/// a .file directive, the second number is the line number and optionally the
1838/// third number is a column position (zero if not specified). The remaining
1839/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001840bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001841
Daniel Dunbareceec052010-07-12 17:45:27 +00001842 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001843 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001844 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001845 if (FileNumber < 1)
1846 return TokError("file number less than one in '.loc' directive");
1847 if (!getContext().ValidateDwarfFileNumber(FileNumber))
1848 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001849 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001850
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001851 int64_t LineNumber = 0;
1852 if (getLexer().is(AsmToken::Integer)) {
1853 LineNumber = getTok().getIntVal();
1854 if (LineNumber < 1)
1855 return TokError("line number less than one in '.loc' directive");
1856 Lex();
1857 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001858
1859 int64_t ColumnPos = 0;
1860 if (getLexer().is(AsmToken::Integer)) {
1861 ColumnPos = getTok().getIntVal();
1862 if (ColumnPos < 0)
1863 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001864 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001865 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001866
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001867 unsigned Flags = 0;
1868 unsigned Isa = 0;
1869 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1870 for (;;) {
1871 if (getLexer().is(AsmToken::EndOfStatement))
1872 break;
1873
1874 StringRef Name;
1875 SMLoc Loc = getTok().getLoc();
1876 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001877 return TokError("unexpected token in '.loc' directive");
1878
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001879 if (Name == "basic_block")
1880 Flags |= DWARF2_FLAG_BASIC_BLOCK;
1881 else if (Name == "prologue_end")
1882 Flags |= DWARF2_FLAG_PROLOGUE_END;
1883 else if (Name == "epilogue_begin")
1884 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
1885 else if (Name == "is_stmt") {
1886 SMLoc Loc = getTok().getLoc();
1887 const MCExpr *Value;
1888 if (getParser().ParseExpression(Value))
1889 return true;
1890 // The expression must be the constant 0 or 1.
1891 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1892 int Value = MCE->getValue();
1893 if (Value == 0)
1894 Flags &= ~DWARF2_FLAG_IS_STMT;
1895 else if (Value == 1)
1896 Flags |= DWARF2_FLAG_IS_STMT;
1897 else
1898 return Error(Loc, "is_stmt value not 0 or 1");
1899 }
1900 else {
1901 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
1902 }
1903 }
1904 else if (Name == "isa") {
1905 SMLoc Loc = getTok().getLoc();
1906 const MCExpr *Value;
1907 if (getParser().ParseExpression(Value))
1908 return true;
1909 // The expression must be a constant greater or equal to 0.
1910 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1911 int Value = MCE->getValue();
1912 if (Value < 0)
1913 return Error(Loc, "isa number less than zero");
1914 Isa = Value;
1915 }
1916 else {
1917 return Error(Loc, "isa number not a constant value");
1918 }
1919 }
1920 else {
1921 return Error(Loc, "unknown sub-directive in '.loc' directive");
1922 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001923
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001924 if (getLexer().is(AsmToken::EndOfStatement))
1925 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001926 }
1927 }
1928
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001929 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001930
1931 return false;
1932}
1933
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001934/// ParseDirectiveMacrosOnOff
1935/// ::= .macros_on
1936/// ::= .macros_off
1937bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1938 SMLoc DirectiveLoc) {
1939 if (getLexer().isNot(AsmToken::EndOfStatement))
1940 return Error(getLexer().getLoc(),
1941 "unexpected token in '" + Directive + "' directive");
1942
1943 getParser().MacrosEnabled = Directive == ".macros_on";
1944
1945 return false;
1946}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001947
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001948/// ParseDirectiveMacro
1949/// ::= .macro name
1950bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1951 SMLoc DirectiveLoc) {
1952 StringRef Name;
1953 if (getParser().ParseIdentifier(Name))
1954 return TokError("expected identifier in directive");
1955
1956 if (getLexer().isNot(AsmToken::EndOfStatement))
1957 return TokError("unexpected token in '.macro' directive");
1958
1959 // Eat the end of statement.
1960 Lex();
1961
1962 AsmToken EndToken, StartToken = getTok();
1963
1964 // Lex the macro definition.
1965 for (;;) {
1966 // Check whether we have reached the end of the file.
1967 if (getLexer().is(AsmToken::Eof))
1968 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
1969
1970 // Otherwise, check whether we have reach the .endmacro.
1971 if (getLexer().is(AsmToken::Identifier) &&
1972 (getTok().getIdentifier() == ".endm" ||
1973 getTok().getIdentifier() == ".endmacro")) {
1974 EndToken = getTok();
1975 Lex();
1976 if (getLexer().isNot(AsmToken::EndOfStatement))
1977 return TokError("unexpected token in '" + EndToken.getIdentifier() +
1978 "' directive");
1979 break;
1980 }
1981
1982 // Otherwise, scan til the end of the statement.
1983 getParser().EatToEndOfStatement();
1984 }
1985
1986 if (getParser().MacroMap.lookup(Name)) {
1987 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
1988 }
1989
1990 const char *BodyStart = StartToken.getLoc().getPointer();
1991 const char *BodyEnd = EndToken.getLoc().getPointer();
1992 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
1993 getParser().MacroMap[Name] = new Macro(Name, Body);
1994 return false;
1995}
1996
1997/// ParseDirectiveEndMacro
1998/// ::= .endm
1999/// ::= .endmacro
2000bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2001 SMLoc DirectiveLoc) {
2002 if (getLexer().isNot(AsmToken::EndOfStatement))
2003 return TokError("unexpected token in '" + Directive + "' directive");
2004
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002005 // If we are inside a macro instantiation, terminate the current
2006 // instantiation.
2007 if (!getParser().ActiveMacros.empty()) {
2008 getParser().HandleMacroExit();
2009 return false;
2010 }
2011
2012 // Otherwise, this .endmacro is a stray entry in the file; well formed
2013 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002014 return TokError("unexpected '" + Directive + "' in file, "
2015 "no current macro definition");
2016}
2017
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002018/// \brief Create an MCAsmParser instance.
2019MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2020 MCContext &C, MCStreamer &Out,
2021 const MCAsmInfo &MAI) {
2022 return new AsmParser(T, SM, C, Out, MAI);
2023}