blob: 2f53b21f287b554375c13f34919b8008e1faa12d [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000034#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000036using namespace llvm;
37
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000038namespace {
39
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000040/// \brief Helper class for tracking macro definitions.
41struct Macro {
42 StringRef Name;
43 StringRef Body;
44
45public:
46 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
47};
48
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000049/// \brief Helper class for storing information about an active macro
50/// instantiation.
51struct MacroInstantiation {
52 /// The macro being instantiated.
53 const Macro *TheMacro;
54
55 /// The macro instantiation with substitutions.
56 MemoryBuffer *Instantiation;
57
58 /// The location of the instantiation.
59 SMLoc InstantiationLoc;
60
61 /// The location where parsing should resume upon instantiation completion.
62 SMLoc ExitLoc;
63
64public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000065 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
66 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000067};
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069/// \brief The concrete assembly parser instance.
70class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000071 friend class GenericAsmParser;
72
Daniel Dunbaraef87e32010-07-18 18:31:38 +000073 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
74 void operator=(const AsmParser &); // DO NOT IMPLEMENT
75private:
76 AsmLexer Lexer;
77 MCContext &Ctx;
78 MCStreamer &Out;
79 SourceMgr &SrcMgr;
80 MCAsmParserExtension *GenericParser;
81 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 /// This is the current buffer index we're lexing from as managed by the
84 /// SourceMgr object.
85 int CurBuffer;
86
87 AsmCond TheCondState;
88 std::vector<AsmCond> TheCondStack;
89
90 /// DirectiveMap - This is a table handlers for directives. Each handler is
91 /// invoked after the directive identifier is read and is responsible for
92 /// parsing and validating the rest of the directive. The handler is passed
93 /// in the directive name and the location of the directive keyword.
94 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000095
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000096 /// MacroMap - Map of currently defined macros.
97 StringMap<Macro*> MacroMap;
98
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000099 /// ActiveMacros - Stack of active macro instantiations.
100 std::vector<MacroInstantiation*> ActiveMacros;
101
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000102 /// Boolean tracking whether macro substitution is enabled.
103 unsigned MacrosEnabled : 1;
104
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000105 /// Flag tracking whether any errors have been encountered.
106 unsigned HadError : 1;
107
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000108public:
109 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
110 const MCAsmInfo &MAI);
111 ~AsmParser();
112
113 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
114
115 void AddDirectiveHandler(MCAsmParserExtension *Object,
116 StringRef Directive,
117 DirectiveHandler Handler) {
118 DirectiveMap[Directive] = std::make_pair(Object, Handler);
119 }
120
121public:
122 /// @name MCAsmParser Interface
123 /// {
124
125 virtual SourceMgr &getSourceManager() { return SrcMgr; }
126 virtual MCAsmLexer &getLexer() { return Lexer; }
127 virtual MCContext &getContext() { return Ctx; }
128 virtual MCStreamer &getStreamer() { return Out; }
129
130 virtual void Warning(SMLoc L, const Twine &Meg);
131 virtual bool Error(SMLoc L, const Twine &Msg);
132
133 const AsmToken &Lex();
134
135 bool ParseExpression(const MCExpr *&Res);
136 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseAbsoluteExpression(int64_t &Res);
139
140 /// }
141
142private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000143 void CheckForValidSection();
144
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 bool ParseStatement();
146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000147 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
148 void HandleMacroExit();
149
150 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000151 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
152 SrcMgr.PrintMessage(Loc, Msg, Type);
153 }
154
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000155 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
156 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000157
158 /// \brief Reset the current lexer position to that given by \arg Loc. The
159 /// current token is not set; clients should ensure Lex() is called
160 /// subsequently.
161 void JumpToLoc(SMLoc Loc);
162
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000164
165 /// \brief Parse up to the end of statement and a return the contents from the
166 /// current token until the end of the statement; the current token on exit
167 /// will be either the EndOfStatement or EOF.
168 StringRef ParseStringToEndOfStatement();
169
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000170 bool ParseAssignment(StringRef Name);
171
172 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
175
176 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
177 /// and set \arg Res to the identifier contents.
178 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000181
182 // ".ascii", ".asciiz", ".string"
183 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000185 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186 bool ParseDirectiveFill(); // ".fill"
187 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000188 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000189 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190 bool ParseDirectiveOrg(); // ".org"
191 // ".align{,32}", ".p2align{,w,l}"
192 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
193
194 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
195 /// accepts a single symbol (which should be a label or an external).
196 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197
198 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
199
200 bool ParseDirectiveAbort(); // ".abort"
201 bool ParseDirectiveInclude(); // ".include"
202
203 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
204 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
205 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
206 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
207
208 /// ParseEscapedString - Parse the current token as a string which may include
209 /// escaped characters and return the string contents.
210 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000211
212 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
213 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214};
215
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000216/// \brief Generic implementations of directive handling, etc. which is shared
217/// (or the default, at least) for all assembler parser.
218class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000219 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
220 void AddDirectiveHandler(StringRef Directive) {
221 getParser().AddDirectiveHandler(this, Directive,
222 HandleDirective<GenericAsmParser, Handler>);
223 }
224
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000225public:
226 GenericAsmParser() {}
227
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000228 AsmParser &getParser() {
229 return (AsmParser&) this->MCAsmParserExtension::getParser();
230 }
231
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000232 virtual void Initialize(MCAsmParser &Parser) {
233 // Call the base implementation.
234 this->MCAsmParserExtension::Initialize(Parser);
235
236 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000241
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000242 // CFI directives.
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
244 ".cfi_startproc");
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
246 ".cfi_endproc");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
248 ".cfi_def_cfa_offset");
249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
250 ".cfi_def_cfa_register");
251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
252 ".cfi_offset");
253 AddDirectiveHandler<
254 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
255 AddDirectiveHandler<
256 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
257
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000258 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
260 ".macros_on");
261 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
262 ".macros_off");
263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000266
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000269 }
270
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000271 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
272 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
273 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000274 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000275 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
276 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
277 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
278 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
279 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
280 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000281
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000282 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000283 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
284 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000285
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000286 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000287};
288
289}
290
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000291namespace llvm {
292
293extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000294extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000295extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000296
297}
298
Chris Lattneraaec2052010-01-19 19:46:13 +0000299enum { DEFAULT_ADDRSPACE = 0 };
300
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000301AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
302 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000303 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000304 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000305 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000306 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000307
308 // Initialize the generic parser.
309 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000310
311 // Initialize the platform / file format parser.
312 //
313 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
314 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000315 if (_MAI.hasMicrosoftFastStdCallMangling()) {
316 PlatformParser = createCOFFAsmParser();
317 PlatformParser->Initialize(*this);
318 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000319 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000320 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000321 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000322 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000323 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000324 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000325}
326
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000327AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000328 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
329
330 // Destroy any macros.
331 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
332 ie = MacroMap.end(); it != ie; ++it)
333 delete it->getValue();
334
Daniel Dunbare4749702010-07-12 18:12:02 +0000335 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000336 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000337}
338
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000339void AsmParser::PrintMacroInstantiations() {
340 // Print the active macro instantiation stack.
341 for (std::vector<MacroInstantiation*>::const_reverse_iterator
342 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
343 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
344 "note");
345}
346
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000347void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000348 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000349 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000350}
351
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000352bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000353 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000354 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000355 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000356 return true;
357}
358
Sean Callananfd0b0282010-01-21 00:19:58 +0000359bool AsmParser::EnterIncludeFile(const std::string &Filename) {
360 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
361 if (NewBuf == -1)
362 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000363
Sean Callananfd0b0282010-01-21 00:19:58 +0000364 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000365
Sean Callananfd0b0282010-01-21 00:19:58 +0000366 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000367
Sean Callananfd0b0282010-01-21 00:19:58 +0000368 return false;
369}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000370
371void AsmParser::JumpToLoc(SMLoc Loc) {
372 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
373 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
374}
375
Sean Callananfd0b0282010-01-21 00:19:58 +0000376const AsmToken &AsmParser::Lex() {
377 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000378
Sean Callananfd0b0282010-01-21 00:19:58 +0000379 if (tok->is(AsmToken::Eof)) {
380 // If this is the end of an included file, pop the parent file off the
381 // include stack.
382 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
383 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000384 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000385 tok = &Lexer.Lex();
386 }
387 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000388
Sean Callananfd0b0282010-01-21 00:19:58 +0000389 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000390 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000391
Sean Callananfd0b0282010-01-21 00:19:58 +0000392 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000393}
394
Chris Lattner79180e22010-04-05 23:15:42 +0000395bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000396 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000397 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000398 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000399
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000400 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000401 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000402
403 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000404 AsmCond StartingCondState = TheCondState;
405
Chris Lattnerb717fb02009-07-02 21:53:43 +0000406 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000407 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000408 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000409
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000410 // We had an error, validate that one was emitted and recover by skipping to
411 // the next line.
412 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000413 EatToEndOfStatement();
414 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000415
416 if (TheCondState.TheCond != StartingCondState.TheCond ||
417 TheCondState.Ignore != StartingCondState.Ignore)
418 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000419
420 // Check to see there are no empty DwarfFile slots.
421 const std::vector<MCDwarfFile *> &MCDwarfFiles =
422 getContext().getMCDwarfFiles();
423 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000424 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000425 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000426 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000427
Chris Lattner79180e22010-04-05 23:15:42 +0000428 // Finalize the output stream if there are no errors and if the client wants
429 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000430 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000431 Out.Finish();
432
Chris Lattnerb717fb02009-07-02 21:53:43 +0000433 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000434}
435
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000436void AsmParser::CheckForValidSection() {
437 if (!getStreamer().getCurrentSection()) {
438 TokError("expected section directive before assembly directive");
439 Out.SwitchSection(Ctx.getMachOSection(
440 "__TEXT", "__text",
441 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
442 0, SectionKind::getText()));
443 }
444}
445
Chris Lattner2cf5f142009-06-22 01:29:09 +0000446/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
447void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 while (Lexer.isNot(AsmToken::EndOfStatement) &&
449 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000450 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000451
Chris Lattner2cf5f142009-06-22 01:29:09 +0000452 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000454 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000455}
456
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000457StringRef AsmParser::ParseStringToEndOfStatement() {
458 const char *Start = getTok().getLoc().getPointer();
459
460 while (Lexer.isNot(AsmToken::EndOfStatement) &&
461 Lexer.isNot(AsmToken::Eof))
462 Lex();
463
464 const char *End = getTok().getLoc().getPointer();
465 return StringRef(Start, End - Start);
466}
Chris Lattnerc4193832009-06-22 05:51:26 +0000467
Chris Lattner74ec1a32009-06-22 06:32:03 +0000468/// ParseParenExpr - Parse a paren expression and return it.
469/// NOTE: This assumes the leading '(' has already been consumed.
470///
471/// parenexpr ::= expr)
472///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000473bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000474 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000475 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000476 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000477 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000478 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000479 return false;
480}
Chris Lattnerc4193832009-06-22 05:51:26 +0000481
Chris Lattner74ec1a32009-06-22 06:32:03 +0000482/// ParsePrimaryExpr - Parse a primary expression and return it.
483/// primaryexpr ::= (parenexpr
484/// primaryexpr ::= symbol
485/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000486/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000487/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000488bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000489 switch (Lexer.getKind()) {
490 default:
491 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000492 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000493 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000494 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000495 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000496 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000497 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000498 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000499 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000500 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000501 EndLoc = Lexer.getLoc();
502
503 StringRef Identifier;
504 if (ParseIdentifier(Identifier))
505 return false;
506
Daniel Dunbarfffff912009-10-16 01:34:54 +0000507 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000508 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000509 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000510
511 // Lookup the symbol variant if used.
512 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000513 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000514 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000515 if (Variant == MCSymbolRefExpr::VK_Invalid) {
516 Variant = MCSymbolRefExpr::VK_None;
517 TokError("invalid variant '" + Split.second + "'");
518 }
519 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000520
Daniel Dunbarfffff912009-10-16 01:34:54 +0000521 // If this is an absolute variable reference, substitute it now to preserve
522 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000523 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000524 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000525 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000526
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000527 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000528 return false;
529 }
530
531 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000532 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000533 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000534 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000535 case AsmToken::Integer: {
536 SMLoc Loc = getTok().getLoc();
537 int64_t IntVal = getTok().getIntVal();
538 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000539 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000540 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000541 // Look for 'b' or 'f' following an Integer as a directional label
542 if (Lexer.getKind() == AsmToken::Identifier) {
543 StringRef IDVal = getTok().getString();
544 if (IDVal == "f" || IDVal == "b"){
545 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
546 IDVal == "f" ? 1 : 0);
547 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
548 getContext());
549 if(IDVal == "b" && Sym->isUndefined())
550 return Error(Loc, "invalid reference to undefined symbol");
551 EndLoc = Lexer.getLoc();
552 Lex(); // Eat identifier.
553 }
554 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000555 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000556 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000557 case AsmToken::Dot: {
558 // This is a '.' reference, which references the current PC. Emit a
559 // temporary label to the streamer and refer to it.
560 MCSymbol *Sym = Ctx.CreateTempSymbol();
561 Out.EmitLabel(Sym);
562 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
563 EndLoc = Lexer.getLoc();
564 Lex(); // Eat identifier.
565 return false;
566 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000567
Daniel Dunbar3f872332009-07-28 16:08:33 +0000568 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000569 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000570 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000571 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000572 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000573 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000574 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000575 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000576 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000577 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000578 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000579 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000581 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000582 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000583 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000584 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000585 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000586 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000587 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000588 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000589 }
590}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000591
Chris Lattnerb4307b32010-01-15 19:28:38 +0000592bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000593 SMLoc EndLoc;
594 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000595}
596
Daniel Dunbarcceba832010-09-17 02:47:07 +0000597const MCExpr *
598AsmParser::ApplyModifierToExpr(const MCExpr *E,
599 MCSymbolRefExpr::VariantKind Variant) {
600 // Recurse over the given expression, rebuilding it to apply the given variant
601 // if there is exactly one symbol.
602 switch (E->getKind()) {
603 case MCExpr::Target:
604 case MCExpr::Constant:
605 return 0;
606
607 case MCExpr::SymbolRef: {
608 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
609
610 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
611 TokError("invalid variant on expression '" +
612 getTok().getIdentifier() + "' (already modified)");
613 return E;
614 }
615
616 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
617 }
618
619 case MCExpr::Unary: {
620 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
621 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
622 if (!Sub)
623 return 0;
624 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
625 }
626
627 case MCExpr::Binary: {
628 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
629 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
630 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
631
632 if (!LHS && !RHS)
633 return 0;
634
635 if (!LHS) LHS = BE->getLHS();
636 if (!RHS) RHS = BE->getRHS();
637
638 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
639 }
640 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000641
642 assert(0 && "Invalid expression kind!");
643 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000644}
645
Chris Lattner74ec1a32009-06-22 06:32:03 +0000646/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000647///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000648/// expr ::= expr +,- expr -> lowest.
649/// expr ::= expr |,^,&,! expr -> middle.
650/// expr ::= expr *,/,%,<<,>> expr -> highest.
651/// expr ::= primaryexpr
652///
Chris Lattner54482b42010-01-15 19:39:23 +0000653bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000654 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000655 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000656 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
657 return true;
658
Daniel Dunbarcceba832010-09-17 02:47:07 +0000659 // As a special case, we support 'a op b @ modifier' by rewriting the
660 // expression to include the modifier. This is inefficient, but in general we
661 // expect users to use 'a@modifier op b'.
662 if (Lexer.getKind() == AsmToken::At) {
663 Lex();
664
665 if (Lexer.isNot(AsmToken::Identifier))
666 return TokError("unexpected symbol modifier following '@'");
667
668 MCSymbolRefExpr::VariantKind Variant =
669 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
670 if (Variant == MCSymbolRefExpr::VK_Invalid)
671 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
672
673 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
674 if (!ModifiedRes) {
675 return TokError("invalid modifier '" + getTok().getIdentifier() +
676 "' (no symbols present)");
677 return true;
678 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000679
Daniel Dunbarcceba832010-09-17 02:47:07 +0000680 Res = ModifiedRes;
681 Lex();
682 }
683
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000684 // Try to constant fold it up front, if possible.
685 int64_t Value;
686 if (Res->EvaluateAsAbsolute(Value))
687 Res = MCConstantExpr::Create(Value, getContext());
688
689 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000690}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000691
Chris Lattnerb4307b32010-01-15 19:28:38 +0000692bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000693 Res = 0;
694 return ParseParenExpr(Res, EndLoc) ||
695 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000696}
697
Daniel Dunbar475839e2009-06-29 20:37:27 +0000698bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000699 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000700
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000701 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000702 if (ParseExpression(Expr))
703 return true;
704
Daniel Dunbare00b0112009-10-16 01:57:52 +0000705 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000706 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000707
708 return false;
709}
710
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000711static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000712 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000713 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000714 default:
715 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000716
Daniel Dunbarcceba832010-09-17 02:47:07 +0000717 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000718 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000719 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000720 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000721 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000722 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000723 return 1;
724
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000725
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000726 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000727 //
728 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000729 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000730 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000731 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000732 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000733 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000734 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000735 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000736 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000737 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000738
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000739 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000740 case AsmToken::EqualEqual:
741 Kind = MCBinaryExpr::EQ;
742 return 3;
743 case AsmToken::ExclaimEqual:
744 case AsmToken::LessGreater:
745 Kind = MCBinaryExpr::NE;
746 return 3;
747 case AsmToken::Less:
748 Kind = MCBinaryExpr::LT;
749 return 3;
750 case AsmToken::LessEqual:
751 Kind = MCBinaryExpr::LTE;
752 return 3;
753 case AsmToken::Greater:
754 Kind = MCBinaryExpr::GT;
755 return 3;
756 case AsmToken::GreaterEqual:
757 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000758 return 3;
759
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000760 // High Intermediate Precedence: +, -
761 case AsmToken::Plus:
762 Kind = MCBinaryExpr::Add;
763 return 4;
764 case AsmToken::Minus:
765 Kind = MCBinaryExpr::Sub;
766 return 4;
767
Daniel Dunbar475839e2009-06-29 20:37:27 +0000768 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000769 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000770 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000771 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000772 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000773 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000774 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000775 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000776 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000777 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000779 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000780 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000782 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000783 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000784 }
785}
786
787
788/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
789/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000790bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
791 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000792 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000793 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000794 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000795
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000796 // If the next token is lower precedence than we are allowed to eat, return
797 // successfully with what we ate already.
798 if (TokPrec < Precedence)
799 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000800
Sean Callanan79ed1a82010-01-19 20:22:31 +0000801 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000802
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000803 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000804 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000805 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000806
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000807 // If BinOp binds less tightly with RHS than the operator after RHS, let
808 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000809 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000810 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000811 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000812 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000813 }
814
Daniel Dunbar475839e2009-06-29 20:37:27 +0000815 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000816 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000817 }
818}
819
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000820
821
822
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000823/// ParseStatement:
824/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000825/// ::= Label* Directive ...Operands... EndOfStatement
826/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000827bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000828 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000829 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000830 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000831 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000832 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000833
834 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000835 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000836 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000837 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000838 int64_t LocalLabelVal = -1;
839 // GUESS allow an integer followed by a ':' as a directional local label
840 if (Lexer.is(AsmToken::Integer)) {
841 LocalLabelVal = getTok().getIntVal();
842 if (LocalLabelVal < 0) {
843 if (!TheCondState.Ignore)
844 return TokError("unexpected token at start of statement");
845 IDVal = "";
846 }
847 else {
848 IDVal = getTok().getString();
849 Lex(); // Consume the integer token to be used as an identifier token.
850 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000851 if (!TheCondState.Ignore)
852 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000853 }
854 }
855 }
856 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000857 if (!TheCondState.Ignore)
858 return TokError("unexpected token at start of statement");
859 IDVal = "";
860 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000861
Chris Lattner7834fac2010-04-17 18:14:27 +0000862 // Handle conditional assembly here before checking for skipping. We
863 // have to do this so that .endif isn't skipped in a ".if 0" block for
864 // example.
865 if (IDVal == ".if")
866 return ParseDirectiveIf(IDLoc);
867 if (IDVal == ".elseif")
868 return ParseDirectiveElseIf(IDLoc);
869 if (IDVal == ".else")
870 return ParseDirectiveElse(IDLoc);
871 if (IDVal == ".endif")
872 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000873
Chris Lattner7834fac2010-04-17 18:14:27 +0000874 // If we are in a ".if 0" block, ignore this statement.
875 if (TheCondState.Ignore) {
876 EatToEndOfStatement();
877 return false;
878 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000879
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000880 // FIXME: Recurse on local labels?
881
882 // See what kind of statement we have.
883 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000885 CheckForValidSection();
886
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000887 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000888 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000889
890 // Diagnose attempt to use a variable as a label.
891 //
892 // FIXME: Diagnostics. Note the location of the definition as a label.
893 // FIXME: This doesn't diagnose assignment to a symbol which has been
894 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000895 MCSymbol *Sym;
896 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000897 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000898 else
899 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000900 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000901 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000902
Daniel Dunbar959fd882009-08-26 22:13:22 +0000903 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000904 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000905
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000906 // Consume any end of statement token, if present, to avoid spurious
907 // AddBlankLine calls().
908 if (Lexer.is(AsmToken::EndOfStatement)) {
909 Lex();
910 if (Lexer.is(AsmToken::Eof))
911 return false;
912 }
913
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000914 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000915 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000916
Daniel Dunbar3f872332009-07-28 16:08:33 +0000917 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000918 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000919 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000920
Daniel Dunbare2ace502009-08-31 08:09:09 +0000921 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000922
923 default: // Normal instruction or directive.
924 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000925 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000926
927 // If macros are enabled, check to see if this is a macro instantiation.
928 if (MacrosEnabled)
929 if (const Macro *M = MacroMap.lookup(IDVal))
930 return HandleMacroEntry(IDVal, IDLoc, M);
931
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000932 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000933 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000934 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000935 if (IDVal == ".set" || IDVal == ".equ")
936 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000937
Daniel Dunbara0d14262009-06-24 23:30:00 +0000938 // Data directives
939
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000940 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000941 return ParseDirectiveAscii(IDVal, false);
942 if (IDVal == ".asciz" || IDVal == ".string")
943 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000944
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000945 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000946 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000947 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000948 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000949 if (IDVal == ".value")
950 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000951 if (IDVal == ".2byte")
952 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000953 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000954 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000955 if (IDVal == ".int")
956 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000957 if (IDVal == ".4byte")
958 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000959 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000961 if (IDVal == ".8byte")
962 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000963 if (IDVal == ".single")
964 return ParseDirectiveRealValue(APFloat::IEEEsingle);
965 if (IDVal == ".double")
966 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000967
Eli Friedman5d68ec22010-07-19 04:17:25 +0000968 if (IDVal == ".align") {
969 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
970 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
971 }
972 if (IDVal == ".align32") {
973 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
974 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
975 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000976 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000977 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000978 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000979 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000980 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000981 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000982 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000983 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000984 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000985 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000986 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000987 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
988
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000990 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000991
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000992 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000993 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000994 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000995 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000996 if (IDVal == ".zero")
997 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000999 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001000
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001001 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001002 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001003 // ELF only? Should it be here?
1004 if (IDVal == ".local")
1005 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001006 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001007 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001008 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001009 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001010 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001011 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001012 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001013 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001014 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001015 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001016 if (IDVal == ".symbol_resolver")
1017 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001018 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001019 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001020 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001021 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001022 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001023 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001024 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001025 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001026 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001027 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001028 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001029 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001030 if (IDVal == ".weak_def_can_be_hidden")
1031 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001032
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001033 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001034 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001035 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001036 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001037
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001038 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001039 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001040 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001041 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001042
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001043 // Look up the handler in the handler table.
1044 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1045 DirectiveMap.lookup(IDVal);
1046 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001047 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001048
Kevin Enderby9c656452009-09-10 20:51:44 +00001049 // Target hook for parsing target specific directives.
1050 if (!getTargetParser().ParseDirective(ID))
1051 return false;
1052
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001053 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001054 EatToEndOfStatement();
1055 return false;
1056 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001057
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001058 CheckForValidSection();
1059
Chris Lattnera7f13542010-05-19 23:34:33 +00001060 // Canonicalize the opcode to lower case.
1061 SmallString<128> Opcode;
1062 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1063 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001064
Chris Lattner98986712010-01-14 22:21:20 +00001065 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001066 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001067 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001068
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001069 // Dump the parsed representation, if requested.
1070 if (getShowParsedOperands()) {
1071 SmallString<256> Str;
1072 raw_svector_ostream OS(Str);
1073 OS << "parsed instruction: [";
1074 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1075 if (i != 0)
1076 OS << ", ";
1077 ParsedOperands[i]->dump(OS);
1078 }
1079 OS << "]";
1080
1081 PrintMessage(IDLoc, OS.str(), "note");
1082 }
1083
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001084 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001085 if (!HadError)
1086 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1087 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001088
Chris Lattner98986712010-01-14 22:21:20 +00001089 // Free any parsed operands.
1090 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1091 delete ParsedOperands[i];
1092
Chris Lattnercbf8a982010-09-11 16:18:25 +00001093 // Don't skip the rest of the line, the instruction parser is responsible for
1094 // that.
1095 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001096}
Chris Lattner9a023f72009-06-24 04:43:34 +00001097
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001098MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1099 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001100 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1101{
1102 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1103 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001104 SmallString<256> Buf;
1105 raw_svector_ostream OS(Buf);
1106
1107 StringRef Body = M->Body;
1108 while (!Body.empty()) {
1109 // Scan for the next substitution.
1110 std::size_t End = Body.size(), Pos = 0;
1111 for (; Pos != End; ++Pos) {
1112 // Check for a substitution or escape.
1113 if (Body[Pos] != '$' || Pos + 1 == End)
1114 continue;
1115
1116 char Next = Body[Pos + 1];
1117 if (Next == '$' || Next == 'n' || isdigit(Next))
1118 break;
1119 }
1120
1121 // Add the prefix.
1122 OS << Body.slice(0, Pos);
1123
1124 // Check if we reached the end.
1125 if (Pos == End)
1126 break;
1127
1128 switch (Body[Pos+1]) {
1129 // $$ => $
1130 case '$':
1131 OS << '$';
1132 break;
1133
1134 // $n => number of arguments
1135 case 'n':
1136 OS << A.size();
1137 break;
1138
1139 // $[0-9] => argument
1140 default: {
1141 // Missing arguments are ignored.
1142 unsigned Index = Body[Pos+1] - '0';
1143 if (Index >= A.size())
1144 break;
1145
1146 // Otherwise substitute with the token values, with spaces eliminated.
1147 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1148 ie = A[Index].end(); it != ie; ++it)
1149 OS << it->getString();
1150 break;
1151 }
1152 }
1153
1154 // Update the scan point.
1155 Body = Body.substr(Pos + 2);
1156 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001157
1158 // We include the .endmacro in the buffer as our queue to exit the macro
1159 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001160 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001161
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001162 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001163}
1164
1165bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1166 const Macro *M) {
1167 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1168 // this, although we should protect against infinite loops.
1169 if (ActiveMacros.size() == 20)
1170 return TokError("macros cannot be nested more than 20 levels deep");
1171
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001172 // Parse the macro instantiation arguments.
1173 std::vector<std::vector<AsmToken> > MacroArguments;
1174 MacroArguments.push_back(std::vector<AsmToken>());
1175 unsigned ParenLevel = 0;
1176 for (;;) {
1177 if (Lexer.is(AsmToken::Eof))
1178 return TokError("unexpected token in macro instantiation");
1179 if (Lexer.is(AsmToken::EndOfStatement))
1180 break;
1181
1182 // If we aren't inside parentheses and this is a comma, start a new token
1183 // list.
1184 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1185 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001186 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001187 // Adjust the current parentheses level.
1188 if (Lexer.is(AsmToken::LParen))
1189 ++ParenLevel;
1190 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1191 --ParenLevel;
1192
1193 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001194 MacroArguments.back().push_back(getTok());
1195 }
1196 Lex();
1197 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001198
1199 // Create the macro instantiation object and add to the current macro
1200 // instantiation stack.
1201 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001202 getTok().getLoc(),
1203 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001204 ActiveMacros.push_back(MI);
1205
1206 // Jump to the macro instantiation and prime the lexer.
1207 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1208 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1209 Lex();
1210
1211 return false;
1212}
1213
1214void AsmParser::HandleMacroExit() {
1215 // Jump to the EndOfStatement we should return to, and consume it.
1216 JumpToLoc(ActiveMacros.back()->ExitLoc);
1217 Lex();
1218
1219 // Pop the instantiation entry.
1220 delete ActiveMacros.back();
1221 ActiveMacros.pop_back();
1222}
1223
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001224static void MarkUsed(const MCExpr *Value) {
1225 switch (Value->getKind()) {
1226 case MCExpr::Binary:
1227 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1228 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1229 break;
1230 case MCExpr::Target:
1231 case MCExpr::Constant:
1232 break;
1233 case MCExpr::SymbolRef: {
1234 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1235 break;
1236 }
1237 case MCExpr::Unary:
1238 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1239 break;
1240 }
1241}
1242
Benjamin Kramer38e59892010-07-14 22:38:02 +00001243bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001244 // FIXME: Use better location, we should use proper tokens.
1245 SMLoc EqualLoc = Lexer.getLoc();
1246
Daniel Dunbar821e3332009-08-31 08:09:28 +00001247 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001248 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001249 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001250
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001251 MarkUsed(Value);
1252
Daniel Dunbar3f872332009-07-28 16:08:33 +00001253 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001254 return TokError("unexpected token in assignment");
1255
1256 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001257 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001258
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001259 // Validate that the LHS is allowed to be a variable (either it has not been
1260 // used as a symbol, or it is an absolute symbol).
1261 MCSymbol *Sym = getContext().LookupSymbol(Name);
1262 if (Sym) {
1263 // Diagnose assignment to a label.
1264 //
1265 // FIXME: Diagnostics. Note the location of the definition as a label.
1266 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001267 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001268 ; // Allow redefinitions of undefined symbols only used in directives.
1269 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001270 return Error(EqualLoc, "redefinition of '" + Name + "'");
1271 else if (!Sym->isVariable())
1272 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001273 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001274 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1275 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001276
1277 // Don't count these checks as uses.
1278 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001279 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001280 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001281
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001282 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001283
1284 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001285 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001286
1287 return false;
1288}
1289
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001290/// ParseIdentifier:
1291/// ::= identifier
1292/// ::= string
1293bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001294 // The assembler has relaxed rules for accepting identifiers, in particular we
1295 // allow things like '.globl $foo', which would normally be separate
1296 // tokens. At this level, we have already lexed so we cannot (currently)
1297 // handle this as a context dependent token, instead we detect adjacent tokens
1298 // and return the combined identifier.
1299 if (Lexer.is(AsmToken::Dollar)) {
1300 SMLoc DollarLoc = getLexer().getLoc();
1301
1302 // Consume the dollar sign, and check for a following identifier.
1303 Lex();
1304 if (Lexer.isNot(AsmToken::Identifier))
1305 return true;
1306
1307 // We have a '$' followed by an identifier, make sure they are adjacent.
1308 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1309 return true;
1310
1311 // Construct the joined identifier and consume the token.
1312 Res = StringRef(DollarLoc.getPointer(),
1313 getTok().getIdentifier().size() + 1);
1314 Lex();
1315 return false;
1316 }
1317
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001318 if (Lexer.isNot(AsmToken::Identifier) &&
1319 Lexer.isNot(AsmToken::String))
1320 return true;
1321
Sean Callanan18b83232010-01-19 21:44:56 +00001322 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001323
Sean Callanan79ed1a82010-01-19 20:22:31 +00001324 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001325
1326 return false;
1327}
1328
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001329/// ParseDirectiveSet:
1330/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001331bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001332 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001333
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001334 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001335 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001336
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001337 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001338 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001339 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001340
Daniel Dunbare2ace502009-08-31 08:09:09 +00001341 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001342}
1343
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001344bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001345 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001346
1347 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001348 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001349 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1350 if (Str[i] != '\\') {
1351 Data += Str[i];
1352 continue;
1353 }
1354
1355 // Recognize escaped characters. Note that this escape semantics currently
1356 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1357 ++i;
1358 if (i == e)
1359 return TokError("unexpected backslash at end of string");
1360
1361 // Recognize octal sequences.
1362 if ((unsigned) (Str[i] - '0') <= 7) {
1363 // Consume up to three octal characters.
1364 unsigned Value = Str[i] - '0';
1365
1366 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1367 ++i;
1368 Value = Value * 8 + (Str[i] - '0');
1369
1370 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1371 ++i;
1372 Value = Value * 8 + (Str[i] - '0');
1373 }
1374 }
1375
1376 if (Value > 255)
1377 return TokError("invalid octal escape sequence (out of range)");
1378
1379 Data += (unsigned char) Value;
1380 continue;
1381 }
1382
1383 // Otherwise recognize individual escapes.
1384 switch (Str[i]) {
1385 default:
1386 // Just reject invalid escape sequences for now.
1387 return TokError("invalid escape sequence (unrecognized character)");
1388
1389 case 'b': Data += '\b'; break;
1390 case 'f': Data += '\f'; break;
1391 case 'n': Data += '\n'; break;
1392 case 'r': Data += '\r'; break;
1393 case 't': Data += '\t'; break;
1394 case '"': Data += '"'; break;
1395 case '\\': Data += '\\'; break;
1396 }
1397 }
1398
1399 return false;
1400}
1401
Daniel Dunbara0d14262009-06-24 23:30:00 +00001402/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001403/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1404bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001405 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001406 CheckForValidSection();
1407
Daniel Dunbara0d14262009-06-24 23:30:00 +00001408 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001409 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001410 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001411
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001412 std::string Data;
1413 if (ParseEscapedString(Data))
1414 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001415
1416 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001417 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001418 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1419
Sean Callanan79ed1a82010-01-19 20:22:31 +00001420 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001421
1422 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001423 break;
1424
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001425 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001426 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001427 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001428 }
1429 }
1430
Sean Callanan79ed1a82010-01-19 20:22:31 +00001431 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001432 return false;
1433}
1434
1435/// ParseDirectiveValue
1436/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1437bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001438 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001439 CheckForValidSection();
1440
Daniel Dunbara0d14262009-06-24 23:30:00 +00001441 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001442 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001443 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001444 return true;
1445
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001446 // Special case constant expressions to match code generator.
1447 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001448 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001449 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001450 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001451
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001452 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001453 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001454
Daniel Dunbara0d14262009-06-24 23:30:00 +00001455 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001456 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001457 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001458 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001459 }
1460 }
1461
Sean Callanan79ed1a82010-01-19 20:22:31 +00001462 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001463 return false;
1464}
1465
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001466/// ParseDirectiveRealValue
1467/// ::= (.single | .double) [ expression (, expression)* ]
1468bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1469 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1470 CheckForValidSection();
1471
1472 for (;;) {
1473 // We don't truly support arithmetic on floating point expressions, so we
1474 // have to manually parse unary prefixes.
1475 bool IsNeg = false;
1476 if (getLexer().is(AsmToken::Minus)) {
1477 Lex();
1478 IsNeg = true;
1479 } else if (getLexer().is(AsmToken::Plus))
1480 Lex();
1481
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001482 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001483 getLexer().isNot(AsmToken::Real))
1484 return TokError("unexpected token in directive");
1485
1486 // Convert to an APFloat.
1487 APFloat Value(Semantics);
1488 if (Value.convertFromString(getTok().getString(),
1489 APFloat::rmNearestTiesToEven) ==
1490 APFloat::opInvalidOp)
1491 return TokError("invalid floating point literal");
1492 if (IsNeg)
1493 Value.changeSign();
1494
1495 // Consume the numeric token.
1496 Lex();
1497
1498 // Emit the value as an integer.
1499 APInt AsInt = Value.bitcastToAPInt();
1500 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1501 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1502
1503 if (getLexer().is(AsmToken::EndOfStatement))
1504 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001505
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001506 if (getLexer().isNot(AsmToken::Comma))
1507 return TokError("unexpected token in directive");
1508 Lex();
1509 }
1510 }
1511
1512 Lex();
1513 return false;
1514}
1515
Daniel Dunbara0d14262009-06-24 23:30:00 +00001516/// ParseDirectiveSpace
1517/// ::= .space expression [ , expression ]
1518bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001519 CheckForValidSection();
1520
Daniel Dunbara0d14262009-06-24 23:30:00 +00001521 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001522 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001523 return true;
1524
1525 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001526 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1527 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001528 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001529 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001530
Daniel Dunbar475839e2009-06-29 20:37:27 +00001531 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001532 return true;
1533
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001534 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001535 return TokError("unexpected token in '.space' directive");
1536 }
1537
Sean Callanan79ed1a82010-01-19 20:22:31 +00001538 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001539
1540 if (NumBytes <= 0)
1541 return TokError("invalid number of bytes in '.space' directive");
1542
1543 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001544 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001545
1546 return false;
1547}
1548
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001549/// ParseDirectiveZero
1550/// ::= .zero expression
1551bool AsmParser::ParseDirectiveZero() {
1552 CheckForValidSection();
1553
1554 int64_t NumBytes;
1555 if (ParseAbsoluteExpression(NumBytes))
1556 return true;
1557
Rafael Espindolae452b172010-10-05 19:42:57 +00001558 int64_t Val = 0;
1559 if (getLexer().is(AsmToken::Comma)) {
1560 Lex();
1561 if (ParseAbsoluteExpression(Val))
1562 return true;
1563 }
1564
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001565 if (getLexer().isNot(AsmToken::EndOfStatement))
1566 return TokError("unexpected token in '.zero' directive");
1567
1568 Lex();
1569
Rafael Espindolae452b172010-10-05 19:42:57 +00001570 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001571
1572 return false;
1573}
1574
Daniel Dunbara0d14262009-06-24 23:30:00 +00001575/// ParseDirectiveFill
1576/// ::= .fill expression , expression , expression
1577bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001578 CheckForValidSection();
1579
Daniel Dunbara0d14262009-06-24 23:30:00 +00001580 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001581 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001582 return true;
1583
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001584 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001585 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001586 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001587
Daniel Dunbara0d14262009-06-24 23:30:00 +00001588 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001589 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001590 return true;
1591
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001592 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001593 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001594 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001595
Daniel Dunbara0d14262009-06-24 23:30:00 +00001596 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001597 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001598 return true;
1599
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001600 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001601 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001602
Sean Callanan79ed1a82010-01-19 20:22:31 +00001603 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001604
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001605 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1606 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001607
1608 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001609 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001610
1611 return false;
1612}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001613
1614/// ParseDirectiveOrg
1615/// ::= .org expression [ , expression ]
1616bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001617 CheckForValidSection();
1618
Daniel Dunbar821e3332009-08-31 08:09:28 +00001619 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001620 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001621 return true;
1622
1623 // Parse optional fill expression.
1624 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001625 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1626 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001627 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001628 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001629
Daniel Dunbar475839e2009-06-29 20:37:27 +00001630 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001631 return true;
1632
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001633 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001634 return TokError("unexpected token in '.org' directive");
1635 }
1636
Sean Callanan79ed1a82010-01-19 20:22:31 +00001637 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001638
1639 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1640 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001641 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001642
1643 return false;
1644}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001645
1646/// ParseDirectiveAlign
1647/// ::= {.align, ...} expression [ , expression [ , expression ]]
1648bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001649 CheckForValidSection();
1650
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001651 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001652 int64_t Alignment;
1653 if (ParseAbsoluteExpression(Alignment))
1654 return true;
1655
1656 SMLoc MaxBytesLoc;
1657 bool HasFillExpr = false;
1658 int64_t FillExpr = 0;
1659 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001660 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1661 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001662 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001663 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001664
1665 // The fill expression can be omitted while specifying a maximum number of
1666 // alignment bytes, e.g:
1667 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001668 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001669 HasFillExpr = true;
1670 if (ParseAbsoluteExpression(FillExpr))
1671 return true;
1672 }
1673
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001674 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1675 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001676 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001677 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001678
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001680 if (ParseAbsoluteExpression(MaxBytesToFill))
1681 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001682
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001684 return TokError("unexpected token in directive");
1685 }
1686 }
1687
Sean Callanan79ed1a82010-01-19 20:22:31 +00001688 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001689
Daniel Dunbar648ac512010-05-17 21:54:30 +00001690 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001691 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001692
1693 // Compute alignment in bytes.
1694 if (IsPow2) {
1695 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001696 if (Alignment >= 32) {
1697 Error(AlignmentLoc, "invalid alignment value");
1698 Alignment = 31;
1699 }
1700
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001701 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001702 }
1703
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001704 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001705 if (MaxBytesLoc.isValid()) {
1706 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001707 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1708 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001709 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001710 }
1711
1712 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001713 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1714 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001715 MaxBytesToFill = 0;
1716 }
1717 }
1718
Daniel Dunbar648ac512010-05-17 21:54:30 +00001719 // Check whether we should use optimal code alignment for this .align
1720 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001721 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001722 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1723 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001724 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001725 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001726 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001727 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1728 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001729 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001730
1731 return false;
1732}
1733
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001734/// ParseDirectiveSymbolAttribute
1735/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001736bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001737 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001738 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001739 StringRef Name;
1740
1741 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001742 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001743
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001744 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001745
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001746 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001747
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001748 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001749 break;
1750
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001752 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001753 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001754 }
1755 }
1756
Sean Callanan79ed1a82010-01-19 20:22:31 +00001757 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001758 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001759}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001760
1761/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001762/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1763bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001764 CheckForValidSection();
1765
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001766 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001767 StringRef Name;
1768 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001769 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001770
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001771 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001772 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001773
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001774 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001775 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001776 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001777
1778 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001779 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001780 if (ParseAbsoluteExpression(Size))
1781 return true;
1782
1783 int64_t Pow2Alignment = 0;
1784 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001785 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001786 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001787 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001788 if (ParseAbsoluteExpression(Pow2Alignment))
1789 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001790
Chris Lattner258281d2010-01-19 06:22:22 +00001791 // If this target takes alignments in bytes (not log) validate and convert.
1792 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1793 if (!isPowerOf2_64(Pow2Alignment))
1794 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1795 Pow2Alignment = Log2_64(Pow2Alignment);
1796 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001797 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001798
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001799 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001800 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001801
Sean Callanan79ed1a82010-01-19 20:22:31 +00001802 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001803
Chris Lattner1fc3d752009-07-09 17:25:12 +00001804 // NOTE: a size of zero for a .comm should create a undefined symbol
1805 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001806 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001807 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1808 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001809
Eric Christopherc260a3e2010-05-14 01:38:54 +00001810 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001811 // may internally end up wanting an alignment in bytes.
1812 // FIXME: Diagnose overflow.
1813 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001814 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1815 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001816
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001817 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001818 return Error(IDLoc, "invalid symbol redefinition");
1819
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001820 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001821 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001822 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 getStreamer().EmitZerofill(Ctx.getMachOSection(
1824 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1825 0, SectionKind::getBSS()),
1826 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001827 return false;
1828 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001829
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001830 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001831 return false;
1832}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001833
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001834/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001835/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001836bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001837 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001839
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001840 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001841 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001842 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001843
Sean Callanan79ed1a82010-01-19 20:22:31 +00001844 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001845
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001846 if (Str.empty())
1847 Error(Loc, ".abort detected. Assembly stopping.");
1848 else
1849 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001850 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001851
1852 return false;
1853}
Kevin Enderby71148242009-07-14 21:35:03 +00001854
Kevin Enderby1f049b22009-07-14 23:21:55 +00001855/// ParseDirectiveInclude
1856/// ::= .include "filename"
1857bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001858 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001859 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001860
Sean Callanan18b83232010-01-19 21:44:56 +00001861 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001862 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001863 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001864
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001865 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001866 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001867
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001868 // Strip the quotes.
1869 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001870
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001871 // Attempt to switch the lexer to the included file before consuming the end
1872 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001873 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001874 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001875 return true;
1876 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001877
1878 return false;
1879}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001880
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001881/// ParseDirectiveIf
1882/// ::= .if expression
1883bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001884 TheCondStack.push_back(TheCondState);
1885 TheCondState.TheCond = AsmCond::IfCond;
1886 if(TheCondState.Ignore) {
1887 EatToEndOfStatement();
1888 }
1889 else {
1890 int64_t ExprValue;
1891 if (ParseAbsoluteExpression(ExprValue))
1892 return true;
1893
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001894 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001895 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001896
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001898
1899 TheCondState.CondMet = ExprValue;
1900 TheCondState.Ignore = !TheCondState.CondMet;
1901 }
1902
1903 return false;
1904}
1905
1906/// ParseDirectiveElseIf
1907/// ::= .elseif expression
1908bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1909 if (TheCondState.TheCond != AsmCond::IfCond &&
1910 TheCondState.TheCond != AsmCond::ElseIfCond)
1911 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1912 " an .elseif");
1913 TheCondState.TheCond = AsmCond::ElseIfCond;
1914
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001915 bool LastIgnoreState = false;
1916 if (!TheCondStack.empty())
1917 LastIgnoreState = TheCondStack.back().Ignore;
1918 if (LastIgnoreState || TheCondState.CondMet) {
1919 TheCondState.Ignore = true;
1920 EatToEndOfStatement();
1921 }
1922 else {
1923 int64_t ExprValue;
1924 if (ParseAbsoluteExpression(ExprValue))
1925 return true;
1926
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001927 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001928 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001929
Sean Callanan79ed1a82010-01-19 20:22:31 +00001930 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001931 TheCondState.CondMet = ExprValue;
1932 TheCondState.Ignore = !TheCondState.CondMet;
1933 }
1934
1935 return false;
1936}
1937
1938/// ParseDirectiveElse
1939/// ::= .else
1940bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001941 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001942 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001943
Sean Callanan79ed1a82010-01-19 20:22:31 +00001944 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001945
1946 if (TheCondState.TheCond != AsmCond::IfCond &&
1947 TheCondState.TheCond != AsmCond::ElseIfCond)
1948 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1949 ".elseif");
1950 TheCondState.TheCond = AsmCond::ElseCond;
1951 bool LastIgnoreState = false;
1952 if (!TheCondStack.empty())
1953 LastIgnoreState = TheCondStack.back().Ignore;
1954 if (LastIgnoreState || TheCondState.CondMet)
1955 TheCondState.Ignore = true;
1956 else
1957 TheCondState.Ignore = false;
1958
1959 return false;
1960}
1961
1962/// ParseDirectiveEndIf
1963/// ::= .endif
1964bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001965 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001966 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001967
Sean Callanan79ed1a82010-01-19 20:22:31 +00001968 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001969
1970 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1971 TheCondStack.empty())
1972 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1973 ".else");
1974 if (!TheCondStack.empty()) {
1975 TheCondState = TheCondStack.back();
1976 TheCondStack.pop_back();
1977 }
1978
1979 return false;
1980}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001981
1982/// ParseDirectiveFile
1983/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001984bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001985 // FIXME: I'm not sure what this is.
1986 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001987 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001988 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001989 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001990 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001991
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001992 if (FileNumber < 1)
1993 return TokError("file number less than one");
1994 }
1995
Daniel Dunbareceec052010-07-12 17:45:27 +00001996 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001997 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001998
Chris Lattnerd32e8032010-01-25 19:02:58 +00001999 StringRef Filename = getTok().getString();
2000 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002001 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002002
Daniel Dunbareceec052010-07-12 17:45:27 +00002003 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002004 return TokError("unexpected token in '.file' directive");
2005
Chris Lattnerd32e8032010-01-25 19:02:58 +00002006 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002007 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002008 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002009 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002010 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002011 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002012
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002013 return false;
2014}
2015
2016/// ParseDirectiveLine
2017/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002018bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002019 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2020 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002021 return TokError("unexpected token in '.line' directive");
2022
Sean Callanan18b83232010-01-19 21:44:56 +00002023 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002024 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002025 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002026
2027 // FIXME: Do something with the .line.
2028 }
2029
Daniel Dunbareceec052010-07-12 17:45:27 +00002030 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002031 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002032
2033 return false;
2034}
2035
2036
2037/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002038/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002039/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2040/// The first number is a file number, must have been previously assigned with
2041/// a .file directive, the second number is the line number and optionally the
2042/// third number is a column position (zero if not specified). The remaining
2043/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002044bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002045
Daniel Dunbareceec052010-07-12 17:45:27 +00002046 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002047 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002048 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002049 if (FileNumber < 1)
2050 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002051 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002052 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002053 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002054
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002055 int64_t LineNumber = 0;
2056 if (getLexer().is(AsmToken::Integer)) {
2057 LineNumber = getTok().getIntVal();
2058 if (LineNumber < 1)
2059 return TokError("line number less than one in '.loc' directive");
2060 Lex();
2061 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002062
2063 int64_t ColumnPos = 0;
2064 if (getLexer().is(AsmToken::Integer)) {
2065 ColumnPos = getTok().getIntVal();
2066 if (ColumnPos < 0)
2067 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002068 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002069 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002070
Kevin Enderbyc0957932010-09-30 16:52:03 +00002071 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002072 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002073 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002074 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2075 for (;;) {
2076 if (getLexer().is(AsmToken::EndOfStatement))
2077 break;
2078
2079 StringRef Name;
2080 SMLoc Loc = getTok().getLoc();
2081 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002082 return TokError("unexpected token in '.loc' directive");
2083
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002084 if (Name == "basic_block")
2085 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2086 else if (Name == "prologue_end")
2087 Flags |= DWARF2_FLAG_PROLOGUE_END;
2088 else if (Name == "epilogue_begin")
2089 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2090 else if (Name == "is_stmt") {
2091 SMLoc Loc = getTok().getLoc();
2092 const MCExpr *Value;
2093 if (getParser().ParseExpression(Value))
2094 return true;
2095 // The expression must be the constant 0 or 1.
2096 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2097 int Value = MCE->getValue();
2098 if (Value == 0)
2099 Flags &= ~DWARF2_FLAG_IS_STMT;
2100 else if (Value == 1)
2101 Flags |= DWARF2_FLAG_IS_STMT;
2102 else
2103 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002104 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002105 else {
2106 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2107 }
2108 }
2109 else if (Name == "isa") {
2110 SMLoc Loc = getTok().getLoc();
2111 const MCExpr *Value;
2112 if (getParser().ParseExpression(Value))
2113 return true;
2114 // The expression must be a constant greater or equal to 0.
2115 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2116 int Value = MCE->getValue();
2117 if (Value < 0)
2118 return Error(Loc, "isa number less than zero");
2119 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002120 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002121 else {
2122 return Error(Loc, "isa number not a constant value");
2123 }
2124 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002125 else if (Name == "discriminator") {
2126 if (getParser().ParseAbsoluteExpression(Discriminator))
2127 return true;
2128 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002129 else {
2130 return Error(Loc, "unknown sub-directive in '.loc' directive");
2131 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002132
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002133 if (getLexer().is(AsmToken::EndOfStatement))
2134 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002135 }
2136 }
2137
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002138 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2139 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002140
2141 return false;
2142}
2143
Daniel Dunbar138abae2010-10-16 04:56:42 +00002144/// ParseDirectiveStabs
2145/// ::= .stabs string, number, number, number
2146bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2147 SMLoc DirectiveLoc) {
2148 return TokError("unsupported directive '" + Directive + "'");
2149}
2150
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002151/// ParseDirectiveCFIStartProc
2152/// ::= .cfi_startproc
2153bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2154 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002155 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002156}
2157
2158/// ParseDirectiveCFIEndProc
2159/// ::= .cfi_endproc
2160bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002161 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002162}
2163
2164/// ParseDirectiveCFIDefCfaOffset
2165/// ::= .cfi_def_cfa_offset offset
2166bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2167 SMLoc DirectiveLoc) {
2168 int64_t Offset = 0;
2169 if (getParser().ParseAbsoluteExpression(Offset))
2170 return true;
2171
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002172 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002173}
2174
2175/// ParseDirectiveCFIDefCfaRegister
2176/// ::= .cfi_def_cfa_register register
2177bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2178 SMLoc DirectiveLoc) {
2179 int64_t Register = 0;
2180 if (getParser().ParseAbsoluteExpression(Register))
2181 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002182
2183 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002184}
2185
2186/// ParseDirectiveCFIOffset
2187/// ::= .cfi_off register, offset
2188bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2189 int64_t Register = 0;
2190 int64_t Offset = 0;
2191 if (getParser().ParseAbsoluteExpression(Register))
2192 return true;
2193
2194 if (getLexer().isNot(AsmToken::Comma))
2195 return TokError("unexpected token in directive");
2196 Lex();
2197
2198 if (getParser().ParseAbsoluteExpression(Offset))
2199 return true;
2200
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002201 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002202}
2203
2204/// ParseDirectiveCFIPersonalityOrLsda
2205/// ::= .cfi_personality encoding, [symbol_name]
2206/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002207bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002208 SMLoc DirectiveLoc) {
2209 int64_t Encoding = 0;
2210 if (getParser().ParseAbsoluteExpression(Encoding))
2211 return true;
2212 if (Encoding == 255)
2213 return false;
2214
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002215 if (Encoding != 0)
2216 return TokError("unsupported encoding.");
2217
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002218 if (getLexer().isNot(AsmToken::Comma))
2219 return TokError("unexpected token in directive");
2220 Lex();
2221
2222 StringRef Name;
2223 if (getParser().ParseIdentifier(Name))
2224 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002225
2226 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2227
2228 if (IDVal == ".cfi_personality")
2229 return getStreamer().EmitCFIPersonality(Sym);
2230 else {
2231 assert(IDVal == ".cfi_lsda");
2232 return getStreamer().EmitCFILsda(Sym);
2233 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002234}
2235
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002236/// ParseDirectiveMacrosOnOff
2237/// ::= .macros_on
2238/// ::= .macros_off
2239bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2240 SMLoc DirectiveLoc) {
2241 if (getLexer().isNot(AsmToken::EndOfStatement))
2242 return Error(getLexer().getLoc(),
2243 "unexpected token in '" + Directive + "' directive");
2244
2245 getParser().MacrosEnabled = Directive == ".macros_on";
2246
2247 return false;
2248}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002249
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002250/// ParseDirectiveMacro
2251/// ::= .macro name
2252bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2253 SMLoc DirectiveLoc) {
2254 StringRef Name;
2255 if (getParser().ParseIdentifier(Name))
2256 return TokError("expected identifier in directive");
2257
2258 if (getLexer().isNot(AsmToken::EndOfStatement))
2259 return TokError("unexpected token in '.macro' directive");
2260
2261 // Eat the end of statement.
2262 Lex();
2263
2264 AsmToken EndToken, StartToken = getTok();
2265
2266 // Lex the macro definition.
2267 for (;;) {
2268 // Check whether we have reached the end of the file.
2269 if (getLexer().is(AsmToken::Eof))
2270 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2271
2272 // Otherwise, check whether we have reach the .endmacro.
2273 if (getLexer().is(AsmToken::Identifier) &&
2274 (getTok().getIdentifier() == ".endm" ||
2275 getTok().getIdentifier() == ".endmacro")) {
2276 EndToken = getTok();
2277 Lex();
2278 if (getLexer().isNot(AsmToken::EndOfStatement))
2279 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2280 "' directive");
2281 break;
2282 }
2283
2284 // Otherwise, scan til the end of the statement.
2285 getParser().EatToEndOfStatement();
2286 }
2287
2288 if (getParser().MacroMap.lookup(Name)) {
2289 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2290 }
2291
2292 const char *BodyStart = StartToken.getLoc().getPointer();
2293 const char *BodyEnd = EndToken.getLoc().getPointer();
2294 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2295 getParser().MacroMap[Name] = new Macro(Name, Body);
2296 return false;
2297}
2298
2299/// ParseDirectiveEndMacro
2300/// ::= .endm
2301/// ::= .endmacro
2302bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2303 SMLoc DirectiveLoc) {
2304 if (getLexer().isNot(AsmToken::EndOfStatement))
2305 return TokError("unexpected token in '" + Directive + "' directive");
2306
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002307 // If we are inside a macro instantiation, terminate the current
2308 // instantiation.
2309 if (!getParser().ActiveMacros.empty()) {
2310 getParser().HandleMacroExit();
2311 return false;
2312 }
2313
2314 // Otherwise, this .endmacro is a stray entry in the file; well formed
2315 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002316 return TokError("unexpected '" + Directive + "' in file, "
2317 "no current macro definition");
2318}
2319
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002320bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002321 getParser().CheckForValidSection();
2322
2323 const MCExpr *Value;
2324
2325 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002326 return true;
2327
2328 if (getLexer().isNot(AsmToken::EndOfStatement))
2329 return TokError("unexpected token in directive");
2330
2331 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002332 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002333 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002334 getStreamer().EmitULEB128Value(Value);
2335
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002336 return false;
2337}
2338
2339
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002340/// \brief Create an MCAsmParser instance.
2341MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2342 MCContext &C, MCStreamer &Out,
2343 const MCAsmInfo &MAI) {
2344 return new AsmParser(T, SM, C, Out, MAI);
2345}