blob: a05f767328c8cab3b2a242654f559ef165a1c4f3 [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"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000028#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000032#include "llvm/Support/CommandLine.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
90 MCAsmParserExtension *GenericParser;
91 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000092
Daniel Dunbaraef87e32010-07-18 18:31:38 +000093 /// This is the current buffer index we're lexing from as managed by the
94 /// SourceMgr object.
95 int CurBuffer;
96
97 AsmCond TheCondState;
98 std::vector<AsmCond> TheCondStack;
99
100 /// DirectiveMap - This is a table handlers for directives. Each handler is
101 /// invoked after the directive identifier is read and is responsible for
102 /// parsing and validating the rest of the directive. The handler is passed
103 /// in the directive name and the location of the directive keyword.
104 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000105
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000106 /// MacroMap - Map of currently defined macros.
107 StringMap<Macro*> MacroMap;
108
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000109 /// ActiveMacros - Stack of active macro instantiations.
110 std::vector<MacroInstantiation*> ActiveMacros;
111
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000112 /// Boolean tracking whether macro substitution is enabled.
113 unsigned MacrosEnabled : 1;
114
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000115 /// Flag tracking whether any errors have been encountered.
116 unsigned HadError : 1;
117
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000118 /// The values from the last parsed cpp hash file line comment if any.
119 StringRef CppHashFilename;
120 int64_t CppHashLineNumber;
121 SMLoc CppHashLoc;
122
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000123public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000124 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000125 const MCAsmInfo &MAI);
126 ~AsmParser();
127
128 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
129
130 void AddDirectiveHandler(MCAsmParserExtension *Object,
131 StringRef Directive,
132 DirectiveHandler Handler) {
133 DirectiveMap[Directive] = std::make_pair(Object, Handler);
134 }
135
136public:
137 /// @name MCAsmParser Interface
138 /// {
139
140 virtual SourceMgr &getSourceManager() { return SrcMgr; }
141 virtual MCAsmLexer &getLexer() { return Lexer; }
142 virtual MCContext &getContext() { return Ctx; }
143 virtual MCStreamer &getStreamer() { return Out; }
144
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000145 virtual bool Warning(SMLoc L, const Twine &Msg,
146 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
147 virtual bool Error(SMLoc L, const Twine &Msg,
148 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000149
150 const AsmToken &Lex();
151
152 bool ParseExpression(const MCExpr *&Res);
153 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
154 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
155 virtual bool ParseAbsoluteExpression(int64_t &Res);
156
157 /// }
158
159private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000160 void CheckForValidSection();
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000163 void EatToEndOfLine();
164 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000165
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000166 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000167 bool expandMacro(SmallString<256> &Buf, StringRef Body,
168 const std::vector<StringRef> &Parameters,
169 const std::vector<std::vector<AsmToken> > &A,
170 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000171 void HandleMacroExit();
172
173 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000174 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000175 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
176 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000177 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000178 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
181 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000182
183 /// \brief Reset the current lexer position to that given by \arg Loc. The
184 /// current token is not set; clients should ensure Lex() is called
185 /// subsequently.
186 void JumpToLoc(SMLoc Loc);
187
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000189
190 /// \brief Parse up to the end of statement and a return the contents from the
191 /// current token until the end of the statement; the current token on exit
192 /// will be either the EndOfStatement or EOF.
193 StringRef ParseStringToEndOfStatement();
194
Nico Weber4c4c7322011-01-28 03:04:41 +0000195 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000196
197 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
198 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
199 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000200 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000201
202 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
203 /// and set \arg Res to the identifier contents.
204 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000205
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000206 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000207
208 // ".ascii", ".asciiz", ".string"
209 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000211 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212 bool ParseDirectiveFill(); // ".fill"
213 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000214 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000215 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000216 bool ParseDirectiveOrg(); // ".org"
217 // ".align{,32}", ".p2align{,w,l}"
218 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
219
220 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
221 /// accepts a single symbol (which should be a label or an external).
222 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000223
224 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
225
226 bool ParseDirectiveAbort(); // ".abort"
227 bool ParseDirectiveInclude(); // ".include"
228
229 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000230 // ".ifdef" or ".ifndef", depending on expect_defined
231 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000232 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
233 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
234 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
235
236 /// ParseEscapedString - Parse the current token as a string which may include
237 /// escaped characters and return the string contents.
238 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000239
240 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
241 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000242};
243
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000244/// \brief Generic implementations of directive handling, etc. which is shared
245/// (or the default, at least) for all assembler parser.
246class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000247 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
248 void AddDirectiveHandler(StringRef Directive) {
249 getParser().AddDirectiveHandler(this, Directive,
250 HandleDirective<GenericAsmParser, Handler>);
251 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000252public:
253 GenericAsmParser() {}
254
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000255 AsmParser &getParser() {
256 return (AsmParser&) this->MCAsmParserExtension::getParser();
257 }
258
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000259 virtual void Initialize(MCAsmParser &Parser) {
260 // Call the base implementation.
261 this->MCAsmParserExtension::Initialize(Parser);
262
263 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000268
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000269 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
271 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
273 ".cfi_startproc");
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
275 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
277 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
279 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
281 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
283 ".cfi_def_cfa_register");
284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
285 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
287 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000288 AddDirectiveHandler<
289 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
290 AddDirectiveHandler<
291 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000292 AddDirectiveHandler<
293 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
294 AddDirectiveHandler<
295 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000296 AddDirectiveHandler<
297 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000298
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000299 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
301 ".macros_on");
302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
303 ".macros_off");
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
305 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000307
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
309 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000310 }
311
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000312 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
313
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000314 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
315 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
316 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000317 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000318 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000319 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
320 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000321 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000322 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000323 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000324 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
325 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000326 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000327 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000328 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
329 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000330 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000331
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000332 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000333 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
334 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000335
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000336 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000337};
338
339}
340
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000341namespace llvm {
342
343extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000344extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000345extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000346
347}
348
Chris Lattneraaec2052010-01-19 19:46:13 +0000349enum { DEFAULT_ADDRSPACE = 0 };
350
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000351AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000352 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000353 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000354 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000355 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
356 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000357 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000358
359 // Initialize the generic parser.
360 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000361
362 // Initialize the platform / file format parser.
363 //
364 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
365 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000366 if (_MAI.hasMicrosoftFastStdCallMangling()) {
367 PlatformParser = createCOFFAsmParser();
368 PlatformParser->Initialize(*this);
369 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000370 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000371 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000372 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000373 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000374 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000375 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000376}
377
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000378AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000379 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
380
381 // Destroy any macros.
382 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
383 ie = MacroMap.end(); it != ie; ++it)
384 delete it->getValue();
385
Daniel Dunbare4749702010-07-12 18:12:02 +0000386 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000387 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000388}
389
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000390void AsmParser::PrintMacroInstantiations() {
391 // Print the active macro instantiation stack.
392 for (std::vector<MacroInstantiation*>::const_reverse_iterator
393 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000394 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
395 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000396}
397
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000398bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000399 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000400 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000401 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000402 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000403 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000404}
405
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000406bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000407 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000408 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000409 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000410 return true;
411}
412
Sean Callananfd0b0282010-01-21 00:19:58 +0000413bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000414 std::string IncludedFile;
415 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000416 if (NewBuf == -1)
417 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000418
Sean Callananfd0b0282010-01-21 00:19:58 +0000419 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000420
Sean Callananfd0b0282010-01-21 00:19:58 +0000421 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000422
Sean Callananfd0b0282010-01-21 00:19:58 +0000423 return false;
424}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000425
426void AsmParser::JumpToLoc(SMLoc Loc) {
427 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
428 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
429}
430
Sean Callananfd0b0282010-01-21 00:19:58 +0000431const AsmToken &AsmParser::Lex() {
432 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000433
Sean Callananfd0b0282010-01-21 00:19:58 +0000434 if (tok->is(AsmToken::Eof)) {
435 // If this is the end of an included file, pop the parent file off the
436 // include stack.
437 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
438 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000439 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000440 tok = &Lexer.Lex();
441 }
442 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000443
Sean Callananfd0b0282010-01-21 00:19:58 +0000444 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000445 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000446
Sean Callananfd0b0282010-01-21 00:19:58 +0000447 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000448}
449
Chris Lattner79180e22010-04-05 23:15:42 +0000450bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000451 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000452 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000453 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000454
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000455 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000456 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000457
458 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000459 AsmCond StartingCondState = TheCondState;
460
Chris Lattnerb717fb02009-07-02 21:53:43 +0000461 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000463 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000464
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000465 // We had an error, validate that one was emitted and recover by skipping to
466 // the next line.
467 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000468 EatToEndOfStatement();
469 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000470
471 if (TheCondState.TheCond != StartingCondState.TheCond ||
472 TheCondState.Ignore != StartingCondState.Ignore)
473 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000474
475 // Check to see there are no empty DwarfFile slots.
476 const std::vector<MCDwarfFile *> &MCDwarfFiles =
477 getContext().getMCDwarfFiles();
478 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000479 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000480 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000481 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000482
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000483 // Check to see that all assembler local symbols were actually defined.
484 // Targets that don't do subsections via symbols may not want this, though,
485 // so conservatively exclude them. Only do this if we're finalizing, though,
486 // as otherwise we won't necessarilly have seen everything yet.
487 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
488 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
489 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
490 e = Symbols.end();
491 i != e; ++i) {
492 MCSymbol *Sym = i->getValue();
493 // Variable symbols may not be marked as defined, so check those
494 // explicitly. If we know it's a variable, we have a definition for
495 // the purposes of this check.
496 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
497 // FIXME: We would really like to refer back to where the symbol was
498 // first referenced for a source location. We need to add something
499 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000500 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
501 "assembler local symbol '" + Sym->getName() +
502 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000503 }
504 }
505
506
Chris Lattner79180e22010-04-05 23:15:42 +0000507 // Finalize the output stream if there are no errors and if the client wants
508 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000509 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000510 Out.Finish();
511
Chris Lattnerb717fb02009-07-02 21:53:43 +0000512 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000513}
514
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000515void AsmParser::CheckForValidSection() {
516 if (!getStreamer().getCurrentSection()) {
517 TokError("expected section directive before assembly directive");
518 Out.SwitchSection(Ctx.getMachOSection(
519 "__TEXT", "__text",
520 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
521 0, SectionKind::getText()));
522 }
523}
524
Chris Lattner2cf5f142009-06-22 01:29:09 +0000525/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
526void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000527 while (Lexer.isNot(AsmToken::EndOfStatement) &&
528 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000529 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000530
Chris Lattner2cf5f142009-06-22 01:29:09 +0000531 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000532 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000533 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000534}
535
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000536StringRef AsmParser::ParseStringToEndOfStatement() {
537 const char *Start = getTok().getLoc().getPointer();
538
539 while (Lexer.isNot(AsmToken::EndOfStatement) &&
540 Lexer.isNot(AsmToken::Eof))
541 Lex();
542
543 const char *End = getTok().getLoc().getPointer();
544 return StringRef(Start, End - Start);
545}
Chris Lattnerc4193832009-06-22 05:51:26 +0000546
Chris Lattner74ec1a32009-06-22 06:32:03 +0000547/// ParseParenExpr - Parse a paren expression and return it.
548/// NOTE: This assumes the leading '(' has already been consumed.
549///
550/// parenexpr ::= expr)
551///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000552bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000553 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000554 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000555 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000556 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000557 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000558 return false;
559}
Chris Lattnerc4193832009-06-22 05:51:26 +0000560
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000561/// ParseBracketExpr - Parse a bracket expression and return it.
562/// NOTE: This assumes the leading '[' has already been consumed.
563///
564/// bracketexpr ::= expr]
565///
566bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
567 if (ParseExpression(Res)) return true;
568 if (Lexer.isNot(AsmToken::RBrac))
569 return TokError("expected ']' in brackets expression");
570 EndLoc = Lexer.getLoc();
571 Lex();
572 return false;
573}
574
Chris Lattner74ec1a32009-06-22 06:32:03 +0000575/// ParsePrimaryExpr - Parse a primary expression and return it.
576/// primaryexpr ::= (parenexpr
577/// primaryexpr ::= symbol
578/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000579/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000580/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000581bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000582 switch (Lexer.getKind()) {
583 default:
584 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000585 // If we have an error assume that we've already handled it.
586 case AsmToken::Error:
587 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000588 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000589 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000590 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000591 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000592 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000593 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000594 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000595 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000596 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000597 EndLoc = Lexer.getLoc();
598
599 StringRef Identifier;
600 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000601 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000602
Daniel Dunbarfffff912009-10-16 01:34:54 +0000603 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000604 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000605 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000606
607 // Lookup the symbol variant if used.
608 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000609 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000610 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000611 if (Variant == MCSymbolRefExpr::VK_Invalid) {
612 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000613 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000614 }
615 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000616
Daniel Dunbarfffff912009-10-16 01:34:54 +0000617 // If this is an absolute variable reference, substitute it now to preserve
618 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000619 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000620 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000621 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000622
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000623 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000624 return false;
625 }
626
627 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000628 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000629 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000630 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000631 case AsmToken::Integer: {
632 SMLoc Loc = getTok().getLoc();
633 int64_t IntVal = getTok().getIntVal();
634 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000635 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000636 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000637 // Look for 'b' or 'f' following an Integer as a directional label
638 if (Lexer.getKind() == AsmToken::Identifier) {
639 StringRef IDVal = getTok().getString();
640 if (IDVal == "f" || IDVal == "b"){
641 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
642 IDVal == "f" ? 1 : 0);
643 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
644 getContext());
645 if(IDVal == "b" && Sym->isUndefined())
646 return Error(Loc, "invalid reference to undefined symbol");
647 EndLoc = Lexer.getLoc();
648 Lex(); // Eat identifier.
649 }
650 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000651 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000652 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000653 case AsmToken::Real: {
654 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000655 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000656 Res = MCConstantExpr::Create(IntVal, getContext());
657 Lex(); // Eat token.
658 return false;
659 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000660 case AsmToken::Dot: {
661 // This is a '.' reference, which references the current PC. Emit a
662 // temporary label to the streamer and refer to it.
663 MCSymbol *Sym = Ctx.CreateTempSymbol();
664 Out.EmitLabel(Sym);
665 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
666 EndLoc = Lexer.getLoc();
667 Lex(); // Eat identifier.
668 return false;
669 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000670 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000671 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000672 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000673 case AsmToken::LBrac:
674 if (!PlatformParser->HasBracketExpressions())
675 return TokError("brackets expression not supported on this target");
676 Lex(); // Eat the '['.
677 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000678 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000679 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000680 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000681 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000682 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000683 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000684 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000685 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000686 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000687 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000688 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000689 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000690 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000691 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000692 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000693 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000694 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000695 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000696 }
697}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000698
Chris Lattnerb4307b32010-01-15 19:28:38 +0000699bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000700 SMLoc EndLoc;
701 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000702}
703
Daniel Dunbarcceba832010-09-17 02:47:07 +0000704const MCExpr *
705AsmParser::ApplyModifierToExpr(const MCExpr *E,
706 MCSymbolRefExpr::VariantKind Variant) {
707 // Recurse over the given expression, rebuilding it to apply the given variant
708 // if there is exactly one symbol.
709 switch (E->getKind()) {
710 case MCExpr::Target:
711 case MCExpr::Constant:
712 return 0;
713
714 case MCExpr::SymbolRef: {
715 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
716
717 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
718 TokError("invalid variant on expression '" +
719 getTok().getIdentifier() + "' (already modified)");
720 return E;
721 }
722
723 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
724 }
725
726 case MCExpr::Unary: {
727 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
728 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
729 if (!Sub)
730 return 0;
731 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
732 }
733
734 case MCExpr::Binary: {
735 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
736 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
737 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
738
739 if (!LHS && !RHS)
740 return 0;
741
742 if (!LHS) LHS = BE->getLHS();
743 if (!RHS) RHS = BE->getRHS();
744
745 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
746 }
747 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000748
749 assert(0 && "Invalid expression kind!");
750 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000751}
752
Chris Lattner74ec1a32009-06-22 06:32:03 +0000753/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000754///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000755/// expr ::= expr &&,|| expr -> lowest.
756/// expr ::= expr |,^,&,! expr
757/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
758/// expr ::= expr <<,>> expr
759/// expr ::= expr +,- expr
760/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000761/// expr ::= primaryexpr
762///
Chris Lattner54482b42010-01-15 19:39:23 +0000763bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000764 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000765 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000766 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
767 return true;
768
Daniel Dunbarcceba832010-09-17 02:47:07 +0000769 // As a special case, we support 'a op b @ modifier' by rewriting the
770 // expression to include the modifier. This is inefficient, but in general we
771 // expect users to use 'a@modifier op b'.
772 if (Lexer.getKind() == AsmToken::At) {
773 Lex();
774
775 if (Lexer.isNot(AsmToken::Identifier))
776 return TokError("unexpected symbol modifier following '@'");
777
778 MCSymbolRefExpr::VariantKind Variant =
779 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
780 if (Variant == MCSymbolRefExpr::VK_Invalid)
781 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
782
783 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
784 if (!ModifiedRes) {
785 return TokError("invalid modifier '" + getTok().getIdentifier() +
786 "' (no symbols present)");
787 return true;
788 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000789
Daniel Dunbarcceba832010-09-17 02:47:07 +0000790 Res = ModifiedRes;
791 Lex();
792 }
793
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000794 // Try to constant fold it up front, if possible.
795 int64_t Value;
796 if (Res->EvaluateAsAbsolute(Value))
797 Res = MCConstantExpr::Create(Value, getContext());
798
799 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000800}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000801
Chris Lattnerb4307b32010-01-15 19:28:38 +0000802bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000803 Res = 0;
804 return ParseParenExpr(Res, EndLoc) ||
805 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000806}
807
Daniel Dunbar475839e2009-06-29 20:37:27 +0000808bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000809 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000810
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000811 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000812 if (ParseExpression(Expr))
813 return true;
814
Daniel Dunbare00b0112009-10-16 01:57:52 +0000815 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000816 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000817
818 return false;
819}
820
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000821static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000822 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000823 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000824 default:
825 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000826
Jim Grosbachfbe16812011-08-20 16:24:13 +0000827 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000828 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000829 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000830 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000831 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000832 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000833 return 1;
834
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000835
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000836 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000837 //
838 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000839 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000840 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000841 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000843 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000844 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000846 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000847 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000848
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000849 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000850 case AsmToken::EqualEqual:
851 Kind = MCBinaryExpr::EQ;
852 return 3;
853 case AsmToken::ExclaimEqual:
854 case AsmToken::LessGreater:
855 Kind = MCBinaryExpr::NE;
856 return 3;
857 case AsmToken::Less:
858 Kind = MCBinaryExpr::LT;
859 return 3;
860 case AsmToken::LessEqual:
861 Kind = MCBinaryExpr::LTE;
862 return 3;
863 case AsmToken::Greater:
864 Kind = MCBinaryExpr::GT;
865 return 3;
866 case AsmToken::GreaterEqual:
867 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000868 return 3;
869
Jim Grosbachfbe16812011-08-20 16:24:13 +0000870 // Intermediate Precedence: <<, >>
871 case AsmToken::LessLess:
872 Kind = MCBinaryExpr::Shl;
873 return 4;
874 case AsmToken::GreaterGreater:
875 Kind = MCBinaryExpr::Shr;
876 return 4;
877
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000878 // High Intermediate Precedence: +, -
879 case AsmToken::Plus:
880 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000881 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000882 case AsmToken::Minus:
883 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000884 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000885
Jim Grosbachfbe16812011-08-20 16:24:13 +0000886 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000887 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000888 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000889 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000890 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000891 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000892 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000893 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000894 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000895 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000896 }
897}
898
899
900/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
901/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000902bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
903 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000904 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000905 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000906 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000907
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000908 // If the next token is lower precedence than we are allowed to eat, return
909 // successfully with what we ate already.
910 if (TokPrec < Precedence)
911 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000912
Sean Callanan79ed1a82010-01-19 20:22:31 +0000913 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000914
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000915 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000916 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000917 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000918
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000919 // If BinOp binds less tightly with RHS than the operator after RHS, let
920 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000921 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000922 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000923 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000924 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000925 }
926
Daniel Dunbar475839e2009-06-29 20:37:27 +0000927 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000928 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000929 }
930}
931
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000932
933
934
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000935/// ParseStatement:
936/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000937/// ::= Label* Directive ...Operands... EndOfStatement
938/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000939bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000940 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000941 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000942 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000943 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000944 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000945
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000946 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000947 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000948 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000949 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000950 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000951 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000952 if (Lexer.is(AsmToken::Hash))
953 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000954
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000955 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000956 if (Lexer.is(AsmToken::Integer)) {
957 LocalLabelVal = getTok().getIntVal();
958 if (LocalLabelVal < 0) {
959 if (!TheCondState.Ignore)
960 return TokError("unexpected token at start of statement");
961 IDVal = "";
962 }
963 else {
964 IDVal = getTok().getString();
965 Lex(); // Consume the integer token to be used as an identifier token.
966 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000967 if (!TheCondState.Ignore)
968 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000969 }
970 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000971
972 } else if (Lexer.is(AsmToken::Dot)) {
973 // Treat '.' as a valid identifier in this context.
974 Lex();
975 IDVal = ".";
976
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000977 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000978 if (!TheCondState.Ignore)
979 return TokError("unexpected token at start of statement");
980 IDVal = "";
981 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000982
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000983
Chris Lattner7834fac2010-04-17 18:14:27 +0000984 // Handle conditional assembly here before checking for skipping. We
985 // have to do this so that .endif isn't skipped in a ".if 0" block for
986 // example.
987 if (IDVal == ".if")
988 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000989 if (IDVal == ".ifdef")
990 return ParseDirectiveIfdef(IDLoc, true);
991 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
992 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000993 if (IDVal == ".elseif")
994 return ParseDirectiveElseIf(IDLoc);
995 if (IDVal == ".else")
996 return ParseDirectiveElse(IDLoc);
997 if (IDVal == ".endif")
998 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000999
Chris Lattner7834fac2010-04-17 18:14:27 +00001000 // If we are in a ".if 0" block, ignore this statement.
1001 if (TheCondState.Ignore) {
1002 EatToEndOfStatement();
1003 return false;
1004 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001005
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001006 // FIXME: Recurse on local labels?
1007
1008 // See what kind of statement we have.
1009 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001010 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001011 CheckForValidSection();
1012
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001013 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001014 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001015
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001016 // Diagnose attempt to use '.' as a label.
1017 if (IDVal == ".")
1018 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1019
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001020 // Diagnose attempt to use a variable as a label.
1021 //
1022 // FIXME: Diagnostics. Note the location of the definition as a label.
1023 // FIXME: This doesn't diagnose assignment to a symbol which has been
1024 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001025 MCSymbol *Sym;
1026 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001027 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001028 else
1029 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001030 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001031 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001032
Daniel Dunbar959fd882009-08-26 22:13:22 +00001033 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001034 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001035
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001036 // Consume any end of statement token, if present, to avoid spurious
1037 // AddBlankLine calls().
1038 if (Lexer.is(AsmToken::EndOfStatement)) {
1039 Lex();
1040 if (Lexer.is(AsmToken::Eof))
1041 return false;
1042 }
1043
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001044 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001045 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001046
Daniel Dunbar3f872332009-07-28 16:08:33 +00001047 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001048 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001049 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001050
Nico Weber4c4c7322011-01-28 03:04:41 +00001051 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001052
1053 default: // Normal instruction or directive.
1054 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001055 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001056
1057 // If macros are enabled, check to see if this is a macro instantiation.
1058 if (MacrosEnabled)
1059 if (const Macro *M = MacroMap.lookup(IDVal))
1060 return HandleMacroEntry(IDVal, IDLoc, M);
1061
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001062 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001063 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001064 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001065 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001066 return ParseDirectiveSet(IDVal, true);
1067 if (IDVal == ".equiv")
1068 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001069
Daniel Dunbara0d14262009-06-24 23:30:00 +00001070 // Data directives
1071
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001072 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001073 return ParseDirectiveAscii(IDVal, false);
1074 if (IDVal == ".asciz" || IDVal == ".string")
1075 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001077 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001079 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001081 if (IDVal == ".value")
1082 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001083 if (IDVal == ".2byte")
1084 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001085 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001087 if (IDVal == ".int")
1088 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001089 if (IDVal == ".4byte")
1090 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001091 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001093 if (IDVal == ".8byte")
1094 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001095 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001096 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1097 if (IDVal == ".double")
1098 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001099
Eli Friedman5d68ec22010-07-19 04:17:25 +00001100 if (IDVal == ".align") {
1101 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1102 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1103 }
1104 if (IDVal == ".align32") {
1105 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1106 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1107 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001108 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001109 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001110 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001111 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001112 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001113 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001114 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001115 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001116 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001117 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001118 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001119 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1120
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001121 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001122 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001123
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001125 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001126 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001128 if (IDVal == ".zero")
1129 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001131 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001132
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001133 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001134 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001135 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001136 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001137 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001138 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001139 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001140 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001141 if (IDVal == ".symbol_resolver")
1142 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001143 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001144 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001145 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001146 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001147 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001148 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001149 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001150 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001151 if (IDVal == ".weak_def_can_be_hidden")
1152 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001153
Hans Wennborg5cc64912011-06-18 13:51:54 +00001154 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001155 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001156 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001157 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001158
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001159 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001160 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001161 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001162 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001163
Evan Chengbd27f5a2011-07-27 00:38:12 +00001164 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001165 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001166
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001167 // Look up the handler in the handler table.
1168 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1169 DirectiveMap.lookup(IDVal);
1170 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001171 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001172
Kevin Enderby9c656452009-09-10 20:51:44 +00001173 // Target hook for parsing target specific directives.
1174 if (!getTargetParser().ParseDirective(ID))
1175 return false;
1176
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001177 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001178 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001179 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001180 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001181
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001182 CheckForValidSection();
1183
Chris Lattnera7f13542010-05-19 23:34:33 +00001184 // Canonicalize the opcode to lower case.
1185 SmallString<128> Opcode;
1186 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1187 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001188
Chris Lattner98986712010-01-14 22:21:20 +00001189 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001190 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001191 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001192
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001193 // Dump the parsed representation, if requested.
1194 if (getShowParsedOperands()) {
1195 SmallString<256> Str;
1196 raw_svector_ostream OS(Str);
1197 OS << "parsed instruction: [";
1198 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1199 if (i != 0)
1200 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001201 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001202 }
1203 OS << "]";
1204
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001205 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001206 }
1207
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001208 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001209 if (!HadError)
1210 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1211 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001212
Chris Lattner98986712010-01-14 22:21:20 +00001213 // Free any parsed operands.
1214 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1215 delete ParsedOperands[i];
1216
Chris Lattnercbf8a982010-09-11 16:18:25 +00001217 // Don't skip the rest of the line, the instruction parser is responsible for
1218 // that.
1219 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001220}
Chris Lattner9a023f72009-06-24 04:43:34 +00001221
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001222/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1223/// since they may not be able to be tokenized to get to the end of line token.
1224void AsmParser::EatToEndOfLine() {
1225 Lexer.LexUntilEndOfLine();
1226 // Eat EOL.
1227 Lex();
1228}
1229
1230/// ParseCppHashLineFilenameComment as this:
1231/// ::= # number "filename"
1232/// or just as a full line comment if it doesn't have a number and a string.
1233bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1234 Lex(); // Eat the hash token.
1235
1236 if (getLexer().isNot(AsmToken::Integer)) {
1237 // Consume the line since in cases it is not a well-formed line directive,
1238 // as if were simply a full line comment.
1239 EatToEndOfLine();
1240 return false;
1241 }
1242
1243 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001244 Lex();
1245
1246 if (getLexer().isNot(AsmToken::String)) {
1247 EatToEndOfLine();
1248 return false;
1249 }
1250
1251 StringRef Filename = getTok().getString();
1252 // Get rid of the enclosing quotes.
1253 Filename = Filename.substr(1, Filename.size()-2);
1254
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001255 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1256 CppHashLoc = L;
1257 CppHashFilename = Filename;
1258 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001259
1260 // Ignore any trailing characters, they're just comment.
1261 EatToEndOfLine();
1262 return false;
1263}
1264
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001265/// DiagHandler - will use the the last parsed cpp hash line filename comment
1266/// for the Filename and LineNo if any in the diagnostic.
1267void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1268 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1269 raw_ostream &OS = errs();
1270
1271 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1272 const SMLoc &DiagLoc = Diag.getLoc();
1273 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1274 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1275
1276 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1277 // before printing the message.
1278 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1279 if (DiagCurBuffer > 0) {
1280 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1281 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1282 }
1283
1284 // If we have not parsed a cpp hash line filename comment or the source
1285 // manager changed or buffer changed (like in a nested include) then just
1286 // print the normal diagnostic using its Filename and LineNo.
1287 if (!Parser->CppHashLineNumber ||
1288 &DiagSrcMgr != &Parser->SrcMgr ||
1289 DiagBuf != CppHashBuf) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001290 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001291 return;
1292 }
1293
1294 // Use the CppHashFilename and calculate a line number based on the
1295 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1296 // the diagnostic.
1297 const std::string Filename = Parser->CppHashFilename;
1298
1299 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1300 int CppHashLocLineNo =
1301 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1302 int LineNo = Parser->CppHashLineNumber - 1 +
1303 (DiagLocLineNo - CppHashLocLineNo);
1304
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001305 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1306 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001307 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001308 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001309
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001310 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001311}
1312
Rafael Espindola65366442011-06-05 02:43:45 +00001313bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1314 const std::vector<StringRef> &Parameters,
1315 const std::vector<std::vector<AsmToken> > &A,
1316 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001317 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001318 unsigned NParameters = Parameters.size();
1319 if (NParameters != 0 && NParameters != A.size())
1320 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001321
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001322 while (!Body.empty()) {
1323 // Scan for the next substitution.
1324 std::size_t End = Body.size(), Pos = 0;
1325 for (; Pos != End; ++Pos) {
1326 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001327 if (!NParameters) {
1328 // This macro has no parameters, look for $0, $1, etc.
1329 if (Body[Pos] != '$' || Pos + 1 == End)
1330 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001331
Rafael Espindola65366442011-06-05 02:43:45 +00001332 char Next = Body[Pos + 1];
1333 if (Next == '$' || Next == 'n' || isdigit(Next))
1334 break;
1335 } else {
1336 // This macro has parameters, look for \foo, \bar, etc.
1337 if (Body[Pos] == '\\' && Pos + 1 != End)
1338 break;
1339 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001340 }
1341
1342 // Add the prefix.
1343 OS << Body.slice(0, Pos);
1344
1345 // Check if we reached the end.
1346 if (Pos == End)
1347 break;
1348
Rafael Espindola65366442011-06-05 02:43:45 +00001349 if (!NParameters) {
1350 switch (Body[Pos+1]) {
1351 // $$ => $
1352 case '$':
1353 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001354 break;
1355
Rafael Espindola65366442011-06-05 02:43:45 +00001356 // $n => number of arguments
1357 case 'n':
1358 OS << A.size();
1359 break;
1360
1361 // $[0-9] => argument
1362 default: {
1363 // Missing arguments are ignored.
1364 unsigned Index = Body[Pos+1] - '0';
1365 if (Index >= A.size())
1366 break;
1367
1368 // Otherwise substitute with the token values, with spaces eliminated.
1369 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1370 ie = A[Index].end(); it != ie; ++it)
1371 OS << it->getString();
1372 break;
1373 }
1374 }
1375 Pos += 2;
1376 } else {
1377 unsigned I = Pos + 1;
1378 while (isalnum(Body[I]) && I + 1 != End)
1379 ++I;
1380
1381 const char *Begin = Body.data() + Pos +1;
1382 StringRef Argument(Begin, I - (Pos +1));
1383 unsigned Index = 0;
1384 for (; Index < NParameters; ++Index)
1385 if (Parameters[Index] == Argument)
1386 break;
1387
1388 // FIXME: We should error at the macro definition.
1389 if (Index == NParameters)
1390 return Error(L, "Parameter not found");
1391
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001392 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1393 ie = A[Index].end(); it != ie; ++it)
1394 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001395
Rafael Espindola65366442011-06-05 02:43:45 +00001396 Pos += 1 + Argument.size();
1397 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001398 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001399 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001400 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001401
1402 // We include the .endmacro in the buffer as our queue to exit the macro
1403 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001404 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001405 return false;
1406}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001407
Rafael Espindola65366442011-06-05 02:43:45 +00001408MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1409 MemoryBuffer *I)
1410 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1411{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001412}
1413
1414bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1415 const Macro *M) {
1416 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1417 // this, although we should protect against infinite loops.
1418 if (ActiveMacros.size() == 20)
1419 return TokError("macros cannot be nested more than 20 levels deep");
1420
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001421 // Parse the macro instantiation arguments.
1422 std::vector<std::vector<AsmToken> > MacroArguments;
1423 MacroArguments.push_back(std::vector<AsmToken>());
1424 unsigned ParenLevel = 0;
1425 for (;;) {
1426 if (Lexer.is(AsmToken::Eof))
1427 return TokError("unexpected token in macro instantiation");
1428 if (Lexer.is(AsmToken::EndOfStatement))
1429 break;
1430
1431 // If we aren't inside parentheses and this is a comma, start a new token
1432 // list.
1433 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1434 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001435 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001436 // Adjust the current parentheses level.
1437 if (Lexer.is(AsmToken::LParen))
1438 ++ParenLevel;
1439 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1440 --ParenLevel;
1441
1442 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001443 MacroArguments.back().push_back(getTok());
1444 }
1445 Lex();
1446 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001447
Rafael Espindola65366442011-06-05 02:43:45 +00001448 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1449 // to hold the macro body with substitutions.
1450 SmallString<256> Buf;
1451 StringRef Body = M->Body;
1452
1453 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1454 return true;
1455
1456 MemoryBuffer *Instantiation =
1457 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1458
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001459 // Create the macro instantiation object and add to the current macro
1460 // instantiation stack.
1461 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001462 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001463 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001464 ActiveMacros.push_back(MI);
1465
1466 // Jump to the macro instantiation and prime the lexer.
1467 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1468 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1469 Lex();
1470
1471 return false;
1472}
1473
1474void AsmParser::HandleMacroExit() {
1475 // Jump to the EndOfStatement we should return to, and consume it.
1476 JumpToLoc(ActiveMacros.back()->ExitLoc);
1477 Lex();
1478
1479 // Pop the instantiation entry.
1480 delete ActiveMacros.back();
1481 ActiveMacros.pop_back();
1482}
1483
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001484static void MarkUsed(const MCExpr *Value) {
1485 switch (Value->getKind()) {
1486 case MCExpr::Binary:
1487 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1488 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1489 break;
1490 case MCExpr::Target:
1491 case MCExpr::Constant:
1492 break;
1493 case MCExpr::SymbolRef: {
1494 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1495 break;
1496 }
1497 case MCExpr::Unary:
1498 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1499 break;
1500 }
1501}
1502
Nico Weber4c4c7322011-01-28 03:04:41 +00001503bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001504 // FIXME: Use better location, we should use proper tokens.
1505 SMLoc EqualLoc = Lexer.getLoc();
1506
Daniel Dunbar821e3332009-08-31 08:09:28 +00001507 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001508 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001509 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001510
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001511 MarkUsed(Value);
1512
Daniel Dunbar3f872332009-07-28 16:08:33 +00001513 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001514 return TokError("unexpected token in assignment");
1515
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001516 // Error on assignment to '.'.
1517 if (Name == ".") {
1518 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1519 "(use '.space' or '.org').)"));
1520 }
1521
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001522 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001523 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001524
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001525 // Validate that the LHS is allowed to be a variable (either it has not been
1526 // used as a symbol, or it is an absolute symbol).
1527 MCSymbol *Sym = getContext().LookupSymbol(Name);
1528 if (Sym) {
1529 // Diagnose assignment to a label.
1530 //
1531 // FIXME: Diagnostics. Note the location of the definition as a label.
1532 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001533 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001534 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001535 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001536 return Error(EqualLoc, "redefinition of '" + Name + "'");
1537 else if (!Sym->isVariable())
1538 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001539 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001540 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1541 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001542
1543 // Don't count these checks as uses.
1544 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001545 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001546 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001547
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001548 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001549
1550 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001551 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001552
1553 return false;
1554}
1555
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001556/// ParseIdentifier:
1557/// ::= identifier
1558/// ::= string
1559bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001560 // The assembler has relaxed rules for accepting identifiers, in particular we
1561 // allow things like '.globl $foo', which would normally be separate
1562 // tokens. At this level, we have already lexed so we cannot (currently)
1563 // handle this as a context dependent token, instead we detect adjacent tokens
1564 // and return the combined identifier.
1565 if (Lexer.is(AsmToken::Dollar)) {
1566 SMLoc DollarLoc = getLexer().getLoc();
1567
1568 // Consume the dollar sign, and check for a following identifier.
1569 Lex();
1570 if (Lexer.isNot(AsmToken::Identifier))
1571 return true;
1572
1573 // We have a '$' followed by an identifier, make sure they are adjacent.
1574 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1575 return true;
1576
1577 // Construct the joined identifier and consume the token.
1578 Res = StringRef(DollarLoc.getPointer(),
1579 getTok().getIdentifier().size() + 1);
1580 Lex();
1581 return false;
1582 }
1583
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001584 if (Lexer.isNot(AsmToken::Identifier) &&
1585 Lexer.isNot(AsmToken::String))
1586 return true;
1587
Sean Callanan18b83232010-01-19 21:44:56 +00001588 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001589
Sean Callanan79ed1a82010-01-19 20:22:31 +00001590 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001591
1592 return false;
1593}
1594
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001595/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001596/// ::= .equ identifier ',' expression
1597/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001598/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001599bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001600 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001601
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001602 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001603 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001604
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001605 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001606 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001607 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001608
Nico Weber4c4c7322011-01-28 03:04:41 +00001609 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001610}
1611
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001612bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001613 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001614
1615 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001616 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001617 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1618 if (Str[i] != '\\') {
1619 Data += Str[i];
1620 continue;
1621 }
1622
1623 // Recognize escaped characters. Note that this escape semantics currently
1624 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1625 ++i;
1626 if (i == e)
1627 return TokError("unexpected backslash at end of string");
1628
1629 // Recognize octal sequences.
1630 if ((unsigned) (Str[i] - '0') <= 7) {
1631 // Consume up to three octal characters.
1632 unsigned Value = Str[i] - '0';
1633
1634 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1635 ++i;
1636 Value = Value * 8 + (Str[i] - '0');
1637
1638 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1639 ++i;
1640 Value = Value * 8 + (Str[i] - '0');
1641 }
1642 }
1643
1644 if (Value > 255)
1645 return TokError("invalid octal escape sequence (out of range)");
1646
1647 Data += (unsigned char) Value;
1648 continue;
1649 }
1650
1651 // Otherwise recognize individual escapes.
1652 switch (Str[i]) {
1653 default:
1654 // Just reject invalid escape sequences for now.
1655 return TokError("invalid escape sequence (unrecognized character)");
1656
1657 case 'b': Data += '\b'; break;
1658 case 'f': Data += '\f'; break;
1659 case 'n': Data += '\n'; break;
1660 case 'r': Data += '\r'; break;
1661 case 't': Data += '\t'; break;
1662 case '"': Data += '"'; break;
1663 case '\\': Data += '\\'; break;
1664 }
1665 }
1666
1667 return false;
1668}
1669
Daniel Dunbara0d14262009-06-24 23:30:00 +00001670/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001671/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1672bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001673 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001674 CheckForValidSection();
1675
Daniel Dunbara0d14262009-06-24 23:30:00 +00001676 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001677 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001678 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001680 std::string Data;
1681 if (ParseEscapedString(Data))
1682 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683
1684 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001685 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001686 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1687
Sean Callanan79ed1a82010-01-19 20:22:31 +00001688 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001689
1690 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001691 break;
1692
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001693 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001694 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001695 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001696 }
1697 }
1698
Sean Callanan79ed1a82010-01-19 20:22:31 +00001699 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001700 return false;
1701}
1702
1703/// ParseDirectiveValue
1704/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1705bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001707 CheckForValidSection();
1708
Daniel Dunbara0d14262009-06-24 23:30:00 +00001709 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001710 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001711 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001712 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001713 return true;
1714
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001715 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001716 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1717 assert(Size <= 8 && "Invalid size");
1718 uint64_t IntValue = MCE->getValue();
1719 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1720 return Error(ExprLoc, "literal value out of range for directive");
1721 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1722 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001723 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001724
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001725 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001726 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001727
Daniel Dunbara0d14262009-06-24 23:30:00 +00001728 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001729 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001730 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001731 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001732 }
1733 }
1734
Sean Callanan79ed1a82010-01-19 20:22:31 +00001735 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001736 return false;
1737}
1738
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001739/// ParseDirectiveRealValue
1740/// ::= (.single | .double) [ expression (, expression)* ]
1741bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1742 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1743 CheckForValidSection();
1744
1745 for (;;) {
1746 // We don't truly support arithmetic on floating point expressions, so we
1747 // have to manually parse unary prefixes.
1748 bool IsNeg = false;
1749 if (getLexer().is(AsmToken::Minus)) {
1750 Lex();
1751 IsNeg = true;
1752 } else if (getLexer().is(AsmToken::Plus))
1753 Lex();
1754
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001755 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001756 getLexer().isNot(AsmToken::Real) &&
1757 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001758 return TokError("unexpected token in directive");
1759
1760 // Convert to an APFloat.
1761 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001762 StringRef IDVal = getTok().getString();
1763 if (getLexer().is(AsmToken::Identifier)) {
1764 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1765 Value = APFloat::getInf(Semantics);
1766 else if (!IDVal.compare_lower("nan"))
1767 Value = APFloat::getNaN(Semantics, false, ~0);
1768 else
1769 return TokError("invalid floating point literal");
1770 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001771 APFloat::opInvalidOp)
1772 return TokError("invalid floating point literal");
1773 if (IsNeg)
1774 Value.changeSign();
1775
1776 // Consume the numeric token.
1777 Lex();
1778
1779 // Emit the value as an integer.
1780 APInt AsInt = Value.bitcastToAPInt();
1781 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1782 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1783
1784 if (getLexer().is(AsmToken::EndOfStatement))
1785 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001786
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001787 if (getLexer().isNot(AsmToken::Comma))
1788 return TokError("unexpected token in directive");
1789 Lex();
1790 }
1791 }
1792
1793 Lex();
1794 return false;
1795}
1796
Daniel Dunbara0d14262009-06-24 23:30:00 +00001797/// ParseDirectiveSpace
1798/// ::= .space expression [ , expression ]
1799bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001800 CheckForValidSection();
1801
Daniel Dunbara0d14262009-06-24 23:30:00 +00001802 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001803 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001804 return true;
1805
1806 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001807 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1808 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001809 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001810 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001811
Daniel Dunbar475839e2009-06-29 20:37:27 +00001812 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001813 return true;
1814
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001815 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001816 return TokError("unexpected token in '.space' directive");
1817 }
1818
Sean Callanan79ed1a82010-01-19 20:22:31 +00001819 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001820
1821 if (NumBytes <= 0)
1822 return TokError("invalid number of bytes in '.space' directive");
1823
1824 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001825 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001826
1827 return false;
1828}
1829
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001830/// ParseDirectiveZero
1831/// ::= .zero expression
1832bool AsmParser::ParseDirectiveZero() {
1833 CheckForValidSection();
1834
1835 int64_t NumBytes;
1836 if (ParseAbsoluteExpression(NumBytes))
1837 return true;
1838
Rafael Espindolae452b172010-10-05 19:42:57 +00001839 int64_t Val = 0;
1840 if (getLexer().is(AsmToken::Comma)) {
1841 Lex();
1842 if (ParseAbsoluteExpression(Val))
1843 return true;
1844 }
1845
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001846 if (getLexer().isNot(AsmToken::EndOfStatement))
1847 return TokError("unexpected token in '.zero' directive");
1848
1849 Lex();
1850
Rafael Espindolae452b172010-10-05 19:42:57 +00001851 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001852
1853 return false;
1854}
1855
Daniel Dunbara0d14262009-06-24 23:30:00 +00001856/// ParseDirectiveFill
1857/// ::= .fill expression , expression , expression
1858bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001859 CheckForValidSection();
1860
Daniel Dunbara0d14262009-06-24 23:30:00 +00001861 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001862 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001863 return true;
1864
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001865 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001866 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001867 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001868
Daniel Dunbara0d14262009-06-24 23:30:00 +00001869 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001870 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001871 return true;
1872
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001873 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001874 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001875 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001876
Daniel Dunbara0d14262009-06-24 23:30:00 +00001877 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001878 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001879 return true;
1880
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001881 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001882 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001883
Sean Callanan79ed1a82010-01-19 20:22:31 +00001884 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001885
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001886 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1887 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001888
1889 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001890 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001891
1892 return false;
1893}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001894
1895/// ParseDirectiveOrg
1896/// ::= .org expression [ , expression ]
1897bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001898 CheckForValidSection();
1899
Daniel Dunbar821e3332009-08-31 08:09:28 +00001900 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001901 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001902 return true;
1903
1904 // Parse optional fill expression.
1905 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001906 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1907 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001908 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001909 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001910
Daniel Dunbar475839e2009-06-29 20:37:27 +00001911 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001912 return true;
1913
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001914 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001915 return TokError("unexpected token in '.org' directive");
1916 }
1917
Sean Callanan79ed1a82010-01-19 20:22:31 +00001918 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001919
1920 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1921 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001922 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001923
1924 return false;
1925}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001926
1927/// ParseDirectiveAlign
1928/// ::= {.align, ...} expression [ , expression [ , expression ]]
1929bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001930 CheckForValidSection();
1931
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001933 int64_t Alignment;
1934 if (ParseAbsoluteExpression(Alignment))
1935 return true;
1936
1937 SMLoc MaxBytesLoc;
1938 bool HasFillExpr = false;
1939 int64_t FillExpr = 0;
1940 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001941 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1942 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001943 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001944 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001945
1946 // The fill expression can be omitted while specifying a maximum number of
1947 // alignment bytes, e.g:
1948 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001949 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001950 HasFillExpr = true;
1951 if (ParseAbsoluteExpression(FillExpr))
1952 return true;
1953 }
1954
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001955 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1956 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001957 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001958 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001959
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001960 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001961 if (ParseAbsoluteExpression(MaxBytesToFill))
1962 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001963
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001964 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001965 return TokError("unexpected token in directive");
1966 }
1967 }
1968
Sean Callanan79ed1a82010-01-19 20:22:31 +00001969 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001970
Daniel Dunbar648ac512010-05-17 21:54:30 +00001971 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001972 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001973
1974 // Compute alignment in bytes.
1975 if (IsPow2) {
1976 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001977 if (Alignment >= 32) {
1978 Error(AlignmentLoc, "invalid alignment value");
1979 Alignment = 31;
1980 }
1981
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001982 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001983 }
1984
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001985 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001986 if (MaxBytesLoc.isValid()) {
1987 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001988 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1989 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001990 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001991 }
1992
1993 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001994 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1995 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001996 MaxBytesToFill = 0;
1997 }
1998 }
1999
Daniel Dunbar648ac512010-05-17 21:54:30 +00002000 // Check whether we should use optimal code alignment for this .align
2001 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002002 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002003 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2004 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002005 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002006 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002007 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002008 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2009 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002010 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002011
2012 return false;
2013}
2014
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002015/// ParseDirectiveSymbolAttribute
2016/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002017bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002018 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002019 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002020 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002021 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002022
2023 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002024 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002025
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002026 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002027
Jim Grosbach10ec6502011-09-15 17:56:49 +00002028 // Assembler local symbols don't make any sense here. Complain loudly.
2029 if (Sym->isTemporary())
2030 return Error(Loc, "non-local symbol required in directive");
2031
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002032 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002033
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002034 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002035 break;
2036
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002037 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002038 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002039 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002040 }
2041 }
2042
Sean Callanan79ed1a82010-01-19 20:22:31 +00002043 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002044 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002045}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002046
2047/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002048/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2049bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002050 CheckForValidSection();
2051
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002052 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002053 StringRef Name;
2054 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002055 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002056
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002057 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002058 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002059
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002061 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002062 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002063
2064 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002065 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002066 if (ParseAbsoluteExpression(Size))
2067 return true;
2068
2069 int64_t Pow2Alignment = 0;
2070 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002071 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002072 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002073 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002074 if (ParseAbsoluteExpression(Pow2Alignment))
2075 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002076
Chris Lattner258281d2010-01-19 06:22:22 +00002077 // If this target takes alignments in bytes (not log) validate and convert.
2078 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2079 if (!isPowerOf2_64(Pow2Alignment))
2080 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2081 Pow2Alignment = Log2_64(Pow2Alignment);
2082 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002083 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002084
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002085 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002086 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002087
Sean Callanan79ed1a82010-01-19 20:22:31 +00002088 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002089
Chris Lattner1fc3d752009-07-09 17:25:12 +00002090 // NOTE: a size of zero for a .comm should create a undefined symbol
2091 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002092 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002093 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2094 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002095
Eric Christopherc260a3e2010-05-14 01:38:54 +00002096 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002097 // may internally end up wanting an alignment in bytes.
2098 // FIXME: Diagnose overflow.
2099 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002100 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2101 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002102
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002103 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002104 return Error(IDLoc, "invalid symbol redefinition");
2105
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002106 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002107 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002108 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002109 getStreamer().EmitZerofill(Ctx.getMachOSection(
2110 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2111 0, SectionKind::getBSS()),
2112 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002113 return false;
2114 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002115
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002116 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002117 return false;
2118}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002119
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002120/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002121/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002122bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002123 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002124 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002125
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002126 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002127 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002128 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002129
Sean Callanan79ed1a82010-01-19 20:22:31 +00002130 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002131
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002132 if (Str.empty())
2133 Error(Loc, ".abort detected. Assembly stopping.");
2134 else
2135 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002136 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002137
2138 return false;
2139}
Kevin Enderby71148242009-07-14 21:35:03 +00002140
Kevin Enderby1f049b22009-07-14 23:21:55 +00002141/// ParseDirectiveInclude
2142/// ::= .include "filename"
2143bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002144 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002145 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002146
Sean Callanan18b83232010-01-19 21:44:56 +00002147 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002148 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002149 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002150
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002151 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002152 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002153
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002154 // Strip the quotes.
2155 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002156
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002157 // Attempt to switch the lexer to the included file before consuming the end
2158 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002159 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002160 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002161 return true;
2162 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002163
2164 return false;
2165}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002166
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002167/// ParseDirectiveIf
2168/// ::= .if expression
2169bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002170 TheCondStack.push_back(TheCondState);
2171 TheCondState.TheCond = AsmCond::IfCond;
2172 if(TheCondState.Ignore) {
2173 EatToEndOfStatement();
2174 }
2175 else {
2176 int64_t ExprValue;
2177 if (ParseAbsoluteExpression(ExprValue))
2178 return true;
2179
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002180 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002181 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002182
Sean Callanan79ed1a82010-01-19 20:22:31 +00002183 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002184
2185 TheCondState.CondMet = ExprValue;
2186 TheCondState.Ignore = !TheCondState.CondMet;
2187 }
2188
2189 return false;
2190}
2191
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002192bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2193 StringRef Name;
2194 TheCondStack.push_back(TheCondState);
2195 TheCondState.TheCond = AsmCond::IfCond;
2196
2197 if (TheCondState.Ignore) {
2198 EatToEndOfStatement();
2199 } else {
2200 if (ParseIdentifier(Name))
2201 return TokError("expected identifier after '.ifdef'");
2202
2203 Lex();
2204
2205 MCSymbol *Sym = getContext().LookupSymbol(Name);
2206
2207 if (expect_defined)
2208 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2209 else
2210 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2211 TheCondState.Ignore = !TheCondState.CondMet;
2212 }
2213
2214 return false;
2215}
2216
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002217/// ParseDirectiveElseIf
2218/// ::= .elseif expression
2219bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2220 if (TheCondState.TheCond != AsmCond::IfCond &&
2221 TheCondState.TheCond != AsmCond::ElseIfCond)
2222 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2223 " an .elseif");
2224 TheCondState.TheCond = AsmCond::ElseIfCond;
2225
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002226 bool LastIgnoreState = false;
2227 if (!TheCondStack.empty())
2228 LastIgnoreState = TheCondStack.back().Ignore;
2229 if (LastIgnoreState || TheCondState.CondMet) {
2230 TheCondState.Ignore = true;
2231 EatToEndOfStatement();
2232 }
2233 else {
2234 int64_t ExprValue;
2235 if (ParseAbsoluteExpression(ExprValue))
2236 return true;
2237
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002238 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002239 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002240
Sean Callanan79ed1a82010-01-19 20:22:31 +00002241 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002242 TheCondState.CondMet = ExprValue;
2243 TheCondState.Ignore = !TheCondState.CondMet;
2244 }
2245
2246 return false;
2247}
2248
2249/// ParseDirectiveElse
2250/// ::= .else
2251bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002252 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002253 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002254
Sean Callanan79ed1a82010-01-19 20:22:31 +00002255 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002256
2257 if (TheCondState.TheCond != AsmCond::IfCond &&
2258 TheCondState.TheCond != AsmCond::ElseIfCond)
2259 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2260 ".elseif");
2261 TheCondState.TheCond = AsmCond::ElseCond;
2262 bool LastIgnoreState = false;
2263 if (!TheCondStack.empty())
2264 LastIgnoreState = TheCondStack.back().Ignore;
2265 if (LastIgnoreState || TheCondState.CondMet)
2266 TheCondState.Ignore = true;
2267 else
2268 TheCondState.Ignore = false;
2269
2270 return false;
2271}
2272
2273/// ParseDirectiveEndIf
2274/// ::= .endif
2275bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002276 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002277 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002278
Sean Callanan79ed1a82010-01-19 20:22:31 +00002279 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002280
2281 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2282 TheCondStack.empty())
2283 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2284 ".else");
2285 if (!TheCondStack.empty()) {
2286 TheCondState = TheCondStack.back();
2287 TheCondStack.pop_back();
2288 }
2289
2290 return false;
2291}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002292
2293/// ParseDirectiveFile
2294/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002295bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002296 // FIXME: I'm not sure what this is.
2297 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002298 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002299 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002300 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002301 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002302
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002303 if (FileNumber < 1)
2304 return TokError("file number less than one");
2305 }
2306
Daniel Dunbareceec052010-07-12 17:45:27 +00002307 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002308 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002309
Chris Lattnerd32e8032010-01-25 19:02:58 +00002310 StringRef Filename = getTok().getString();
2311 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002312 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002313
Daniel Dunbareceec052010-07-12 17:45:27 +00002314 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002315 return TokError("unexpected token in '.file' directive");
2316
Chris Lattnerd32e8032010-01-25 19:02:58 +00002317 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002318 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002319 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002320 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002321 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002322 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002323
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002324 return false;
2325}
2326
2327/// ParseDirectiveLine
2328/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002329bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002330 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2331 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002332 return TokError("unexpected token in '.line' directive");
2333
Sean Callanan18b83232010-01-19 21:44:56 +00002334 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002335 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002336 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002337
2338 // FIXME: Do something with the .line.
2339 }
2340
Daniel Dunbareceec052010-07-12 17:45:27 +00002341 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002342 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002343
2344 return false;
2345}
2346
2347
2348/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002349/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002350/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2351/// The first number is a file number, must have been previously assigned with
2352/// a .file directive, the second number is the line number and optionally the
2353/// third number is a column position (zero if not specified). The remaining
2354/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002355bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002356
Daniel Dunbareceec052010-07-12 17:45:27 +00002357 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002358 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002359 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002360 if (FileNumber < 1)
2361 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002362 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002363 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002364 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002365
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002366 int64_t LineNumber = 0;
2367 if (getLexer().is(AsmToken::Integer)) {
2368 LineNumber = getTok().getIntVal();
2369 if (LineNumber < 1)
2370 return TokError("line number less than one in '.loc' directive");
2371 Lex();
2372 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002373
2374 int64_t ColumnPos = 0;
2375 if (getLexer().is(AsmToken::Integer)) {
2376 ColumnPos = getTok().getIntVal();
2377 if (ColumnPos < 0)
2378 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002379 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002380 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002381
Kevin Enderbyc0957932010-09-30 16:52:03 +00002382 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002383 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002384 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002385 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2386 for (;;) {
2387 if (getLexer().is(AsmToken::EndOfStatement))
2388 break;
2389
2390 StringRef Name;
2391 SMLoc Loc = getTok().getLoc();
2392 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002393 return TokError("unexpected token in '.loc' directive");
2394
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002395 if (Name == "basic_block")
2396 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2397 else if (Name == "prologue_end")
2398 Flags |= DWARF2_FLAG_PROLOGUE_END;
2399 else if (Name == "epilogue_begin")
2400 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2401 else if (Name == "is_stmt") {
2402 SMLoc Loc = getTok().getLoc();
2403 const MCExpr *Value;
2404 if (getParser().ParseExpression(Value))
2405 return true;
2406 // The expression must be the constant 0 or 1.
2407 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2408 int Value = MCE->getValue();
2409 if (Value == 0)
2410 Flags &= ~DWARF2_FLAG_IS_STMT;
2411 else if (Value == 1)
2412 Flags |= DWARF2_FLAG_IS_STMT;
2413 else
2414 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002415 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002416 else {
2417 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2418 }
2419 }
2420 else if (Name == "isa") {
2421 SMLoc Loc = getTok().getLoc();
2422 const MCExpr *Value;
2423 if (getParser().ParseExpression(Value))
2424 return true;
2425 // The expression must be a constant greater or equal to 0.
2426 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2427 int Value = MCE->getValue();
2428 if (Value < 0)
2429 return Error(Loc, "isa number less than zero");
2430 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002431 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002432 else {
2433 return Error(Loc, "isa number not a constant value");
2434 }
2435 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002436 else if (Name == "discriminator") {
2437 if (getParser().ParseAbsoluteExpression(Discriminator))
2438 return true;
2439 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002440 else {
2441 return Error(Loc, "unknown sub-directive in '.loc' directive");
2442 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002443
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002444 if (getLexer().is(AsmToken::EndOfStatement))
2445 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002446 }
2447 }
2448
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002449 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002450 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002451
2452 return false;
2453}
2454
Daniel Dunbar138abae2010-10-16 04:56:42 +00002455/// ParseDirectiveStabs
2456/// ::= .stabs string, number, number, number
2457bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2458 SMLoc DirectiveLoc) {
2459 return TokError("unsupported directive '" + Directive + "'");
2460}
2461
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002462/// ParseDirectiveCFISections
2463/// ::= .cfi_sections section [, section]
2464bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2465 SMLoc DirectiveLoc) {
2466 StringRef Name;
2467 bool EH = false;
2468 bool Debug = false;
2469
2470 if (getParser().ParseIdentifier(Name))
2471 return TokError("Expected an identifier");
2472
2473 if (Name == ".eh_frame")
2474 EH = true;
2475 else if (Name == ".debug_frame")
2476 Debug = true;
2477
2478 if (getLexer().is(AsmToken::Comma)) {
2479 Lex();
2480
2481 if (getParser().ParseIdentifier(Name))
2482 return TokError("Expected an identifier");
2483
2484 if (Name == ".eh_frame")
2485 EH = true;
2486 else if (Name == ".debug_frame")
2487 Debug = true;
2488 }
2489
2490 getStreamer().EmitCFISections(EH, Debug);
2491
2492 return false;
2493}
2494
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002495/// ParseDirectiveCFIStartProc
2496/// ::= .cfi_startproc
2497bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2498 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002499 getStreamer().EmitCFIStartProc();
2500 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002501}
2502
2503/// ParseDirectiveCFIEndProc
2504/// ::= .cfi_endproc
2505bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002506 getStreamer().EmitCFIEndProc();
2507 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002508}
2509
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002510/// ParseRegisterOrRegisterNumber - parse register name or number.
2511bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2512 SMLoc DirectiveLoc) {
2513 unsigned RegNo;
2514
Jim Grosbach6f888a82011-06-02 17:14:04 +00002515 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002516 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2517 DirectiveLoc))
2518 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002519 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002520 } else
2521 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002522
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002523 return false;
2524}
2525
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002526/// ParseDirectiveCFIDefCfa
2527/// ::= .cfi_def_cfa register, offset
2528bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2529 SMLoc DirectiveLoc) {
2530 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002531 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002532 return true;
2533
2534 if (getLexer().isNot(AsmToken::Comma))
2535 return TokError("unexpected token in directive");
2536 Lex();
2537
2538 int64_t Offset = 0;
2539 if (getParser().ParseAbsoluteExpression(Offset))
2540 return true;
2541
Rafael Espindola066c2f42011-04-12 23:59:07 +00002542 getStreamer().EmitCFIDefCfa(Register, Offset);
2543 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002544}
2545
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002546/// ParseDirectiveCFIDefCfaOffset
2547/// ::= .cfi_def_cfa_offset offset
2548bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2549 SMLoc DirectiveLoc) {
2550 int64_t Offset = 0;
2551 if (getParser().ParseAbsoluteExpression(Offset))
2552 return true;
2553
Rafael Espindola066c2f42011-04-12 23:59:07 +00002554 getStreamer().EmitCFIDefCfaOffset(Offset);
2555 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002556}
2557
2558/// ParseDirectiveCFIAdjustCfaOffset
2559/// ::= .cfi_adjust_cfa_offset adjustment
2560bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2561 SMLoc DirectiveLoc) {
2562 int64_t Adjustment = 0;
2563 if (getParser().ParseAbsoluteExpression(Adjustment))
2564 return true;
2565
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002566 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2567 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002568}
2569
2570/// ParseDirectiveCFIDefCfaRegister
2571/// ::= .cfi_def_cfa_register register
2572bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2573 SMLoc DirectiveLoc) {
2574 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002575 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002576 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002577
Rafael Espindola066c2f42011-04-12 23:59:07 +00002578 getStreamer().EmitCFIDefCfaRegister(Register);
2579 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002580}
2581
2582/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002583/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002584bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2585 int64_t Register = 0;
2586 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002587
2588 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002589 return true;
2590
2591 if (getLexer().isNot(AsmToken::Comma))
2592 return TokError("unexpected token in directive");
2593 Lex();
2594
2595 if (getParser().ParseAbsoluteExpression(Offset))
2596 return true;
2597
Rafael Espindola066c2f42011-04-12 23:59:07 +00002598 getStreamer().EmitCFIOffset(Register, Offset);
2599 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002600}
2601
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002602/// ParseDirectiveCFIRelOffset
2603/// ::= .cfi_rel_offset register, offset
2604bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2605 SMLoc DirectiveLoc) {
2606 int64_t Register = 0;
2607
2608 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2609 return true;
2610
2611 if (getLexer().isNot(AsmToken::Comma))
2612 return TokError("unexpected token in directive");
2613 Lex();
2614
2615 int64_t Offset = 0;
2616 if (getParser().ParseAbsoluteExpression(Offset))
2617 return true;
2618
Rafael Espindola25f492e2011-04-12 16:12:03 +00002619 getStreamer().EmitCFIRelOffset(Register, Offset);
2620 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002621}
2622
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002623static bool isValidEncoding(int64_t Encoding) {
2624 if (Encoding & ~0xff)
2625 return false;
2626
2627 if (Encoding == dwarf::DW_EH_PE_omit)
2628 return true;
2629
2630 const unsigned Format = Encoding & 0xf;
2631 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2632 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2633 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2634 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2635 return false;
2636
Rafael Espindolacaf11582010-12-29 04:31:26 +00002637 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002638 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002639 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002640 return false;
2641
2642 return true;
2643}
2644
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002645/// ParseDirectiveCFIPersonalityOrLsda
2646/// ::= .cfi_personality encoding, [symbol_name]
2647/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002648bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002649 SMLoc DirectiveLoc) {
2650 int64_t Encoding = 0;
2651 if (getParser().ParseAbsoluteExpression(Encoding))
2652 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002653 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002654 return false;
2655
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002656 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002657 return TokError("unsupported encoding.");
2658
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002659 if (getLexer().isNot(AsmToken::Comma))
2660 return TokError("unexpected token in directive");
2661 Lex();
2662
2663 StringRef Name;
2664 if (getParser().ParseIdentifier(Name))
2665 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002666
2667 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2668
2669 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002670 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002671 else {
2672 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002673 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002674 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002675 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002676}
2677
Rafael Espindolafe024d02010-12-28 18:36:23 +00002678/// ParseDirectiveCFIRememberState
2679/// ::= .cfi_remember_state
2680bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2681 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002682 getStreamer().EmitCFIRememberState();
2683 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002684}
2685
2686/// ParseDirectiveCFIRestoreState
2687/// ::= .cfi_remember_state
2688bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2689 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002690 getStreamer().EmitCFIRestoreState();
2691 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002692}
2693
Rafael Espindolac5754392011-04-12 15:31:05 +00002694/// ParseDirectiveCFISameValue
2695/// ::= .cfi_same_value register
2696bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2697 SMLoc DirectiveLoc) {
2698 int64_t Register = 0;
2699
2700 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2701 return true;
2702
2703 getStreamer().EmitCFISameValue(Register);
2704
2705 return false;
2706}
2707
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002708/// ParseDirectiveMacrosOnOff
2709/// ::= .macros_on
2710/// ::= .macros_off
2711bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2712 SMLoc DirectiveLoc) {
2713 if (getLexer().isNot(AsmToken::EndOfStatement))
2714 return Error(getLexer().getLoc(),
2715 "unexpected token in '" + Directive + "' directive");
2716
2717 getParser().MacrosEnabled = Directive == ".macros_on";
2718
2719 return false;
2720}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002721
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002722/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002723/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002724bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2725 SMLoc DirectiveLoc) {
2726 StringRef Name;
2727 if (getParser().ParseIdentifier(Name))
2728 return TokError("expected identifier in directive");
2729
Rafael Espindola65366442011-06-05 02:43:45 +00002730 std::vector<StringRef> Parameters;
2731 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2732 for(;;) {
2733 StringRef Parameter;
2734 if (getParser().ParseIdentifier(Parameter))
2735 return TokError("expected identifier in directive");
2736 Parameters.push_back(Parameter);
2737
2738 if (getLexer().isNot(AsmToken::Comma))
2739 break;
2740 Lex();
2741 }
2742 }
2743
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002744 if (getLexer().isNot(AsmToken::EndOfStatement))
2745 return TokError("unexpected token in '.macro' directive");
2746
2747 // Eat the end of statement.
2748 Lex();
2749
2750 AsmToken EndToken, StartToken = getTok();
2751
2752 // Lex the macro definition.
2753 for (;;) {
2754 // Check whether we have reached the end of the file.
2755 if (getLexer().is(AsmToken::Eof))
2756 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2757
2758 // Otherwise, check whether we have reach the .endmacro.
2759 if (getLexer().is(AsmToken::Identifier) &&
2760 (getTok().getIdentifier() == ".endm" ||
2761 getTok().getIdentifier() == ".endmacro")) {
2762 EndToken = getTok();
2763 Lex();
2764 if (getLexer().isNot(AsmToken::EndOfStatement))
2765 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2766 "' directive");
2767 break;
2768 }
2769
2770 // Otherwise, scan til the end of the statement.
2771 getParser().EatToEndOfStatement();
2772 }
2773
2774 if (getParser().MacroMap.lookup(Name)) {
2775 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2776 }
2777
2778 const char *BodyStart = StartToken.getLoc().getPointer();
2779 const char *BodyEnd = EndToken.getLoc().getPointer();
2780 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002781 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002782 return false;
2783}
2784
2785/// ParseDirectiveEndMacro
2786/// ::= .endm
2787/// ::= .endmacro
2788bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2789 SMLoc DirectiveLoc) {
2790 if (getLexer().isNot(AsmToken::EndOfStatement))
2791 return TokError("unexpected token in '" + Directive + "' directive");
2792
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002793 // If we are inside a macro instantiation, terminate the current
2794 // instantiation.
2795 if (!getParser().ActiveMacros.empty()) {
2796 getParser().HandleMacroExit();
2797 return false;
2798 }
2799
2800 // Otherwise, this .endmacro is a stray entry in the file; well formed
2801 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002802 return TokError("unexpected '" + Directive + "' in file, "
2803 "no current macro definition");
2804}
2805
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002806bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002807 getParser().CheckForValidSection();
2808
2809 const MCExpr *Value;
2810
2811 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002812 return true;
2813
2814 if (getLexer().isNot(AsmToken::EndOfStatement))
2815 return TokError("unexpected token in directive");
2816
2817 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002818 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002819 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002820 getStreamer().EmitULEB128Value(Value);
2821
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002822 return false;
2823}
2824
2825
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002826/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002827MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002828 MCContext &C, MCStreamer &Out,
2829 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002830 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002831}