blob: 891930d9e7c40bb9c51f9a8ec4b1047afb0ea70f [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
Nico Weber4c4c7322011-01-28 03:04:41 +0000171 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000172
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"
Nico Weber4c4c7322011-01-28 03:04:41 +0000190 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
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
Nico Weber4c4c7322011-01-28 03:04:41 +0000944 return ParseAssignment(IDVal, true);
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")
Nico Weber4c4c7322011-01-28 03:04:41 +0000959 return ParseDirectiveSet(IDVal, true);
960 if (IDVal == ".equiv")
961 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000962
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963 // Data directives
964
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000965 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000966 return ParseDirectiveAscii(IDVal, false);
967 if (IDVal == ".asciz" || IDVal == ".string")
968 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000970 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000971 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000972 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000973 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000974 if (IDVal == ".value")
975 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000976 if (IDVal == ".2byte")
977 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000978 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000979 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000980 if (IDVal == ".int")
981 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000982 if (IDVal == ".4byte")
983 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000984 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000985 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000986 if (IDVal == ".8byte")
987 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +0000988 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000989 return ParseDirectiveRealValue(APFloat::IEEEsingle);
990 if (IDVal == ".double")
991 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000992
Eli Friedman5d68ec22010-07-19 04:17:25 +0000993 if (IDVal == ".align") {
994 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
995 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
996 }
997 if (IDVal == ".align32") {
998 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
999 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1000 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001001 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001002 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001003 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001004 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001005 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001006 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001007 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001008 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001009 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001010 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001011 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001012 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1013
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001014 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001015 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001016
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001017 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001018 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001019 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001021 if (IDVal == ".zero")
1022 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001024 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001025
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001026 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001027 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001028 // ELF only? Should it be here?
1029 if (IDVal == ".local")
1030 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001032 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001033 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001034 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001035 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001036 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001037 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001038 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001039 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001040 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001041 if (IDVal == ".symbol_resolver")
1042 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001043 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001044 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001045 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001046 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001047 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001048 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001049 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001050 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001051 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001052 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001053 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001054 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001055 if (IDVal == ".weak_def_can_be_hidden")
1056 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001057
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001058 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001059 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001060 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001061 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001062
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001063 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001064 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001065 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001066 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001067
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001068 // Look up the handler in the handler table.
1069 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1070 DirectiveMap.lookup(IDVal);
1071 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001072 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001073
Kevin Enderby9c656452009-09-10 20:51:44 +00001074 // Target hook for parsing target specific directives.
1075 if (!getTargetParser().ParseDirective(ID))
1076 return false;
1077
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001078 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001079 EatToEndOfStatement();
1080 return false;
1081 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001082
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001083 CheckForValidSection();
1084
Chris Lattnera7f13542010-05-19 23:34:33 +00001085 // Canonicalize the opcode to lower case.
1086 SmallString<128> Opcode;
1087 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1088 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001089
Chris Lattner98986712010-01-14 22:21:20 +00001090 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001091 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001092 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001093
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001094 // Dump the parsed representation, if requested.
1095 if (getShowParsedOperands()) {
1096 SmallString<256> Str;
1097 raw_svector_ostream OS(Str);
1098 OS << "parsed instruction: [";
1099 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1100 if (i != 0)
1101 OS << ", ";
1102 ParsedOperands[i]->dump(OS);
1103 }
1104 OS << "]";
1105
1106 PrintMessage(IDLoc, OS.str(), "note");
1107 }
1108
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001109 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001110 if (!HadError)
1111 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1112 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001113
Chris Lattner98986712010-01-14 22:21:20 +00001114 // Free any parsed operands.
1115 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1116 delete ParsedOperands[i];
1117
Chris Lattnercbf8a982010-09-11 16:18:25 +00001118 // Don't skip the rest of the line, the instruction parser is responsible for
1119 // that.
1120 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001121}
Chris Lattner9a023f72009-06-24 04:43:34 +00001122
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001123MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1124 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001125 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1126{
1127 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1128 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001129 SmallString<256> Buf;
1130 raw_svector_ostream OS(Buf);
1131
1132 StringRef Body = M->Body;
1133 while (!Body.empty()) {
1134 // Scan for the next substitution.
1135 std::size_t End = Body.size(), Pos = 0;
1136 for (; Pos != End; ++Pos) {
1137 // Check for a substitution or escape.
1138 if (Body[Pos] != '$' || Pos + 1 == End)
1139 continue;
1140
1141 char Next = Body[Pos + 1];
1142 if (Next == '$' || Next == 'n' || isdigit(Next))
1143 break;
1144 }
1145
1146 // Add the prefix.
1147 OS << Body.slice(0, Pos);
1148
1149 // Check if we reached the end.
1150 if (Pos == End)
1151 break;
1152
1153 switch (Body[Pos+1]) {
1154 // $$ => $
1155 case '$':
1156 OS << '$';
1157 break;
1158
1159 // $n => number of arguments
1160 case 'n':
1161 OS << A.size();
1162 break;
1163
1164 // $[0-9] => argument
1165 default: {
1166 // Missing arguments are ignored.
1167 unsigned Index = Body[Pos+1] - '0';
1168 if (Index >= A.size())
1169 break;
1170
1171 // Otherwise substitute with the token values, with spaces eliminated.
1172 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1173 ie = A[Index].end(); it != ie; ++it)
1174 OS << it->getString();
1175 break;
1176 }
1177 }
1178
1179 // Update the scan point.
1180 Body = Body.substr(Pos + 2);
1181 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001182
1183 // We include the .endmacro in the buffer as our queue to exit the macro
1184 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001185 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001186
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001187 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001188}
1189
1190bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1191 const Macro *M) {
1192 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1193 // this, although we should protect against infinite loops.
1194 if (ActiveMacros.size() == 20)
1195 return TokError("macros cannot be nested more than 20 levels deep");
1196
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001197 // Parse the macro instantiation arguments.
1198 std::vector<std::vector<AsmToken> > MacroArguments;
1199 MacroArguments.push_back(std::vector<AsmToken>());
1200 unsigned ParenLevel = 0;
1201 for (;;) {
1202 if (Lexer.is(AsmToken::Eof))
1203 return TokError("unexpected token in macro instantiation");
1204 if (Lexer.is(AsmToken::EndOfStatement))
1205 break;
1206
1207 // If we aren't inside parentheses and this is a comma, start a new token
1208 // list.
1209 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1210 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001211 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001212 // Adjust the current parentheses level.
1213 if (Lexer.is(AsmToken::LParen))
1214 ++ParenLevel;
1215 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1216 --ParenLevel;
1217
1218 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001219 MacroArguments.back().push_back(getTok());
1220 }
1221 Lex();
1222 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001223
1224 // Create the macro instantiation object and add to the current macro
1225 // instantiation stack.
1226 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001227 getTok().getLoc(),
1228 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001229 ActiveMacros.push_back(MI);
1230
1231 // Jump to the macro instantiation and prime the lexer.
1232 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1233 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1234 Lex();
1235
1236 return false;
1237}
1238
1239void AsmParser::HandleMacroExit() {
1240 // Jump to the EndOfStatement we should return to, and consume it.
1241 JumpToLoc(ActiveMacros.back()->ExitLoc);
1242 Lex();
1243
1244 // Pop the instantiation entry.
1245 delete ActiveMacros.back();
1246 ActiveMacros.pop_back();
1247}
1248
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001249static void MarkUsed(const MCExpr *Value) {
1250 switch (Value->getKind()) {
1251 case MCExpr::Binary:
1252 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1253 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1254 break;
1255 case MCExpr::Target:
1256 case MCExpr::Constant:
1257 break;
1258 case MCExpr::SymbolRef: {
1259 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1260 break;
1261 }
1262 case MCExpr::Unary:
1263 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1264 break;
1265 }
1266}
1267
Nico Weber4c4c7322011-01-28 03:04:41 +00001268bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001269 // FIXME: Use better location, we should use proper tokens.
1270 SMLoc EqualLoc = Lexer.getLoc();
1271
Daniel Dunbar821e3332009-08-31 08:09:28 +00001272 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001273 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001274 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001275
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001276 MarkUsed(Value);
1277
Daniel Dunbar3f872332009-07-28 16:08:33 +00001278 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001279 return TokError("unexpected token in assignment");
1280
1281 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001282 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001283
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001284 // Validate that the LHS is allowed to be a variable (either it has not been
1285 // used as a symbol, or it is an absolute symbol).
1286 MCSymbol *Sym = getContext().LookupSymbol(Name);
1287 if (Sym) {
1288 // Diagnose assignment to a label.
1289 //
1290 // FIXME: Diagnostics. Note the location of the definition as a label.
1291 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001292 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001293 ; // Allow redefinitions of undefined symbols only used in directives.
Nico Weber4c4c7322011-01-28 03:04:41 +00001294 else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001295 return Error(EqualLoc, "redefinition of '" + Name + "'");
1296 else if (!Sym->isVariable())
1297 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001298 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001299 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1300 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001301
1302 // Don't count these checks as uses.
1303 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001304 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001305 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001306
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001307 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001308
1309 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001310 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001311
1312 return false;
1313}
1314
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001315/// ParseIdentifier:
1316/// ::= identifier
1317/// ::= string
1318bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001319 // The assembler has relaxed rules for accepting identifiers, in particular we
1320 // allow things like '.globl $foo', which would normally be separate
1321 // tokens. At this level, we have already lexed so we cannot (currently)
1322 // handle this as a context dependent token, instead we detect adjacent tokens
1323 // and return the combined identifier.
1324 if (Lexer.is(AsmToken::Dollar)) {
1325 SMLoc DollarLoc = getLexer().getLoc();
1326
1327 // Consume the dollar sign, and check for a following identifier.
1328 Lex();
1329 if (Lexer.isNot(AsmToken::Identifier))
1330 return true;
1331
1332 // We have a '$' followed by an identifier, make sure they are adjacent.
1333 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1334 return true;
1335
1336 // Construct the joined identifier and consume the token.
1337 Res = StringRef(DollarLoc.getPointer(),
1338 getTok().getIdentifier().size() + 1);
1339 Lex();
1340 return false;
1341 }
1342
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001343 if (Lexer.isNot(AsmToken::Identifier) &&
1344 Lexer.isNot(AsmToken::String))
1345 return true;
1346
Sean Callanan18b83232010-01-19 21:44:56 +00001347 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001348
Sean Callanan79ed1a82010-01-19 20:22:31 +00001349 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001350
1351 return false;
1352}
1353
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001354/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001355/// ::= .equ identifier ',' expression
1356/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001357/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001358bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001359 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001360
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001361 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001362 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001363
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001364 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001365 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001366 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001367
Nico Weber4c4c7322011-01-28 03:04:41 +00001368 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001369}
1370
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001371bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001373
1374 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001375 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001376 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1377 if (Str[i] != '\\') {
1378 Data += Str[i];
1379 continue;
1380 }
1381
1382 // Recognize escaped characters. Note that this escape semantics currently
1383 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1384 ++i;
1385 if (i == e)
1386 return TokError("unexpected backslash at end of string");
1387
1388 // Recognize octal sequences.
1389 if ((unsigned) (Str[i] - '0') <= 7) {
1390 // Consume up to three octal characters.
1391 unsigned Value = 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 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1398 ++i;
1399 Value = Value * 8 + (Str[i] - '0');
1400 }
1401 }
1402
1403 if (Value > 255)
1404 return TokError("invalid octal escape sequence (out of range)");
1405
1406 Data += (unsigned char) Value;
1407 continue;
1408 }
1409
1410 // Otherwise recognize individual escapes.
1411 switch (Str[i]) {
1412 default:
1413 // Just reject invalid escape sequences for now.
1414 return TokError("invalid escape sequence (unrecognized character)");
1415
1416 case 'b': Data += '\b'; break;
1417 case 'f': Data += '\f'; break;
1418 case 'n': Data += '\n'; break;
1419 case 'r': Data += '\r'; break;
1420 case 't': Data += '\t'; break;
1421 case '"': Data += '"'; break;
1422 case '\\': Data += '\\'; break;
1423 }
1424 }
1425
1426 return false;
1427}
1428
Daniel Dunbara0d14262009-06-24 23:30:00 +00001429/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001430/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1431bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001432 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001433 CheckForValidSection();
1434
Daniel Dunbara0d14262009-06-24 23:30:00 +00001435 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001436 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001437 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001438
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001439 std::string Data;
1440 if (ParseEscapedString(Data))
1441 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001442
1443 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001444 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001445 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1446
Sean Callanan79ed1a82010-01-19 20:22:31 +00001447 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001448
1449 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001450 break;
1451
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001452 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001453 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001454 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001455 }
1456 }
1457
Sean Callanan79ed1a82010-01-19 20:22:31 +00001458 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001459 return false;
1460}
1461
1462/// ParseDirectiveValue
1463/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1464bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001465 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001466 CheckForValidSection();
1467
Daniel Dunbara0d14262009-06-24 23:30:00 +00001468 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001469 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001470 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001471 return true;
1472
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001473 // Special case constant expressions to match code generator.
1474 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001475 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001476 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001478
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001479 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001480 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001481
Daniel Dunbara0d14262009-06-24 23:30:00 +00001482 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001483 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001484 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001485 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001486 }
1487 }
1488
Sean Callanan79ed1a82010-01-19 20:22:31 +00001489 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001490 return false;
1491}
1492
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001493/// ParseDirectiveRealValue
1494/// ::= (.single | .double) [ expression (, expression)* ]
1495bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1496 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1497 CheckForValidSection();
1498
1499 for (;;) {
1500 // We don't truly support arithmetic on floating point expressions, so we
1501 // have to manually parse unary prefixes.
1502 bool IsNeg = false;
1503 if (getLexer().is(AsmToken::Minus)) {
1504 Lex();
1505 IsNeg = true;
1506 } else if (getLexer().is(AsmToken::Plus))
1507 Lex();
1508
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001509 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001510 getLexer().isNot(AsmToken::Real))
1511 return TokError("unexpected token in directive");
1512
1513 // Convert to an APFloat.
1514 APFloat Value(Semantics);
1515 if (Value.convertFromString(getTok().getString(),
1516 APFloat::rmNearestTiesToEven) ==
1517 APFloat::opInvalidOp)
1518 return TokError("invalid floating point literal");
1519 if (IsNeg)
1520 Value.changeSign();
1521
1522 // Consume the numeric token.
1523 Lex();
1524
1525 // Emit the value as an integer.
1526 APInt AsInt = Value.bitcastToAPInt();
1527 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1528 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1529
1530 if (getLexer().is(AsmToken::EndOfStatement))
1531 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001532
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001533 if (getLexer().isNot(AsmToken::Comma))
1534 return TokError("unexpected token in directive");
1535 Lex();
1536 }
1537 }
1538
1539 Lex();
1540 return false;
1541}
1542
Daniel Dunbara0d14262009-06-24 23:30:00 +00001543/// ParseDirectiveSpace
1544/// ::= .space expression [ , expression ]
1545bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001546 CheckForValidSection();
1547
Daniel Dunbara0d14262009-06-24 23:30:00 +00001548 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001549 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001550 return true;
1551
1552 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001553 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1554 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001556 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001557
Daniel Dunbar475839e2009-06-29 20:37:27 +00001558 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001559 return true;
1560
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001562 return TokError("unexpected token in '.space' directive");
1563 }
1564
Sean Callanan79ed1a82010-01-19 20:22:31 +00001565 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001566
1567 if (NumBytes <= 0)
1568 return TokError("invalid number of bytes in '.space' directive");
1569
1570 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001571 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001572
1573 return false;
1574}
1575
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001576/// ParseDirectiveZero
1577/// ::= .zero expression
1578bool AsmParser::ParseDirectiveZero() {
1579 CheckForValidSection();
1580
1581 int64_t NumBytes;
1582 if (ParseAbsoluteExpression(NumBytes))
1583 return true;
1584
Rafael Espindolae452b172010-10-05 19:42:57 +00001585 int64_t Val = 0;
1586 if (getLexer().is(AsmToken::Comma)) {
1587 Lex();
1588 if (ParseAbsoluteExpression(Val))
1589 return true;
1590 }
1591
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001592 if (getLexer().isNot(AsmToken::EndOfStatement))
1593 return TokError("unexpected token in '.zero' directive");
1594
1595 Lex();
1596
Rafael Espindolae452b172010-10-05 19:42:57 +00001597 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001598
1599 return false;
1600}
1601
Daniel Dunbara0d14262009-06-24 23:30:00 +00001602/// ParseDirectiveFill
1603/// ::= .fill expression , expression , expression
1604bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001605 CheckForValidSection();
1606
Daniel Dunbara0d14262009-06-24 23:30:00 +00001607 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001608 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001609 return true;
1610
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001611 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001612 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001613 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001614
Daniel Dunbara0d14262009-06-24 23:30:00 +00001615 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001616 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001617 return true;
1618
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001619 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001620 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001621 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001622
Daniel Dunbara0d14262009-06-24 23:30:00 +00001623 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001624 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001625 return true;
1626
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001627 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001628 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001629
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001631
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001632 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1633 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001634
1635 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001637
1638 return false;
1639}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001640
1641/// ParseDirectiveOrg
1642/// ::= .org expression [ , expression ]
1643bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001644 CheckForValidSection();
1645
Daniel Dunbar821e3332009-08-31 08:09:28 +00001646 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001647 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001648 return true;
1649
1650 // Parse optional fill expression.
1651 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001652 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1653 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001654 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001655 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001656
Daniel Dunbar475839e2009-06-29 20:37:27 +00001657 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001658 return true;
1659
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001660 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001661 return TokError("unexpected token in '.org' directive");
1662 }
1663
Sean Callanan79ed1a82010-01-19 20:22:31 +00001664 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001665
1666 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1667 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001668 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001669
1670 return false;
1671}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001672
1673/// ParseDirectiveAlign
1674/// ::= {.align, ...} expression [ , expression [ , expression ]]
1675bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001676 CheckForValidSection();
1677
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001678 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001679 int64_t Alignment;
1680 if (ParseAbsoluteExpression(Alignment))
1681 return true;
1682
1683 SMLoc MaxBytesLoc;
1684 bool HasFillExpr = false;
1685 int64_t FillExpr = 0;
1686 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1688 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001689 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001690 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001691
1692 // The fill expression can be omitted while specifying a maximum number of
1693 // alignment bytes, e.g:
1694 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001696 HasFillExpr = true;
1697 if (ParseAbsoluteExpression(FillExpr))
1698 return true;
1699 }
1700
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1702 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001703 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001704 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001705
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001707 if (ParseAbsoluteExpression(MaxBytesToFill))
1708 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001709
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001710 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001711 return TokError("unexpected token in directive");
1712 }
1713 }
1714
Sean Callanan79ed1a82010-01-19 20:22:31 +00001715 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001716
Daniel Dunbar648ac512010-05-17 21:54:30 +00001717 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001718 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001719
1720 // Compute alignment in bytes.
1721 if (IsPow2) {
1722 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001723 if (Alignment >= 32) {
1724 Error(AlignmentLoc, "invalid alignment value");
1725 Alignment = 31;
1726 }
1727
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001728 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001729 }
1730
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001731 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001732 if (MaxBytesLoc.isValid()) {
1733 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001734 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1735 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001736 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001737 }
1738
1739 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001740 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1741 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001742 MaxBytesToFill = 0;
1743 }
1744 }
1745
Daniel Dunbar648ac512010-05-17 21:54:30 +00001746 // Check whether we should use optimal code alignment for this .align
1747 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001748 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001749 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1750 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001752 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001753 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001754 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1755 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001756 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001757
1758 return false;
1759}
1760
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001761/// ParseDirectiveSymbolAttribute
1762/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001763bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001764 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001765 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001766 StringRef Name;
1767
1768 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001769 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001770
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001771 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001772
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001773 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001774
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001775 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001776 break;
1777
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001778 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001779 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001780 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001781 }
1782 }
1783
Sean Callanan79ed1a82010-01-19 20:22:31 +00001784 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001785 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001786}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001787
1788/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001789/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1790bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001791 CheckForValidSection();
1792
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001793 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001794 StringRef Name;
1795 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001796 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001797
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001798 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001799 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001800
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001802 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001803 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001804
1805 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001806 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001807 if (ParseAbsoluteExpression(Size))
1808 return true;
1809
1810 int64_t Pow2Alignment = 0;
1811 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001812 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001813 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001814 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001815 if (ParseAbsoluteExpression(Pow2Alignment))
1816 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001817
Chris Lattner258281d2010-01-19 06:22:22 +00001818 // If this target takes alignments in bytes (not log) validate and convert.
1819 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1820 if (!isPowerOf2_64(Pow2Alignment))
1821 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1822 Pow2Alignment = Log2_64(Pow2Alignment);
1823 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001824 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001825
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001826 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001827 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001828
Sean Callanan79ed1a82010-01-19 20:22:31 +00001829 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001830
Chris Lattner1fc3d752009-07-09 17:25:12 +00001831 // NOTE: a size of zero for a .comm should create a undefined symbol
1832 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001833 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001834 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1835 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001836
Eric Christopherc260a3e2010-05-14 01:38:54 +00001837 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001838 // may internally end up wanting an alignment in bytes.
1839 // FIXME: Diagnose overflow.
1840 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001841 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1842 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001843
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001844 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001845 return Error(IDLoc, "invalid symbol redefinition");
1846
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001847 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001848 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001849 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001850 getStreamer().EmitZerofill(Ctx.getMachOSection(
1851 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1852 0, SectionKind::getBSS()),
1853 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001854 return false;
1855 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001856
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001857 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001858 return false;
1859}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001860
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001861/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001862/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001863bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001864 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001865 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001866
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001867 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001868 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001869 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001870
Sean Callanan79ed1a82010-01-19 20:22:31 +00001871 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001872
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001873 if (Str.empty())
1874 Error(Loc, ".abort detected. Assembly stopping.");
1875 else
1876 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001877 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001878
1879 return false;
1880}
Kevin Enderby71148242009-07-14 21:35:03 +00001881
Kevin Enderby1f049b22009-07-14 23:21:55 +00001882/// ParseDirectiveInclude
1883/// ::= .include "filename"
1884bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001885 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001886 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001887
Sean Callanan18b83232010-01-19 21:44:56 +00001888 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001889 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001890 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001891
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001892 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001893 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001894
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001895 // Strip the quotes.
1896 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001897
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001898 // Attempt to switch the lexer to the included file before consuming the end
1899 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001900 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001901 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001902 return true;
1903 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001904
1905 return false;
1906}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001907
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001908/// ParseDirectiveIf
1909/// ::= .if expression
1910bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001911 TheCondStack.push_back(TheCondState);
1912 TheCondState.TheCond = AsmCond::IfCond;
1913 if(TheCondState.Ignore) {
1914 EatToEndOfStatement();
1915 }
1916 else {
1917 int64_t ExprValue;
1918 if (ParseAbsoluteExpression(ExprValue))
1919 return true;
1920
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001921 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001922 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001923
Sean Callanan79ed1a82010-01-19 20:22:31 +00001924 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001925
1926 TheCondState.CondMet = ExprValue;
1927 TheCondState.Ignore = !TheCondState.CondMet;
1928 }
1929
1930 return false;
1931}
1932
1933/// ParseDirectiveElseIf
1934/// ::= .elseif expression
1935bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1936 if (TheCondState.TheCond != AsmCond::IfCond &&
1937 TheCondState.TheCond != AsmCond::ElseIfCond)
1938 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1939 " an .elseif");
1940 TheCondState.TheCond = AsmCond::ElseIfCond;
1941
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001942 bool LastIgnoreState = false;
1943 if (!TheCondStack.empty())
1944 LastIgnoreState = TheCondStack.back().Ignore;
1945 if (LastIgnoreState || TheCondState.CondMet) {
1946 TheCondState.Ignore = true;
1947 EatToEndOfStatement();
1948 }
1949 else {
1950 int64_t ExprValue;
1951 if (ParseAbsoluteExpression(ExprValue))
1952 return true;
1953
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001954 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001955 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001956
Sean Callanan79ed1a82010-01-19 20:22:31 +00001957 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001958 TheCondState.CondMet = ExprValue;
1959 TheCondState.Ignore = !TheCondState.CondMet;
1960 }
1961
1962 return false;
1963}
1964
1965/// ParseDirectiveElse
1966/// ::= .else
1967bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001968 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001969 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001970
Sean Callanan79ed1a82010-01-19 20:22:31 +00001971 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001972
1973 if (TheCondState.TheCond != AsmCond::IfCond &&
1974 TheCondState.TheCond != AsmCond::ElseIfCond)
1975 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1976 ".elseif");
1977 TheCondState.TheCond = AsmCond::ElseCond;
1978 bool LastIgnoreState = false;
1979 if (!TheCondStack.empty())
1980 LastIgnoreState = TheCondStack.back().Ignore;
1981 if (LastIgnoreState || TheCondState.CondMet)
1982 TheCondState.Ignore = true;
1983 else
1984 TheCondState.Ignore = false;
1985
1986 return false;
1987}
1988
1989/// ParseDirectiveEndIf
1990/// ::= .endif
1991bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001992 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001993 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001994
Sean Callanan79ed1a82010-01-19 20:22:31 +00001995 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001996
1997 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1998 TheCondStack.empty())
1999 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2000 ".else");
2001 if (!TheCondStack.empty()) {
2002 TheCondState = TheCondStack.back();
2003 TheCondStack.pop_back();
2004 }
2005
2006 return false;
2007}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002008
2009/// ParseDirectiveFile
2010/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002011bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002012 // FIXME: I'm not sure what this is.
2013 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002014 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002015 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002016 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002017 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002018
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002019 if (FileNumber < 1)
2020 return TokError("file number less than one");
2021 }
2022
Daniel Dunbareceec052010-07-12 17:45:27 +00002023 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002024 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002025
Chris Lattnerd32e8032010-01-25 19:02:58 +00002026 StringRef Filename = getTok().getString();
2027 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002028 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002029
Daniel Dunbareceec052010-07-12 17:45:27 +00002030 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002031 return TokError("unexpected token in '.file' directive");
2032
Chris Lattnerd32e8032010-01-25 19:02:58 +00002033 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002034 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002035 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002036 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002037 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002038 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002039
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002040 return false;
2041}
2042
2043/// ParseDirectiveLine
2044/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002045bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002046 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2047 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002048 return TokError("unexpected token in '.line' directive");
2049
Sean Callanan18b83232010-01-19 21:44:56 +00002050 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002051 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002052 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002053
2054 // FIXME: Do something with the .line.
2055 }
2056
Daniel Dunbareceec052010-07-12 17:45:27 +00002057 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002058 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002059
2060 return false;
2061}
2062
2063
2064/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002065/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002066/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2067/// The first number is a file number, must have been previously assigned with
2068/// a .file directive, the second number is the line number and optionally the
2069/// third number is a column position (zero if not specified). The remaining
2070/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002071bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002072
Daniel Dunbareceec052010-07-12 17:45:27 +00002073 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002074 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002075 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002076 if (FileNumber < 1)
2077 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002078 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002079 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002080 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002081
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002082 int64_t LineNumber = 0;
2083 if (getLexer().is(AsmToken::Integer)) {
2084 LineNumber = getTok().getIntVal();
2085 if (LineNumber < 1)
2086 return TokError("line number less than one in '.loc' directive");
2087 Lex();
2088 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002089
2090 int64_t ColumnPos = 0;
2091 if (getLexer().is(AsmToken::Integer)) {
2092 ColumnPos = getTok().getIntVal();
2093 if (ColumnPos < 0)
2094 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002095 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002096 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002097
Kevin Enderbyc0957932010-09-30 16:52:03 +00002098 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002099 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002100 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002101 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2102 for (;;) {
2103 if (getLexer().is(AsmToken::EndOfStatement))
2104 break;
2105
2106 StringRef Name;
2107 SMLoc Loc = getTok().getLoc();
2108 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002109 return TokError("unexpected token in '.loc' directive");
2110
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002111 if (Name == "basic_block")
2112 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2113 else if (Name == "prologue_end")
2114 Flags |= DWARF2_FLAG_PROLOGUE_END;
2115 else if (Name == "epilogue_begin")
2116 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2117 else if (Name == "is_stmt") {
2118 SMLoc Loc = getTok().getLoc();
2119 const MCExpr *Value;
2120 if (getParser().ParseExpression(Value))
2121 return true;
2122 // The expression must be the constant 0 or 1.
2123 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2124 int Value = MCE->getValue();
2125 if (Value == 0)
2126 Flags &= ~DWARF2_FLAG_IS_STMT;
2127 else if (Value == 1)
2128 Flags |= DWARF2_FLAG_IS_STMT;
2129 else
2130 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002131 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002132 else {
2133 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2134 }
2135 }
2136 else if (Name == "isa") {
2137 SMLoc Loc = getTok().getLoc();
2138 const MCExpr *Value;
2139 if (getParser().ParseExpression(Value))
2140 return true;
2141 // The expression must be a constant greater or equal to 0.
2142 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2143 int Value = MCE->getValue();
2144 if (Value < 0)
2145 return Error(Loc, "isa number less than zero");
2146 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002147 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002148 else {
2149 return Error(Loc, "isa number not a constant value");
2150 }
2151 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002152 else if (Name == "discriminator") {
2153 if (getParser().ParseAbsoluteExpression(Discriminator))
2154 return true;
2155 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002156 else {
2157 return Error(Loc, "unknown sub-directive in '.loc' directive");
2158 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002159
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002160 if (getLexer().is(AsmToken::EndOfStatement))
2161 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002162 }
2163 }
2164
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002165 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2166 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002167
2168 return false;
2169}
2170
Daniel Dunbar138abae2010-10-16 04:56:42 +00002171/// ParseDirectiveStabs
2172/// ::= .stabs string, number, number, number
2173bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2174 SMLoc DirectiveLoc) {
2175 return TokError("unsupported directive '" + Directive + "'");
2176}
2177
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002178/// ParseDirectiveCFIStartProc
2179/// ::= .cfi_startproc
2180bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2181 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002182 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002183}
2184
2185/// ParseDirectiveCFIEndProc
2186/// ::= .cfi_endproc
2187bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002188 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002189}
2190
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002191/// ParseRegisterOrRegisterNumber - parse register name or number.
2192bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2193 SMLoc DirectiveLoc) {
2194 unsigned RegNo;
2195
2196 if (getLexer().is(AsmToken::Percent)) {
2197 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2198 DirectiveLoc))
2199 return true;
2200 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2201 } else
2202 return getParser().ParseAbsoluteExpression(Register);
2203
2204 return false;
2205}
2206
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002207/// ParseDirectiveCFIDefCfa
2208/// ::= .cfi_def_cfa register, offset
2209bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2210 SMLoc DirectiveLoc) {
2211 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002212 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002213 return true;
2214
2215 if (getLexer().isNot(AsmToken::Comma))
2216 return TokError("unexpected token in directive");
2217 Lex();
2218
2219 int64_t Offset = 0;
2220 if (getParser().ParseAbsoluteExpression(Offset))
2221 return true;
2222
2223 return getStreamer().EmitCFIDefCfa(Register, Offset);
2224}
2225
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002226/// ParseDirectiveCFIDefCfaOffset
2227/// ::= .cfi_def_cfa_offset offset
2228bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2229 SMLoc DirectiveLoc) {
2230 int64_t Offset = 0;
2231 if (getParser().ParseAbsoluteExpression(Offset))
2232 return true;
2233
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002234 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002235}
2236
2237/// ParseDirectiveCFIDefCfaRegister
2238/// ::= .cfi_def_cfa_register register
2239bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2240 SMLoc DirectiveLoc) {
2241 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002242 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002243 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002244
2245 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002246}
2247
2248/// ParseDirectiveCFIOffset
2249/// ::= .cfi_off register, offset
2250bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2251 int64_t Register = 0;
2252 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002253
2254 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002255 return true;
2256
2257 if (getLexer().isNot(AsmToken::Comma))
2258 return TokError("unexpected token in directive");
2259 Lex();
2260
2261 if (getParser().ParseAbsoluteExpression(Offset))
2262 return true;
2263
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002264 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002265}
2266
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002267static bool isValidEncoding(int64_t Encoding) {
2268 if (Encoding & ~0xff)
2269 return false;
2270
2271 if (Encoding == dwarf::DW_EH_PE_omit)
2272 return true;
2273
2274 const unsigned Format = Encoding & 0xf;
2275 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2276 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2277 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2278 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2279 return false;
2280
Rafael Espindolacaf11582010-12-29 04:31:26 +00002281 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002282 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002283 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002284 return false;
2285
2286 return true;
2287}
2288
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002289/// ParseDirectiveCFIPersonalityOrLsda
2290/// ::= .cfi_personality encoding, [symbol_name]
2291/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002292bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002293 SMLoc DirectiveLoc) {
2294 int64_t Encoding = 0;
2295 if (getParser().ParseAbsoluteExpression(Encoding))
2296 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002297 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002298 return false;
2299
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002300 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002301 return TokError("unsupported encoding.");
2302
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002303 if (getLexer().isNot(AsmToken::Comma))
2304 return TokError("unexpected token in directive");
2305 Lex();
2306
2307 StringRef Name;
2308 if (getParser().ParseIdentifier(Name))
2309 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002310
2311 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2312
2313 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002314 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002315 else {
2316 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002317 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002318 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002319}
2320
Rafael Espindolafe024d02010-12-28 18:36:23 +00002321/// ParseDirectiveCFIRememberState
2322/// ::= .cfi_remember_state
2323bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2324 SMLoc DirectiveLoc) {
2325 return getStreamer().EmitCFIRememberState();
2326}
2327
2328/// ParseDirectiveCFIRestoreState
2329/// ::= .cfi_remember_state
2330bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2331 SMLoc DirectiveLoc) {
2332 return getStreamer().EmitCFIRestoreState();
2333}
2334
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002335/// ParseDirectiveMacrosOnOff
2336/// ::= .macros_on
2337/// ::= .macros_off
2338bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2339 SMLoc DirectiveLoc) {
2340 if (getLexer().isNot(AsmToken::EndOfStatement))
2341 return Error(getLexer().getLoc(),
2342 "unexpected token in '" + Directive + "' directive");
2343
2344 getParser().MacrosEnabled = Directive == ".macros_on";
2345
2346 return false;
2347}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002348
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002349/// ParseDirectiveMacro
2350/// ::= .macro name
2351bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2352 SMLoc DirectiveLoc) {
2353 StringRef Name;
2354 if (getParser().ParseIdentifier(Name))
2355 return TokError("expected identifier in directive");
2356
2357 if (getLexer().isNot(AsmToken::EndOfStatement))
2358 return TokError("unexpected token in '.macro' directive");
2359
2360 // Eat the end of statement.
2361 Lex();
2362
2363 AsmToken EndToken, StartToken = getTok();
2364
2365 // Lex the macro definition.
2366 for (;;) {
2367 // Check whether we have reached the end of the file.
2368 if (getLexer().is(AsmToken::Eof))
2369 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2370
2371 // Otherwise, check whether we have reach the .endmacro.
2372 if (getLexer().is(AsmToken::Identifier) &&
2373 (getTok().getIdentifier() == ".endm" ||
2374 getTok().getIdentifier() == ".endmacro")) {
2375 EndToken = getTok();
2376 Lex();
2377 if (getLexer().isNot(AsmToken::EndOfStatement))
2378 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2379 "' directive");
2380 break;
2381 }
2382
2383 // Otherwise, scan til the end of the statement.
2384 getParser().EatToEndOfStatement();
2385 }
2386
2387 if (getParser().MacroMap.lookup(Name)) {
2388 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2389 }
2390
2391 const char *BodyStart = StartToken.getLoc().getPointer();
2392 const char *BodyEnd = EndToken.getLoc().getPointer();
2393 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2394 getParser().MacroMap[Name] = new Macro(Name, Body);
2395 return false;
2396}
2397
2398/// ParseDirectiveEndMacro
2399/// ::= .endm
2400/// ::= .endmacro
2401bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2402 SMLoc DirectiveLoc) {
2403 if (getLexer().isNot(AsmToken::EndOfStatement))
2404 return TokError("unexpected token in '" + Directive + "' directive");
2405
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002406 // If we are inside a macro instantiation, terminate the current
2407 // instantiation.
2408 if (!getParser().ActiveMacros.empty()) {
2409 getParser().HandleMacroExit();
2410 return false;
2411 }
2412
2413 // Otherwise, this .endmacro is a stray entry in the file; well formed
2414 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002415 return TokError("unexpected '" + Directive + "' in file, "
2416 "no current macro definition");
2417}
2418
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002419bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002420 getParser().CheckForValidSection();
2421
2422 const MCExpr *Value;
2423
2424 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002425 return true;
2426
2427 if (getLexer().isNot(AsmToken::EndOfStatement))
2428 return TokError("unexpected token in directive");
2429
2430 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002431 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002432 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002433 getStreamer().EmitULEB128Value(Value);
2434
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002435 return false;
2436}
2437
2438
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002439/// \brief Create an MCAsmParser instance.
2440MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2441 MCContext &C, MCStreamer &Out,
2442 const MCAsmInfo &MAI) {
2443 return new AsmParser(T, SM, C, Out, MAI);
2444}