blob: 76c309e02765eec24a912933958ce55e218a2d04 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000034#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000036using namespace llvm;
37
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000038namespace {
39
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000040/// \brief Helper class for tracking macro definitions.
41struct Macro {
42 StringRef Name;
43 StringRef Body;
44
45public:
46 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
47};
48
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000049/// \brief Helper class for storing information about an active macro
50/// instantiation.
51struct MacroInstantiation {
52 /// The macro being instantiated.
53 const Macro *TheMacro;
54
55 /// The macro instantiation with substitutions.
56 MemoryBuffer *Instantiation;
57
58 /// The location of the instantiation.
59 SMLoc InstantiationLoc;
60
61 /// The location where parsing should resume upon instantiation completion.
62 SMLoc ExitLoc;
63
64public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000065 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
66 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000067};
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069/// \brief The concrete assembly parser instance.
70class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000071 friend class GenericAsmParser;
72
Daniel Dunbaraef87e32010-07-18 18:31:38 +000073 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
74 void operator=(const AsmParser &); // DO NOT IMPLEMENT
75private:
76 AsmLexer Lexer;
77 MCContext &Ctx;
78 MCStreamer &Out;
79 SourceMgr &SrcMgr;
80 MCAsmParserExtension *GenericParser;
81 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 /// This is the current buffer index we're lexing from as managed by the
84 /// SourceMgr object.
85 int CurBuffer;
86
87 AsmCond TheCondState;
88 std::vector<AsmCond> TheCondStack;
89
90 /// DirectiveMap - This is a table handlers for directives. Each handler is
91 /// invoked after the directive identifier is read and is responsible for
92 /// parsing and validating the rest of the directive. The handler is passed
93 /// in the directive name and the location of the directive keyword.
94 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000095
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000096 /// MacroMap - Map of currently defined macros.
97 StringMap<Macro*> MacroMap;
98
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000099 /// ActiveMacros - Stack of active macro instantiations.
100 std::vector<MacroInstantiation*> ActiveMacros;
101
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000102 /// Boolean tracking whether macro substitution is enabled.
103 unsigned MacrosEnabled : 1;
104
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000105 /// Flag tracking whether any errors have been encountered.
106 unsigned HadError : 1;
107
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000108public:
109 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
110 const MCAsmInfo &MAI);
111 ~AsmParser();
112
113 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
114
115 void AddDirectiveHandler(MCAsmParserExtension *Object,
116 StringRef Directive,
117 DirectiveHandler Handler) {
118 DirectiveMap[Directive] = std::make_pair(Object, Handler);
119 }
120
121public:
122 /// @name MCAsmParser Interface
123 /// {
124
125 virtual SourceMgr &getSourceManager() { return SrcMgr; }
126 virtual MCAsmLexer &getLexer() { return Lexer; }
127 virtual MCContext &getContext() { return Ctx; }
128 virtual MCStreamer &getStreamer() { return Out; }
129
130 virtual void Warning(SMLoc L, const Twine &Meg);
131 virtual bool Error(SMLoc L, const Twine &Msg);
132
133 const AsmToken &Lex();
134
135 bool ParseExpression(const MCExpr *&Res);
136 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseAbsoluteExpression(int64_t &Res);
139
140 /// }
141
142private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000143 void CheckForValidSection();
144
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 bool ParseStatement();
146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000147 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
148 void HandleMacroExit();
149
150 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000151 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
152 SrcMgr.PrintMessage(Loc, Msg, Type);
153 }
154
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000155 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
156 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000157
158 /// \brief Reset the current lexer position to that given by \arg Loc. The
159 /// current token is not set; clients should ensure Lex() is called
160 /// subsequently.
161 void JumpToLoc(SMLoc Loc);
162
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000164
165 /// \brief Parse up to the end of statement and a return the contents from the
166 /// current token until the end of the statement; the current token on exit
167 /// will be either the EndOfStatement or EOF.
168 StringRef ParseStringToEndOfStatement();
169
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000170 bool ParseAssignment(StringRef Name);
171
172 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
175
176 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
177 /// and set \arg Res to the identifier contents.
178 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000181
182 // ".ascii", ".asciiz", ".string"
183 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000185 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186 bool ParseDirectiveFill(); // ".fill"
187 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000188 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000189 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190 bool ParseDirectiveOrg(); // ".org"
191 // ".align{,32}", ".p2align{,w,l}"
192 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
193
194 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
195 /// accepts a single symbol (which should be a label or an external).
196 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197
198 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
199
200 bool ParseDirectiveAbort(); // ".abort"
201 bool ParseDirectiveInclude(); // ".include"
202
203 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
204 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
205 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
206 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
207
208 /// ParseEscapedString - Parse the current token as a string which may include
209 /// escaped characters and return the string contents.
210 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000211
212 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
213 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214};
215
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000216/// \brief Generic implementations of directive handling, etc. which is shared
217/// (or the default, at least) for all assembler parser.
218class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000219 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
220 void AddDirectiveHandler(StringRef Directive) {
221 getParser().AddDirectiveHandler(this, Directive,
222 HandleDirective<GenericAsmParser, Handler>);
223 }
224
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000225public:
226 GenericAsmParser() {}
227
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000228 AsmParser &getParser() {
229 return (AsmParser&) this->MCAsmParserExtension::getParser();
230 }
231
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000232 virtual void Initialize(MCAsmParser &Parser) {
233 // Call the base implementation.
234 this->MCAsmParserExtension::Initialize(Parser);
235
236 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000241
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000242 // CFI directives.
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
244 ".cfi_startproc");
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
246 ".cfi_endproc");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
248 ".cfi_def_cfa_offset");
249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
250 ".cfi_def_cfa_register");
251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
252 ".cfi_offset");
253 AddDirectiveHandler<
254 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
255 AddDirectiveHandler<
256 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000257 AddDirectiveHandler<
258 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
259 AddDirectiveHandler<
260 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000261
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000262 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
264 ".macros_on");
265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
266 ".macros_off");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000270
271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000273 }
274
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000275 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
276 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
277 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000278 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000279 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
280 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
281 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
282 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
283 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
284 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000285 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
286 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000287
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000288 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000289 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
290 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000291
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000292 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000293};
294
295}
296
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000297namespace llvm {
298
299extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000300extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000301extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000302
303}
304
Chris Lattneraaec2052010-01-19 19:46:13 +0000305enum { DEFAULT_ADDRSPACE = 0 };
306
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000307AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
308 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000309 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000310 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000311 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000312 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000313
314 // Initialize the generic parser.
315 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000316
317 // Initialize the platform / file format parser.
318 //
319 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
320 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000321 if (_MAI.hasMicrosoftFastStdCallMangling()) {
322 PlatformParser = createCOFFAsmParser();
323 PlatformParser->Initialize(*this);
324 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000325 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000326 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000327 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000328 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000329 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000330 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000331}
332
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000333AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000334 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
335
336 // Destroy any macros.
337 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
338 ie = MacroMap.end(); it != ie; ++it)
339 delete it->getValue();
340
Daniel Dunbare4749702010-07-12 18:12:02 +0000341 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000342 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000343}
344
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000345void AsmParser::PrintMacroInstantiations() {
346 // Print the active macro instantiation stack.
347 for (std::vector<MacroInstantiation*>::const_reverse_iterator
348 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
349 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
350 "note");
351}
352
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000353void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000354 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000355 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000356}
357
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000358bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000359 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000360 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000361 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000362 return true;
363}
364
Sean Callananfd0b0282010-01-21 00:19:58 +0000365bool AsmParser::EnterIncludeFile(const std::string &Filename) {
366 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
367 if (NewBuf == -1)
368 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000369
Sean Callananfd0b0282010-01-21 00:19:58 +0000370 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000371
Sean Callananfd0b0282010-01-21 00:19:58 +0000372 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000373
Sean Callananfd0b0282010-01-21 00:19:58 +0000374 return false;
375}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000376
377void AsmParser::JumpToLoc(SMLoc Loc) {
378 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
379 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
380}
381
Sean Callananfd0b0282010-01-21 00:19:58 +0000382const AsmToken &AsmParser::Lex() {
383 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000384
Sean Callananfd0b0282010-01-21 00:19:58 +0000385 if (tok->is(AsmToken::Eof)) {
386 // If this is the end of an included file, pop the parent file off the
387 // include stack.
388 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
389 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000390 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000391 tok = &Lexer.Lex();
392 }
393 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000394
Sean Callananfd0b0282010-01-21 00:19:58 +0000395 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000396 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000397
Sean Callananfd0b0282010-01-21 00:19:58 +0000398 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000399}
400
Chris Lattner79180e22010-04-05 23:15:42 +0000401bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000402 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000403 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000404 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000405
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000406 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000407 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000408
409 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000410 AsmCond StartingCondState = TheCondState;
411
Chris Lattnerb717fb02009-07-02 21:53:43 +0000412 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000413 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000414 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000415
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000416 // We had an error, validate that one was emitted and recover by skipping to
417 // the next line.
418 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000419 EatToEndOfStatement();
420 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000421
422 if (TheCondState.TheCond != StartingCondState.TheCond ||
423 TheCondState.Ignore != StartingCondState.Ignore)
424 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000425
426 // Check to see there are no empty DwarfFile slots.
427 const std::vector<MCDwarfFile *> &MCDwarfFiles =
428 getContext().getMCDwarfFiles();
429 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000430 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000431 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000432 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000433
Chris Lattner79180e22010-04-05 23:15:42 +0000434 // Finalize the output stream if there are no errors and if the client wants
435 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000436 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000437 Out.Finish();
438
Chris Lattnerb717fb02009-07-02 21:53:43 +0000439 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000440}
441
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000442void AsmParser::CheckForValidSection() {
443 if (!getStreamer().getCurrentSection()) {
444 TokError("expected section directive before assembly directive");
445 Out.SwitchSection(Ctx.getMachOSection(
446 "__TEXT", "__text",
447 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
448 0, SectionKind::getText()));
449 }
450}
451
Chris Lattner2cf5f142009-06-22 01:29:09 +0000452/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
453void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000454 while (Lexer.isNot(AsmToken::EndOfStatement) &&
455 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000456 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000457
Chris Lattner2cf5f142009-06-22 01:29:09 +0000458 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000459 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000460 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000461}
462
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000463StringRef AsmParser::ParseStringToEndOfStatement() {
464 const char *Start = getTok().getLoc().getPointer();
465
466 while (Lexer.isNot(AsmToken::EndOfStatement) &&
467 Lexer.isNot(AsmToken::Eof))
468 Lex();
469
470 const char *End = getTok().getLoc().getPointer();
471 return StringRef(Start, End - Start);
472}
Chris Lattnerc4193832009-06-22 05:51:26 +0000473
Chris Lattner74ec1a32009-06-22 06:32:03 +0000474/// ParseParenExpr - Parse a paren expression and return it.
475/// NOTE: This assumes the leading '(' has already been consumed.
476///
477/// parenexpr ::= expr)
478///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000479bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000480 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000481 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000482 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000483 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000484 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000485 return false;
486}
Chris Lattnerc4193832009-06-22 05:51:26 +0000487
Chris Lattner74ec1a32009-06-22 06:32:03 +0000488/// ParsePrimaryExpr - Parse a primary expression and return it.
489/// primaryexpr ::= (parenexpr
490/// primaryexpr ::= symbol
491/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000492/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000493/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000494bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000495 switch (Lexer.getKind()) {
496 default:
497 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000498 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000499 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000500 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000501 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000502 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000503 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000504 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000505 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000506 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000507 EndLoc = Lexer.getLoc();
508
509 StringRef Identifier;
510 if (ParseIdentifier(Identifier))
511 return false;
512
Daniel Dunbarfffff912009-10-16 01:34:54 +0000513 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000514 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000515 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000516
517 // Lookup the symbol variant if used.
518 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000519 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000520 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000521 if (Variant == MCSymbolRefExpr::VK_Invalid) {
522 Variant = MCSymbolRefExpr::VK_None;
523 TokError("invalid variant '" + Split.second + "'");
524 }
525 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000526
Daniel Dunbarfffff912009-10-16 01:34:54 +0000527 // If this is an absolute variable reference, substitute it now to preserve
528 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000529 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000530 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000531 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000532
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000533 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000534 return false;
535 }
536
537 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000538 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000539 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000540 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000541 case AsmToken::Integer: {
542 SMLoc Loc = getTok().getLoc();
543 int64_t IntVal = getTok().getIntVal();
544 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000545 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000546 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000547 // Look for 'b' or 'f' following an Integer as a directional label
548 if (Lexer.getKind() == AsmToken::Identifier) {
549 StringRef IDVal = getTok().getString();
550 if (IDVal == "f" || IDVal == "b"){
551 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
552 IDVal == "f" ? 1 : 0);
553 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
554 getContext());
555 if(IDVal == "b" && Sym->isUndefined())
556 return Error(Loc, "invalid reference to undefined symbol");
557 EndLoc = Lexer.getLoc();
558 Lex(); // Eat identifier.
559 }
560 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000561 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000562 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000563 case AsmToken::Dot: {
564 // This is a '.' reference, which references the current PC. Emit a
565 // temporary label to the streamer and refer to it.
566 MCSymbol *Sym = Ctx.CreateTempSymbol();
567 Out.EmitLabel(Sym);
568 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
569 EndLoc = Lexer.getLoc();
570 Lex(); // Eat identifier.
571 return false;
572 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000573
Daniel Dunbar3f872332009-07-28 16:08:33 +0000574 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000575 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000576 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000577 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000578 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000579 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000581 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000582 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000583 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000584 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000585 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000586 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000587 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000588 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000589 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000590 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000591 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000592 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000593 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000594 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000595 }
596}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000597
Chris Lattnerb4307b32010-01-15 19:28:38 +0000598bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000599 SMLoc EndLoc;
600 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000601}
602
Daniel Dunbarcceba832010-09-17 02:47:07 +0000603const MCExpr *
604AsmParser::ApplyModifierToExpr(const MCExpr *E,
605 MCSymbolRefExpr::VariantKind Variant) {
606 // Recurse over the given expression, rebuilding it to apply the given variant
607 // if there is exactly one symbol.
608 switch (E->getKind()) {
609 case MCExpr::Target:
610 case MCExpr::Constant:
611 return 0;
612
613 case MCExpr::SymbolRef: {
614 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
615
616 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
617 TokError("invalid variant on expression '" +
618 getTok().getIdentifier() + "' (already modified)");
619 return E;
620 }
621
622 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
623 }
624
625 case MCExpr::Unary: {
626 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
627 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
628 if (!Sub)
629 return 0;
630 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
631 }
632
633 case MCExpr::Binary: {
634 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
635 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
636 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
637
638 if (!LHS && !RHS)
639 return 0;
640
641 if (!LHS) LHS = BE->getLHS();
642 if (!RHS) RHS = BE->getRHS();
643
644 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
645 }
646 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000647
648 assert(0 && "Invalid expression kind!");
649 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000650}
651
Chris Lattner74ec1a32009-06-22 06:32:03 +0000652/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000653///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000654/// expr ::= expr +,- expr -> lowest.
655/// expr ::= expr |,^,&,! expr -> middle.
656/// expr ::= expr *,/,%,<<,>> expr -> highest.
657/// expr ::= primaryexpr
658///
Chris Lattner54482b42010-01-15 19:39:23 +0000659bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000660 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000661 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000662 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
663 return true;
664
Daniel Dunbarcceba832010-09-17 02:47:07 +0000665 // As a special case, we support 'a op b @ modifier' by rewriting the
666 // expression to include the modifier. This is inefficient, but in general we
667 // expect users to use 'a@modifier op b'.
668 if (Lexer.getKind() == AsmToken::At) {
669 Lex();
670
671 if (Lexer.isNot(AsmToken::Identifier))
672 return TokError("unexpected symbol modifier following '@'");
673
674 MCSymbolRefExpr::VariantKind Variant =
675 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
676 if (Variant == MCSymbolRefExpr::VK_Invalid)
677 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
678
679 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
680 if (!ModifiedRes) {
681 return TokError("invalid modifier '" + getTok().getIdentifier() +
682 "' (no symbols present)");
683 return true;
684 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000685
Daniel Dunbarcceba832010-09-17 02:47:07 +0000686 Res = ModifiedRes;
687 Lex();
688 }
689
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000690 // Try to constant fold it up front, if possible.
691 int64_t Value;
692 if (Res->EvaluateAsAbsolute(Value))
693 Res = MCConstantExpr::Create(Value, getContext());
694
695 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000696}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000697
Chris Lattnerb4307b32010-01-15 19:28:38 +0000698bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000699 Res = 0;
700 return ParseParenExpr(Res, EndLoc) ||
701 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000702}
703
Daniel Dunbar475839e2009-06-29 20:37:27 +0000704bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000705 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000706
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000707 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000708 if (ParseExpression(Expr))
709 return true;
710
Daniel Dunbare00b0112009-10-16 01:57:52 +0000711 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000712 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000713
714 return false;
715}
716
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000717static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000718 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000719 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000720 default:
721 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000722
Daniel Dunbarcceba832010-09-17 02:47:07 +0000723 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000724 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000725 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000726 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000727 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000728 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000729 return 1;
730
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000731
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000732 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000733 //
734 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000735 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000736 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000737 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000738 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000739 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000740 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000741 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000742 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000743 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000744
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000745 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000746 case AsmToken::EqualEqual:
747 Kind = MCBinaryExpr::EQ;
748 return 3;
749 case AsmToken::ExclaimEqual:
750 case AsmToken::LessGreater:
751 Kind = MCBinaryExpr::NE;
752 return 3;
753 case AsmToken::Less:
754 Kind = MCBinaryExpr::LT;
755 return 3;
756 case AsmToken::LessEqual:
757 Kind = MCBinaryExpr::LTE;
758 return 3;
759 case AsmToken::Greater:
760 Kind = MCBinaryExpr::GT;
761 return 3;
762 case AsmToken::GreaterEqual:
763 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000764 return 3;
765
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000766 // High Intermediate Precedence: +, -
767 case AsmToken::Plus:
768 Kind = MCBinaryExpr::Add;
769 return 4;
770 case AsmToken::Minus:
771 Kind = MCBinaryExpr::Sub;
772 return 4;
773
Daniel Dunbar475839e2009-06-29 20:37:27 +0000774 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000775 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000776 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000777 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000779 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000780 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000782 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000783 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000784 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000785 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000786 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000788 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000789 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000790 }
791}
792
793
794/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
795/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000796bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
797 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000798 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000799 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000800 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000801
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000802 // If the next token is lower precedence than we are allowed to eat, return
803 // successfully with what we ate already.
804 if (TokPrec < Precedence)
805 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000806
Sean Callanan79ed1a82010-01-19 20:22:31 +0000807 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000808
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000809 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000810 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000811 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000812
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000813 // If BinOp binds less tightly with RHS than the operator after RHS, let
814 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000815 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000816 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000817 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000818 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000819 }
820
Daniel Dunbar475839e2009-06-29 20:37:27 +0000821 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000822 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000823 }
824}
825
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000826
827
828
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000829/// ParseStatement:
830/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000831/// ::= Label* Directive ...Operands... EndOfStatement
832/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000833bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000834 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000835 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000836 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000837 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000838 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000839
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000840 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000841 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000842 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000843 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000844 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000845 // A full line comment is a '#' as the first token.
846 if (Lexer.is(AsmToken::Hash)) {
847 EatToEndOfStatement();
848 return false;
849 }
850 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000851 if (Lexer.is(AsmToken::Integer)) {
852 LocalLabelVal = getTok().getIntVal();
853 if (LocalLabelVal < 0) {
854 if (!TheCondState.Ignore)
855 return TokError("unexpected token at start of statement");
856 IDVal = "";
857 }
858 else {
859 IDVal = getTok().getString();
860 Lex(); // Consume the integer token to be used as an identifier token.
861 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000862 if (!TheCondState.Ignore)
863 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000864 }
865 }
866 }
867 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000868 if (!TheCondState.Ignore)
869 return TokError("unexpected token at start of statement");
870 IDVal = "";
871 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000872
Chris Lattner7834fac2010-04-17 18:14:27 +0000873 // Handle conditional assembly here before checking for skipping. We
874 // have to do this so that .endif isn't skipped in a ".if 0" block for
875 // example.
876 if (IDVal == ".if")
877 return ParseDirectiveIf(IDLoc);
878 if (IDVal == ".elseif")
879 return ParseDirectiveElseIf(IDLoc);
880 if (IDVal == ".else")
881 return ParseDirectiveElse(IDLoc);
882 if (IDVal == ".endif")
883 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000884
Chris Lattner7834fac2010-04-17 18:14:27 +0000885 // If we are in a ".if 0" block, ignore this statement.
886 if (TheCondState.Ignore) {
887 EatToEndOfStatement();
888 return false;
889 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000890
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000891 // FIXME: Recurse on local labels?
892
893 // See what kind of statement we have.
894 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000895 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000896 CheckForValidSection();
897
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000898 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000899 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000900
901 // Diagnose attempt to use a variable as a label.
902 //
903 // FIXME: Diagnostics. Note the location of the definition as a label.
904 // FIXME: This doesn't diagnose assignment to a symbol which has been
905 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000906 MCSymbol *Sym;
907 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000908 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000909 else
910 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000911 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000912 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000913
Daniel Dunbar959fd882009-08-26 22:13:22 +0000914 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000915 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000916
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000917 // Consume any end of statement token, if present, to avoid spurious
918 // AddBlankLine calls().
919 if (Lexer.is(AsmToken::EndOfStatement)) {
920 Lex();
921 if (Lexer.is(AsmToken::Eof))
922 return false;
923 }
924
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000925 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000926 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000927
Daniel Dunbar3f872332009-07-28 16:08:33 +0000928 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000929 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000930 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000931
Daniel Dunbare2ace502009-08-31 08:09:09 +0000932 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000933
934 default: // Normal instruction or directive.
935 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000936 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000937
938 // If macros are enabled, check to see if this is a macro instantiation.
939 if (MacrosEnabled)
940 if (const Macro *M = MacroMap.lookup(IDVal))
941 return HandleMacroEntry(IDVal, IDLoc, M);
942
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000943 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000944 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000945 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000946 if (IDVal == ".set" || IDVal == ".equ")
947 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000948
Daniel Dunbara0d14262009-06-24 23:30:00 +0000949 // Data directives
950
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000951 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000952 return ParseDirectiveAscii(IDVal, false);
953 if (IDVal == ".asciz" || IDVal == ".string")
954 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000955
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000956 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000957 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000958 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000960 if (IDVal == ".value")
961 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000962 if (IDVal == ".2byte")
963 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000964 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000965 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000966 if (IDVal == ".int")
967 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000968 if (IDVal == ".4byte")
969 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000970 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000971 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000972 if (IDVal == ".8byte")
973 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000974 if (IDVal == ".single")
975 return ParseDirectiveRealValue(APFloat::IEEEsingle);
976 if (IDVal == ".double")
977 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000978
Eli Friedman5d68ec22010-07-19 04:17:25 +0000979 if (IDVal == ".align") {
980 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
981 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
982 }
983 if (IDVal == ".align32") {
984 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
985 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
986 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000987 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000988 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000990 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000992 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000993 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000994 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000995 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000996 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000997 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000998 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
999
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001000 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001001 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001002
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001003 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001004 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001005 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001006 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001007 if (IDVal == ".zero")
1008 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001009
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001010 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001011
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001012 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001013 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001014 // ELF only? Should it be here?
1015 if (IDVal == ".local")
1016 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001017 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001018 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001019 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001020 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001021 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001022 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001023 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001024 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001025 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001026 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001027 if (IDVal == ".symbol_resolver")
1028 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001029 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001030 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001032 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001033 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001034 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001035 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001036 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001037 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001038 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001039 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001040 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001041 if (IDVal == ".weak_def_can_be_hidden")
1042 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001043
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001044 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001045 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001046 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001047 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001048
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001049 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001050 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001051 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001052 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001053
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001054 // Look up the handler in the handler table.
1055 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1056 DirectiveMap.lookup(IDVal);
1057 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001058 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001059
Kevin Enderby9c656452009-09-10 20:51:44 +00001060 // Target hook for parsing target specific directives.
1061 if (!getTargetParser().ParseDirective(ID))
1062 return false;
1063
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001064 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001065 EatToEndOfStatement();
1066 return false;
1067 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001068
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001069 CheckForValidSection();
1070
Chris Lattnera7f13542010-05-19 23:34:33 +00001071 // Canonicalize the opcode to lower case.
1072 SmallString<128> Opcode;
1073 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1074 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001075
Chris Lattner98986712010-01-14 22:21:20 +00001076 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001077 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001078 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001079
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001080 // Dump the parsed representation, if requested.
1081 if (getShowParsedOperands()) {
1082 SmallString<256> Str;
1083 raw_svector_ostream OS(Str);
1084 OS << "parsed instruction: [";
1085 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1086 if (i != 0)
1087 OS << ", ";
1088 ParsedOperands[i]->dump(OS);
1089 }
1090 OS << "]";
1091
1092 PrintMessage(IDLoc, OS.str(), "note");
1093 }
1094
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001095 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001096 if (!HadError)
1097 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1098 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001099
Chris Lattner98986712010-01-14 22:21:20 +00001100 // Free any parsed operands.
1101 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1102 delete ParsedOperands[i];
1103
Chris Lattnercbf8a982010-09-11 16:18:25 +00001104 // Don't skip the rest of the line, the instruction parser is responsible for
1105 // that.
1106 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001107}
Chris Lattner9a023f72009-06-24 04:43:34 +00001108
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001109MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1110 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001111 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1112{
1113 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1114 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001115 SmallString<256> Buf;
1116 raw_svector_ostream OS(Buf);
1117
1118 StringRef Body = M->Body;
1119 while (!Body.empty()) {
1120 // Scan for the next substitution.
1121 std::size_t End = Body.size(), Pos = 0;
1122 for (; Pos != End; ++Pos) {
1123 // Check for a substitution or escape.
1124 if (Body[Pos] != '$' || Pos + 1 == End)
1125 continue;
1126
1127 char Next = Body[Pos + 1];
1128 if (Next == '$' || Next == 'n' || isdigit(Next))
1129 break;
1130 }
1131
1132 // Add the prefix.
1133 OS << Body.slice(0, Pos);
1134
1135 // Check if we reached the end.
1136 if (Pos == End)
1137 break;
1138
1139 switch (Body[Pos+1]) {
1140 // $$ => $
1141 case '$':
1142 OS << '$';
1143 break;
1144
1145 // $n => number of arguments
1146 case 'n':
1147 OS << A.size();
1148 break;
1149
1150 // $[0-9] => argument
1151 default: {
1152 // Missing arguments are ignored.
1153 unsigned Index = Body[Pos+1] - '0';
1154 if (Index >= A.size())
1155 break;
1156
1157 // Otherwise substitute with the token values, with spaces eliminated.
1158 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1159 ie = A[Index].end(); it != ie; ++it)
1160 OS << it->getString();
1161 break;
1162 }
1163 }
1164
1165 // Update the scan point.
1166 Body = Body.substr(Pos + 2);
1167 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001168
1169 // We include the .endmacro in the buffer as our queue to exit the macro
1170 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001171 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001172
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001173 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001174}
1175
1176bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1177 const Macro *M) {
1178 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1179 // this, although we should protect against infinite loops.
1180 if (ActiveMacros.size() == 20)
1181 return TokError("macros cannot be nested more than 20 levels deep");
1182
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001183 // Parse the macro instantiation arguments.
1184 std::vector<std::vector<AsmToken> > MacroArguments;
1185 MacroArguments.push_back(std::vector<AsmToken>());
1186 unsigned ParenLevel = 0;
1187 for (;;) {
1188 if (Lexer.is(AsmToken::Eof))
1189 return TokError("unexpected token in macro instantiation");
1190 if (Lexer.is(AsmToken::EndOfStatement))
1191 break;
1192
1193 // If we aren't inside parentheses and this is a comma, start a new token
1194 // list.
1195 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1196 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001197 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001198 // Adjust the current parentheses level.
1199 if (Lexer.is(AsmToken::LParen))
1200 ++ParenLevel;
1201 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1202 --ParenLevel;
1203
1204 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001205 MacroArguments.back().push_back(getTok());
1206 }
1207 Lex();
1208 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001209
1210 // Create the macro instantiation object and add to the current macro
1211 // instantiation stack.
1212 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001213 getTok().getLoc(),
1214 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001215 ActiveMacros.push_back(MI);
1216
1217 // Jump to the macro instantiation and prime the lexer.
1218 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1219 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1220 Lex();
1221
1222 return false;
1223}
1224
1225void AsmParser::HandleMacroExit() {
1226 // Jump to the EndOfStatement we should return to, and consume it.
1227 JumpToLoc(ActiveMacros.back()->ExitLoc);
1228 Lex();
1229
1230 // Pop the instantiation entry.
1231 delete ActiveMacros.back();
1232 ActiveMacros.pop_back();
1233}
1234
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001235static void MarkUsed(const MCExpr *Value) {
1236 switch (Value->getKind()) {
1237 case MCExpr::Binary:
1238 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1239 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1240 break;
1241 case MCExpr::Target:
1242 case MCExpr::Constant:
1243 break;
1244 case MCExpr::SymbolRef: {
1245 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1246 break;
1247 }
1248 case MCExpr::Unary:
1249 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1250 break;
1251 }
1252}
1253
Benjamin Kramer38e59892010-07-14 22:38:02 +00001254bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001255 // FIXME: Use better location, we should use proper tokens.
1256 SMLoc EqualLoc = Lexer.getLoc();
1257
Daniel Dunbar821e3332009-08-31 08:09:28 +00001258 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001259 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001260 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001261
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001262 MarkUsed(Value);
1263
Daniel Dunbar3f872332009-07-28 16:08:33 +00001264 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001265 return TokError("unexpected token in assignment");
1266
1267 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001268 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001269
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001270 // Validate that the LHS is allowed to be a variable (either it has not been
1271 // used as a symbol, or it is an absolute symbol).
1272 MCSymbol *Sym = getContext().LookupSymbol(Name);
1273 if (Sym) {
1274 // Diagnose assignment to a label.
1275 //
1276 // FIXME: Diagnostics. Note the location of the definition as a label.
1277 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001278 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001279 ; // Allow redefinitions of undefined symbols only used in directives.
1280 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001281 return Error(EqualLoc, "redefinition of '" + Name + "'");
1282 else if (!Sym->isVariable())
1283 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001284 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001285 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1286 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001287
1288 // Don't count these checks as uses.
1289 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001290 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001291 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001292
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001293 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001294
1295 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001296 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001297
1298 return false;
1299}
1300
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001301/// ParseIdentifier:
1302/// ::= identifier
1303/// ::= string
1304bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001305 // The assembler has relaxed rules for accepting identifiers, in particular we
1306 // allow things like '.globl $foo', which would normally be separate
1307 // tokens. At this level, we have already lexed so we cannot (currently)
1308 // handle this as a context dependent token, instead we detect adjacent tokens
1309 // and return the combined identifier.
1310 if (Lexer.is(AsmToken::Dollar)) {
1311 SMLoc DollarLoc = getLexer().getLoc();
1312
1313 // Consume the dollar sign, and check for a following identifier.
1314 Lex();
1315 if (Lexer.isNot(AsmToken::Identifier))
1316 return true;
1317
1318 // We have a '$' followed by an identifier, make sure they are adjacent.
1319 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1320 return true;
1321
1322 // Construct the joined identifier and consume the token.
1323 Res = StringRef(DollarLoc.getPointer(),
1324 getTok().getIdentifier().size() + 1);
1325 Lex();
1326 return false;
1327 }
1328
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001329 if (Lexer.isNot(AsmToken::Identifier) &&
1330 Lexer.isNot(AsmToken::String))
1331 return true;
1332
Sean Callanan18b83232010-01-19 21:44:56 +00001333 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001334
Sean Callanan79ed1a82010-01-19 20:22:31 +00001335 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001336
1337 return false;
1338}
1339
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001340/// ParseDirectiveSet:
1341/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001342bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001343 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001344
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001345 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001346 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001347
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001348 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001349 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001350 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001351
Daniel Dunbare2ace502009-08-31 08:09:09 +00001352 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001353}
1354
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001355bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001357
1358 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001359 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001360 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1361 if (Str[i] != '\\') {
1362 Data += Str[i];
1363 continue;
1364 }
1365
1366 // Recognize escaped characters. Note that this escape semantics currently
1367 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1368 ++i;
1369 if (i == e)
1370 return TokError("unexpected backslash at end of string");
1371
1372 // Recognize octal sequences.
1373 if ((unsigned) (Str[i] - '0') <= 7) {
1374 // Consume up to three octal characters.
1375 unsigned Value = Str[i] - '0';
1376
1377 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1378 ++i;
1379 Value = Value * 8 + (Str[i] - '0');
1380
1381 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1382 ++i;
1383 Value = Value * 8 + (Str[i] - '0');
1384 }
1385 }
1386
1387 if (Value > 255)
1388 return TokError("invalid octal escape sequence (out of range)");
1389
1390 Data += (unsigned char) Value;
1391 continue;
1392 }
1393
1394 // Otherwise recognize individual escapes.
1395 switch (Str[i]) {
1396 default:
1397 // Just reject invalid escape sequences for now.
1398 return TokError("invalid escape sequence (unrecognized character)");
1399
1400 case 'b': Data += '\b'; break;
1401 case 'f': Data += '\f'; break;
1402 case 'n': Data += '\n'; break;
1403 case 'r': Data += '\r'; break;
1404 case 't': Data += '\t'; break;
1405 case '"': Data += '"'; break;
1406 case '\\': Data += '\\'; break;
1407 }
1408 }
1409
1410 return false;
1411}
1412
Daniel Dunbara0d14262009-06-24 23:30:00 +00001413/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001414/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1415bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001416 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001417 CheckForValidSection();
1418
Daniel Dunbara0d14262009-06-24 23:30:00 +00001419 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001420 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001421 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001422
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001423 std::string Data;
1424 if (ParseEscapedString(Data))
1425 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001426
1427 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001428 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001429 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1430
Sean Callanan79ed1a82010-01-19 20:22:31 +00001431 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001432
1433 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001434 break;
1435
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001436 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001437 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001438 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001439 }
1440 }
1441
Sean Callanan79ed1a82010-01-19 20:22:31 +00001442 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001443 return false;
1444}
1445
1446/// ParseDirectiveValue
1447/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1448bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001449 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001450 CheckForValidSection();
1451
Daniel Dunbara0d14262009-06-24 23:30:00 +00001452 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001453 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001454 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001455 return true;
1456
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001457 // Special case constant expressions to match code generator.
1458 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001459 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001460 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001461 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001462
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001463 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001464 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001465
Daniel Dunbara0d14262009-06-24 23:30:00 +00001466 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001467 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001468 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001469 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001470 }
1471 }
1472
Sean Callanan79ed1a82010-01-19 20:22:31 +00001473 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001474 return false;
1475}
1476
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001477/// ParseDirectiveRealValue
1478/// ::= (.single | .double) [ expression (, expression)* ]
1479bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1480 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1481 CheckForValidSection();
1482
1483 for (;;) {
1484 // We don't truly support arithmetic on floating point expressions, so we
1485 // have to manually parse unary prefixes.
1486 bool IsNeg = false;
1487 if (getLexer().is(AsmToken::Minus)) {
1488 Lex();
1489 IsNeg = true;
1490 } else if (getLexer().is(AsmToken::Plus))
1491 Lex();
1492
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001493 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001494 getLexer().isNot(AsmToken::Real))
1495 return TokError("unexpected token in directive");
1496
1497 // Convert to an APFloat.
1498 APFloat Value(Semantics);
1499 if (Value.convertFromString(getTok().getString(),
1500 APFloat::rmNearestTiesToEven) ==
1501 APFloat::opInvalidOp)
1502 return TokError("invalid floating point literal");
1503 if (IsNeg)
1504 Value.changeSign();
1505
1506 // Consume the numeric token.
1507 Lex();
1508
1509 // Emit the value as an integer.
1510 APInt AsInt = Value.bitcastToAPInt();
1511 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1512 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1513
1514 if (getLexer().is(AsmToken::EndOfStatement))
1515 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001516
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001517 if (getLexer().isNot(AsmToken::Comma))
1518 return TokError("unexpected token in directive");
1519 Lex();
1520 }
1521 }
1522
1523 Lex();
1524 return false;
1525}
1526
Daniel Dunbara0d14262009-06-24 23:30:00 +00001527/// ParseDirectiveSpace
1528/// ::= .space expression [ , expression ]
1529bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001530 CheckForValidSection();
1531
Daniel Dunbara0d14262009-06-24 23:30:00 +00001532 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001533 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001534 return true;
1535
1536 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001537 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1538 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001539 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001540 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001541
Daniel Dunbar475839e2009-06-29 20:37:27 +00001542 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001543 return true;
1544
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001545 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001546 return TokError("unexpected token in '.space' directive");
1547 }
1548
Sean Callanan79ed1a82010-01-19 20:22:31 +00001549 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001550
1551 if (NumBytes <= 0)
1552 return TokError("invalid number of bytes in '.space' directive");
1553
1554 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001556
1557 return false;
1558}
1559
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001560/// ParseDirectiveZero
1561/// ::= .zero expression
1562bool AsmParser::ParseDirectiveZero() {
1563 CheckForValidSection();
1564
1565 int64_t NumBytes;
1566 if (ParseAbsoluteExpression(NumBytes))
1567 return true;
1568
Rafael Espindolae452b172010-10-05 19:42:57 +00001569 int64_t Val = 0;
1570 if (getLexer().is(AsmToken::Comma)) {
1571 Lex();
1572 if (ParseAbsoluteExpression(Val))
1573 return true;
1574 }
1575
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001576 if (getLexer().isNot(AsmToken::EndOfStatement))
1577 return TokError("unexpected token in '.zero' directive");
1578
1579 Lex();
1580
Rafael Espindolae452b172010-10-05 19:42:57 +00001581 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001582
1583 return false;
1584}
1585
Daniel Dunbara0d14262009-06-24 23:30:00 +00001586/// ParseDirectiveFill
1587/// ::= .fill expression , expression , expression
1588bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001589 CheckForValidSection();
1590
Daniel Dunbara0d14262009-06-24 23:30:00 +00001591 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001592 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001593 return true;
1594
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001595 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001596 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001597 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001598
Daniel Dunbara0d14262009-06-24 23:30:00 +00001599 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001600 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001601 return true;
1602
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001603 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001604 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001605 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001606
Daniel Dunbara0d14262009-06-24 23:30:00 +00001607 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001608 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001609 return true;
1610
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001611 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001612 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001613
Sean Callanan79ed1a82010-01-19 20:22:31 +00001614 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001615
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001616 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1617 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001618
1619 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001620 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001621
1622 return false;
1623}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001624
1625/// ParseDirectiveOrg
1626/// ::= .org expression [ , expression ]
1627bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001628 CheckForValidSection();
1629
Daniel Dunbar821e3332009-08-31 08:09:28 +00001630 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001631 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001632 return true;
1633
1634 // Parse optional fill expression.
1635 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1637 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001638 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001640
Daniel Dunbar475839e2009-06-29 20:37:27 +00001641 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001642 return true;
1643
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001644 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001645 return TokError("unexpected token in '.org' directive");
1646 }
1647
Sean Callanan79ed1a82010-01-19 20:22:31 +00001648 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001649
1650 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1651 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001652 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001653
1654 return false;
1655}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001656
1657/// ParseDirectiveAlign
1658/// ::= {.align, ...} expression [ , expression [ , expression ]]
1659bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001660 CheckForValidSection();
1661
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001662 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001663 int64_t Alignment;
1664 if (ParseAbsoluteExpression(Alignment))
1665 return true;
1666
1667 SMLoc MaxBytesLoc;
1668 bool HasFillExpr = false;
1669 int64_t FillExpr = 0;
1670 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001671 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1672 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001673 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001674 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001675
1676 // The fill expression can be omitted while specifying a maximum number of
1677 // alignment bytes, e.g:
1678 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001680 HasFillExpr = true;
1681 if (ParseAbsoluteExpression(FillExpr))
1682 return true;
1683 }
1684
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001685 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1686 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001687 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001688 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001689
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001690 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001691 if (ParseAbsoluteExpression(MaxBytesToFill))
1692 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001693
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001695 return TokError("unexpected token in directive");
1696 }
1697 }
1698
Sean Callanan79ed1a82010-01-19 20:22:31 +00001699 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001700
Daniel Dunbar648ac512010-05-17 21:54:30 +00001701 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001702 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001703
1704 // Compute alignment in bytes.
1705 if (IsPow2) {
1706 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001707 if (Alignment >= 32) {
1708 Error(AlignmentLoc, "invalid alignment value");
1709 Alignment = 31;
1710 }
1711
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001712 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001713 }
1714
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001715 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001716 if (MaxBytesLoc.isValid()) {
1717 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001718 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1719 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001720 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001721 }
1722
1723 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001724 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1725 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001726 MaxBytesToFill = 0;
1727 }
1728 }
1729
Daniel Dunbar648ac512010-05-17 21:54:30 +00001730 // Check whether we should use optimal code alignment for this .align
1731 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001732 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001733 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1734 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001735 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001736 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001737 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001738 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1739 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001740 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001741
1742 return false;
1743}
1744
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001745/// ParseDirectiveSymbolAttribute
1746/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001747bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001748 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001749 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001750 StringRef Name;
1751
1752 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001753 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001754
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001755 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001756
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001757 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001758
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001760 break;
1761
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001762 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001763 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001764 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001765 }
1766 }
1767
Sean Callanan79ed1a82010-01-19 20:22:31 +00001768 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001769 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001770}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001771
1772/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001773/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1774bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001775 CheckForValidSection();
1776
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001777 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001778 StringRef Name;
1779 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001780 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001781
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001782 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001783 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001784
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001785 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001786 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001787 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001788
1789 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001790 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001791 if (ParseAbsoluteExpression(Size))
1792 return true;
1793
1794 int64_t Pow2Alignment = 0;
1795 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001796 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001797 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001798 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001799 if (ParseAbsoluteExpression(Pow2Alignment))
1800 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001801
Chris Lattner258281d2010-01-19 06:22:22 +00001802 // If this target takes alignments in bytes (not log) validate and convert.
1803 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1804 if (!isPowerOf2_64(Pow2Alignment))
1805 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1806 Pow2Alignment = Log2_64(Pow2Alignment);
1807 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001808 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001809
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001810 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001811 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001812
Sean Callanan79ed1a82010-01-19 20:22:31 +00001813 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001814
Chris Lattner1fc3d752009-07-09 17:25:12 +00001815 // NOTE: a size of zero for a .comm should create a undefined symbol
1816 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001817 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001818 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1819 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001820
Eric Christopherc260a3e2010-05-14 01:38:54 +00001821 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001822 // may internally end up wanting an alignment in bytes.
1823 // FIXME: Diagnose overflow.
1824 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001825 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1826 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001827
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001828 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001829 return Error(IDLoc, "invalid symbol redefinition");
1830
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001831 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001832 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001833 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001834 getStreamer().EmitZerofill(Ctx.getMachOSection(
1835 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1836 0, SectionKind::getBSS()),
1837 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001838 return false;
1839 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001840
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001841 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001842 return false;
1843}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001844
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001845/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001846/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001847bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001848 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001849 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001850
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001851 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001852 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001853 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001854
Sean Callanan79ed1a82010-01-19 20:22:31 +00001855 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001856
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001857 if (Str.empty())
1858 Error(Loc, ".abort detected. Assembly stopping.");
1859 else
1860 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001861 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001862
1863 return false;
1864}
Kevin Enderby71148242009-07-14 21:35:03 +00001865
Kevin Enderby1f049b22009-07-14 23:21:55 +00001866/// ParseDirectiveInclude
1867/// ::= .include "filename"
1868bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001869 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001870 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001871
Sean Callanan18b83232010-01-19 21:44:56 +00001872 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001873 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001874 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001875
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001876 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001877 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001878
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001879 // Strip the quotes.
1880 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001881
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001882 // Attempt to switch the lexer to the included file before consuming the end
1883 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001884 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001885 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001886 return true;
1887 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001888
1889 return false;
1890}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001891
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001892/// ParseDirectiveIf
1893/// ::= .if expression
1894bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001895 TheCondStack.push_back(TheCondState);
1896 TheCondState.TheCond = AsmCond::IfCond;
1897 if(TheCondState.Ignore) {
1898 EatToEndOfStatement();
1899 }
1900 else {
1901 int64_t ExprValue;
1902 if (ParseAbsoluteExpression(ExprValue))
1903 return true;
1904
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001905 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001906 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001907
Sean Callanan79ed1a82010-01-19 20:22:31 +00001908 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001909
1910 TheCondState.CondMet = ExprValue;
1911 TheCondState.Ignore = !TheCondState.CondMet;
1912 }
1913
1914 return false;
1915}
1916
1917/// ParseDirectiveElseIf
1918/// ::= .elseif expression
1919bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1920 if (TheCondState.TheCond != AsmCond::IfCond &&
1921 TheCondState.TheCond != AsmCond::ElseIfCond)
1922 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1923 " an .elseif");
1924 TheCondState.TheCond = AsmCond::ElseIfCond;
1925
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001926 bool LastIgnoreState = false;
1927 if (!TheCondStack.empty())
1928 LastIgnoreState = TheCondStack.back().Ignore;
1929 if (LastIgnoreState || TheCondState.CondMet) {
1930 TheCondState.Ignore = true;
1931 EatToEndOfStatement();
1932 }
1933 else {
1934 int64_t ExprValue;
1935 if (ParseAbsoluteExpression(ExprValue))
1936 return true;
1937
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001938 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001939 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001940
Sean Callanan79ed1a82010-01-19 20:22:31 +00001941 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001942 TheCondState.CondMet = ExprValue;
1943 TheCondState.Ignore = !TheCondState.CondMet;
1944 }
1945
1946 return false;
1947}
1948
1949/// ParseDirectiveElse
1950/// ::= .else
1951bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001952 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001953 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001954
Sean Callanan79ed1a82010-01-19 20:22:31 +00001955 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001956
1957 if (TheCondState.TheCond != AsmCond::IfCond &&
1958 TheCondState.TheCond != AsmCond::ElseIfCond)
1959 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1960 ".elseif");
1961 TheCondState.TheCond = AsmCond::ElseCond;
1962 bool LastIgnoreState = false;
1963 if (!TheCondStack.empty())
1964 LastIgnoreState = TheCondStack.back().Ignore;
1965 if (LastIgnoreState || TheCondState.CondMet)
1966 TheCondState.Ignore = true;
1967 else
1968 TheCondState.Ignore = false;
1969
1970 return false;
1971}
1972
1973/// ParseDirectiveEndIf
1974/// ::= .endif
1975bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001976 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001977 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001978
Sean Callanan79ed1a82010-01-19 20:22:31 +00001979 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001980
1981 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1982 TheCondStack.empty())
1983 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1984 ".else");
1985 if (!TheCondStack.empty()) {
1986 TheCondState = TheCondStack.back();
1987 TheCondStack.pop_back();
1988 }
1989
1990 return false;
1991}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001992
1993/// ParseDirectiveFile
1994/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001995bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001996 // FIXME: I'm not sure what this is.
1997 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001998 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001999 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002000 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002001 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002002
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002003 if (FileNumber < 1)
2004 return TokError("file number less than one");
2005 }
2006
Daniel Dunbareceec052010-07-12 17:45:27 +00002007 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002008 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002009
Chris Lattnerd32e8032010-01-25 19:02:58 +00002010 StringRef Filename = getTok().getString();
2011 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002012 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002013
Daniel Dunbareceec052010-07-12 17:45:27 +00002014 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002015 return TokError("unexpected token in '.file' directive");
2016
Chris Lattnerd32e8032010-01-25 19:02:58 +00002017 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002018 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002019 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002020 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002021 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002022 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002023
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002024 return false;
2025}
2026
2027/// ParseDirectiveLine
2028/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002029bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002030 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2031 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002032 return TokError("unexpected token in '.line' directive");
2033
Sean Callanan18b83232010-01-19 21:44:56 +00002034 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002035 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002036 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002037
2038 // FIXME: Do something with the .line.
2039 }
2040
Daniel Dunbareceec052010-07-12 17:45:27 +00002041 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002042 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002043
2044 return false;
2045}
2046
2047
2048/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002049/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002050/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2051/// The first number is a file number, must have been previously assigned with
2052/// a .file directive, the second number is the line number and optionally the
2053/// third number is a column position (zero if not specified). The remaining
2054/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002055bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002056
Daniel Dunbareceec052010-07-12 17:45:27 +00002057 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002058 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002059 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002060 if (FileNumber < 1)
2061 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002062 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002063 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002064 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002065
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002066 int64_t LineNumber = 0;
2067 if (getLexer().is(AsmToken::Integer)) {
2068 LineNumber = getTok().getIntVal();
2069 if (LineNumber < 1)
2070 return TokError("line number less than one in '.loc' directive");
2071 Lex();
2072 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002073
2074 int64_t ColumnPos = 0;
2075 if (getLexer().is(AsmToken::Integer)) {
2076 ColumnPos = getTok().getIntVal();
2077 if (ColumnPos < 0)
2078 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002079 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002080 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002081
Kevin Enderbyc0957932010-09-30 16:52:03 +00002082 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002083 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002084 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002085 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2086 for (;;) {
2087 if (getLexer().is(AsmToken::EndOfStatement))
2088 break;
2089
2090 StringRef Name;
2091 SMLoc Loc = getTok().getLoc();
2092 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002093 return TokError("unexpected token in '.loc' directive");
2094
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002095 if (Name == "basic_block")
2096 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2097 else if (Name == "prologue_end")
2098 Flags |= DWARF2_FLAG_PROLOGUE_END;
2099 else if (Name == "epilogue_begin")
2100 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2101 else if (Name == "is_stmt") {
2102 SMLoc Loc = getTok().getLoc();
2103 const MCExpr *Value;
2104 if (getParser().ParseExpression(Value))
2105 return true;
2106 // The expression must be the constant 0 or 1.
2107 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2108 int Value = MCE->getValue();
2109 if (Value == 0)
2110 Flags &= ~DWARF2_FLAG_IS_STMT;
2111 else if (Value == 1)
2112 Flags |= DWARF2_FLAG_IS_STMT;
2113 else
2114 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002115 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002116 else {
2117 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2118 }
2119 }
2120 else if (Name == "isa") {
2121 SMLoc Loc = getTok().getLoc();
2122 const MCExpr *Value;
2123 if (getParser().ParseExpression(Value))
2124 return true;
2125 // The expression must be a constant greater or equal to 0.
2126 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2127 int Value = MCE->getValue();
2128 if (Value < 0)
2129 return Error(Loc, "isa number less than zero");
2130 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002131 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002132 else {
2133 return Error(Loc, "isa number not a constant value");
2134 }
2135 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002136 else if (Name == "discriminator") {
2137 if (getParser().ParseAbsoluteExpression(Discriminator))
2138 return true;
2139 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002140 else {
2141 return Error(Loc, "unknown sub-directive in '.loc' directive");
2142 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002143
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002144 if (getLexer().is(AsmToken::EndOfStatement))
2145 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002146 }
2147 }
2148
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002149 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2150 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002151
2152 return false;
2153}
2154
Daniel Dunbar138abae2010-10-16 04:56:42 +00002155/// ParseDirectiveStabs
2156/// ::= .stabs string, number, number, number
2157bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2158 SMLoc DirectiveLoc) {
2159 return TokError("unsupported directive '" + Directive + "'");
2160}
2161
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002162/// ParseDirectiveCFIStartProc
2163/// ::= .cfi_startproc
2164bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2165 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002166 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002167}
2168
2169/// ParseDirectiveCFIEndProc
2170/// ::= .cfi_endproc
2171bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002172 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002173}
2174
2175/// ParseDirectiveCFIDefCfaOffset
2176/// ::= .cfi_def_cfa_offset offset
2177bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2178 SMLoc DirectiveLoc) {
2179 int64_t Offset = 0;
2180 if (getParser().ParseAbsoluteExpression(Offset))
2181 return true;
2182
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002183 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002184}
2185
2186/// ParseDirectiveCFIDefCfaRegister
2187/// ::= .cfi_def_cfa_register register
2188bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2189 SMLoc DirectiveLoc) {
2190 int64_t Register = 0;
2191 if (getParser().ParseAbsoluteExpression(Register))
2192 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002193
2194 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002195}
2196
2197/// ParseDirectiveCFIOffset
2198/// ::= .cfi_off register, offset
2199bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2200 int64_t Register = 0;
2201 int64_t Offset = 0;
2202 if (getParser().ParseAbsoluteExpression(Register))
2203 return true;
2204
2205 if (getLexer().isNot(AsmToken::Comma))
2206 return TokError("unexpected token in directive");
2207 Lex();
2208
2209 if (getParser().ParseAbsoluteExpression(Offset))
2210 return true;
2211
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002212 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002213}
2214
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002215static bool isValidEncoding(int64_t Encoding) {
2216 if (Encoding & ~0xff)
2217 return false;
2218
2219 if (Encoding == dwarf::DW_EH_PE_omit)
2220 return true;
2221
2222 const unsigned Format = Encoding & 0xf;
2223 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2224 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2225 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2226 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2227 return false;
2228
2229 const unsigned Application = Encoding & 0xf0;
2230 if (Application != dwarf::DW_EH_PE_absptr &&
2231 Application != dwarf::DW_EH_PE_pcrel &&
2232 Application != dwarf::DW_EH_PE_indirect)
2233 return false;
2234
2235 return true;
2236}
2237
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002238/// ParseDirectiveCFIPersonalityOrLsda
2239/// ::= .cfi_personality encoding, [symbol_name]
2240/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002241bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002242 SMLoc DirectiveLoc) {
2243 int64_t Encoding = 0;
2244 if (getParser().ParseAbsoluteExpression(Encoding))
2245 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002246 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002247 return false;
2248
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002249 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002250 return TokError("unsupported encoding.");
2251
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002252 if (getLexer().isNot(AsmToken::Comma))
2253 return TokError("unexpected token in directive");
2254 Lex();
2255
2256 StringRef Name;
2257 if (getParser().ParseIdentifier(Name))
2258 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002259
2260 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2261
2262 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002263 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002264 else {
2265 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002266 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002267 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002268}
2269
Rafael Espindolafe024d02010-12-28 18:36:23 +00002270/// ParseDirectiveCFIRememberState
2271/// ::= .cfi_remember_state
2272bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2273 SMLoc DirectiveLoc) {
2274 return getStreamer().EmitCFIRememberState();
2275}
2276
2277/// ParseDirectiveCFIRestoreState
2278/// ::= .cfi_remember_state
2279bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2280 SMLoc DirectiveLoc) {
2281 return getStreamer().EmitCFIRestoreState();
2282}
2283
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002284/// ParseDirectiveMacrosOnOff
2285/// ::= .macros_on
2286/// ::= .macros_off
2287bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2288 SMLoc DirectiveLoc) {
2289 if (getLexer().isNot(AsmToken::EndOfStatement))
2290 return Error(getLexer().getLoc(),
2291 "unexpected token in '" + Directive + "' directive");
2292
2293 getParser().MacrosEnabled = Directive == ".macros_on";
2294
2295 return false;
2296}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002297
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002298/// ParseDirectiveMacro
2299/// ::= .macro name
2300bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2301 SMLoc DirectiveLoc) {
2302 StringRef Name;
2303 if (getParser().ParseIdentifier(Name))
2304 return TokError("expected identifier in directive");
2305
2306 if (getLexer().isNot(AsmToken::EndOfStatement))
2307 return TokError("unexpected token in '.macro' directive");
2308
2309 // Eat the end of statement.
2310 Lex();
2311
2312 AsmToken EndToken, StartToken = getTok();
2313
2314 // Lex the macro definition.
2315 for (;;) {
2316 // Check whether we have reached the end of the file.
2317 if (getLexer().is(AsmToken::Eof))
2318 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2319
2320 // Otherwise, check whether we have reach the .endmacro.
2321 if (getLexer().is(AsmToken::Identifier) &&
2322 (getTok().getIdentifier() == ".endm" ||
2323 getTok().getIdentifier() == ".endmacro")) {
2324 EndToken = getTok();
2325 Lex();
2326 if (getLexer().isNot(AsmToken::EndOfStatement))
2327 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2328 "' directive");
2329 break;
2330 }
2331
2332 // Otherwise, scan til the end of the statement.
2333 getParser().EatToEndOfStatement();
2334 }
2335
2336 if (getParser().MacroMap.lookup(Name)) {
2337 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2338 }
2339
2340 const char *BodyStart = StartToken.getLoc().getPointer();
2341 const char *BodyEnd = EndToken.getLoc().getPointer();
2342 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2343 getParser().MacroMap[Name] = new Macro(Name, Body);
2344 return false;
2345}
2346
2347/// ParseDirectiveEndMacro
2348/// ::= .endm
2349/// ::= .endmacro
2350bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2351 SMLoc DirectiveLoc) {
2352 if (getLexer().isNot(AsmToken::EndOfStatement))
2353 return TokError("unexpected token in '" + Directive + "' directive");
2354
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002355 // If we are inside a macro instantiation, terminate the current
2356 // instantiation.
2357 if (!getParser().ActiveMacros.empty()) {
2358 getParser().HandleMacroExit();
2359 return false;
2360 }
2361
2362 // Otherwise, this .endmacro is a stray entry in the file; well formed
2363 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002364 return TokError("unexpected '" + Directive + "' in file, "
2365 "no current macro definition");
2366}
2367
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002368bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002369 getParser().CheckForValidSection();
2370
2371 const MCExpr *Value;
2372
2373 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002374 return true;
2375
2376 if (getLexer().isNot(AsmToken::EndOfStatement))
2377 return TokError("unexpected token in directive");
2378
2379 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002380 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002381 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002382 getStreamer().EmitULEB128Value(Value);
2383
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002384 return false;
2385}
2386
2387
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002388/// \brief Create an MCAsmParser instance.
2389MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2390 MCContext &C, MCStreamer &Out,
2391 const MCAsmInfo &MAI) {
2392 return new AsmParser(T, SM, C, Out, MAI);
2393}