blob: b72894db78b3c6f9abd86d9683e44bd4908e6803 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000034#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000036using namespace llvm;
37
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000038namespace {
39
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000040/// \brief Helper class for tracking macro definitions.
41struct Macro {
42 StringRef Name;
43 StringRef Body;
44
45public:
46 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
47};
48
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000049/// \brief Helper class for storing information about an active macro
50/// instantiation.
51struct MacroInstantiation {
52 /// The macro being instantiated.
53 const Macro *TheMacro;
54
55 /// The macro instantiation with substitutions.
56 MemoryBuffer *Instantiation;
57
58 /// The location of the instantiation.
59 SMLoc InstantiationLoc;
60
61 /// The location where parsing should resume upon instantiation completion.
62 SMLoc ExitLoc;
63
64public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000065 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
66 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000067};
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069/// \brief The concrete assembly parser instance.
70class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000071 friend class GenericAsmParser;
72
Daniel Dunbaraef87e32010-07-18 18:31:38 +000073 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
74 void operator=(const AsmParser &); // DO NOT IMPLEMENT
75private:
76 AsmLexer Lexer;
77 MCContext &Ctx;
78 MCStreamer &Out;
79 SourceMgr &SrcMgr;
80 MCAsmParserExtension *GenericParser;
81 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 /// This is the current buffer index we're lexing from as managed by the
84 /// SourceMgr object.
85 int CurBuffer;
86
87 AsmCond TheCondState;
88 std::vector<AsmCond> TheCondStack;
89
90 /// DirectiveMap - This is a table handlers for directives. Each handler is
91 /// invoked after the directive identifier is read and is responsible for
92 /// parsing and validating the rest of the directive. The handler is passed
93 /// in the directive name and the location of the directive keyword.
94 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000095
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000096 /// MacroMap - Map of currently defined macros.
97 StringMap<Macro*> MacroMap;
98
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000099 /// ActiveMacros - Stack of active macro instantiations.
100 std::vector<MacroInstantiation*> ActiveMacros;
101
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000102 /// Boolean tracking whether macro substitution is enabled.
103 unsigned MacrosEnabled : 1;
104
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000105 /// Flag tracking whether any errors have been encountered.
106 unsigned HadError : 1;
107
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000108public:
109 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
110 const MCAsmInfo &MAI);
111 ~AsmParser();
112
113 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
114
115 void AddDirectiveHandler(MCAsmParserExtension *Object,
116 StringRef Directive,
117 DirectiveHandler Handler) {
118 DirectiveMap[Directive] = std::make_pair(Object, Handler);
119 }
120
121public:
122 /// @name MCAsmParser Interface
123 /// {
124
125 virtual SourceMgr &getSourceManager() { return SrcMgr; }
126 virtual MCAsmLexer &getLexer() { return Lexer; }
127 virtual MCContext &getContext() { return Ctx; }
128 virtual MCStreamer &getStreamer() { return Out; }
129
130 virtual void Warning(SMLoc L, const Twine &Meg);
131 virtual bool Error(SMLoc L, const Twine &Msg);
132
133 const AsmToken &Lex();
134
135 bool ParseExpression(const MCExpr *&Res);
136 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseAbsoluteExpression(int64_t &Res);
139
140 /// }
141
142private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000143 void CheckForValidSection();
144
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 bool ParseStatement();
146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000147 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
148 void HandleMacroExit();
149
150 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000151 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
152 SrcMgr.PrintMessage(Loc, Msg, Type);
153 }
154
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000155 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
156 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000157
158 /// \brief Reset the current lexer position to that given by \arg Loc. The
159 /// current token is not set; clients should ensure Lex() is called
160 /// subsequently.
161 void JumpToLoc(SMLoc Loc);
162
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000164
165 /// \brief Parse up to the end of statement and a return the contents from the
166 /// current token until the end of the statement; the current token on exit
167 /// will be either the EndOfStatement or EOF.
168 StringRef ParseStringToEndOfStatement();
169
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000170 bool ParseAssignment(StringRef Name);
171
172 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
175
176 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
177 /// and set \arg Res to the identifier contents.
178 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000181
182 // ".ascii", ".asciiz", ".string"
183 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000185 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186 bool ParseDirectiveFill(); // ".fill"
187 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000188 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000189 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190 bool ParseDirectiveOrg(); // ".org"
191 // ".align{,32}", ".p2align{,w,l}"
192 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
193
194 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
195 /// accepts a single symbol (which should be a label or an external).
196 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197
198 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
199
200 bool ParseDirectiveAbort(); // ".abort"
201 bool ParseDirectiveInclude(); // ".include"
202
203 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
204 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
205 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
206 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
207
208 /// ParseEscapedString - Parse the current token as a string which may include
209 /// escaped characters and return the string contents.
210 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000211
212 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
213 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214};
215
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000216/// \brief Generic implementations of directive handling, etc. which is shared
217/// (or the default, at least) for all assembler parser.
218class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000219 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
220 void AddDirectiveHandler(StringRef Directive) {
221 getParser().AddDirectiveHandler(this, Directive,
222 HandleDirective<GenericAsmParser, Handler>);
223 }
224
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000225public:
226 GenericAsmParser() {}
227
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000228 AsmParser &getParser() {
229 return (AsmParser&) this->MCAsmParserExtension::getParser();
230 }
231
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000232 virtual void Initialize(MCAsmParser &Parser) {
233 // Call the base implementation.
234 this->MCAsmParserExtension::Initialize(Parser);
235
236 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000241
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000242 // CFI directives.
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
244 ".cfi_startproc");
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
246 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
248 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
250 ".cfi_def_cfa_offset");
251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
252 ".cfi_def_cfa_register");
253 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
254 ".cfi_offset");
255 AddDirectiveHandler<
256 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
257 AddDirectiveHandler<
258 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000259 AddDirectiveHandler<
260 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
261 AddDirectiveHandler<
262 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000263
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000264 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
266 ".macros_on");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
268 ".macros_off");
269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000272
273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000275 }
276
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000277 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
278 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
279 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000280 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000281 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
282 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000283 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000284 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
285 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
286 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
287 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000288 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
289 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000290
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000291 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000292 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
293 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000294
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000295 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000296};
297
298}
299
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000300namespace llvm {
301
302extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000303extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000304extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000305
306}
307
Chris Lattneraaec2052010-01-19 19:46:13 +0000308enum { DEFAULT_ADDRSPACE = 0 };
309
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000310AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
311 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000312 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000313 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000314 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000315 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000316
317 // Initialize the generic parser.
318 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000319
320 // Initialize the platform / file format parser.
321 //
322 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
323 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000324 if (_MAI.hasMicrosoftFastStdCallMangling()) {
325 PlatformParser = createCOFFAsmParser();
326 PlatformParser->Initialize(*this);
327 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000328 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000329 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000330 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000331 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000332 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000333 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000334}
335
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000336AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000337 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
338
339 // Destroy any macros.
340 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
341 ie = MacroMap.end(); it != ie; ++it)
342 delete it->getValue();
343
Daniel Dunbare4749702010-07-12 18:12:02 +0000344 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000345 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000346}
347
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000348void AsmParser::PrintMacroInstantiations() {
349 // Print the active macro instantiation stack.
350 for (std::vector<MacroInstantiation*>::const_reverse_iterator
351 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
352 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
353 "note");
354}
355
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000356void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000357 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000358 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000359}
360
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000361bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000362 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000363 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000364 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000365 return true;
366}
367
Sean Callananfd0b0282010-01-21 00:19:58 +0000368bool AsmParser::EnterIncludeFile(const std::string &Filename) {
369 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
370 if (NewBuf == -1)
371 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000372
Sean Callananfd0b0282010-01-21 00:19:58 +0000373 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000374
Sean Callananfd0b0282010-01-21 00:19:58 +0000375 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000376
Sean Callananfd0b0282010-01-21 00:19:58 +0000377 return false;
378}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000379
380void AsmParser::JumpToLoc(SMLoc Loc) {
381 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
382 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
383}
384
Sean Callananfd0b0282010-01-21 00:19:58 +0000385const AsmToken &AsmParser::Lex() {
386 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000387
Sean Callananfd0b0282010-01-21 00:19:58 +0000388 if (tok->is(AsmToken::Eof)) {
389 // If this is the end of an included file, pop the parent file off the
390 // include stack.
391 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
392 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000393 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000394 tok = &Lexer.Lex();
395 }
396 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000397
Sean Callananfd0b0282010-01-21 00:19:58 +0000398 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000399 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000400
Sean Callananfd0b0282010-01-21 00:19:58 +0000401 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000402}
403
Chris Lattner79180e22010-04-05 23:15:42 +0000404bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000405 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000406 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000407 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000408
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000409 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000410 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000411
412 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000413 AsmCond StartingCondState = TheCondState;
414
Chris Lattnerb717fb02009-07-02 21:53:43 +0000415 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000416 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000417 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000418
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000419 // We had an error, validate that one was emitted and recover by skipping to
420 // the next line.
421 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000422 EatToEndOfStatement();
423 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000424
425 if (TheCondState.TheCond != StartingCondState.TheCond ||
426 TheCondState.Ignore != StartingCondState.Ignore)
427 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000428
429 // Check to see there are no empty DwarfFile slots.
430 const std::vector<MCDwarfFile *> &MCDwarfFiles =
431 getContext().getMCDwarfFiles();
432 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000433 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000434 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000435 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000436
Chris Lattner79180e22010-04-05 23:15:42 +0000437 // Finalize the output stream if there are no errors and if the client wants
438 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000439 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000440 Out.Finish();
441
Chris Lattnerb717fb02009-07-02 21:53:43 +0000442 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000443}
444
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000445void AsmParser::CheckForValidSection() {
446 if (!getStreamer().getCurrentSection()) {
447 TokError("expected section directive before assembly directive");
448 Out.SwitchSection(Ctx.getMachOSection(
449 "__TEXT", "__text",
450 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
451 0, SectionKind::getText()));
452 }
453}
454
Chris Lattner2cf5f142009-06-22 01:29:09 +0000455/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
456void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000457 while (Lexer.isNot(AsmToken::EndOfStatement) &&
458 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000459 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000460
Chris Lattner2cf5f142009-06-22 01:29:09 +0000461 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000463 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000464}
465
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000466StringRef AsmParser::ParseStringToEndOfStatement() {
467 const char *Start = getTok().getLoc().getPointer();
468
469 while (Lexer.isNot(AsmToken::EndOfStatement) &&
470 Lexer.isNot(AsmToken::Eof))
471 Lex();
472
473 const char *End = getTok().getLoc().getPointer();
474 return StringRef(Start, End - Start);
475}
Chris Lattnerc4193832009-06-22 05:51:26 +0000476
Chris Lattner74ec1a32009-06-22 06:32:03 +0000477/// ParseParenExpr - Parse a paren expression and return it.
478/// NOTE: This assumes the leading '(' has already been consumed.
479///
480/// parenexpr ::= expr)
481///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000482bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000483 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000484 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000485 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000486 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000487 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000488 return false;
489}
Chris Lattnerc4193832009-06-22 05:51:26 +0000490
Chris Lattner74ec1a32009-06-22 06:32:03 +0000491/// ParsePrimaryExpr - Parse a primary expression and return it.
492/// primaryexpr ::= (parenexpr
493/// primaryexpr ::= symbol
494/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000495/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000496/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000497bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000498 switch (Lexer.getKind()) {
499 default:
500 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000501 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000502 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000503 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000504 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000505 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000506 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000507 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000508 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000509 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000510 EndLoc = Lexer.getLoc();
511
512 StringRef Identifier;
513 if (ParseIdentifier(Identifier))
514 return false;
515
Daniel Dunbarfffff912009-10-16 01:34:54 +0000516 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000517 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000518 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000519
520 // Lookup the symbol variant if used.
521 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000522 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000523 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000524 if (Variant == MCSymbolRefExpr::VK_Invalid) {
525 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000526 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000527 }
528 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000529
Daniel Dunbarfffff912009-10-16 01:34:54 +0000530 // If this is an absolute variable reference, substitute it now to preserve
531 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000532 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000533 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000534 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000535
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000536 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000537 return false;
538 }
539
540 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000541 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000542 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000543 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000544 case AsmToken::Integer: {
545 SMLoc Loc = getTok().getLoc();
546 int64_t IntVal = getTok().getIntVal();
547 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000548 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000549 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000550 // Look for 'b' or 'f' following an Integer as a directional label
551 if (Lexer.getKind() == AsmToken::Identifier) {
552 StringRef IDVal = getTok().getString();
553 if (IDVal == "f" || IDVal == "b"){
554 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
555 IDVal == "f" ? 1 : 0);
556 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
557 getContext());
558 if(IDVal == "b" && Sym->isUndefined())
559 return Error(Loc, "invalid reference to undefined symbol");
560 EndLoc = Lexer.getLoc();
561 Lex(); // Eat identifier.
562 }
563 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000564 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000565 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000566 case AsmToken::Real: {
567 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
568 int64_t IntVal = RealVal.bitcastToAPInt().getSExtValue();
569 Res = MCConstantExpr::Create(IntVal, getContext());
570 Lex(); // Eat token.
571 return false;
572 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000573 case AsmToken::Dot: {
574 // This is a '.' reference, which references the current PC. Emit a
575 // temporary label to the streamer and refer to it.
576 MCSymbol *Sym = Ctx.CreateTempSymbol();
577 Out.EmitLabel(Sym);
578 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
579 EndLoc = Lexer.getLoc();
580 Lex(); // Eat identifier.
581 return false;
582 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000583 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000584 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000585 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000586 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000587 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000588 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000589 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000590 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000591 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000592 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000593 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000594 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000595 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000596 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000597 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000598 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000599 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000600 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000601 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000602 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000603 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000604 }
605}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000606
Chris Lattnerb4307b32010-01-15 19:28:38 +0000607bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000608 SMLoc EndLoc;
609 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000610}
611
Daniel Dunbarcceba832010-09-17 02:47:07 +0000612const MCExpr *
613AsmParser::ApplyModifierToExpr(const MCExpr *E,
614 MCSymbolRefExpr::VariantKind Variant) {
615 // Recurse over the given expression, rebuilding it to apply the given variant
616 // if there is exactly one symbol.
617 switch (E->getKind()) {
618 case MCExpr::Target:
619 case MCExpr::Constant:
620 return 0;
621
622 case MCExpr::SymbolRef: {
623 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
624
625 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
626 TokError("invalid variant on expression '" +
627 getTok().getIdentifier() + "' (already modified)");
628 return E;
629 }
630
631 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
632 }
633
634 case MCExpr::Unary: {
635 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
636 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
637 if (!Sub)
638 return 0;
639 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
640 }
641
642 case MCExpr::Binary: {
643 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
644 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
645 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
646
647 if (!LHS && !RHS)
648 return 0;
649
650 if (!LHS) LHS = BE->getLHS();
651 if (!RHS) RHS = BE->getRHS();
652
653 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
654 }
655 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000656
657 assert(0 && "Invalid expression kind!");
658 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000659}
660
Chris Lattner74ec1a32009-06-22 06:32:03 +0000661/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000662///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000663/// expr ::= expr +,- expr -> lowest.
664/// expr ::= expr |,^,&,! expr -> middle.
665/// expr ::= expr *,/,%,<<,>> expr -> highest.
666/// expr ::= primaryexpr
667///
Chris Lattner54482b42010-01-15 19:39:23 +0000668bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000669 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000670 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000671 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
672 return true;
673
Daniel Dunbarcceba832010-09-17 02:47:07 +0000674 // As a special case, we support 'a op b @ modifier' by rewriting the
675 // expression to include the modifier. This is inefficient, but in general we
676 // expect users to use 'a@modifier op b'.
677 if (Lexer.getKind() == AsmToken::At) {
678 Lex();
679
680 if (Lexer.isNot(AsmToken::Identifier))
681 return TokError("unexpected symbol modifier following '@'");
682
683 MCSymbolRefExpr::VariantKind Variant =
684 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
685 if (Variant == MCSymbolRefExpr::VK_Invalid)
686 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
687
688 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
689 if (!ModifiedRes) {
690 return TokError("invalid modifier '" + getTok().getIdentifier() +
691 "' (no symbols present)");
692 return true;
693 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000694
Daniel Dunbarcceba832010-09-17 02:47:07 +0000695 Res = ModifiedRes;
696 Lex();
697 }
698
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000699 // Try to constant fold it up front, if possible.
700 int64_t Value;
701 if (Res->EvaluateAsAbsolute(Value))
702 Res = MCConstantExpr::Create(Value, getContext());
703
704 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000705}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000706
Chris Lattnerb4307b32010-01-15 19:28:38 +0000707bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000708 Res = 0;
709 return ParseParenExpr(Res, EndLoc) ||
710 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000711}
712
Daniel Dunbar475839e2009-06-29 20:37:27 +0000713bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000714 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000715
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000716 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000717 if (ParseExpression(Expr))
718 return true;
719
Daniel Dunbare00b0112009-10-16 01:57:52 +0000720 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000721 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000722
723 return false;
724}
725
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000726static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000727 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000728 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000729 default:
730 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000731
Daniel Dunbarcceba832010-09-17 02:47:07 +0000732 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000733 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000734 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000735 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000736 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000737 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000738 return 1;
739
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000740
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000741 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000742 //
743 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000744 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000745 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000746 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000747 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000748 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000749 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000751 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000752 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000753
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000754 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000755 case AsmToken::EqualEqual:
756 Kind = MCBinaryExpr::EQ;
757 return 3;
758 case AsmToken::ExclaimEqual:
759 case AsmToken::LessGreater:
760 Kind = MCBinaryExpr::NE;
761 return 3;
762 case AsmToken::Less:
763 Kind = MCBinaryExpr::LT;
764 return 3;
765 case AsmToken::LessEqual:
766 Kind = MCBinaryExpr::LTE;
767 return 3;
768 case AsmToken::Greater:
769 Kind = MCBinaryExpr::GT;
770 return 3;
771 case AsmToken::GreaterEqual:
772 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000773 return 3;
774
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000775 // High Intermediate Precedence: +, -
776 case AsmToken::Plus:
777 Kind = MCBinaryExpr::Add;
778 return 4;
779 case AsmToken::Minus:
780 Kind = MCBinaryExpr::Sub;
781 return 4;
782
Daniel Dunbar475839e2009-06-29 20:37:27 +0000783 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000784 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000785 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000786 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000788 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000789 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000791 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000792 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000793 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000794 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000795 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000796 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000797 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000798 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000799 }
800}
801
802
803/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
804/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000805bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
806 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000807 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000808 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000809 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000810
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000811 // If the next token is lower precedence than we are allowed to eat, return
812 // successfully with what we ate already.
813 if (TokPrec < Precedence)
814 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000815
Sean Callanan79ed1a82010-01-19 20:22:31 +0000816 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000817
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000818 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000819 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000820 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000821
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000822 // If BinOp binds less tightly with RHS than the operator after RHS, let
823 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000824 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000825 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000826 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000827 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000828 }
829
Daniel Dunbar475839e2009-06-29 20:37:27 +0000830 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000831 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000832 }
833}
834
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000835
836
837
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000838/// ParseStatement:
839/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000840/// ::= Label* Directive ...Operands... EndOfStatement
841/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000842bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000843 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000844 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000845 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000846 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000847 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000848
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000849 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000850 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000851 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000852 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000853 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000854 // A full line comment is a '#' as the first token.
855 if (Lexer.is(AsmToken::Hash)) {
856 EatToEndOfStatement();
857 return false;
858 }
859 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000860 if (Lexer.is(AsmToken::Integer)) {
861 LocalLabelVal = getTok().getIntVal();
862 if (LocalLabelVal < 0) {
863 if (!TheCondState.Ignore)
864 return TokError("unexpected token at start of statement");
865 IDVal = "";
866 }
867 else {
868 IDVal = getTok().getString();
869 Lex(); // Consume the integer token to be used as an identifier token.
870 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000871 if (!TheCondState.Ignore)
872 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000873 }
874 }
875 }
876 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000877 if (!TheCondState.Ignore)
878 return TokError("unexpected token at start of statement");
879 IDVal = "";
880 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000881
Chris Lattner7834fac2010-04-17 18:14:27 +0000882 // Handle conditional assembly here before checking for skipping. We
883 // have to do this so that .endif isn't skipped in a ".if 0" block for
884 // example.
885 if (IDVal == ".if")
886 return ParseDirectiveIf(IDLoc);
887 if (IDVal == ".elseif")
888 return ParseDirectiveElseIf(IDLoc);
889 if (IDVal == ".else")
890 return ParseDirectiveElse(IDLoc);
891 if (IDVal == ".endif")
892 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000893
Chris Lattner7834fac2010-04-17 18:14:27 +0000894 // If we are in a ".if 0" block, ignore this statement.
895 if (TheCondState.Ignore) {
896 EatToEndOfStatement();
897 return false;
898 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000899
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000900 // FIXME: Recurse on local labels?
901
902 // See what kind of statement we have.
903 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000904 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000905 CheckForValidSection();
906
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000907 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000908 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000909
910 // Diagnose attempt to use a variable as a label.
911 //
912 // FIXME: Diagnostics. Note the location of the definition as a label.
913 // FIXME: This doesn't diagnose assignment to a symbol which has been
914 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000915 MCSymbol *Sym;
916 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000917 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000918 else
919 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000920 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000921 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000922
Daniel Dunbar959fd882009-08-26 22:13:22 +0000923 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000924 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000925
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000926 // Consume any end of statement token, if present, to avoid spurious
927 // AddBlankLine calls().
928 if (Lexer.is(AsmToken::EndOfStatement)) {
929 Lex();
930 if (Lexer.is(AsmToken::Eof))
931 return false;
932 }
933
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000934 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000935 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000936
Daniel Dunbar3f872332009-07-28 16:08:33 +0000937 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000938 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000939 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000940
Daniel Dunbare2ace502009-08-31 08:09:09 +0000941 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000942
943 default: // Normal instruction or directive.
944 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000945 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000946
947 // If macros are enabled, check to see if this is a macro instantiation.
948 if (MacrosEnabled)
949 if (const Macro *M = MacroMap.lookup(IDVal))
950 return HandleMacroEntry(IDVal, IDLoc, M);
951
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000952 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000953 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000954 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000955 if (IDVal == ".set" || IDVal == ".equ")
956 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000957
Daniel Dunbara0d14262009-06-24 23:30:00 +0000958 // Data directives
959
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000960 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000961 return ParseDirectiveAscii(IDVal, false);
962 if (IDVal == ".asciz" || IDVal == ".string")
963 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000965 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000967 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000968 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000969 if (IDVal == ".value")
970 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000971 if (IDVal == ".2byte")
972 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000973 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000975 if (IDVal == ".int")
976 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000977 if (IDVal == ".4byte")
978 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000979 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000981 if (IDVal == ".8byte")
982 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000983 if (IDVal == ".single")
984 return ParseDirectiveRealValue(APFloat::IEEEsingle);
985 if (IDVal == ".double")
986 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000987
Eli Friedman5d68ec22010-07-19 04:17:25 +0000988 if (IDVal == ".align") {
989 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
990 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
991 }
992 if (IDVal == ".align32") {
993 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
994 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
995 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000996 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000997 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000998 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000999 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001000 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001001 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001002 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001003 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001004 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001005 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001006 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001007 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1008
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001009 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001010 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001011
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001012 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001013 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001014 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001016 if (IDVal == ".zero")
1017 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001019 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001020
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001021 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001022 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001023 // ELF only? Should it be here?
1024 if (IDVal == ".local")
1025 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001026 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001027 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001028 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001029 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001030 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001031 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001032 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001033 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001034 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001035 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001036 if (IDVal == ".symbol_resolver")
1037 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001038 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001039 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001040 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001041 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001042 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001043 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001044 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001045 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001046 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001047 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001048 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001049 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001050 if (IDVal == ".weak_def_can_be_hidden")
1051 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001052
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001053 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001054 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001055 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001056 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001057
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001058 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001059 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001060 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001061 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001062
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001063 // Look up the handler in the handler table.
1064 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1065 DirectiveMap.lookup(IDVal);
1066 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001067 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001068
Kevin Enderby9c656452009-09-10 20:51:44 +00001069 // Target hook for parsing target specific directives.
1070 if (!getTargetParser().ParseDirective(ID))
1071 return false;
1072
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001073 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001074 EatToEndOfStatement();
1075 return false;
1076 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001077
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001078 CheckForValidSection();
1079
Chris Lattnera7f13542010-05-19 23:34:33 +00001080 // Canonicalize the opcode to lower case.
1081 SmallString<128> Opcode;
1082 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1083 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001084
Chris Lattner98986712010-01-14 22:21:20 +00001085 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001086 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001087 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001088
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001089 // Dump the parsed representation, if requested.
1090 if (getShowParsedOperands()) {
1091 SmallString<256> Str;
1092 raw_svector_ostream OS(Str);
1093 OS << "parsed instruction: [";
1094 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1095 if (i != 0)
1096 OS << ", ";
1097 ParsedOperands[i]->dump(OS);
1098 }
1099 OS << "]";
1100
1101 PrintMessage(IDLoc, OS.str(), "note");
1102 }
1103
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001104 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001105 if (!HadError)
1106 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1107 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001108
Chris Lattner98986712010-01-14 22:21:20 +00001109 // Free any parsed operands.
1110 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1111 delete ParsedOperands[i];
1112
Chris Lattnercbf8a982010-09-11 16:18:25 +00001113 // Don't skip the rest of the line, the instruction parser is responsible for
1114 // that.
1115 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001116}
Chris Lattner9a023f72009-06-24 04:43:34 +00001117
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001118MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1119 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001120 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1121{
1122 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1123 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001124 SmallString<256> Buf;
1125 raw_svector_ostream OS(Buf);
1126
1127 StringRef Body = M->Body;
1128 while (!Body.empty()) {
1129 // Scan for the next substitution.
1130 std::size_t End = Body.size(), Pos = 0;
1131 for (; Pos != End; ++Pos) {
1132 // Check for a substitution or escape.
1133 if (Body[Pos] != '$' || Pos + 1 == End)
1134 continue;
1135
1136 char Next = Body[Pos + 1];
1137 if (Next == '$' || Next == 'n' || isdigit(Next))
1138 break;
1139 }
1140
1141 // Add the prefix.
1142 OS << Body.slice(0, Pos);
1143
1144 // Check if we reached the end.
1145 if (Pos == End)
1146 break;
1147
1148 switch (Body[Pos+1]) {
1149 // $$ => $
1150 case '$':
1151 OS << '$';
1152 break;
1153
1154 // $n => number of arguments
1155 case 'n':
1156 OS << A.size();
1157 break;
1158
1159 // $[0-9] => argument
1160 default: {
1161 // Missing arguments are ignored.
1162 unsigned Index = Body[Pos+1] - '0';
1163 if (Index >= A.size())
1164 break;
1165
1166 // Otherwise substitute with the token values, with spaces eliminated.
1167 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1168 ie = A[Index].end(); it != ie; ++it)
1169 OS << it->getString();
1170 break;
1171 }
1172 }
1173
1174 // Update the scan point.
1175 Body = Body.substr(Pos + 2);
1176 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001177
1178 // We include the .endmacro in the buffer as our queue to exit the macro
1179 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001180 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001181
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001182 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001183}
1184
1185bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1186 const Macro *M) {
1187 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1188 // this, although we should protect against infinite loops.
1189 if (ActiveMacros.size() == 20)
1190 return TokError("macros cannot be nested more than 20 levels deep");
1191
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001192 // Parse the macro instantiation arguments.
1193 std::vector<std::vector<AsmToken> > MacroArguments;
1194 MacroArguments.push_back(std::vector<AsmToken>());
1195 unsigned ParenLevel = 0;
1196 for (;;) {
1197 if (Lexer.is(AsmToken::Eof))
1198 return TokError("unexpected token in macro instantiation");
1199 if (Lexer.is(AsmToken::EndOfStatement))
1200 break;
1201
1202 // If we aren't inside parentheses and this is a comma, start a new token
1203 // list.
1204 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1205 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001206 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001207 // Adjust the current parentheses level.
1208 if (Lexer.is(AsmToken::LParen))
1209 ++ParenLevel;
1210 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1211 --ParenLevel;
1212
1213 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001214 MacroArguments.back().push_back(getTok());
1215 }
1216 Lex();
1217 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001218
1219 // Create the macro instantiation object and add to the current macro
1220 // instantiation stack.
1221 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001222 getTok().getLoc(),
1223 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001224 ActiveMacros.push_back(MI);
1225
1226 // Jump to the macro instantiation and prime the lexer.
1227 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1228 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1229 Lex();
1230
1231 return false;
1232}
1233
1234void AsmParser::HandleMacroExit() {
1235 // Jump to the EndOfStatement we should return to, and consume it.
1236 JumpToLoc(ActiveMacros.back()->ExitLoc);
1237 Lex();
1238
1239 // Pop the instantiation entry.
1240 delete ActiveMacros.back();
1241 ActiveMacros.pop_back();
1242}
1243
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001244static void MarkUsed(const MCExpr *Value) {
1245 switch (Value->getKind()) {
1246 case MCExpr::Binary:
1247 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1248 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1249 break;
1250 case MCExpr::Target:
1251 case MCExpr::Constant:
1252 break;
1253 case MCExpr::SymbolRef: {
1254 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1255 break;
1256 }
1257 case MCExpr::Unary:
1258 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1259 break;
1260 }
1261}
1262
Benjamin Kramer38e59892010-07-14 22:38:02 +00001263bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001264 // FIXME: Use better location, we should use proper tokens.
1265 SMLoc EqualLoc = Lexer.getLoc();
1266
Daniel Dunbar821e3332009-08-31 08:09:28 +00001267 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001268 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001269 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001270
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001271 MarkUsed(Value);
1272
Daniel Dunbar3f872332009-07-28 16:08:33 +00001273 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001274 return TokError("unexpected token in assignment");
1275
1276 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001277 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001278
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001279 // Validate that the LHS is allowed to be a variable (either it has not been
1280 // used as a symbol, or it is an absolute symbol).
1281 MCSymbol *Sym = getContext().LookupSymbol(Name);
1282 if (Sym) {
1283 // Diagnose assignment to a label.
1284 //
1285 // FIXME: Diagnostics. Note the location of the definition as a label.
1286 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001287 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001288 ; // Allow redefinitions of undefined symbols only used in directives.
1289 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001290 return Error(EqualLoc, "redefinition of '" + Name + "'");
1291 else if (!Sym->isVariable())
1292 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001293 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001294 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1295 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001296
1297 // Don't count these checks as uses.
1298 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001299 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001300 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001301
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001302 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001303
1304 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001305 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001306
1307 return false;
1308}
1309
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001310/// ParseIdentifier:
1311/// ::= identifier
1312/// ::= string
1313bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001314 // The assembler has relaxed rules for accepting identifiers, in particular we
1315 // allow things like '.globl $foo', which would normally be separate
1316 // tokens. At this level, we have already lexed so we cannot (currently)
1317 // handle this as a context dependent token, instead we detect adjacent tokens
1318 // and return the combined identifier.
1319 if (Lexer.is(AsmToken::Dollar)) {
1320 SMLoc DollarLoc = getLexer().getLoc();
1321
1322 // Consume the dollar sign, and check for a following identifier.
1323 Lex();
1324 if (Lexer.isNot(AsmToken::Identifier))
1325 return true;
1326
1327 // We have a '$' followed by an identifier, make sure they are adjacent.
1328 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1329 return true;
1330
1331 // Construct the joined identifier and consume the token.
1332 Res = StringRef(DollarLoc.getPointer(),
1333 getTok().getIdentifier().size() + 1);
1334 Lex();
1335 return false;
1336 }
1337
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001338 if (Lexer.isNot(AsmToken::Identifier) &&
1339 Lexer.isNot(AsmToken::String))
1340 return true;
1341
Sean Callanan18b83232010-01-19 21:44:56 +00001342 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001343
Sean Callanan79ed1a82010-01-19 20:22:31 +00001344 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001345
1346 return false;
1347}
1348
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001349/// ParseDirectiveSet:
1350/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001351bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001352 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001353
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001354 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001355 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001356
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001357 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001358 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001359 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001360
Daniel Dunbare2ace502009-08-31 08:09:09 +00001361 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001362}
1363
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001364bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001365 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001366
1367 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001368 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001369 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1370 if (Str[i] != '\\') {
1371 Data += Str[i];
1372 continue;
1373 }
1374
1375 // Recognize escaped characters. Note that this escape semantics currently
1376 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1377 ++i;
1378 if (i == e)
1379 return TokError("unexpected backslash at end of string");
1380
1381 // Recognize octal sequences.
1382 if ((unsigned) (Str[i] - '0') <= 7) {
1383 // Consume up to three octal characters.
1384 unsigned Value = Str[i] - '0';
1385
1386 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1387 ++i;
1388 Value = Value * 8 + (Str[i] - '0');
1389
1390 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1391 ++i;
1392 Value = Value * 8 + (Str[i] - '0');
1393 }
1394 }
1395
1396 if (Value > 255)
1397 return TokError("invalid octal escape sequence (out of range)");
1398
1399 Data += (unsigned char) Value;
1400 continue;
1401 }
1402
1403 // Otherwise recognize individual escapes.
1404 switch (Str[i]) {
1405 default:
1406 // Just reject invalid escape sequences for now.
1407 return TokError("invalid escape sequence (unrecognized character)");
1408
1409 case 'b': Data += '\b'; break;
1410 case 'f': Data += '\f'; break;
1411 case 'n': Data += '\n'; break;
1412 case 'r': Data += '\r'; break;
1413 case 't': Data += '\t'; break;
1414 case '"': Data += '"'; break;
1415 case '\\': Data += '\\'; break;
1416 }
1417 }
1418
1419 return false;
1420}
1421
Daniel Dunbara0d14262009-06-24 23:30:00 +00001422/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001423/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1424bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001425 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001426 CheckForValidSection();
1427
Daniel Dunbara0d14262009-06-24 23:30:00 +00001428 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001429 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001430 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001431
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001432 std::string Data;
1433 if (ParseEscapedString(Data))
1434 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001435
1436 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001437 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001438 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1439
Sean Callanan79ed1a82010-01-19 20:22:31 +00001440 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001441
1442 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001443 break;
1444
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001445 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001446 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001447 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001448 }
1449 }
1450
Sean Callanan79ed1a82010-01-19 20:22:31 +00001451 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001452 return false;
1453}
1454
1455/// ParseDirectiveValue
1456/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1457bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001458 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001459 CheckForValidSection();
1460
Daniel Dunbara0d14262009-06-24 23:30:00 +00001461 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001462 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001463 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001464 return true;
1465
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001466 // Special case constant expressions to match code generator.
1467 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001468 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001469 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001470 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001471
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001472 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001473 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001474
Daniel Dunbara0d14262009-06-24 23:30:00 +00001475 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001476 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001477 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001478 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001479 }
1480 }
1481
Sean Callanan79ed1a82010-01-19 20:22:31 +00001482 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001483 return false;
1484}
1485
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001486/// ParseDirectiveRealValue
1487/// ::= (.single | .double) [ expression (, expression)* ]
1488bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1489 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1490 CheckForValidSection();
1491
1492 for (;;) {
1493 // We don't truly support arithmetic on floating point expressions, so we
1494 // have to manually parse unary prefixes.
1495 bool IsNeg = false;
1496 if (getLexer().is(AsmToken::Minus)) {
1497 Lex();
1498 IsNeg = true;
1499 } else if (getLexer().is(AsmToken::Plus))
1500 Lex();
1501
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001502 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001503 getLexer().isNot(AsmToken::Real))
1504 return TokError("unexpected token in directive");
1505
1506 // Convert to an APFloat.
1507 APFloat Value(Semantics);
1508 if (Value.convertFromString(getTok().getString(),
1509 APFloat::rmNearestTiesToEven) ==
1510 APFloat::opInvalidOp)
1511 return TokError("invalid floating point literal");
1512 if (IsNeg)
1513 Value.changeSign();
1514
1515 // Consume the numeric token.
1516 Lex();
1517
1518 // Emit the value as an integer.
1519 APInt AsInt = Value.bitcastToAPInt();
1520 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1521 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1522
1523 if (getLexer().is(AsmToken::EndOfStatement))
1524 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001525
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001526 if (getLexer().isNot(AsmToken::Comma))
1527 return TokError("unexpected token in directive");
1528 Lex();
1529 }
1530 }
1531
1532 Lex();
1533 return false;
1534}
1535
Daniel Dunbara0d14262009-06-24 23:30:00 +00001536/// ParseDirectiveSpace
1537/// ::= .space expression [ , expression ]
1538bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001539 CheckForValidSection();
1540
Daniel Dunbara0d14262009-06-24 23:30:00 +00001541 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001542 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001543 return true;
1544
1545 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001546 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1547 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001548 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001549 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001550
Daniel Dunbar475839e2009-06-29 20:37:27 +00001551 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001552 return true;
1553
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001554 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555 return TokError("unexpected token in '.space' directive");
1556 }
1557
Sean Callanan79ed1a82010-01-19 20:22:31 +00001558 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001559
1560 if (NumBytes <= 0)
1561 return TokError("invalid number of bytes in '.space' directive");
1562
1563 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001564 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001565
1566 return false;
1567}
1568
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001569/// ParseDirectiveZero
1570/// ::= .zero expression
1571bool AsmParser::ParseDirectiveZero() {
1572 CheckForValidSection();
1573
1574 int64_t NumBytes;
1575 if (ParseAbsoluteExpression(NumBytes))
1576 return true;
1577
Rafael Espindolae452b172010-10-05 19:42:57 +00001578 int64_t Val = 0;
1579 if (getLexer().is(AsmToken::Comma)) {
1580 Lex();
1581 if (ParseAbsoluteExpression(Val))
1582 return true;
1583 }
1584
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001585 if (getLexer().isNot(AsmToken::EndOfStatement))
1586 return TokError("unexpected token in '.zero' directive");
1587
1588 Lex();
1589
Rafael Espindolae452b172010-10-05 19:42:57 +00001590 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001591
1592 return false;
1593}
1594
Daniel Dunbara0d14262009-06-24 23:30:00 +00001595/// ParseDirectiveFill
1596/// ::= .fill expression , expression , expression
1597bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001598 CheckForValidSection();
1599
Daniel Dunbara0d14262009-06-24 23:30:00 +00001600 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001601 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001602 return true;
1603
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001604 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001605 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001606 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001607
Daniel Dunbara0d14262009-06-24 23:30:00 +00001608 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001609 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001610 return true;
1611
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001612 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001613 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001614 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001615
Daniel Dunbara0d14262009-06-24 23:30:00 +00001616 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001617 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001618 return true;
1619
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001620 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001621 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001622
Sean Callanan79ed1a82010-01-19 20:22:31 +00001623 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001624
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001625 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1626 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001627
1628 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001629 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001630
1631 return false;
1632}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001633
1634/// ParseDirectiveOrg
1635/// ::= .org expression [ , expression ]
1636bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001637 CheckForValidSection();
1638
Daniel Dunbar821e3332009-08-31 08:09:28 +00001639 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001640 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001641 return true;
1642
1643 // Parse optional fill expression.
1644 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001645 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1646 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001647 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001648 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001649
Daniel Dunbar475839e2009-06-29 20:37:27 +00001650 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001651 return true;
1652
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001653 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001654 return TokError("unexpected token in '.org' directive");
1655 }
1656
Sean Callanan79ed1a82010-01-19 20:22:31 +00001657 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001658
1659 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1660 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001661 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001662
1663 return false;
1664}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001665
1666/// ParseDirectiveAlign
1667/// ::= {.align, ...} expression [ , expression [ , expression ]]
1668bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001669 CheckForValidSection();
1670
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001671 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001672 int64_t Alignment;
1673 if (ParseAbsoluteExpression(Alignment))
1674 return true;
1675
1676 SMLoc MaxBytesLoc;
1677 bool HasFillExpr = false;
1678 int64_t FillExpr = 0;
1679 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001680 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1681 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001682 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001683 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001684
1685 // The fill expression can be omitted while specifying a maximum number of
1686 // alignment bytes, e.g:
1687 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001688 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001689 HasFillExpr = true;
1690 if (ParseAbsoluteExpression(FillExpr))
1691 return true;
1692 }
1693
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1695 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001696 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001697 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001698
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001699 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001700 if (ParseAbsoluteExpression(MaxBytesToFill))
1701 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001702
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001703 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001704 return TokError("unexpected token in directive");
1705 }
1706 }
1707
Sean Callanan79ed1a82010-01-19 20:22:31 +00001708 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001709
Daniel Dunbar648ac512010-05-17 21:54:30 +00001710 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001711 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001712
1713 // Compute alignment in bytes.
1714 if (IsPow2) {
1715 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001716 if (Alignment >= 32) {
1717 Error(AlignmentLoc, "invalid alignment value");
1718 Alignment = 31;
1719 }
1720
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001721 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001722 }
1723
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001724 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001725 if (MaxBytesLoc.isValid()) {
1726 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001727 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1728 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001729 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001730 }
1731
1732 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001733 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1734 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001735 MaxBytesToFill = 0;
1736 }
1737 }
1738
Daniel Dunbar648ac512010-05-17 21:54:30 +00001739 // Check whether we should use optimal code alignment for this .align
1740 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001741 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001742 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1743 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001744 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001745 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001746 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001747 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1748 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001749 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001750
1751 return false;
1752}
1753
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001754/// ParseDirectiveSymbolAttribute
1755/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001756bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001757 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001758 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001759 StringRef Name;
1760
1761 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001762 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001763
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001764 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001765
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001766 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001767
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001768 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001769 break;
1770
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001772 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001773 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001774 }
1775 }
1776
Sean Callanan79ed1a82010-01-19 20:22:31 +00001777 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001778 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001779}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001780
1781/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001782/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1783bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001784 CheckForValidSection();
1785
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001786 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001787 StringRef Name;
1788 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001789 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001790
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001791 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001792 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001793
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001794 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001795 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001796 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001797
1798 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001799 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001800 if (ParseAbsoluteExpression(Size))
1801 return true;
1802
1803 int64_t Pow2Alignment = 0;
1804 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001805 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001806 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001807 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001808 if (ParseAbsoluteExpression(Pow2Alignment))
1809 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001810
Chris Lattner258281d2010-01-19 06:22:22 +00001811 // If this target takes alignments in bytes (not log) validate and convert.
1812 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1813 if (!isPowerOf2_64(Pow2Alignment))
1814 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1815 Pow2Alignment = Log2_64(Pow2Alignment);
1816 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001817 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001818
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001819 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001820 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001821
Sean Callanan79ed1a82010-01-19 20:22:31 +00001822 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001823
Chris Lattner1fc3d752009-07-09 17:25:12 +00001824 // NOTE: a size of zero for a .comm should create a undefined symbol
1825 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001826 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001827 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1828 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001829
Eric Christopherc260a3e2010-05-14 01:38:54 +00001830 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001831 // may internally end up wanting an alignment in bytes.
1832 // FIXME: Diagnose overflow.
1833 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001834 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1835 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001836
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001837 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001838 return Error(IDLoc, "invalid symbol redefinition");
1839
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001840 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001841 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001842 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001843 getStreamer().EmitZerofill(Ctx.getMachOSection(
1844 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1845 0, SectionKind::getBSS()),
1846 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001847 return false;
1848 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001849
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001850 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001851 return false;
1852}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001853
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001854/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001855/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001856bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001857 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001858 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001859
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001860 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001861 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001862 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001863
Sean Callanan79ed1a82010-01-19 20:22:31 +00001864 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001865
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001866 if (Str.empty())
1867 Error(Loc, ".abort detected. Assembly stopping.");
1868 else
1869 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001870 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001871
1872 return false;
1873}
Kevin Enderby71148242009-07-14 21:35:03 +00001874
Kevin Enderby1f049b22009-07-14 23:21:55 +00001875/// ParseDirectiveInclude
1876/// ::= .include "filename"
1877bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001878 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001879 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001880
Sean Callanan18b83232010-01-19 21:44:56 +00001881 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001883 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001884
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001885 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001886 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001887
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001888 // Strip the quotes.
1889 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001890
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001891 // Attempt to switch the lexer to the included file before consuming the end
1892 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001893 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001894 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001895 return true;
1896 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001897
1898 return false;
1899}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001900
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001901/// ParseDirectiveIf
1902/// ::= .if expression
1903bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001904 TheCondStack.push_back(TheCondState);
1905 TheCondState.TheCond = AsmCond::IfCond;
1906 if(TheCondState.Ignore) {
1907 EatToEndOfStatement();
1908 }
1909 else {
1910 int64_t ExprValue;
1911 if (ParseAbsoluteExpression(ExprValue))
1912 return true;
1913
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001914 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001915 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001916
Sean Callanan79ed1a82010-01-19 20:22:31 +00001917 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001918
1919 TheCondState.CondMet = ExprValue;
1920 TheCondState.Ignore = !TheCondState.CondMet;
1921 }
1922
1923 return false;
1924}
1925
1926/// ParseDirectiveElseIf
1927/// ::= .elseif expression
1928bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1929 if (TheCondState.TheCond != AsmCond::IfCond &&
1930 TheCondState.TheCond != AsmCond::ElseIfCond)
1931 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1932 " an .elseif");
1933 TheCondState.TheCond = AsmCond::ElseIfCond;
1934
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001935 bool LastIgnoreState = false;
1936 if (!TheCondStack.empty())
1937 LastIgnoreState = TheCondStack.back().Ignore;
1938 if (LastIgnoreState || TheCondState.CondMet) {
1939 TheCondState.Ignore = true;
1940 EatToEndOfStatement();
1941 }
1942 else {
1943 int64_t ExprValue;
1944 if (ParseAbsoluteExpression(ExprValue))
1945 return true;
1946
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001947 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001948 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001949
Sean Callanan79ed1a82010-01-19 20:22:31 +00001950 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001951 TheCondState.CondMet = ExprValue;
1952 TheCondState.Ignore = !TheCondState.CondMet;
1953 }
1954
1955 return false;
1956}
1957
1958/// ParseDirectiveElse
1959/// ::= .else
1960bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001961 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001962 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001963
Sean Callanan79ed1a82010-01-19 20:22:31 +00001964 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001965
1966 if (TheCondState.TheCond != AsmCond::IfCond &&
1967 TheCondState.TheCond != AsmCond::ElseIfCond)
1968 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1969 ".elseif");
1970 TheCondState.TheCond = AsmCond::ElseCond;
1971 bool LastIgnoreState = false;
1972 if (!TheCondStack.empty())
1973 LastIgnoreState = TheCondStack.back().Ignore;
1974 if (LastIgnoreState || TheCondState.CondMet)
1975 TheCondState.Ignore = true;
1976 else
1977 TheCondState.Ignore = false;
1978
1979 return false;
1980}
1981
1982/// ParseDirectiveEndIf
1983/// ::= .endif
1984bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001985 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001986 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001987
Sean Callanan79ed1a82010-01-19 20:22:31 +00001988 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001989
1990 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1991 TheCondStack.empty())
1992 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1993 ".else");
1994 if (!TheCondStack.empty()) {
1995 TheCondState = TheCondStack.back();
1996 TheCondStack.pop_back();
1997 }
1998
1999 return false;
2000}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002001
2002/// ParseDirectiveFile
2003/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002004bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002005 // FIXME: I'm not sure what this is.
2006 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002007 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002008 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002009 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002010 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002011
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002012 if (FileNumber < 1)
2013 return TokError("file number less than one");
2014 }
2015
Daniel Dunbareceec052010-07-12 17:45:27 +00002016 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002017 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002018
Chris Lattnerd32e8032010-01-25 19:02:58 +00002019 StringRef Filename = getTok().getString();
2020 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002021 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002022
Daniel Dunbareceec052010-07-12 17:45:27 +00002023 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002024 return TokError("unexpected token in '.file' directive");
2025
Chris Lattnerd32e8032010-01-25 19:02:58 +00002026 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002027 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002028 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002029 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002030 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002031 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002032
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002033 return false;
2034}
2035
2036/// ParseDirectiveLine
2037/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002038bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002039 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2040 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002041 return TokError("unexpected token in '.line' directive");
2042
Sean Callanan18b83232010-01-19 21:44:56 +00002043 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002044 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002045 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002046
2047 // FIXME: Do something with the .line.
2048 }
2049
Daniel Dunbareceec052010-07-12 17:45:27 +00002050 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002051 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002052
2053 return false;
2054}
2055
2056
2057/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002058/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002059/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2060/// The first number is a file number, must have been previously assigned with
2061/// a .file directive, the second number is the line number and optionally the
2062/// third number is a column position (zero if not specified). The remaining
2063/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002064bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002065
Daniel Dunbareceec052010-07-12 17:45:27 +00002066 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002067 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002068 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002069 if (FileNumber < 1)
2070 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002071 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002072 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002073 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002074
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002075 int64_t LineNumber = 0;
2076 if (getLexer().is(AsmToken::Integer)) {
2077 LineNumber = getTok().getIntVal();
2078 if (LineNumber < 1)
2079 return TokError("line number less than one in '.loc' directive");
2080 Lex();
2081 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002082
2083 int64_t ColumnPos = 0;
2084 if (getLexer().is(AsmToken::Integer)) {
2085 ColumnPos = getTok().getIntVal();
2086 if (ColumnPos < 0)
2087 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002088 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002089 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002090
Kevin Enderbyc0957932010-09-30 16:52:03 +00002091 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002092 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002093 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002094 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2095 for (;;) {
2096 if (getLexer().is(AsmToken::EndOfStatement))
2097 break;
2098
2099 StringRef Name;
2100 SMLoc Loc = getTok().getLoc();
2101 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002102 return TokError("unexpected token in '.loc' directive");
2103
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002104 if (Name == "basic_block")
2105 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2106 else if (Name == "prologue_end")
2107 Flags |= DWARF2_FLAG_PROLOGUE_END;
2108 else if (Name == "epilogue_begin")
2109 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2110 else if (Name == "is_stmt") {
2111 SMLoc Loc = getTok().getLoc();
2112 const MCExpr *Value;
2113 if (getParser().ParseExpression(Value))
2114 return true;
2115 // The expression must be the constant 0 or 1.
2116 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2117 int Value = MCE->getValue();
2118 if (Value == 0)
2119 Flags &= ~DWARF2_FLAG_IS_STMT;
2120 else if (Value == 1)
2121 Flags |= DWARF2_FLAG_IS_STMT;
2122 else
2123 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002124 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002125 else {
2126 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2127 }
2128 }
2129 else if (Name == "isa") {
2130 SMLoc Loc = getTok().getLoc();
2131 const MCExpr *Value;
2132 if (getParser().ParseExpression(Value))
2133 return true;
2134 // The expression must be a constant greater or equal to 0.
2135 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2136 int Value = MCE->getValue();
2137 if (Value < 0)
2138 return Error(Loc, "isa number less than zero");
2139 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002140 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002141 else {
2142 return Error(Loc, "isa number not a constant value");
2143 }
2144 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002145 else if (Name == "discriminator") {
2146 if (getParser().ParseAbsoluteExpression(Discriminator))
2147 return true;
2148 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002149 else {
2150 return Error(Loc, "unknown sub-directive in '.loc' directive");
2151 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002152
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002153 if (getLexer().is(AsmToken::EndOfStatement))
2154 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002155 }
2156 }
2157
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002158 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2159 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002160
2161 return false;
2162}
2163
Daniel Dunbar138abae2010-10-16 04:56:42 +00002164/// ParseDirectiveStabs
2165/// ::= .stabs string, number, number, number
2166bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2167 SMLoc DirectiveLoc) {
2168 return TokError("unsupported directive '" + Directive + "'");
2169}
2170
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002171/// ParseDirectiveCFIStartProc
2172/// ::= .cfi_startproc
2173bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2174 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002175 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002176}
2177
2178/// ParseDirectiveCFIEndProc
2179/// ::= .cfi_endproc
2180bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002181 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002182}
2183
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002184/// ParseDirectiveCFIDefCfa
2185/// ::= .cfi_def_cfa register, offset
2186bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2187 SMLoc DirectiveLoc) {
2188 int64_t Register = 0;
2189 if (getParser().ParseAbsoluteExpression(Register))
2190 return true;
2191
2192 if (getLexer().isNot(AsmToken::Comma))
2193 return TokError("unexpected token in directive");
2194 Lex();
2195
2196 int64_t Offset = 0;
2197 if (getParser().ParseAbsoluteExpression(Offset))
2198 return true;
2199
2200 return getStreamer().EmitCFIDefCfa(Register, Offset);
2201}
2202
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002203/// ParseDirectiveCFIDefCfaOffset
2204/// ::= .cfi_def_cfa_offset offset
2205bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2206 SMLoc DirectiveLoc) {
2207 int64_t Offset = 0;
2208 if (getParser().ParseAbsoluteExpression(Offset))
2209 return true;
2210
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002211 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002212}
2213
2214/// ParseDirectiveCFIDefCfaRegister
2215/// ::= .cfi_def_cfa_register register
2216bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2217 SMLoc DirectiveLoc) {
2218 int64_t Register = 0;
2219 if (getParser().ParseAbsoluteExpression(Register))
2220 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002221
2222 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002223}
2224
2225/// ParseDirectiveCFIOffset
2226/// ::= .cfi_off register, offset
2227bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2228 int64_t Register = 0;
2229 int64_t Offset = 0;
2230 if (getParser().ParseAbsoluteExpression(Register))
2231 return true;
2232
2233 if (getLexer().isNot(AsmToken::Comma))
2234 return TokError("unexpected token in directive");
2235 Lex();
2236
2237 if (getParser().ParseAbsoluteExpression(Offset))
2238 return true;
2239
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002240 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002241}
2242
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002243static bool isValidEncoding(int64_t Encoding) {
2244 if (Encoding & ~0xff)
2245 return false;
2246
2247 if (Encoding == dwarf::DW_EH_PE_omit)
2248 return true;
2249
2250 const unsigned Format = Encoding & 0xf;
2251 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2252 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2253 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2254 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2255 return false;
2256
Rafael Espindolacaf11582010-12-29 04:31:26 +00002257 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002258 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002259 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002260 return false;
2261
2262 return true;
2263}
2264
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002265/// ParseDirectiveCFIPersonalityOrLsda
2266/// ::= .cfi_personality encoding, [symbol_name]
2267/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002268bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002269 SMLoc DirectiveLoc) {
2270 int64_t Encoding = 0;
2271 if (getParser().ParseAbsoluteExpression(Encoding))
2272 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002273 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002274 return false;
2275
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002276 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002277 return TokError("unsupported encoding.");
2278
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002279 if (getLexer().isNot(AsmToken::Comma))
2280 return TokError("unexpected token in directive");
2281 Lex();
2282
2283 StringRef Name;
2284 if (getParser().ParseIdentifier(Name))
2285 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002286
2287 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2288
2289 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002290 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002291 else {
2292 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002293 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002294 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002295}
2296
Rafael Espindolafe024d02010-12-28 18:36:23 +00002297/// ParseDirectiveCFIRememberState
2298/// ::= .cfi_remember_state
2299bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2300 SMLoc DirectiveLoc) {
2301 return getStreamer().EmitCFIRememberState();
2302}
2303
2304/// ParseDirectiveCFIRestoreState
2305/// ::= .cfi_remember_state
2306bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2307 SMLoc DirectiveLoc) {
2308 return getStreamer().EmitCFIRestoreState();
2309}
2310
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002311/// ParseDirectiveMacrosOnOff
2312/// ::= .macros_on
2313/// ::= .macros_off
2314bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2315 SMLoc DirectiveLoc) {
2316 if (getLexer().isNot(AsmToken::EndOfStatement))
2317 return Error(getLexer().getLoc(),
2318 "unexpected token in '" + Directive + "' directive");
2319
2320 getParser().MacrosEnabled = Directive == ".macros_on";
2321
2322 return false;
2323}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002324
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002325/// ParseDirectiveMacro
2326/// ::= .macro name
2327bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2328 SMLoc DirectiveLoc) {
2329 StringRef Name;
2330 if (getParser().ParseIdentifier(Name))
2331 return TokError("expected identifier in directive");
2332
2333 if (getLexer().isNot(AsmToken::EndOfStatement))
2334 return TokError("unexpected token in '.macro' directive");
2335
2336 // Eat the end of statement.
2337 Lex();
2338
2339 AsmToken EndToken, StartToken = getTok();
2340
2341 // Lex the macro definition.
2342 for (;;) {
2343 // Check whether we have reached the end of the file.
2344 if (getLexer().is(AsmToken::Eof))
2345 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2346
2347 // Otherwise, check whether we have reach the .endmacro.
2348 if (getLexer().is(AsmToken::Identifier) &&
2349 (getTok().getIdentifier() == ".endm" ||
2350 getTok().getIdentifier() == ".endmacro")) {
2351 EndToken = getTok();
2352 Lex();
2353 if (getLexer().isNot(AsmToken::EndOfStatement))
2354 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2355 "' directive");
2356 break;
2357 }
2358
2359 // Otherwise, scan til the end of the statement.
2360 getParser().EatToEndOfStatement();
2361 }
2362
2363 if (getParser().MacroMap.lookup(Name)) {
2364 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2365 }
2366
2367 const char *BodyStart = StartToken.getLoc().getPointer();
2368 const char *BodyEnd = EndToken.getLoc().getPointer();
2369 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2370 getParser().MacroMap[Name] = new Macro(Name, Body);
2371 return false;
2372}
2373
2374/// ParseDirectiveEndMacro
2375/// ::= .endm
2376/// ::= .endmacro
2377bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2378 SMLoc DirectiveLoc) {
2379 if (getLexer().isNot(AsmToken::EndOfStatement))
2380 return TokError("unexpected token in '" + Directive + "' directive");
2381
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002382 // If we are inside a macro instantiation, terminate the current
2383 // instantiation.
2384 if (!getParser().ActiveMacros.empty()) {
2385 getParser().HandleMacroExit();
2386 return false;
2387 }
2388
2389 // Otherwise, this .endmacro is a stray entry in the file; well formed
2390 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002391 return TokError("unexpected '" + Directive + "' in file, "
2392 "no current macro definition");
2393}
2394
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002395bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002396 getParser().CheckForValidSection();
2397
2398 const MCExpr *Value;
2399
2400 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002401 return true;
2402
2403 if (getLexer().isNot(AsmToken::EndOfStatement))
2404 return TokError("unexpected token in directive");
2405
2406 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002407 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002408 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002409 getStreamer().EmitULEB128Value(Value);
2410
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002411 return false;
2412}
2413
2414
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002415/// \brief Create an MCAsmParser instance.
2416MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2417 MCContext &C, MCStreamer &Out,
2418 const MCAsmInfo &MAI) {
2419 return new AsmParser(T, SM, C, Out, MAI);
2420}