blob: 9cb15b496fb2dcbfbeb39928210b2ad4adb315c0 [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"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000033#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000035#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000036#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000037using namespace llvm;
38
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000039namespace {
40
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000041/// \brief Helper class for tracking macro definitions.
42struct Macro {
43 StringRef Name;
44 StringRef Body;
45
46public:
47 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
48};
49
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000050/// \brief Helper class for storing information about an active macro
51/// instantiation.
52struct MacroInstantiation {
53 /// The macro being instantiated.
54 const Macro *TheMacro;
55
56 /// The macro instantiation with substitutions.
57 MemoryBuffer *Instantiation;
58
59 /// The location of the instantiation.
60 SMLoc InstantiationLoc;
61
62 /// The location where parsing should resume upon instantiation completion.
63 SMLoc ExitLoc;
64
65public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000066 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
67 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000068};
69
Daniel Dunbaraef87e32010-07-18 18:31:38 +000070/// \brief The concrete assembly parser instance.
71class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000072 friend class GenericAsmParser;
73
Daniel Dunbaraef87e32010-07-18 18:31:38 +000074 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
75 void operator=(const AsmParser &); // DO NOT IMPLEMENT
76private:
77 AsmLexer Lexer;
78 MCContext &Ctx;
79 MCStreamer &Out;
80 SourceMgr &SrcMgr;
81 MCAsmParserExtension *GenericParser;
82 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000083
Daniel Dunbaraef87e32010-07-18 18:31:38 +000084 /// This is the current buffer index we're lexing from as managed by the
85 /// SourceMgr object.
86 int CurBuffer;
87
88 AsmCond TheCondState;
89 std::vector<AsmCond> TheCondStack;
90
91 /// DirectiveMap - This is a table handlers for directives. Each handler is
92 /// invoked after the directive identifier is read and is responsible for
93 /// parsing and validating the rest of the directive. The handler is passed
94 /// in the directive name and the location of the directive keyword.
95 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000096
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000097 /// MacroMap - Map of currently defined macros.
98 StringMap<Macro*> MacroMap;
99
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000100 /// ActiveMacros - Stack of active macro instantiations.
101 std::vector<MacroInstantiation*> ActiveMacros;
102
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000103 /// Boolean tracking whether macro substitution is enabled.
104 unsigned MacrosEnabled : 1;
105
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000106 /// Flag tracking whether any errors have been encountered.
107 unsigned HadError : 1;
108
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000109public:
110 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
111 const MCAsmInfo &MAI);
112 ~AsmParser();
113
114 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
115
116 void AddDirectiveHandler(MCAsmParserExtension *Object,
117 StringRef Directive,
118 DirectiveHandler Handler) {
119 DirectiveMap[Directive] = std::make_pair(Object, Handler);
120 }
121
122public:
123 /// @name MCAsmParser Interface
124 /// {
125
126 virtual SourceMgr &getSourceManager() { return SrcMgr; }
127 virtual MCAsmLexer &getLexer() { return Lexer; }
128 virtual MCContext &getContext() { return Ctx; }
129 virtual MCStreamer &getStreamer() { return Out; }
130
131 virtual void Warning(SMLoc L, const Twine &Meg);
132 virtual bool Error(SMLoc L, const Twine &Msg);
133
134 const AsmToken &Lex();
135
136 bool ParseExpression(const MCExpr *&Res);
137 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
139 virtual bool ParseAbsoluteExpression(int64_t &Res);
140
141 /// }
142
143private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000144 void CheckForValidSection();
145
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 bool ParseStatement();
147
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000148 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
149 void HandleMacroExit();
150
151 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000152 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
153 SrcMgr.PrintMessage(Loc, Msg, Type);
154 }
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
157 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000158
159 /// \brief Reset the current lexer position to that given by \arg Loc. The
160 /// current token is not set; clients should ensure Lex() is called
161 /// subsequently.
162 void JumpToLoc(SMLoc Loc);
163
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000165
166 /// \brief Parse up to the end of statement and a return the contents from the
167 /// current token until the end of the statement; the current token on exit
168 /// will be either the EndOfStatement or EOF.
169 StringRef ParseStringToEndOfStatement();
170
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000171 bool ParseAssignment(StringRef Name);
172
173 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
175 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
176
177 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
178 /// and set \arg Res to the identifier contents.
179 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000180
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000181 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000182
183 // ".ascii", ".asciiz", ".string"
184 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000186 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187 bool ParseDirectiveFill(); // ".fill"
188 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000189 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000190 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000191 bool ParseDirectiveOrg(); // ".org"
192 // ".align{,32}", ".p2align{,w,l}"
193 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
194
195 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
196 /// accepts a single symbol (which should be a label or an external).
197 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198
199 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
200
201 bool ParseDirectiveAbort(); // ".abort"
202 bool ParseDirectiveInclude(); // ".include"
203
204 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
205 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
206 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
207 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
208
209 /// ParseEscapedString - Parse the current token as a string which may include
210 /// escaped characters and return the string contents.
211 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000212
213 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
214 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000215};
216
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000217/// \brief Generic implementations of directive handling, etc. which is shared
218/// (or the default, at least) for all assembler parser.
219class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000220 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
221 void AddDirectiveHandler(StringRef Directive) {
222 getParser().AddDirectiveHandler(this, Directive,
223 HandleDirective<GenericAsmParser, Handler>);
224 }
225
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000226public:
227 GenericAsmParser() {}
228
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000229 AsmParser &getParser() {
230 return (AsmParser&) this->MCAsmParserExtension::getParser();
231 }
232
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000233 virtual void Initialize(MCAsmParser &Parser) {
234 // Call the base implementation.
235 this->MCAsmParserExtension::Initialize(Parser);
236
237 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000241 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000242
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000243 // CFI directives.
244 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
245 ".cfi_startproc");
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
247 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
249 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
251 ".cfi_def_cfa_offset");
252 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
253 ".cfi_def_cfa_register");
254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
255 ".cfi_offset");
256 AddDirectiveHandler<
257 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
258 AddDirectiveHandler<
259 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000260 AddDirectiveHandler<
261 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
262 AddDirectiveHandler<
263 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000264
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000265 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
267 ".macros_on");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
269 ".macros_off");
270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000273
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
275 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000276 }
277
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000278 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
279
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000280 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
281 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
282 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000283 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000284 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
285 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000286 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000287 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
288 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
289 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
290 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000291 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
292 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000293
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000294 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000295 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
296 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000297
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000298 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000299};
300
301}
302
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000303namespace llvm {
304
305extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000306extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000307extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000308
309}
310
Chris Lattneraaec2052010-01-19 19:46:13 +0000311enum { DEFAULT_ADDRSPACE = 0 };
312
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000313AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
314 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000315 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000316 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000317 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000318 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000319
320 // Initialize the generic parser.
321 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000322
323 // Initialize the platform / file format parser.
324 //
325 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
326 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000327 if (_MAI.hasMicrosoftFastStdCallMangling()) {
328 PlatformParser = createCOFFAsmParser();
329 PlatformParser->Initialize(*this);
330 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000331 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000332 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000333 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000334 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000335 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000336 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000337}
338
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000339AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000340 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
341
342 // Destroy any macros.
343 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
344 ie = MacroMap.end(); it != ie; ++it)
345 delete it->getValue();
346
Daniel Dunbare4749702010-07-12 18:12:02 +0000347 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000348 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000349}
350
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000351void AsmParser::PrintMacroInstantiations() {
352 // Print the active macro instantiation stack.
353 for (std::vector<MacroInstantiation*>::const_reverse_iterator
354 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
355 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
356 "note");
357}
358
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000359void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000360 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000361 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000362}
363
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000364bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000365 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000366 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000367 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000368 return true;
369}
370
Sean Callananfd0b0282010-01-21 00:19:58 +0000371bool AsmParser::EnterIncludeFile(const std::string &Filename) {
372 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
373 if (NewBuf == -1)
374 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000375
Sean Callananfd0b0282010-01-21 00:19:58 +0000376 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000377
Sean Callananfd0b0282010-01-21 00:19:58 +0000378 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000379
Sean Callananfd0b0282010-01-21 00:19:58 +0000380 return false;
381}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000382
383void AsmParser::JumpToLoc(SMLoc Loc) {
384 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
385 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
386}
387
Sean Callananfd0b0282010-01-21 00:19:58 +0000388const AsmToken &AsmParser::Lex() {
389 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000390
Sean Callananfd0b0282010-01-21 00:19:58 +0000391 if (tok->is(AsmToken::Eof)) {
392 // If this is the end of an included file, pop the parent file off the
393 // include stack.
394 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
395 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000396 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000397 tok = &Lexer.Lex();
398 }
399 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000400
Sean Callananfd0b0282010-01-21 00:19:58 +0000401 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000402 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000403
Sean Callananfd0b0282010-01-21 00:19:58 +0000404 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000405}
406
Chris Lattner79180e22010-04-05 23:15:42 +0000407bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000408 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000409 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000410 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000411
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000412 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000413 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000414
415 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000416 AsmCond StartingCondState = TheCondState;
417
Chris Lattnerb717fb02009-07-02 21:53:43 +0000418 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000420 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000421
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000422 // We had an error, validate that one was emitted and recover by skipping to
423 // the next line.
424 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000425 EatToEndOfStatement();
426 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000427
428 if (TheCondState.TheCond != StartingCondState.TheCond ||
429 TheCondState.Ignore != StartingCondState.Ignore)
430 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000431
432 // Check to see there are no empty DwarfFile slots.
433 const std::vector<MCDwarfFile *> &MCDwarfFiles =
434 getContext().getMCDwarfFiles();
435 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000436 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000437 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000438 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000439
Chris Lattner79180e22010-04-05 23:15:42 +0000440 // Finalize the output stream if there are no errors and if the client wants
441 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000442 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000443 Out.Finish();
444
Chris Lattnerb717fb02009-07-02 21:53:43 +0000445 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000446}
447
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000448void AsmParser::CheckForValidSection() {
449 if (!getStreamer().getCurrentSection()) {
450 TokError("expected section directive before assembly directive");
451 Out.SwitchSection(Ctx.getMachOSection(
452 "__TEXT", "__text",
453 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
454 0, SectionKind::getText()));
455 }
456}
457
Chris Lattner2cf5f142009-06-22 01:29:09 +0000458/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
459void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000460 while (Lexer.isNot(AsmToken::EndOfStatement) &&
461 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000462 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000463
Chris Lattner2cf5f142009-06-22 01:29:09 +0000464 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000465 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000466 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000467}
468
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000469StringRef AsmParser::ParseStringToEndOfStatement() {
470 const char *Start = getTok().getLoc().getPointer();
471
472 while (Lexer.isNot(AsmToken::EndOfStatement) &&
473 Lexer.isNot(AsmToken::Eof))
474 Lex();
475
476 const char *End = getTok().getLoc().getPointer();
477 return StringRef(Start, End - Start);
478}
Chris Lattnerc4193832009-06-22 05:51:26 +0000479
Chris Lattner74ec1a32009-06-22 06:32:03 +0000480/// ParseParenExpr - Parse a paren expression and return it.
481/// NOTE: This assumes the leading '(' has already been consumed.
482///
483/// parenexpr ::= expr)
484///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000485bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000486 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000487 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000488 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000489 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000490 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000491 return false;
492}
Chris Lattnerc4193832009-06-22 05:51:26 +0000493
Chris Lattner74ec1a32009-06-22 06:32:03 +0000494/// ParsePrimaryExpr - Parse a primary expression and return it.
495/// primaryexpr ::= (parenexpr
496/// primaryexpr ::= symbol
497/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000498/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000499/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000500bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000501 switch (Lexer.getKind()) {
502 default:
503 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000504 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000505 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000506 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000507 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000508 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000509 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000510 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000511 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000512 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000513 EndLoc = Lexer.getLoc();
514
515 StringRef Identifier;
516 if (ParseIdentifier(Identifier))
517 return false;
518
Daniel Dunbarfffff912009-10-16 01:34:54 +0000519 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000520 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000521 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000522
523 // Lookup the symbol variant if used.
524 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000525 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000526 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000527 if (Variant == MCSymbolRefExpr::VK_Invalid) {
528 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000529 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000530 }
531 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000532
Daniel Dunbarfffff912009-10-16 01:34:54 +0000533 // If this is an absolute variable reference, substitute it now to preserve
534 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000535 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000536 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000537 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000538
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000539 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000540 return false;
541 }
542
543 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000544 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000545 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000546 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000547 case AsmToken::Integer: {
548 SMLoc Loc = getTok().getLoc();
549 int64_t IntVal = getTok().getIntVal();
550 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000551 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000552 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000553 // Look for 'b' or 'f' following an Integer as a directional label
554 if (Lexer.getKind() == AsmToken::Identifier) {
555 StringRef IDVal = getTok().getString();
556 if (IDVal == "f" || IDVal == "b"){
557 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
558 IDVal == "f" ? 1 : 0);
559 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
560 getContext());
561 if(IDVal == "b" && Sym->isUndefined())
562 return Error(Loc, "invalid reference to undefined symbol");
563 EndLoc = Lexer.getLoc();
564 Lex(); // Eat identifier.
565 }
566 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000567 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000568 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000569 case AsmToken::Real: {
570 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
571 int64_t IntVal = RealVal.bitcastToAPInt().getSExtValue();
572 Res = MCConstantExpr::Create(IntVal, getContext());
573 Lex(); // Eat token.
574 return false;
575 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000576 case AsmToken::Dot: {
577 // This is a '.' reference, which references the current PC. Emit a
578 // temporary label to the streamer and refer to it.
579 MCSymbol *Sym = Ctx.CreateTempSymbol();
580 Out.EmitLabel(Sym);
581 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
582 EndLoc = Lexer.getLoc();
583 Lex(); // Eat identifier.
584 return false;
585 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000586 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000587 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000588 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000589 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000590 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000591 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000592 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000593 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000594 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000595 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000596 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000597 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000598 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000599 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000600 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000601 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000602 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000603 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000604 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000605 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000606 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000607 }
608}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000609
Chris Lattnerb4307b32010-01-15 19:28:38 +0000610bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000611 SMLoc EndLoc;
612 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000613}
614
Daniel Dunbarcceba832010-09-17 02:47:07 +0000615const MCExpr *
616AsmParser::ApplyModifierToExpr(const MCExpr *E,
617 MCSymbolRefExpr::VariantKind Variant) {
618 // Recurse over the given expression, rebuilding it to apply the given variant
619 // if there is exactly one symbol.
620 switch (E->getKind()) {
621 case MCExpr::Target:
622 case MCExpr::Constant:
623 return 0;
624
625 case MCExpr::SymbolRef: {
626 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
627
628 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
629 TokError("invalid variant on expression '" +
630 getTok().getIdentifier() + "' (already modified)");
631 return E;
632 }
633
634 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
635 }
636
637 case MCExpr::Unary: {
638 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
639 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
640 if (!Sub)
641 return 0;
642 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
643 }
644
645 case MCExpr::Binary: {
646 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
647 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
648 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
649
650 if (!LHS && !RHS)
651 return 0;
652
653 if (!LHS) LHS = BE->getLHS();
654 if (!RHS) RHS = BE->getRHS();
655
656 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
657 }
658 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000659
660 assert(0 && "Invalid expression kind!");
661 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000662}
663
Chris Lattner74ec1a32009-06-22 06:32:03 +0000664/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000665///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000666/// expr ::= expr +,- expr -> lowest.
667/// expr ::= expr |,^,&,! expr -> middle.
668/// expr ::= expr *,/,%,<<,>> expr -> highest.
669/// expr ::= primaryexpr
670///
Chris Lattner54482b42010-01-15 19:39:23 +0000671bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000672 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000673 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000674 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
675 return true;
676
Daniel Dunbarcceba832010-09-17 02:47:07 +0000677 // As a special case, we support 'a op b @ modifier' by rewriting the
678 // expression to include the modifier. This is inefficient, but in general we
679 // expect users to use 'a@modifier op b'.
680 if (Lexer.getKind() == AsmToken::At) {
681 Lex();
682
683 if (Lexer.isNot(AsmToken::Identifier))
684 return TokError("unexpected symbol modifier following '@'");
685
686 MCSymbolRefExpr::VariantKind Variant =
687 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
688 if (Variant == MCSymbolRefExpr::VK_Invalid)
689 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
690
691 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
692 if (!ModifiedRes) {
693 return TokError("invalid modifier '" + getTok().getIdentifier() +
694 "' (no symbols present)");
695 return true;
696 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000697
Daniel Dunbarcceba832010-09-17 02:47:07 +0000698 Res = ModifiedRes;
699 Lex();
700 }
701
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000702 // Try to constant fold it up front, if possible.
703 int64_t Value;
704 if (Res->EvaluateAsAbsolute(Value))
705 Res = MCConstantExpr::Create(Value, getContext());
706
707 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000708}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000709
Chris Lattnerb4307b32010-01-15 19:28:38 +0000710bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000711 Res = 0;
712 return ParseParenExpr(Res, EndLoc) ||
713 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000714}
715
Daniel Dunbar475839e2009-06-29 20:37:27 +0000716bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000717 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000718
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000719 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000720 if (ParseExpression(Expr))
721 return true;
722
Daniel Dunbare00b0112009-10-16 01:57:52 +0000723 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000724 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000725
726 return false;
727}
728
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000729static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000730 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000731 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000732 default:
733 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000734
Daniel Dunbarcceba832010-09-17 02:47:07 +0000735 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000736 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000737 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000738 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000739 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000740 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000741 return 1;
742
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000743
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000744 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000745 //
746 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000747 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000748 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000749 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000751 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000752 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000753 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000754 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000755 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000756
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000757 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000758 case AsmToken::EqualEqual:
759 Kind = MCBinaryExpr::EQ;
760 return 3;
761 case AsmToken::ExclaimEqual:
762 case AsmToken::LessGreater:
763 Kind = MCBinaryExpr::NE;
764 return 3;
765 case AsmToken::Less:
766 Kind = MCBinaryExpr::LT;
767 return 3;
768 case AsmToken::LessEqual:
769 Kind = MCBinaryExpr::LTE;
770 return 3;
771 case AsmToken::Greater:
772 Kind = MCBinaryExpr::GT;
773 return 3;
774 case AsmToken::GreaterEqual:
775 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000776 return 3;
777
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000778 // High Intermediate Precedence: +, -
779 case AsmToken::Plus:
780 Kind = MCBinaryExpr::Add;
781 return 4;
782 case AsmToken::Minus:
783 Kind = MCBinaryExpr::Sub;
784 return 4;
785
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000788 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000789 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000791 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000792 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000793 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000794 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000795 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000796 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000797 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000798 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000799 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000800 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000801 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000802 }
803}
804
805
806/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
807/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000808bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
809 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000810 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000811 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000812 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000813
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000814 // If the next token is lower precedence than we are allowed to eat, return
815 // successfully with what we ate already.
816 if (TokPrec < Precedence)
817 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000818
Sean Callanan79ed1a82010-01-19 20:22:31 +0000819 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000820
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000821 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000822 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000823 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000824
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000825 // If BinOp binds less tightly with RHS than the operator after RHS, let
826 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000827 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000828 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000829 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000830 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000831 }
832
Daniel Dunbar475839e2009-06-29 20:37:27 +0000833 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000834 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000835 }
836}
837
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000838
839
840
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000841/// ParseStatement:
842/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000843/// ::= Label* Directive ...Operands... EndOfStatement
844/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000845bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000846 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000847 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000848 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000849 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000850 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000851
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000852 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000853 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000854 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000855 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000856 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000857 // A full line comment is a '#' as the first token.
858 if (Lexer.is(AsmToken::Hash)) {
859 EatToEndOfStatement();
860 return false;
861 }
862 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000863 if (Lexer.is(AsmToken::Integer)) {
864 LocalLabelVal = getTok().getIntVal();
865 if (LocalLabelVal < 0) {
866 if (!TheCondState.Ignore)
867 return TokError("unexpected token at start of statement");
868 IDVal = "";
869 }
870 else {
871 IDVal = getTok().getString();
872 Lex(); // Consume the integer token to be used as an identifier token.
873 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000874 if (!TheCondState.Ignore)
875 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000876 }
877 }
878 }
879 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000880 if (!TheCondState.Ignore)
881 return TokError("unexpected token at start of statement");
882 IDVal = "";
883 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000884
Chris Lattner7834fac2010-04-17 18:14:27 +0000885 // Handle conditional assembly here before checking for skipping. We
886 // have to do this so that .endif isn't skipped in a ".if 0" block for
887 // example.
888 if (IDVal == ".if")
889 return ParseDirectiveIf(IDLoc);
890 if (IDVal == ".elseif")
891 return ParseDirectiveElseIf(IDLoc);
892 if (IDVal == ".else")
893 return ParseDirectiveElse(IDLoc);
894 if (IDVal == ".endif")
895 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000896
Chris Lattner7834fac2010-04-17 18:14:27 +0000897 // If we are in a ".if 0" block, ignore this statement.
898 if (TheCondState.Ignore) {
899 EatToEndOfStatement();
900 return false;
901 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000902
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000903 // FIXME: Recurse on local labels?
904
905 // See what kind of statement we have.
906 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000908 CheckForValidSection();
909
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000910 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000911 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000912
913 // Diagnose attempt to use a variable as a label.
914 //
915 // FIXME: Diagnostics. Note the location of the definition as a label.
916 // FIXME: This doesn't diagnose assignment to a symbol which has been
917 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000918 MCSymbol *Sym;
919 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000920 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000921 else
922 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000923 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000924 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000925
Daniel Dunbar959fd882009-08-26 22:13:22 +0000926 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000927 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000928
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000929 // Consume any end of statement token, if present, to avoid spurious
930 // AddBlankLine calls().
931 if (Lexer.is(AsmToken::EndOfStatement)) {
932 Lex();
933 if (Lexer.is(AsmToken::Eof))
934 return false;
935 }
936
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000937 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000938 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000939
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000941 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000942 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000943
Daniel Dunbare2ace502009-08-31 08:09:09 +0000944 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000945
946 default: // Normal instruction or directive.
947 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000948 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000949
950 // If macros are enabled, check to see if this is a macro instantiation.
951 if (MacrosEnabled)
952 if (const Macro *M = MacroMap.lookup(IDVal))
953 return HandleMacroEntry(IDVal, IDLoc, M);
954
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000955 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000956 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000957 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000958 if (IDVal == ".set" || IDVal == ".equ")
959 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000960
Daniel Dunbara0d14262009-06-24 23:30:00 +0000961 // Data directives
962
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000963 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000964 return ParseDirectiveAscii(IDVal, false);
965 if (IDVal == ".asciz" || IDVal == ".string")
966 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000967
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000968 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000970 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000971 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000972 if (IDVal == ".value")
973 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000974 if (IDVal == ".2byte")
975 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000976 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000978 if (IDVal == ".int")
979 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000980 if (IDVal == ".4byte")
981 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000982 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000983 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000984 if (IDVal == ".8byte")
985 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000986 if (IDVal == ".single")
987 return ParseDirectiveRealValue(APFloat::IEEEsingle);
988 if (IDVal == ".double")
989 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000990
Eli Friedman5d68ec22010-07-19 04:17:25 +0000991 if (IDVal == ".align") {
992 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
993 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
994 }
995 if (IDVal == ".align32") {
996 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
997 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
998 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000999 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001000 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001001 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001002 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001003 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001004 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001005 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001006 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001007 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001008 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001009 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001010 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1011
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001012 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001013 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001014
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001015 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001016 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001017 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001019 if (IDVal == ".zero")
1020 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001022 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001023
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001024 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001025 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001026 // ELF only? Should it be here?
1027 if (IDVal == ".local")
1028 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001029 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001030 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001032 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001033 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001034 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001035 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001036 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001037 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001038 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001039 if (IDVal == ".symbol_resolver")
1040 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001041 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001042 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001043 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001044 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001045 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001046 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001047 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001048 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001049 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001050 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001051 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001052 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001053 if (IDVal == ".weak_def_can_be_hidden")
1054 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001055
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001056 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001057 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001058 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001059 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001060
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001061 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001062 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001063 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001064 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001065
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001066 // Look up the handler in the handler table.
1067 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1068 DirectiveMap.lookup(IDVal);
1069 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001070 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001071
Kevin Enderby9c656452009-09-10 20:51:44 +00001072 // Target hook for parsing target specific directives.
1073 if (!getTargetParser().ParseDirective(ID))
1074 return false;
1075
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001076 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001077 EatToEndOfStatement();
1078 return false;
1079 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001080
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001081 CheckForValidSection();
1082
Chris Lattnera7f13542010-05-19 23:34:33 +00001083 // Canonicalize the opcode to lower case.
1084 SmallString<128> Opcode;
1085 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1086 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001087
Chris Lattner98986712010-01-14 22:21:20 +00001088 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001089 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001090 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001091
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001092 // Dump the parsed representation, if requested.
1093 if (getShowParsedOperands()) {
1094 SmallString<256> Str;
1095 raw_svector_ostream OS(Str);
1096 OS << "parsed instruction: [";
1097 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1098 if (i != 0)
1099 OS << ", ";
1100 ParsedOperands[i]->dump(OS);
1101 }
1102 OS << "]";
1103
1104 PrintMessage(IDLoc, OS.str(), "note");
1105 }
1106
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001107 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001108 if (!HadError)
1109 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1110 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001111
Chris Lattner98986712010-01-14 22:21:20 +00001112 // Free any parsed operands.
1113 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1114 delete ParsedOperands[i];
1115
Chris Lattnercbf8a982010-09-11 16:18:25 +00001116 // Don't skip the rest of the line, the instruction parser is responsible for
1117 // that.
1118 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001119}
Chris Lattner9a023f72009-06-24 04:43:34 +00001120
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001121MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1122 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001123 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1124{
1125 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1126 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001127 SmallString<256> Buf;
1128 raw_svector_ostream OS(Buf);
1129
1130 StringRef Body = M->Body;
1131 while (!Body.empty()) {
1132 // Scan for the next substitution.
1133 std::size_t End = Body.size(), Pos = 0;
1134 for (; Pos != End; ++Pos) {
1135 // Check for a substitution or escape.
1136 if (Body[Pos] != '$' || Pos + 1 == End)
1137 continue;
1138
1139 char Next = Body[Pos + 1];
1140 if (Next == '$' || Next == 'n' || isdigit(Next))
1141 break;
1142 }
1143
1144 // Add the prefix.
1145 OS << Body.slice(0, Pos);
1146
1147 // Check if we reached the end.
1148 if (Pos == End)
1149 break;
1150
1151 switch (Body[Pos+1]) {
1152 // $$ => $
1153 case '$':
1154 OS << '$';
1155 break;
1156
1157 // $n => number of arguments
1158 case 'n':
1159 OS << A.size();
1160 break;
1161
1162 // $[0-9] => argument
1163 default: {
1164 // Missing arguments are ignored.
1165 unsigned Index = Body[Pos+1] - '0';
1166 if (Index >= A.size())
1167 break;
1168
1169 // Otherwise substitute with the token values, with spaces eliminated.
1170 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1171 ie = A[Index].end(); it != ie; ++it)
1172 OS << it->getString();
1173 break;
1174 }
1175 }
1176
1177 // Update the scan point.
1178 Body = Body.substr(Pos + 2);
1179 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001180
1181 // We include the .endmacro in the buffer as our queue to exit the macro
1182 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001183 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001184
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001185 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001186}
1187
1188bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1189 const Macro *M) {
1190 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1191 // this, although we should protect against infinite loops.
1192 if (ActiveMacros.size() == 20)
1193 return TokError("macros cannot be nested more than 20 levels deep");
1194
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001195 // Parse the macro instantiation arguments.
1196 std::vector<std::vector<AsmToken> > MacroArguments;
1197 MacroArguments.push_back(std::vector<AsmToken>());
1198 unsigned ParenLevel = 0;
1199 for (;;) {
1200 if (Lexer.is(AsmToken::Eof))
1201 return TokError("unexpected token in macro instantiation");
1202 if (Lexer.is(AsmToken::EndOfStatement))
1203 break;
1204
1205 // If we aren't inside parentheses and this is a comma, start a new token
1206 // list.
1207 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1208 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001209 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001210 // Adjust the current parentheses level.
1211 if (Lexer.is(AsmToken::LParen))
1212 ++ParenLevel;
1213 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1214 --ParenLevel;
1215
1216 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001217 MacroArguments.back().push_back(getTok());
1218 }
1219 Lex();
1220 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001221
1222 // Create the macro instantiation object and add to the current macro
1223 // instantiation stack.
1224 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001225 getTok().getLoc(),
1226 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001227 ActiveMacros.push_back(MI);
1228
1229 // Jump to the macro instantiation and prime the lexer.
1230 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1231 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1232 Lex();
1233
1234 return false;
1235}
1236
1237void AsmParser::HandleMacroExit() {
1238 // Jump to the EndOfStatement we should return to, and consume it.
1239 JumpToLoc(ActiveMacros.back()->ExitLoc);
1240 Lex();
1241
1242 // Pop the instantiation entry.
1243 delete ActiveMacros.back();
1244 ActiveMacros.pop_back();
1245}
1246
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001247static void MarkUsed(const MCExpr *Value) {
1248 switch (Value->getKind()) {
1249 case MCExpr::Binary:
1250 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1251 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1252 break;
1253 case MCExpr::Target:
1254 case MCExpr::Constant:
1255 break;
1256 case MCExpr::SymbolRef: {
1257 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1258 break;
1259 }
1260 case MCExpr::Unary:
1261 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1262 break;
1263 }
1264}
1265
Benjamin Kramer38e59892010-07-14 22:38:02 +00001266bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001267 // FIXME: Use better location, we should use proper tokens.
1268 SMLoc EqualLoc = Lexer.getLoc();
1269
Daniel Dunbar821e3332009-08-31 08:09:28 +00001270 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001271 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001272 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001273
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001274 MarkUsed(Value);
1275
Daniel Dunbar3f872332009-07-28 16:08:33 +00001276 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001277 return TokError("unexpected token in assignment");
1278
1279 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001280 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001281
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001282 // Validate that the LHS is allowed to be a variable (either it has not been
1283 // used as a symbol, or it is an absolute symbol).
1284 MCSymbol *Sym = getContext().LookupSymbol(Name);
1285 if (Sym) {
1286 // Diagnose assignment to a label.
1287 //
1288 // FIXME: Diagnostics. Note the location of the definition as a label.
1289 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001290 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001291 ; // Allow redefinitions of undefined symbols only used in directives.
1292 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001293 return Error(EqualLoc, "redefinition of '" + Name + "'");
1294 else if (!Sym->isVariable())
1295 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001296 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001297 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1298 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001299
1300 // Don't count these checks as uses.
1301 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001302 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001303 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001304
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001305 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001306
1307 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001308 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001309
1310 return false;
1311}
1312
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001313/// ParseIdentifier:
1314/// ::= identifier
1315/// ::= string
1316bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001317 // The assembler has relaxed rules for accepting identifiers, in particular we
1318 // allow things like '.globl $foo', which would normally be separate
1319 // tokens. At this level, we have already lexed so we cannot (currently)
1320 // handle this as a context dependent token, instead we detect adjacent tokens
1321 // and return the combined identifier.
1322 if (Lexer.is(AsmToken::Dollar)) {
1323 SMLoc DollarLoc = getLexer().getLoc();
1324
1325 // Consume the dollar sign, and check for a following identifier.
1326 Lex();
1327 if (Lexer.isNot(AsmToken::Identifier))
1328 return true;
1329
1330 // We have a '$' followed by an identifier, make sure they are adjacent.
1331 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1332 return true;
1333
1334 // Construct the joined identifier and consume the token.
1335 Res = StringRef(DollarLoc.getPointer(),
1336 getTok().getIdentifier().size() + 1);
1337 Lex();
1338 return false;
1339 }
1340
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001341 if (Lexer.isNot(AsmToken::Identifier) &&
1342 Lexer.isNot(AsmToken::String))
1343 return true;
1344
Sean Callanan18b83232010-01-19 21:44:56 +00001345 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001346
Sean Callanan79ed1a82010-01-19 20:22:31 +00001347 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001348
1349 return false;
1350}
1351
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001352/// ParseDirectiveSet:
1353/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001354bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001355 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001356
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001357 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001358 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001359
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001360 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001361 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001362 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001363
Daniel Dunbare2ace502009-08-31 08:09:09 +00001364 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001365}
1366
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001367bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001368 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001369
1370 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001371 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001372 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1373 if (Str[i] != '\\') {
1374 Data += Str[i];
1375 continue;
1376 }
1377
1378 // Recognize escaped characters. Note that this escape semantics currently
1379 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1380 ++i;
1381 if (i == e)
1382 return TokError("unexpected backslash at end of string");
1383
1384 // Recognize octal sequences.
1385 if ((unsigned) (Str[i] - '0') <= 7) {
1386 // Consume up to three octal characters.
1387 unsigned Value = Str[i] - '0';
1388
1389 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1390 ++i;
1391 Value = Value * 8 + (Str[i] - '0');
1392
1393 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1394 ++i;
1395 Value = Value * 8 + (Str[i] - '0');
1396 }
1397 }
1398
1399 if (Value > 255)
1400 return TokError("invalid octal escape sequence (out of range)");
1401
1402 Data += (unsigned char) Value;
1403 continue;
1404 }
1405
1406 // Otherwise recognize individual escapes.
1407 switch (Str[i]) {
1408 default:
1409 // Just reject invalid escape sequences for now.
1410 return TokError("invalid escape sequence (unrecognized character)");
1411
1412 case 'b': Data += '\b'; break;
1413 case 'f': Data += '\f'; break;
1414 case 'n': Data += '\n'; break;
1415 case 'r': Data += '\r'; break;
1416 case 't': Data += '\t'; break;
1417 case '"': Data += '"'; break;
1418 case '\\': Data += '\\'; break;
1419 }
1420 }
1421
1422 return false;
1423}
1424
Daniel Dunbara0d14262009-06-24 23:30:00 +00001425/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001426/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1427bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001428 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001429 CheckForValidSection();
1430
Daniel Dunbara0d14262009-06-24 23:30:00 +00001431 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001432 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001433 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001434
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001435 std::string Data;
1436 if (ParseEscapedString(Data))
1437 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001438
1439 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001440 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001441 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1442
Sean Callanan79ed1a82010-01-19 20:22:31 +00001443 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001444
1445 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001446 break;
1447
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001448 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001449 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001450 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001451 }
1452 }
1453
Sean Callanan79ed1a82010-01-19 20:22:31 +00001454 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001455 return false;
1456}
1457
1458/// ParseDirectiveValue
1459/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1460bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001461 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001462 CheckForValidSection();
1463
Daniel Dunbara0d14262009-06-24 23:30:00 +00001464 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001465 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001466 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001467 return true;
1468
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001469 // Special case constant expressions to match code generator.
1470 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001471 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001472 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001473 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001474
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001475 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001476 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001477
Daniel Dunbara0d14262009-06-24 23:30:00 +00001478 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001479 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001480 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001481 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001482 }
1483 }
1484
Sean Callanan79ed1a82010-01-19 20:22:31 +00001485 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001486 return false;
1487}
1488
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001489/// ParseDirectiveRealValue
1490/// ::= (.single | .double) [ expression (, expression)* ]
1491bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1492 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1493 CheckForValidSection();
1494
1495 for (;;) {
1496 // We don't truly support arithmetic on floating point expressions, so we
1497 // have to manually parse unary prefixes.
1498 bool IsNeg = false;
1499 if (getLexer().is(AsmToken::Minus)) {
1500 Lex();
1501 IsNeg = true;
1502 } else if (getLexer().is(AsmToken::Plus))
1503 Lex();
1504
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001505 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001506 getLexer().isNot(AsmToken::Real))
1507 return TokError("unexpected token in directive");
1508
1509 // Convert to an APFloat.
1510 APFloat Value(Semantics);
1511 if (Value.convertFromString(getTok().getString(),
1512 APFloat::rmNearestTiesToEven) ==
1513 APFloat::opInvalidOp)
1514 return TokError("invalid floating point literal");
1515 if (IsNeg)
1516 Value.changeSign();
1517
1518 // Consume the numeric token.
1519 Lex();
1520
1521 // Emit the value as an integer.
1522 APInt AsInt = Value.bitcastToAPInt();
1523 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1524 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1525
1526 if (getLexer().is(AsmToken::EndOfStatement))
1527 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001528
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001529 if (getLexer().isNot(AsmToken::Comma))
1530 return TokError("unexpected token in directive");
1531 Lex();
1532 }
1533 }
1534
1535 Lex();
1536 return false;
1537}
1538
Daniel Dunbara0d14262009-06-24 23:30:00 +00001539/// ParseDirectiveSpace
1540/// ::= .space expression [ , expression ]
1541bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001542 CheckForValidSection();
1543
Daniel Dunbara0d14262009-06-24 23:30:00 +00001544 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001545 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001546 return true;
1547
1548 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001549 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1550 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001551 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001552 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001553
Daniel Dunbar475839e2009-06-29 20:37:27 +00001554 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555 return true;
1556
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001557 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001558 return TokError("unexpected token in '.space' directive");
1559 }
1560
Sean Callanan79ed1a82010-01-19 20:22:31 +00001561 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001562
1563 if (NumBytes <= 0)
1564 return TokError("invalid number of bytes in '.space' directive");
1565
1566 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001567 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001568
1569 return false;
1570}
1571
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001572/// ParseDirectiveZero
1573/// ::= .zero expression
1574bool AsmParser::ParseDirectiveZero() {
1575 CheckForValidSection();
1576
1577 int64_t NumBytes;
1578 if (ParseAbsoluteExpression(NumBytes))
1579 return true;
1580
Rafael Espindolae452b172010-10-05 19:42:57 +00001581 int64_t Val = 0;
1582 if (getLexer().is(AsmToken::Comma)) {
1583 Lex();
1584 if (ParseAbsoluteExpression(Val))
1585 return true;
1586 }
1587
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001588 if (getLexer().isNot(AsmToken::EndOfStatement))
1589 return TokError("unexpected token in '.zero' directive");
1590
1591 Lex();
1592
Rafael Espindolae452b172010-10-05 19:42:57 +00001593 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001594
1595 return false;
1596}
1597
Daniel Dunbara0d14262009-06-24 23:30:00 +00001598/// ParseDirectiveFill
1599/// ::= .fill expression , expression , expression
1600bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001601 CheckForValidSection();
1602
Daniel Dunbara0d14262009-06-24 23:30:00 +00001603 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001604 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001605 return true;
1606
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001607 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001608 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001609 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001610
Daniel Dunbara0d14262009-06-24 23:30:00 +00001611 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001612 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001613 return true;
1614
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001615 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001616 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001617 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001618
Daniel Dunbara0d14262009-06-24 23:30:00 +00001619 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001620 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001621 return true;
1622
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001623 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001624 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001625
Sean Callanan79ed1a82010-01-19 20:22:31 +00001626 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001627
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001628 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1629 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001630
1631 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001632 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001633
1634 return false;
1635}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001636
1637/// ParseDirectiveOrg
1638/// ::= .org expression [ , expression ]
1639bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001640 CheckForValidSection();
1641
Daniel Dunbar821e3332009-08-31 08:09:28 +00001642 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001643 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001644 return true;
1645
1646 // Parse optional fill expression.
1647 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001648 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1649 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001650 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001651 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001652
Daniel Dunbar475839e2009-06-29 20:37:27 +00001653 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001654 return true;
1655
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001656 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001657 return TokError("unexpected token in '.org' directive");
1658 }
1659
Sean Callanan79ed1a82010-01-19 20:22:31 +00001660 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001661
1662 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1663 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001664 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001665
1666 return false;
1667}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001668
1669/// ParseDirectiveAlign
1670/// ::= {.align, ...} expression [ , expression [ , expression ]]
1671bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001672 CheckForValidSection();
1673
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001674 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001675 int64_t Alignment;
1676 if (ParseAbsoluteExpression(Alignment))
1677 return true;
1678
1679 SMLoc MaxBytesLoc;
1680 bool HasFillExpr = false;
1681 int64_t FillExpr = 0;
1682 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1684 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001685 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001686 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001687
1688 // The fill expression can be omitted while specifying a maximum number of
1689 // alignment bytes, e.g:
1690 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001691 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001692 HasFillExpr = true;
1693 if (ParseAbsoluteExpression(FillExpr))
1694 return true;
1695 }
1696
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001697 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1698 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001699 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001701
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001702 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001703 if (ParseAbsoluteExpression(MaxBytesToFill))
1704 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001705
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001707 return TokError("unexpected token in directive");
1708 }
1709 }
1710
Sean Callanan79ed1a82010-01-19 20:22:31 +00001711 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001712
Daniel Dunbar648ac512010-05-17 21:54:30 +00001713 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001714 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001715
1716 // Compute alignment in bytes.
1717 if (IsPow2) {
1718 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001719 if (Alignment >= 32) {
1720 Error(AlignmentLoc, "invalid alignment value");
1721 Alignment = 31;
1722 }
1723
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001724 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001725 }
1726
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001727 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001728 if (MaxBytesLoc.isValid()) {
1729 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001730 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1731 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001732 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001733 }
1734
1735 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001736 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1737 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001738 MaxBytesToFill = 0;
1739 }
1740 }
1741
Daniel Dunbar648ac512010-05-17 21:54:30 +00001742 // Check whether we should use optimal code alignment for this .align
1743 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001744 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001745 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1746 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001747 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001748 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001749 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001750 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1751 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001752 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001753
1754 return false;
1755}
1756
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001757/// ParseDirectiveSymbolAttribute
1758/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001759bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001760 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001761 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001762 StringRef Name;
1763
1764 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001765 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001766
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001767 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001768
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001769 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001770
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001772 break;
1773
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001774 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001775 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001776 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001777 }
1778 }
1779
Sean Callanan79ed1a82010-01-19 20:22:31 +00001780 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001781 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001782}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001783
1784/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001785/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1786bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001787 CheckForValidSection();
1788
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001789 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001790 StringRef Name;
1791 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001792 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001793
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001794 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001795 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001796
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001797 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001798 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001799 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001800
1801 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001802 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001803 if (ParseAbsoluteExpression(Size))
1804 return true;
1805
1806 int64_t Pow2Alignment = 0;
1807 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001808 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001809 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001810 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001811 if (ParseAbsoluteExpression(Pow2Alignment))
1812 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001813
Chris Lattner258281d2010-01-19 06:22:22 +00001814 // If this target takes alignments in bytes (not log) validate and convert.
1815 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1816 if (!isPowerOf2_64(Pow2Alignment))
1817 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1818 Pow2Alignment = Log2_64(Pow2Alignment);
1819 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001820 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001821
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001822 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001823 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001824
Sean Callanan79ed1a82010-01-19 20:22:31 +00001825 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001826
Chris Lattner1fc3d752009-07-09 17:25:12 +00001827 // NOTE: a size of zero for a .comm should create a undefined symbol
1828 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001829 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001830 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1831 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001832
Eric Christopherc260a3e2010-05-14 01:38:54 +00001833 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001834 // may internally end up wanting an alignment in bytes.
1835 // FIXME: Diagnose overflow.
1836 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001837 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1838 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001839
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001840 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001841 return Error(IDLoc, "invalid symbol redefinition");
1842
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001843 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001844 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001845 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001846 getStreamer().EmitZerofill(Ctx.getMachOSection(
1847 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1848 0, SectionKind::getBSS()),
1849 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001850 return false;
1851 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001852
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001853 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001854 return false;
1855}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001856
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001857/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001858/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001859bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001860 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001861 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001862
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001863 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001864 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001865 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001866
Sean Callanan79ed1a82010-01-19 20:22:31 +00001867 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001868
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001869 if (Str.empty())
1870 Error(Loc, ".abort detected. Assembly stopping.");
1871 else
1872 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001873 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001874
1875 return false;
1876}
Kevin Enderby71148242009-07-14 21:35:03 +00001877
Kevin Enderby1f049b22009-07-14 23:21:55 +00001878/// ParseDirectiveInclude
1879/// ::= .include "filename"
1880bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001881 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001882 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001883
Sean Callanan18b83232010-01-19 21:44:56 +00001884 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001885 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001886 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001887
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001888 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001889 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001890
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001891 // Strip the quotes.
1892 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001893
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001894 // Attempt to switch the lexer to the included file before consuming the end
1895 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001896 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001897 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001898 return true;
1899 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001900
1901 return false;
1902}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001903
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001904/// ParseDirectiveIf
1905/// ::= .if expression
1906bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001907 TheCondStack.push_back(TheCondState);
1908 TheCondState.TheCond = AsmCond::IfCond;
1909 if(TheCondState.Ignore) {
1910 EatToEndOfStatement();
1911 }
1912 else {
1913 int64_t ExprValue;
1914 if (ParseAbsoluteExpression(ExprValue))
1915 return true;
1916
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001917 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001918 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001919
Sean Callanan79ed1a82010-01-19 20:22:31 +00001920 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001921
1922 TheCondState.CondMet = ExprValue;
1923 TheCondState.Ignore = !TheCondState.CondMet;
1924 }
1925
1926 return false;
1927}
1928
1929/// ParseDirectiveElseIf
1930/// ::= .elseif expression
1931bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1932 if (TheCondState.TheCond != AsmCond::IfCond &&
1933 TheCondState.TheCond != AsmCond::ElseIfCond)
1934 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1935 " an .elseif");
1936 TheCondState.TheCond = AsmCond::ElseIfCond;
1937
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001938 bool LastIgnoreState = false;
1939 if (!TheCondStack.empty())
1940 LastIgnoreState = TheCondStack.back().Ignore;
1941 if (LastIgnoreState || TheCondState.CondMet) {
1942 TheCondState.Ignore = true;
1943 EatToEndOfStatement();
1944 }
1945 else {
1946 int64_t ExprValue;
1947 if (ParseAbsoluteExpression(ExprValue))
1948 return true;
1949
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001950 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001951 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001952
Sean Callanan79ed1a82010-01-19 20:22:31 +00001953 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001954 TheCondState.CondMet = ExprValue;
1955 TheCondState.Ignore = !TheCondState.CondMet;
1956 }
1957
1958 return false;
1959}
1960
1961/// ParseDirectiveElse
1962/// ::= .else
1963bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001964 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001965 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001966
Sean Callanan79ed1a82010-01-19 20:22:31 +00001967 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001968
1969 if (TheCondState.TheCond != AsmCond::IfCond &&
1970 TheCondState.TheCond != AsmCond::ElseIfCond)
1971 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1972 ".elseif");
1973 TheCondState.TheCond = AsmCond::ElseCond;
1974 bool LastIgnoreState = false;
1975 if (!TheCondStack.empty())
1976 LastIgnoreState = TheCondStack.back().Ignore;
1977 if (LastIgnoreState || TheCondState.CondMet)
1978 TheCondState.Ignore = true;
1979 else
1980 TheCondState.Ignore = false;
1981
1982 return false;
1983}
1984
1985/// ParseDirectiveEndIf
1986/// ::= .endif
1987bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001988 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001989 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001990
Sean Callanan79ed1a82010-01-19 20:22:31 +00001991 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001992
1993 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1994 TheCondStack.empty())
1995 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1996 ".else");
1997 if (!TheCondStack.empty()) {
1998 TheCondState = TheCondStack.back();
1999 TheCondStack.pop_back();
2000 }
2001
2002 return false;
2003}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002004
2005/// ParseDirectiveFile
2006/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002007bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002008 // FIXME: I'm not sure what this is.
2009 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002010 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002011 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002012 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002013 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002014
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002015 if (FileNumber < 1)
2016 return TokError("file number less than one");
2017 }
2018
Daniel Dunbareceec052010-07-12 17:45:27 +00002019 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002020 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002021
Chris Lattnerd32e8032010-01-25 19:02:58 +00002022 StringRef Filename = getTok().getString();
2023 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002024 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002025
Daniel Dunbareceec052010-07-12 17:45:27 +00002026 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002027 return TokError("unexpected token in '.file' directive");
2028
Chris Lattnerd32e8032010-01-25 19:02:58 +00002029 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002030 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002031 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002032 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002033 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002034 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002035
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002036 return false;
2037}
2038
2039/// ParseDirectiveLine
2040/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002041bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002042 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2043 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002044 return TokError("unexpected token in '.line' directive");
2045
Sean Callanan18b83232010-01-19 21:44:56 +00002046 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002047 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002048 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002049
2050 // FIXME: Do something with the .line.
2051 }
2052
Daniel Dunbareceec052010-07-12 17:45:27 +00002053 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002054 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002055
2056 return false;
2057}
2058
2059
2060/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002061/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002062/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2063/// The first number is a file number, must have been previously assigned with
2064/// a .file directive, the second number is the line number and optionally the
2065/// third number is a column position (zero if not specified). The remaining
2066/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002067bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002068
Daniel Dunbareceec052010-07-12 17:45:27 +00002069 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002070 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002071 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002072 if (FileNumber < 1)
2073 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002074 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002075 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002076 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002077
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002078 int64_t LineNumber = 0;
2079 if (getLexer().is(AsmToken::Integer)) {
2080 LineNumber = getTok().getIntVal();
2081 if (LineNumber < 1)
2082 return TokError("line number less than one in '.loc' directive");
2083 Lex();
2084 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002085
2086 int64_t ColumnPos = 0;
2087 if (getLexer().is(AsmToken::Integer)) {
2088 ColumnPos = getTok().getIntVal();
2089 if (ColumnPos < 0)
2090 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002091 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002092 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002093
Kevin Enderbyc0957932010-09-30 16:52:03 +00002094 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002095 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002096 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002097 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2098 for (;;) {
2099 if (getLexer().is(AsmToken::EndOfStatement))
2100 break;
2101
2102 StringRef Name;
2103 SMLoc Loc = getTok().getLoc();
2104 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002105 return TokError("unexpected token in '.loc' directive");
2106
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002107 if (Name == "basic_block")
2108 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2109 else if (Name == "prologue_end")
2110 Flags |= DWARF2_FLAG_PROLOGUE_END;
2111 else if (Name == "epilogue_begin")
2112 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2113 else if (Name == "is_stmt") {
2114 SMLoc Loc = getTok().getLoc();
2115 const MCExpr *Value;
2116 if (getParser().ParseExpression(Value))
2117 return true;
2118 // The expression must be the constant 0 or 1.
2119 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2120 int Value = MCE->getValue();
2121 if (Value == 0)
2122 Flags &= ~DWARF2_FLAG_IS_STMT;
2123 else if (Value == 1)
2124 Flags |= DWARF2_FLAG_IS_STMT;
2125 else
2126 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002127 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002128 else {
2129 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2130 }
2131 }
2132 else if (Name == "isa") {
2133 SMLoc Loc = getTok().getLoc();
2134 const MCExpr *Value;
2135 if (getParser().ParseExpression(Value))
2136 return true;
2137 // The expression must be a constant greater or equal to 0.
2138 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2139 int Value = MCE->getValue();
2140 if (Value < 0)
2141 return Error(Loc, "isa number less than zero");
2142 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002143 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002144 else {
2145 return Error(Loc, "isa number not a constant value");
2146 }
2147 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002148 else if (Name == "discriminator") {
2149 if (getParser().ParseAbsoluteExpression(Discriminator))
2150 return true;
2151 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002152 else {
2153 return Error(Loc, "unknown sub-directive in '.loc' directive");
2154 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002155
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002156 if (getLexer().is(AsmToken::EndOfStatement))
2157 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002158 }
2159 }
2160
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002161 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2162 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002163
2164 return false;
2165}
2166
Daniel Dunbar138abae2010-10-16 04:56:42 +00002167/// ParseDirectiveStabs
2168/// ::= .stabs string, number, number, number
2169bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2170 SMLoc DirectiveLoc) {
2171 return TokError("unsupported directive '" + Directive + "'");
2172}
2173
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002174/// ParseDirectiveCFIStartProc
2175/// ::= .cfi_startproc
2176bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2177 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002178 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002179}
2180
2181/// ParseDirectiveCFIEndProc
2182/// ::= .cfi_endproc
2183bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002184 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002185}
2186
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002187/// ParseRegisterOrRegisterNumber - parse register name or number.
2188bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2189 SMLoc DirectiveLoc) {
2190 unsigned RegNo;
2191
2192 if (getLexer().is(AsmToken::Percent)) {
2193 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2194 DirectiveLoc))
2195 return true;
2196 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2197 } else
2198 return getParser().ParseAbsoluteExpression(Register);
2199
2200 return false;
2201}
2202
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002203/// ParseDirectiveCFIDefCfa
2204/// ::= .cfi_def_cfa register, offset
2205bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2206 SMLoc DirectiveLoc) {
2207 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002208 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002209 return true;
2210
2211 if (getLexer().isNot(AsmToken::Comma))
2212 return TokError("unexpected token in directive");
2213 Lex();
2214
2215 int64_t Offset = 0;
2216 if (getParser().ParseAbsoluteExpression(Offset))
2217 return true;
2218
2219 return getStreamer().EmitCFIDefCfa(Register, Offset);
2220}
2221
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002222/// ParseDirectiveCFIDefCfaOffset
2223/// ::= .cfi_def_cfa_offset offset
2224bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2225 SMLoc DirectiveLoc) {
2226 int64_t Offset = 0;
2227 if (getParser().ParseAbsoluteExpression(Offset))
2228 return true;
2229
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002230 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002231}
2232
2233/// ParseDirectiveCFIDefCfaRegister
2234/// ::= .cfi_def_cfa_register register
2235bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2236 SMLoc DirectiveLoc) {
2237 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002238 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002239 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002240
2241 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002242}
2243
2244/// ParseDirectiveCFIOffset
2245/// ::= .cfi_off register, offset
2246bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2247 int64_t Register = 0;
2248 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002249
2250 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002251 return true;
2252
2253 if (getLexer().isNot(AsmToken::Comma))
2254 return TokError("unexpected token in directive");
2255 Lex();
2256
2257 if (getParser().ParseAbsoluteExpression(Offset))
2258 return true;
2259
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002260 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002261}
2262
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002263static bool isValidEncoding(int64_t Encoding) {
2264 if (Encoding & ~0xff)
2265 return false;
2266
2267 if (Encoding == dwarf::DW_EH_PE_omit)
2268 return true;
2269
2270 const unsigned Format = Encoding & 0xf;
2271 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2272 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2273 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2274 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2275 return false;
2276
Rafael Espindolacaf11582010-12-29 04:31:26 +00002277 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002278 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002279 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002280 return false;
2281
2282 return true;
2283}
2284
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002285/// ParseDirectiveCFIPersonalityOrLsda
2286/// ::= .cfi_personality encoding, [symbol_name]
2287/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002288bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002289 SMLoc DirectiveLoc) {
2290 int64_t Encoding = 0;
2291 if (getParser().ParseAbsoluteExpression(Encoding))
2292 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002293 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002294 return false;
2295
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002296 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002297 return TokError("unsupported encoding.");
2298
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002299 if (getLexer().isNot(AsmToken::Comma))
2300 return TokError("unexpected token in directive");
2301 Lex();
2302
2303 StringRef Name;
2304 if (getParser().ParseIdentifier(Name))
2305 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002306
2307 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2308
2309 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002310 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002311 else {
2312 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002313 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002314 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002315}
2316
Rafael Espindolafe024d02010-12-28 18:36:23 +00002317/// ParseDirectiveCFIRememberState
2318/// ::= .cfi_remember_state
2319bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2320 SMLoc DirectiveLoc) {
2321 return getStreamer().EmitCFIRememberState();
2322}
2323
2324/// ParseDirectiveCFIRestoreState
2325/// ::= .cfi_remember_state
2326bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2327 SMLoc DirectiveLoc) {
2328 return getStreamer().EmitCFIRestoreState();
2329}
2330
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002331/// ParseDirectiveMacrosOnOff
2332/// ::= .macros_on
2333/// ::= .macros_off
2334bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2335 SMLoc DirectiveLoc) {
2336 if (getLexer().isNot(AsmToken::EndOfStatement))
2337 return Error(getLexer().getLoc(),
2338 "unexpected token in '" + Directive + "' directive");
2339
2340 getParser().MacrosEnabled = Directive == ".macros_on";
2341
2342 return false;
2343}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002344
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002345/// ParseDirectiveMacro
2346/// ::= .macro name
2347bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2348 SMLoc DirectiveLoc) {
2349 StringRef Name;
2350 if (getParser().ParseIdentifier(Name))
2351 return TokError("expected identifier in directive");
2352
2353 if (getLexer().isNot(AsmToken::EndOfStatement))
2354 return TokError("unexpected token in '.macro' directive");
2355
2356 // Eat the end of statement.
2357 Lex();
2358
2359 AsmToken EndToken, StartToken = getTok();
2360
2361 // Lex the macro definition.
2362 for (;;) {
2363 // Check whether we have reached the end of the file.
2364 if (getLexer().is(AsmToken::Eof))
2365 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2366
2367 // Otherwise, check whether we have reach the .endmacro.
2368 if (getLexer().is(AsmToken::Identifier) &&
2369 (getTok().getIdentifier() == ".endm" ||
2370 getTok().getIdentifier() == ".endmacro")) {
2371 EndToken = getTok();
2372 Lex();
2373 if (getLexer().isNot(AsmToken::EndOfStatement))
2374 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2375 "' directive");
2376 break;
2377 }
2378
2379 // Otherwise, scan til the end of the statement.
2380 getParser().EatToEndOfStatement();
2381 }
2382
2383 if (getParser().MacroMap.lookup(Name)) {
2384 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2385 }
2386
2387 const char *BodyStart = StartToken.getLoc().getPointer();
2388 const char *BodyEnd = EndToken.getLoc().getPointer();
2389 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2390 getParser().MacroMap[Name] = new Macro(Name, Body);
2391 return false;
2392}
2393
2394/// ParseDirectiveEndMacro
2395/// ::= .endm
2396/// ::= .endmacro
2397bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2398 SMLoc DirectiveLoc) {
2399 if (getLexer().isNot(AsmToken::EndOfStatement))
2400 return TokError("unexpected token in '" + Directive + "' directive");
2401
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002402 // If we are inside a macro instantiation, terminate the current
2403 // instantiation.
2404 if (!getParser().ActiveMacros.empty()) {
2405 getParser().HandleMacroExit();
2406 return false;
2407 }
2408
2409 // Otherwise, this .endmacro is a stray entry in the file; well formed
2410 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002411 return TokError("unexpected '" + Directive + "' in file, "
2412 "no current macro definition");
2413}
2414
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002415bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002416 getParser().CheckForValidSection();
2417
2418 const MCExpr *Value;
2419
2420 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002421 return true;
2422
2423 if (getLexer().isNot(AsmToken::EndOfStatement))
2424 return TokError("unexpected token in directive");
2425
2426 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002427 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002428 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002429 getStreamer().EmitULEB128Value(Value);
2430
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002431 return false;
2432}
2433
2434
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002435/// \brief Create an MCAsmParser instance.
2436MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2437 MCContext &C, MCStreamer &Out,
2438 const MCAsmInfo &MAI) {
2439 return new AsmParser(T, SM, C, Out, MAI);
2440}