blob: d7ee1c4ca3e76cda9c7ed1e39dd5906dd6695daf [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;
Benjamin Kramer04a04262011-10-16 10:48:29 +000090 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000094
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095 /// This is the current buffer index we're lexing from as managed by the
96 /// SourceMgr object.
97 int CurBuffer;
98
99 AsmCond TheCondState;
100 std::vector<AsmCond> TheCondStack;
101
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000107
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
110
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
113
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
116
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
119
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
123 SMLoc CppHashLoc;
124
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000125public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000126 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000127 const MCAsmInfo &MAI);
128 ~AsmParser();
129
130 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
131
132 void AddDirectiveHandler(MCAsmParserExtension *Object,
133 StringRef Directive,
134 DirectiveHandler Handler) {
135 DirectiveMap[Directive] = std::make_pair(Object, Handler);
136 }
137
138public:
139 /// @name MCAsmParser Interface
140 /// {
141
142 virtual SourceMgr &getSourceManager() { return SrcMgr; }
143 virtual MCAsmLexer &getLexer() { return Lexer; }
144 virtual MCContext &getContext() { return Ctx; }
145 virtual MCStreamer &getStreamer() { return Out; }
146
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000147 virtual bool Warning(SMLoc L, const Twine &Msg,
148 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
149 virtual bool Error(SMLoc L, const Twine &Msg,
150 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000151
152 const AsmToken &Lex();
153
154 bool ParseExpression(const MCExpr *&Res);
155 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
156 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
157 virtual bool ParseAbsoluteExpression(int64_t &Res);
158
159 /// }
160
161private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000162 void CheckForValidSection();
163
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000165 void EatToEndOfLine();
166 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000167
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000168 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000169 bool expandMacro(SmallString<256> &Buf, StringRef Body,
170 const std::vector<StringRef> &Parameters,
171 const std::vector<std::vector<AsmToken> > &A,
172 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000173 void HandleMacroExit();
174
175 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000176 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000177 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
178 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000179 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000180 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000181
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000182 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
183 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000184
185 /// \brief Reset the current lexer position to that given by \arg Loc. The
186 /// current token is not set; clients should ensure Lex() is called
187 /// subsequently.
188 void JumpToLoc(SMLoc Loc);
189
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000191
192 /// \brief Parse up to the end of statement and a return the contents from the
193 /// current token until the end of the statement; the current token on exit
194 /// will be either the EndOfStatement or EOF.
195 StringRef ParseStringToEndOfStatement();
196
Nico Weber4c4c7322011-01-28 03:04:41 +0000197 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198
199 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
200 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
201 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000202 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000203
204 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
205 /// and set \arg Res to the identifier contents.
206 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000207
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000209
210 // ".ascii", ".asciiz", ".string"
211 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000213 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214 bool ParseDirectiveFill(); // ".fill"
215 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000216 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000217 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218 bool ParseDirectiveOrg(); // ".org"
219 // ".align{,32}", ".p2align{,w,l}"
220 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
221
222 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
223 /// accepts a single symbol (which should be a label or an external).
224 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000225
226 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
227
228 bool ParseDirectiveAbort(); // ".abort"
229 bool ParseDirectiveInclude(); // ".include"
230
231 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000232 // ".ifdef" or ".ifndef", depending on expect_defined
233 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
235 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
236 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
237
238 /// ParseEscapedString - Parse the current token as a string which may include
239 /// escaped characters and return the string contents.
240 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000241
242 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
243 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244};
245
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000246/// \brief Generic implementations of directive handling, etc. which is shared
247/// (or the default, at least) for all assembler parser.
248class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000249 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
250 void AddDirectiveHandler(StringRef Directive) {
251 getParser().AddDirectiveHandler(this, Directive,
252 HandleDirective<GenericAsmParser, Handler>);
253 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000254public:
255 GenericAsmParser() {}
256
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257 AsmParser &getParser() {
258 return (AsmParser&) this->MCAsmParserExtension::getParser();
259 }
260
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000261 virtual void Initialize(MCAsmParser &Parser) {
262 // Call the base implementation.
263 this->MCAsmParserExtension::Initialize(Parser);
264
265 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000270
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000271 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
273 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
275 ".cfi_startproc");
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
277 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
279 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
281 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
283 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
285 ".cfi_def_cfa_register");
286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
287 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000288 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
289 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000290 AddDirectiveHandler<
291 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
292 AddDirectiveHandler<
293 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000294 AddDirectiveHandler<
295 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
296 AddDirectiveHandler<
297 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000298 AddDirectiveHandler<
299 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000300
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000301 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
303 ".macros_on");
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
305 ".macros_off");
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000309
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000312 }
313
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000314 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
315
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000316 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
317 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000319 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000320 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000321 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
322 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000323 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000324 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000325 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000326 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
327 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000328 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000329 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000330 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
331 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000332 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000333
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000334 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000335 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
336 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000337
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000338 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000339};
340
341}
342
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000343namespace llvm {
344
345extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000346extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000347extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000348
349}
350
Chris Lattneraaec2052010-01-19 19:46:13 +0000351enum { DEFAULT_ADDRSPACE = 0 };
352
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000353AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000354 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000355 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000356 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000357 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000358 // Save the old handler.
359 SavedDiagHandler = SrcMgr.getDiagHandler();
360 SavedDiagContext = SrcMgr.getDiagContext();
361 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000362 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000363 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000364
365 // Initialize the generic parser.
366 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000367
368 // Initialize the platform / file format parser.
369 //
370 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
371 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000372 if (_MAI.hasMicrosoftFastStdCallMangling()) {
373 PlatformParser = createCOFFAsmParser();
374 PlatformParser->Initialize(*this);
375 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000376 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000377 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000378 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000379 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000380 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000381 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000382}
383
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000384AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000385 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
386
387 // Destroy any macros.
388 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
389 ie = MacroMap.end(); it != ie; ++it)
390 delete it->getValue();
391
Daniel Dunbare4749702010-07-12 18:12:02 +0000392 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000393 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000394}
395
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000396void AsmParser::PrintMacroInstantiations() {
397 // Print the active macro instantiation stack.
398 for (std::vector<MacroInstantiation*>::const_reverse_iterator
399 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000400 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
401 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000402}
403
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000404bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000405 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000406 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000407 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000408 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000409 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000410}
411
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000412bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000413 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000414 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000415 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000416 return true;
417}
418
Sean Callananfd0b0282010-01-21 00:19:58 +0000419bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000420 std::string IncludedFile;
421 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000422 if (NewBuf == -1)
423 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000424
Sean Callananfd0b0282010-01-21 00:19:58 +0000425 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000426
Sean Callananfd0b0282010-01-21 00:19:58 +0000427 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000428
Sean Callananfd0b0282010-01-21 00:19:58 +0000429 return false;
430}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000431
432void AsmParser::JumpToLoc(SMLoc Loc) {
433 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
434 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
435}
436
Sean Callananfd0b0282010-01-21 00:19:58 +0000437const AsmToken &AsmParser::Lex() {
438 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000439
Sean Callananfd0b0282010-01-21 00:19:58 +0000440 if (tok->is(AsmToken::Eof)) {
441 // If this is the end of an included file, pop the parent file off the
442 // include stack.
443 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
444 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000445 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000446 tok = &Lexer.Lex();
447 }
448 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000449
Sean Callananfd0b0282010-01-21 00:19:58 +0000450 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000451 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000452
Sean Callananfd0b0282010-01-21 00:19:58 +0000453 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000454}
455
Chris Lattner79180e22010-04-05 23:15:42 +0000456bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000457 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000458 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000459 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000460
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000461 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000462 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000463
464 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000465 AsmCond StartingCondState = TheCondState;
466
Chris Lattnerb717fb02009-07-02 21:53:43 +0000467 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000468 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000469 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000470
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000471 // We had an error, validate that one was emitted and recover by skipping to
472 // the next line.
473 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000474 EatToEndOfStatement();
475 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000476
477 if (TheCondState.TheCond != StartingCondState.TheCond ||
478 TheCondState.Ignore != StartingCondState.Ignore)
479 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000480
481 // Check to see there are no empty DwarfFile slots.
482 const std::vector<MCDwarfFile *> &MCDwarfFiles =
483 getContext().getMCDwarfFiles();
484 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000485 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000486 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000487 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000488
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000489 // Check to see that all assembler local symbols were actually defined.
490 // Targets that don't do subsections via symbols may not want this, though,
491 // so conservatively exclude them. Only do this if we're finalizing, though,
492 // as otherwise we won't necessarilly have seen everything yet.
493 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
494 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
495 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
496 e = Symbols.end();
497 i != e; ++i) {
498 MCSymbol *Sym = i->getValue();
499 // Variable symbols may not be marked as defined, so check those
500 // explicitly. If we know it's a variable, we have a definition for
501 // the purposes of this check.
502 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
503 // FIXME: We would really like to refer back to where the symbol was
504 // first referenced for a source location. We need to add something
505 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000506 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
507 "assembler local symbol '" + Sym->getName() +
508 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000509 }
510 }
511
512
Chris Lattner79180e22010-04-05 23:15:42 +0000513 // Finalize the output stream if there are no errors and if the client wants
514 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000515 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000516 Out.Finish();
517
Chris Lattnerb717fb02009-07-02 21:53:43 +0000518 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000519}
520
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000521void AsmParser::CheckForValidSection() {
522 if (!getStreamer().getCurrentSection()) {
523 TokError("expected section directive before assembly directive");
524 Out.SwitchSection(Ctx.getMachOSection(
525 "__TEXT", "__text",
526 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
527 0, SectionKind::getText()));
528 }
529}
530
Chris Lattner2cf5f142009-06-22 01:29:09 +0000531/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
532void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000533 while (Lexer.isNot(AsmToken::EndOfStatement) &&
534 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000535 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000536
Chris Lattner2cf5f142009-06-22 01:29:09 +0000537 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000538 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000539 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000540}
541
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000542StringRef AsmParser::ParseStringToEndOfStatement() {
543 const char *Start = getTok().getLoc().getPointer();
544
545 while (Lexer.isNot(AsmToken::EndOfStatement) &&
546 Lexer.isNot(AsmToken::Eof))
547 Lex();
548
549 const char *End = getTok().getLoc().getPointer();
550 return StringRef(Start, End - Start);
551}
Chris Lattnerc4193832009-06-22 05:51:26 +0000552
Chris Lattner74ec1a32009-06-22 06:32:03 +0000553/// ParseParenExpr - Parse a paren expression and return it.
554/// NOTE: This assumes the leading '(' has already been consumed.
555///
556/// parenexpr ::= expr)
557///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000558bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000559 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000561 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000562 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000563 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000564 return false;
565}
Chris Lattnerc4193832009-06-22 05:51:26 +0000566
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000567/// ParseBracketExpr - Parse a bracket expression and return it.
568/// NOTE: This assumes the leading '[' has already been consumed.
569///
570/// bracketexpr ::= expr]
571///
572bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
573 if (ParseExpression(Res)) return true;
574 if (Lexer.isNot(AsmToken::RBrac))
575 return TokError("expected ']' in brackets expression");
576 EndLoc = Lexer.getLoc();
577 Lex();
578 return false;
579}
580
Chris Lattner74ec1a32009-06-22 06:32:03 +0000581/// ParsePrimaryExpr - Parse a primary expression and return it.
582/// primaryexpr ::= (parenexpr
583/// primaryexpr ::= symbol
584/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000585/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000586/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000587bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000588 switch (Lexer.getKind()) {
589 default:
590 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000591 // If we have an error assume that we've already handled it.
592 case AsmToken::Error:
593 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000594 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000595 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000596 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000597 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000598 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000599 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000600 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000601 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000602 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000603 EndLoc = Lexer.getLoc();
604
605 StringRef Identifier;
606 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000607 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000608
Daniel Dunbarfffff912009-10-16 01:34:54 +0000609 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000610 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000611 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000612
613 // Lookup the symbol variant if used.
614 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000615 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000616 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000617 if (Variant == MCSymbolRefExpr::VK_Invalid) {
618 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000619 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000620 }
621 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000622
Daniel Dunbarfffff912009-10-16 01:34:54 +0000623 // If this is an absolute variable reference, substitute it now to preserve
624 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000625 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000626 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000627 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000628
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000629 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000630 return false;
631 }
632
633 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000634 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000635 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000636 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000637 case AsmToken::Integer: {
638 SMLoc Loc = getTok().getLoc();
639 int64_t IntVal = getTok().getIntVal();
640 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000641 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000642 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000643 // Look for 'b' or 'f' following an Integer as a directional label
644 if (Lexer.getKind() == AsmToken::Identifier) {
645 StringRef IDVal = getTok().getString();
646 if (IDVal == "f" || IDVal == "b"){
647 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
648 IDVal == "f" ? 1 : 0);
649 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
650 getContext());
651 if(IDVal == "b" && Sym->isUndefined())
652 return Error(Loc, "invalid reference to undefined symbol");
653 EndLoc = Lexer.getLoc();
654 Lex(); // Eat identifier.
655 }
656 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000657 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000658 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000659 case AsmToken::Real: {
660 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000661 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000662 Res = MCConstantExpr::Create(IntVal, getContext());
663 Lex(); // Eat token.
664 return false;
665 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000666 case AsmToken::Dot: {
667 // This is a '.' reference, which references the current PC. Emit a
668 // temporary label to the streamer and refer to it.
669 MCSymbol *Sym = Ctx.CreateTempSymbol();
670 Out.EmitLabel(Sym);
671 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
672 EndLoc = Lexer.getLoc();
673 Lex(); // Eat identifier.
674 return false;
675 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000676 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000677 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000678 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000679 case AsmToken::LBrac:
680 if (!PlatformParser->HasBracketExpressions())
681 return TokError("brackets expression not supported on this target");
682 Lex(); // Eat the '['.
683 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000684 case AsmToken::Minus:
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::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000689 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000690 case AsmToken::Plus:
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::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000695 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000696 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000697 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000698 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000699 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000700 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000701 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000702 }
703}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000704
Chris Lattnerb4307b32010-01-15 19:28:38 +0000705bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000706 SMLoc EndLoc;
707 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000708}
709
Daniel Dunbarcceba832010-09-17 02:47:07 +0000710const MCExpr *
711AsmParser::ApplyModifierToExpr(const MCExpr *E,
712 MCSymbolRefExpr::VariantKind Variant) {
713 // Recurse over the given expression, rebuilding it to apply the given variant
714 // if there is exactly one symbol.
715 switch (E->getKind()) {
716 case MCExpr::Target:
717 case MCExpr::Constant:
718 return 0;
719
720 case MCExpr::SymbolRef: {
721 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
722
723 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
724 TokError("invalid variant on expression '" +
725 getTok().getIdentifier() + "' (already modified)");
726 return E;
727 }
728
729 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
730 }
731
732 case MCExpr::Unary: {
733 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
734 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
735 if (!Sub)
736 return 0;
737 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
738 }
739
740 case MCExpr::Binary: {
741 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
742 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
743 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
744
745 if (!LHS && !RHS)
746 return 0;
747
748 if (!LHS) LHS = BE->getLHS();
749 if (!RHS) RHS = BE->getRHS();
750
751 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
752 }
753 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000754
755 assert(0 && "Invalid expression kind!");
756 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000757}
758
Chris Lattner74ec1a32009-06-22 06:32:03 +0000759/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000760///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000761/// expr ::= expr &&,|| expr -> lowest.
762/// expr ::= expr |,^,&,! expr
763/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
764/// expr ::= expr <<,>> expr
765/// expr ::= expr +,- expr
766/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000767/// expr ::= primaryexpr
768///
Chris Lattner54482b42010-01-15 19:39:23 +0000769bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000770 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000771 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000772 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
773 return true;
774
Daniel Dunbarcceba832010-09-17 02:47:07 +0000775 // As a special case, we support 'a op b @ modifier' by rewriting the
776 // expression to include the modifier. This is inefficient, but in general we
777 // expect users to use 'a@modifier op b'.
778 if (Lexer.getKind() == AsmToken::At) {
779 Lex();
780
781 if (Lexer.isNot(AsmToken::Identifier))
782 return TokError("unexpected symbol modifier following '@'");
783
784 MCSymbolRefExpr::VariantKind Variant =
785 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
786 if (Variant == MCSymbolRefExpr::VK_Invalid)
787 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
788
789 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
790 if (!ModifiedRes) {
791 return TokError("invalid modifier '" + getTok().getIdentifier() +
792 "' (no symbols present)");
793 return true;
794 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000795
Daniel Dunbarcceba832010-09-17 02:47:07 +0000796 Res = ModifiedRes;
797 Lex();
798 }
799
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000800 // Try to constant fold it up front, if possible.
801 int64_t Value;
802 if (Res->EvaluateAsAbsolute(Value))
803 Res = MCConstantExpr::Create(Value, getContext());
804
805 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000806}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000807
Chris Lattnerb4307b32010-01-15 19:28:38 +0000808bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000809 Res = 0;
810 return ParseParenExpr(Res, EndLoc) ||
811 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000812}
813
Daniel Dunbar475839e2009-06-29 20:37:27 +0000814bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000815 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000816
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000817 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000818 if (ParseExpression(Expr))
819 return true;
820
Daniel Dunbare00b0112009-10-16 01:57:52 +0000821 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000822 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000823
824 return false;
825}
826
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000827static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000828 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000829 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000830 default:
831 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000832
Jim Grosbachfbe16812011-08-20 16:24:13 +0000833 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000834 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000835 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000836 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000837 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000838 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000839 return 1;
840
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000841
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000842 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000843 //
844 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000846 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000847 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000848 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000849 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000850 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000851 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000852 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000853 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000854
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000855 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000856 case AsmToken::EqualEqual:
857 Kind = MCBinaryExpr::EQ;
858 return 3;
859 case AsmToken::ExclaimEqual:
860 case AsmToken::LessGreater:
861 Kind = MCBinaryExpr::NE;
862 return 3;
863 case AsmToken::Less:
864 Kind = MCBinaryExpr::LT;
865 return 3;
866 case AsmToken::LessEqual:
867 Kind = MCBinaryExpr::LTE;
868 return 3;
869 case AsmToken::Greater:
870 Kind = MCBinaryExpr::GT;
871 return 3;
872 case AsmToken::GreaterEqual:
873 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000874 return 3;
875
Jim Grosbachfbe16812011-08-20 16:24:13 +0000876 // Intermediate Precedence: <<, >>
877 case AsmToken::LessLess:
878 Kind = MCBinaryExpr::Shl;
879 return 4;
880 case AsmToken::GreaterGreater:
881 Kind = MCBinaryExpr::Shr;
882 return 4;
883
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000884 // High Intermediate Precedence: +, -
885 case AsmToken::Plus:
886 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000887 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000888 case AsmToken::Minus:
889 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000890 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000891
Jim Grosbachfbe16812011-08-20 16:24:13 +0000892 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000893 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000894 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000895 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000896 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000897 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000898 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000899 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000900 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000901 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000902 }
903}
904
905
906/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
907/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000908bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
909 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000910 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000911 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000912 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000913
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000914 // If the next token is lower precedence than we are allowed to eat, return
915 // successfully with what we ate already.
916 if (TokPrec < Precedence)
917 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000918
Sean Callanan79ed1a82010-01-19 20:22:31 +0000919 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000920
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000921 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000922 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000923 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000924
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000925 // If BinOp binds less tightly with RHS than the operator after RHS, let
926 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000927 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000928 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000929 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000930 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000931 }
932
Daniel Dunbar475839e2009-06-29 20:37:27 +0000933 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000934 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000935 }
936}
937
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000938
939
940
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000941/// ParseStatement:
942/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000943/// ::= Label* Directive ...Operands... EndOfStatement
944/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000945bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000946 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000947 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000948 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000949 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000950 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000951
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000952 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000953 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000954 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000955 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000956 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000957 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000958 if (Lexer.is(AsmToken::Hash))
959 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000960
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000961 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000962 if (Lexer.is(AsmToken::Integer)) {
963 LocalLabelVal = getTok().getIntVal();
964 if (LocalLabelVal < 0) {
965 if (!TheCondState.Ignore)
966 return TokError("unexpected token at start of statement");
967 IDVal = "";
968 }
969 else {
970 IDVal = getTok().getString();
971 Lex(); // Consume the integer token to be used as an identifier token.
972 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000973 if (!TheCondState.Ignore)
974 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000975 }
976 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000977
978 } else if (Lexer.is(AsmToken::Dot)) {
979 // Treat '.' as a valid identifier in this context.
980 Lex();
981 IDVal = ".";
982
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000983 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000984 if (!TheCondState.Ignore)
985 return TokError("unexpected token at start of statement");
986 IDVal = "";
987 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000988
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000989
Chris Lattner7834fac2010-04-17 18:14:27 +0000990 // Handle conditional assembly here before checking for skipping. We
991 // have to do this so that .endif isn't skipped in a ".if 0" block for
992 // example.
993 if (IDVal == ".if")
994 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000995 if (IDVal == ".ifdef")
996 return ParseDirectiveIfdef(IDLoc, true);
997 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
998 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000999 if (IDVal == ".elseif")
1000 return ParseDirectiveElseIf(IDLoc);
1001 if (IDVal == ".else")
1002 return ParseDirectiveElse(IDLoc);
1003 if (IDVal == ".endif")
1004 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001005
Chris Lattner7834fac2010-04-17 18:14:27 +00001006 // If we are in a ".if 0" block, ignore this statement.
1007 if (TheCondState.Ignore) {
1008 EatToEndOfStatement();
1009 return false;
1010 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001011
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001012 // FIXME: Recurse on local labels?
1013
1014 // See what kind of statement we have.
1015 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001016 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001017 CheckForValidSection();
1018
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001019 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001020 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001021
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001022 // Diagnose attempt to use '.' as a label.
1023 if (IDVal == ".")
1024 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1025
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001026 // Diagnose attempt to use a variable as a label.
1027 //
1028 // FIXME: Diagnostics. Note the location of the definition as a label.
1029 // FIXME: This doesn't diagnose assignment to a symbol which has been
1030 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001031 MCSymbol *Sym;
1032 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001033 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001034 else
1035 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001036 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001037 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001038
Daniel Dunbar959fd882009-08-26 22:13:22 +00001039 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001040 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001041
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001042 // Consume any end of statement token, if present, to avoid spurious
1043 // AddBlankLine calls().
1044 if (Lexer.is(AsmToken::EndOfStatement)) {
1045 Lex();
1046 if (Lexer.is(AsmToken::Eof))
1047 return false;
1048 }
1049
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001050 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001051 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001052
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001054 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001055 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001056
Nico Weber4c4c7322011-01-28 03:04:41 +00001057 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001058
1059 default: // Normal instruction or directive.
1060 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001061 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001062
1063 // If macros are enabled, check to see if this is a macro instantiation.
1064 if (MacrosEnabled)
1065 if (const Macro *M = MacroMap.lookup(IDVal))
1066 return HandleMacroEntry(IDVal, IDLoc, M);
1067
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001068 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001069 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001070 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001071 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001072 return ParseDirectiveSet(IDVal, true);
1073 if (IDVal == ".equiv")
1074 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001075
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076 // Data directives
1077
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001078 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001079 return ParseDirectiveAscii(IDVal, false);
1080 if (IDVal == ".asciz" || IDVal == ".string")
1081 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001083 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001085 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001087 if (IDVal == ".value")
1088 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001089 if (IDVal == ".2byte")
1090 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001091 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001093 if (IDVal == ".int")
1094 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001095 if (IDVal == ".4byte")
1096 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001098 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001099 if (IDVal == ".8byte")
1100 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001101 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001102 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1103 if (IDVal == ".double")
1104 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001105
Eli Friedman5d68ec22010-07-19 04:17:25 +00001106 if (IDVal == ".align") {
1107 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1108 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1109 }
1110 if (IDVal == ".align32") {
1111 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1112 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1113 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001114 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001115 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001116 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001117 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001118 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001119 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001120 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001121 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001123 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001125 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1126
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001127 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001128 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001129
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001130 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001132 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001133 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001134 if (IDVal == ".zero")
1135 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001136
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001137 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001138
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001139 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001140 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001141 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001142 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001143 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001144 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001145 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001146 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001147 if (IDVal == ".symbol_resolver")
1148 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001149 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001150 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001151 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001152 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001153 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001154 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001155 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001156 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001157 if (IDVal == ".weak_def_can_be_hidden")
1158 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001159
Hans Wennborg5cc64912011-06-18 13:51:54 +00001160 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001161 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001162 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001163 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001164
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001165 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001166 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001167 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001168 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001169
Evan Chengbd27f5a2011-07-27 00:38:12 +00001170 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001171 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001172
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001173 // Look up the handler in the handler table.
1174 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1175 DirectiveMap.lookup(IDVal);
1176 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001177 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001178
Kevin Enderby9c656452009-09-10 20:51:44 +00001179 // Target hook for parsing target specific directives.
1180 if (!getTargetParser().ParseDirective(ID))
1181 return false;
1182
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001183 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001184 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001185 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001186 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001187
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001188 CheckForValidSection();
1189
Chris Lattnera7f13542010-05-19 23:34:33 +00001190 // Canonicalize the opcode to lower case.
1191 SmallString<128> Opcode;
1192 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1193 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001194
Chris Lattner98986712010-01-14 22:21:20 +00001195 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001196 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001197 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001198
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001199 // Dump the parsed representation, if requested.
1200 if (getShowParsedOperands()) {
1201 SmallString<256> Str;
1202 raw_svector_ostream OS(Str);
1203 OS << "parsed instruction: [";
1204 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1205 if (i != 0)
1206 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001207 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001208 }
1209 OS << "]";
1210
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001211 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001212 }
1213
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001214 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001215 if (!HadError)
1216 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1217 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001218
Chris Lattner98986712010-01-14 22:21:20 +00001219 // Free any parsed operands.
1220 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1221 delete ParsedOperands[i];
1222
Chris Lattnercbf8a982010-09-11 16:18:25 +00001223 // Don't skip the rest of the line, the instruction parser is responsible for
1224 // that.
1225 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001226}
Chris Lattner9a023f72009-06-24 04:43:34 +00001227
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001228/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1229/// since they may not be able to be tokenized to get to the end of line token.
1230void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001231 if (!Lexer.is(AsmToken::EndOfStatement))
1232 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001233 // Eat EOL.
1234 Lex();
1235}
1236
1237/// ParseCppHashLineFilenameComment as this:
1238/// ::= # number "filename"
1239/// or just as a full line comment if it doesn't have a number and a string.
1240bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1241 Lex(); // Eat the hash token.
1242
1243 if (getLexer().isNot(AsmToken::Integer)) {
1244 // Consume the line since in cases it is not a well-formed line directive,
1245 // as if were simply a full line comment.
1246 EatToEndOfLine();
1247 return false;
1248 }
1249
1250 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001251 Lex();
1252
1253 if (getLexer().isNot(AsmToken::String)) {
1254 EatToEndOfLine();
1255 return false;
1256 }
1257
1258 StringRef Filename = getTok().getString();
1259 // Get rid of the enclosing quotes.
1260 Filename = Filename.substr(1, Filename.size()-2);
1261
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001262 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1263 CppHashLoc = L;
1264 CppHashFilename = Filename;
1265 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001266
1267 // Ignore any trailing characters, they're just comment.
1268 EatToEndOfLine();
1269 return false;
1270}
1271
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001272/// DiagHandler - will use the the last parsed cpp hash line filename comment
1273/// for the Filename and LineNo if any in the diagnostic.
1274void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1275 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1276 raw_ostream &OS = errs();
1277
1278 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1279 const SMLoc &DiagLoc = Diag.getLoc();
1280 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1281 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1282
1283 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1284 // before printing the message.
1285 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001286 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001287 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1288 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1289 }
1290
1291 // If we have not parsed a cpp hash line filename comment or the source
1292 // manager changed or buffer changed (like in a nested include) then just
1293 // print the normal diagnostic using its Filename and LineNo.
1294 if (!Parser->CppHashLineNumber ||
1295 &DiagSrcMgr != &Parser->SrcMgr ||
1296 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001297 if (Parser->SavedDiagHandler)
1298 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1299 else
1300 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001301 return;
1302 }
1303
1304 // Use the CppHashFilename and calculate a line number based on the
1305 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1306 // the diagnostic.
1307 const std::string Filename = Parser->CppHashFilename;
1308
1309 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1310 int CppHashLocLineNo =
1311 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1312 int LineNo = Parser->CppHashLineNumber - 1 +
1313 (DiagLocLineNo - CppHashLocLineNo);
1314
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001315 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1316 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001317 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001318 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001319
Benjamin Kramer04a04262011-10-16 10:48:29 +00001320 if (Parser->SavedDiagHandler)
1321 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1322 else
1323 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001324}
1325
Rafael Espindola65366442011-06-05 02:43:45 +00001326bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1327 const std::vector<StringRef> &Parameters,
1328 const std::vector<std::vector<AsmToken> > &A,
1329 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001330 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001331 unsigned NParameters = Parameters.size();
1332 if (NParameters != 0 && NParameters != A.size())
1333 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001334
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001335 while (!Body.empty()) {
1336 // Scan for the next substitution.
1337 std::size_t End = Body.size(), Pos = 0;
1338 for (; Pos != End; ++Pos) {
1339 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001340 if (!NParameters) {
1341 // This macro has no parameters, look for $0, $1, etc.
1342 if (Body[Pos] != '$' || Pos + 1 == End)
1343 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001344
Rafael Espindola65366442011-06-05 02:43:45 +00001345 char Next = Body[Pos + 1];
1346 if (Next == '$' || Next == 'n' || isdigit(Next))
1347 break;
1348 } else {
1349 // This macro has parameters, look for \foo, \bar, etc.
1350 if (Body[Pos] == '\\' && Pos + 1 != End)
1351 break;
1352 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001353 }
1354
1355 // Add the prefix.
1356 OS << Body.slice(0, Pos);
1357
1358 // Check if we reached the end.
1359 if (Pos == End)
1360 break;
1361
Rafael Espindola65366442011-06-05 02:43:45 +00001362 if (!NParameters) {
1363 switch (Body[Pos+1]) {
1364 // $$ => $
1365 case '$':
1366 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001367 break;
1368
Rafael Espindola65366442011-06-05 02:43:45 +00001369 // $n => number of arguments
1370 case 'n':
1371 OS << A.size();
1372 break;
1373
1374 // $[0-9] => argument
1375 default: {
1376 // Missing arguments are ignored.
1377 unsigned Index = Body[Pos+1] - '0';
1378 if (Index >= A.size())
1379 break;
1380
1381 // Otherwise substitute with the token values, with spaces eliminated.
1382 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1383 ie = A[Index].end(); it != ie; ++it)
1384 OS << it->getString();
1385 break;
1386 }
1387 }
1388 Pos += 2;
1389 } else {
1390 unsigned I = Pos + 1;
1391 while (isalnum(Body[I]) && I + 1 != End)
1392 ++I;
1393
1394 const char *Begin = Body.data() + Pos +1;
1395 StringRef Argument(Begin, I - (Pos +1));
1396 unsigned Index = 0;
1397 for (; Index < NParameters; ++Index)
1398 if (Parameters[Index] == Argument)
1399 break;
1400
1401 // FIXME: We should error at the macro definition.
1402 if (Index == NParameters)
1403 return Error(L, "Parameter not found");
1404
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001405 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1406 ie = A[Index].end(); it != ie; ++it)
1407 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001408
Rafael Espindola65366442011-06-05 02:43:45 +00001409 Pos += 1 + Argument.size();
1410 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001411 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001412 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001413 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001414
1415 // We include the .endmacro in the buffer as our queue to exit the macro
1416 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001417 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001418 return false;
1419}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001420
Rafael Espindola65366442011-06-05 02:43:45 +00001421MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1422 MemoryBuffer *I)
1423 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1424{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001425}
1426
1427bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1428 const Macro *M) {
1429 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1430 // this, although we should protect against infinite loops.
1431 if (ActiveMacros.size() == 20)
1432 return TokError("macros cannot be nested more than 20 levels deep");
1433
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001434 // Parse the macro instantiation arguments.
1435 std::vector<std::vector<AsmToken> > MacroArguments;
1436 MacroArguments.push_back(std::vector<AsmToken>());
1437 unsigned ParenLevel = 0;
1438 for (;;) {
1439 if (Lexer.is(AsmToken::Eof))
1440 return TokError("unexpected token in macro instantiation");
1441 if (Lexer.is(AsmToken::EndOfStatement))
1442 break;
1443
1444 // If we aren't inside parentheses and this is a comma, start a new token
1445 // list.
1446 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1447 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001448 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001449 // Adjust the current parentheses level.
1450 if (Lexer.is(AsmToken::LParen))
1451 ++ParenLevel;
1452 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1453 --ParenLevel;
1454
1455 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001456 MacroArguments.back().push_back(getTok());
1457 }
1458 Lex();
1459 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001460
Rafael Espindola65366442011-06-05 02:43:45 +00001461 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1462 // to hold the macro body with substitutions.
1463 SmallString<256> Buf;
1464 StringRef Body = M->Body;
1465
1466 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1467 return true;
1468
1469 MemoryBuffer *Instantiation =
1470 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1471
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001472 // Create the macro instantiation object and add to the current macro
1473 // instantiation stack.
1474 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001475 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001476 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001477 ActiveMacros.push_back(MI);
1478
1479 // Jump to the macro instantiation and prime the lexer.
1480 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1481 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1482 Lex();
1483
1484 return false;
1485}
1486
1487void AsmParser::HandleMacroExit() {
1488 // Jump to the EndOfStatement we should return to, and consume it.
1489 JumpToLoc(ActiveMacros.back()->ExitLoc);
1490 Lex();
1491
1492 // Pop the instantiation entry.
1493 delete ActiveMacros.back();
1494 ActiveMacros.pop_back();
1495}
1496
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001497static void MarkUsed(const MCExpr *Value) {
1498 switch (Value->getKind()) {
1499 case MCExpr::Binary:
1500 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1501 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1502 break;
1503 case MCExpr::Target:
1504 case MCExpr::Constant:
1505 break;
1506 case MCExpr::SymbolRef: {
1507 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1508 break;
1509 }
1510 case MCExpr::Unary:
1511 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1512 break;
1513 }
1514}
1515
Nico Weber4c4c7322011-01-28 03:04:41 +00001516bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001517 // FIXME: Use better location, we should use proper tokens.
1518 SMLoc EqualLoc = Lexer.getLoc();
1519
Daniel Dunbar821e3332009-08-31 08:09:28 +00001520 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001521 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001522 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001523
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001524 MarkUsed(Value);
1525
Daniel Dunbar3f872332009-07-28 16:08:33 +00001526 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001527 return TokError("unexpected token in assignment");
1528
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001529 // Error on assignment to '.'.
1530 if (Name == ".") {
1531 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1532 "(use '.space' or '.org').)"));
1533 }
1534
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001535 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001536 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001537
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001538 // Validate that the LHS is allowed to be a variable (either it has not been
1539 // used as a symbol, or it is an absolute symbol).
1540 MCSymbol *Sym = getContext().LookupSymbol(Name);
1541 if (Sym) {
1542 // Diagnose assignment to a label.
1543 //
1544 // FIXME: Diagnostics. Note the location of the definition as a label.
1545 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001546 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001547 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001548 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001549 return Error(EqualLoc, "redefinition of '" + Name + "'");
1550 else if (!Sym->isVariable())
1551 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001552 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001553 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1554 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001555
1556 // Don't count these checks as uses.
1557 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001558 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001559 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001560
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001561 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001562
1563 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001564 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001565
1566 return false;
1567}
1568
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001569/// ParseIdentifier:
1570/// ::= identifier
1571/// ::= string
1572bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001573 // The assembler has relaxed rules for accepting identifiers, in particular we
1574 // allow things like '.globl $foo', which would normally be separate
1575 // tokens. At this level, we have already lexed so we cannot (currently)
1576 // handle this as a context dependent token, instead we detect adjacent tokens
1577 // and return the combined identifier.
1578 if (Lexer.is(AsmToken::Dollar)) {
1579 SMLoc DollarLoc = getLexer().getLoc();
1580
1581 // Consume the dollar sign, and check for a following identifier.
1582 Lex();
1583 if (Lexer.isNot(AsmToken::Identifier))
1584 return true;
1585
1586 // We have a '$' followed by an identifier, make sure they are adjacent.
1587 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1588 return true;
1589
1590 // Construct the joined identifier and consume the token.
1591 Res = StringRef(DollarLoc.getPointer(),
1592 getTok().getIdentifier().size() + 1);
1593 Lex();
1594 return false;
1595 }
1596
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001597 if (Lexer.isNot(AsmToken::Identifier) &&
1598 Lexer.isNot(AsmToken::String))
1599 return true;
1600
Sean Callanan18b83232010-01-19 21:44:56 +00001601 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001602
Sean Callanan79ed1a82010-01-19 20:22:31 +00001603 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001604
1605 return false;
1606}
1607
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001608/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001609/// ::= .equ identifier ',' expression
1610/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001611/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001612bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001613 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001614
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001615 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001616 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001617
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001618 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001619 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001620 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001621
Nico Weber4c4c7322011-01-28 03:04:41 +00001622 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001623}
1624
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001625bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001626 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001627
1628 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001629 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001630 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1631 if (Str[i] != '\\') {
1632 Data += Str[i];
1633 continue;
1634 }
1635
1636 // Recognize escaped characters. Note that this escape semantics currently
1637 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1638 ++i;
1639 if (i == e)
1640 return TokError("unexpected backslash at end of string");
1641
1642 // Recognize octal sequences.
1643 if ((unsigned) (Str[i] - '0') <= 7) {
1644 // Consume up to three octal characters.
1645 unsigned Value = Str[i] - '0';
1646
1647 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1648 ++i;
1649 Value = Value * 8 + (Str[i] - '0');
1650
1651 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1652 ++i;
1653 Value = Value * 8 + (Str[i] - '0');
1654 }
1655 }
1656
1657 if (Value > 255)
1658 return TokError("invalid octal escape sequence (out of range)");
1659
1660 Data += (unsigned char) Value;
1661 continue;
1662 }
1663
1664 // Otherwise recognize individual escapes.
1665 switch (Str[i]) {
1666 default:
1667 // Just reject invalid escape sequences for now.
1668 return TokError("invalid escape sequence (unrecognized character)");
1669
1670 case 'b': Data += '\b'; break;
1671 case 'f': Data += '\f'; break;
1672 case 'n': Data += '\n'; break;
1673 case 'r': Data += '\r'; break;
1674 case 't': Data += '\t'; break;
1675 case '"': Data += '"'; break;
1676 case '\\': Data += '\\'; break;
1677 }
1678 }
1679
1680 return false;
1681}
1682
Daniel Dunbara0d14262009-06-24 23:30:00 +00001683/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001684/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1685bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001686 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001687 CheckForValidSection();
1688
Daniel Dunbara0d14262009-06-24 23:30:00 +00001689 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001690 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001691 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001692
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001693 std::string Data;
1694 if (ParseEscapedString(Data))
1695 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001696
1697 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001698 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001699 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1700
Sean Callanan79ed1a82010-01-19 20:22:31 +00001701 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001702
1703 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001704 break;
1705
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001707 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001708 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001709 }
1710 }
1711
Sean Callanan79ed1a82010-01-19 20:22:31 +00001712 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001713 return false;
1714}
1715
1716/// ParseDirectiveValue
1717/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1718bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001719 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001720 CheckForValidSection();
1721
Daniel Dunbara0d14262009-06-24 23:30:00 +00001722 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001723 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001724 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001725 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001726 return true;
1727
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001728 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001729 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1730 assert(Size <= 8 && "Invalid size");
1731 uint64_t IntValue = MCE->getValue();
1732 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1733 return Error(ExprLoc, "literal value out of range for directive");
1734 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1735 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001736 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001737
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001738 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001739 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001740
Daniel Dunbara0d14262009-06-24 23:30:00 +00001741 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001742 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001743 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001744 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001745 }
1746 }
1747
Sean Callanan79ed1a82010-01-19 20:22:31 +00001748 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001749 return false;
1750}
1751
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001752/// ParseDirectiveRealValue
1753/// ::= (.single | .double) [ expression (, expression)* ]
1754bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1755 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1756 CheckForValidSection();
1757
1758 for (;;) {
1759 // We don't truly support arithmetic on floating point expressions, so we
1760 // have to manually parse unary prefixes.
1761 bool IsNeg = false;
1762 if (getLexer().is(AsmToken::Minus)) {
1763 Lex();
1764 IsNeg = true;
1765 } else if (getLexer().is(AsmToken::Plus))
1766 Lex();
1767
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001768 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001769 getLexer().isNot(AsmToken::Real) &&
1770 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001771 return TokError("unexpected token in directive");
1772
1773 // Convert to an APFloat.
1774 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001775 StringRef IDVal = getTok().getString();
1776 if (getLexer().is(AsmToken::Identifier)) {
1777 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1778 Value = APFloat::getInf(Semantics);
1779 else if (!IDVal.compare_lower("nan"))
1780 Value = APFloat::getNaN(Semantics, false, ~0);
1781 else
1782 return TokError("invalid floating point literal");
1783 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001784 APFloat::opInvalidOp)
1785 return TokError("invalid floating point literal");
1786 if (IsNeg)
1787 Value.changeSign();
1788
1789 // Consume the numeric token.
1790 Lex();
1791
1792 // Emit the value as an integer.
1793 APInt AsInt = Value.bitcastToAPInt();
1794 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1795 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1796
1797 if (getLexer().is(AsmToken::EndOfStatement))
1798 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001799
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001800 if (getLexer().isNot(AsmToken::Comma))
1801 return TokError("unexpected token in directive");
1802 Lex();
1803 }
1804 }
1805
1806 Lex();
1807 return false;
1808}
1809
Daniel Dunbara0d14262009-06-24 23:30:00 +00001810/// ParseDirectiveSpace
1811/// ::= .space expression [ , expression ]
1812bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001813 CheckForValidSection();
1814
Daniel Dunbara0d14262009-06-24 23:30:00 +00001815 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001816 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001817 return true;
1818
1819 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001820 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1821 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001822 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001823 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001824
Daniel Dunbar475839e2009-06-29 20:37:27 +00001825 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001826 return true;
1827
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001828 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001829 return TokError("unexpected token in '.space' directive");
1830 }
1831
Sean Callanan79ed1a82010-01-19 20:22:31 +00001832 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001833
1834 if (NumBytes <= 0)
1835 return TokError("invalid number of bytes in '.space' directive");
1836
1837 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001839
1840 return false;
1841}
1842
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001843/// ParseDirectiveZero
1844/// ::= .zero expression
1845bool AsmParser::ParseDirectiveZero() {
1846 CheckForValidSection();
1847
1848 int64_t NumBytes;
1849 if (ParseAbsoluteExpression(NumBytes))
1850 return true;
1851
Rafael Espindolae452b172010-10-05 19:42:57 +00001852 int64_t Val = 0;
1853 if (getLexer().is(AsmToken::Comma)) {
1854 Lex();
1855 if (ParseAbsoluteExpression(Val))
1856 return true;
1857 }
1858
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001859 if (getLexer().isNot(AsmToken::EndOfStatement))
1860 return TokError("unexpected token in '.zero' directive");
1861
1862 Lex();
1863
Rafael Espindolae452b172010-10-05 19:42:57 +00001864 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001865
1866 return false;
1867}
1868
Daniel Dunbara0d14262009-06-24 23:30:00 +00001869/// ParseDirectiveFill
1870/// ::= .fill expression , expression , expression
1871bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001872 CheckForValidSection();
1873
Daniel Dunbara0d14262009-06-24 23:30:00 +00001874 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001875 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001876 return true;
1877
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001878 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001879 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001880 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001881
Daniel Dunbara0d14262009-06-24 23:30:00 +00001882 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001883 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001884 return true;
1885
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001886 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001887 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001888 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001889
Daniel Dunbara0d14262009-06-24 23:30:00 +00001890 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001891 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001892 return true;
1893
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001894 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001895 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001896
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001898
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001899 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1900 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001901
1902 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001903 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001904
1905 return false;
1906}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001907
1908/// ParseDirectiveOrg
1909/// ::= .org expression [ , expression ]
1910bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001911 CheckForValidSection();
1912
Daniel Dunbar821e3332009-08-31 08:09:28 +00001913 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001914 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001915 return true;
1916
1917 // Parse optional fill expression.
1918 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001919 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1920 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001921 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001922 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001923
Daniel Dunbar475839e2009-06-29 20:37:27 +00001924 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001925 return true;
1926
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001927 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001928 return TokError("unexpected token in '.org' directive");
1929 }
1930
Sean Callanan79ed1a82010-01-19 20:22:31 +00001931 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001932
1933 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1934 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001935 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001936
1937 return false;
1938}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001939
1940/// ParseDirectiveAlign
1941/// ::= {.align, ...} expression [ , expression [ , expression ]]
1942bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001943 CheckForValidSection();
1944
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001945 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001946 int64_t Alignment;
1947 if (ParseAbsoluteExpression(Alignment))
1948 return true;
1949
1950 SMLoc MaxBytesLoc;
1951 bool HasFillExpr = false;
1952 int64_t FillExpr = 0;
1953 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001954 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1955 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001956 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001957 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001958
1959 // The fill expression can be omitted while specifying a maximum number of
1960 // alignment bytes, e.g:
1961 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001962 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001963 HasFillExpr = true;
1964 if (ParseAbsoluteExpression(FillExpr))
1965 return true;
1966 }
1967
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001968 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1969 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001970 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001971 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001972
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001973 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001974 if (ParseAbsoluteExpression(MaxBytesToFill))
1975 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001976
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001977 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001978 return TokError("unexpected token in directive");
1979 }
1980 }
1981
Sean Callanan79ed1a82010-01-19 20:22:31 +00001982 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001983
Daniel Dunbar648ac512010-05-17 21:54:30 +00001984 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001985 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001986
1987 // Compute alignment in bytes.
1988 if (IsPow2) {
1989 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001990 if (Alignment >= 32) {
1991 Error(AlignmentLoc, "invalid alignment value");
1992 Alignment = 31;
1993 }
1994
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001995 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001996 }
1997
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001998 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001999 if (MaxBytesLoc.isValid()) {
2000 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002001 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2002 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002003 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002004 }
2005
2006 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002007 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2008 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002009 MaxBytesToFill = 0;
2010 }
2011 }
2012
Daniel Dunbar648ac512010-05-17 21:54:30 +00002013 // Check whether we should use optimal code alignment for this .align
2014 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002015 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002016 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2017 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002018 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002019 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002020 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002021 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2022 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002023 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002024
2025 return false;
2026}
2027
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002028/// ParseDirectiveSymbolAttribute
2029/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002030bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002031 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002032 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002033 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002034 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002035
2036 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002037 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002038
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002039 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002040
Jim Grosbach10ec6502011-09-15 17:56:49 +00002041 // Assembler local symbols don't make any sense here. Complain loudly.
2042 if (Sym->isTemporary())
2043 return Error(Loc, "non-local symbol required in directive");
2044
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002045 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002046
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002047 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002048 break;
2049
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002050 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002051 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002052 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002053 }
2054 }
2055
Sean Callanan79ed1a82010-01-19 20:22:31 +00002056 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002057 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002058}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002059
2060/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002061/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2062bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002063 CheckForValidSection();
2064
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002065 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002066 StringRef Name;
2067 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002068 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002069
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002070 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002071 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002072
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002073 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002074 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002075 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002076
2077 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002078 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002079 if (ParseAbsoluteExpression(Size))
2080 return true;
2081
2082 int64_t Pow2Alignment = 0;
2083 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002084 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002085 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002086 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002087 if (ParseAbsoluteExpression(Pow2Alignment))
2088 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002089
Chris Lattner258281d2010-01-19 06:22:22 +00002090 // If this target takes alignments in bytes (not log) validate and convert.
2091 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2092 if (!isPowerOf2_64(Pow2Alignment))
2093 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2094 Pow2Alignment = Log2_64(Pow2Alignment);
2095 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002096 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002097
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002098 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002099 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002100
Sean Callanan79ed1a82010-01-19 20:22:31 +00002101 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002102
Chris Lattner1fc3d752009-07-09 17:25:12 +00002103 // NOTE: a size of zero for a .comm should create a undefined symbol
2104 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002105 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002106 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2107 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002108
Eric Christopherc260a3e2010-05-14 01:38:54 +00002109 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002110 // may internally end up wanting an alignment in bytes.
2111 // FIXME: Diagnose overflow.
2112 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002113 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2114 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002115
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002116 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002117 return Error(IDLoc, "invalid symbol redefinition");
2118
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002119 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002120 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002121 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002122 getStreamer().EmitZerofill(Ctx.getMachOSection(
2123 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2124 0, SectionKind::getBSS()),
2125 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002126 return false;
2127 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002128
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002129 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002130 return false;
2131}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002132
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002133/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002134/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002135bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002136 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002137 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002138
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002139 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002140 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002141 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002142
Sean Callanan79ed1a82010-01-19 20:22:31 +00002143 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002144
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002145 if (Str.empty())
2146 Error(Loc, ".abort detected. Assembly stopping.");
2147 else
2148 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002149 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002150
2151 return false;
2152}
Kevin Enderby71148242009-07-14 21:35:03 +00002153
Kevin Enderby1f049b22009-07-14 23:21:55 +00002154/// ParseDirectiveInclude
2155/// ::= .include "filename"
2156bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002157 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002158 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002159
Sean Callanan18b83232010-01-19 21:44:56 +00002160 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002161 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002162 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002163
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002164 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002165 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002166
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002167 // Strip the quotes.
2168 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002169
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002170 // Attempt to switch the lexer to the included file before consuming the end
2171 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002172 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002173 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002174 return true;
2175 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002176
2177 return false;
2178}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002179
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002180/// ParseDirectiveIf
2181/// ::= .if expression
2182bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002183 TheCondStack.push_back(TheCondState);
2184 TheCondState.TheCond = AsmCond::IfCond;
2185 if(TheCondState.Ignore) {
2186 EatToEndOfStatement();
2187 }
2188 else {
2189 int64_t ExprValue;
2190 if (ParseAbsoluteExpression(ExprValue))
2191 return true;
2192
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002193 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002194 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002195
Sean Callanan79ed1a82010-01-19 20:22:31 +00002196 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002197
2198 TheCondState.CondMet = ExprValue;
2199 TheCondState.Ignore = !TheCondState.CondMet;
2200 }
2201
2202 return false;
2203}
2204
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002205bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2206 StringRef Name;
2207 TheCondStack.push_back(TheCondState);
2208 TheCondState.TheCond = AsmCond::IfCond;
2209
2210 if (TheCondState.Ignore) {
2211 EatToEndOfStatement();
2212 } else {
2213 if (ParseIdentifier(Name))
2214 return TokError("expected identifier after '.ifdef'");
2215
2216 Lex();
2217
2218 MCSymbol *Sym = getContext().LookupSymbol(Name);
2219
2220 if (expect_defined)
2221 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2222 else
2223 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2224 TheCondState.Ignore = !TheCondState.CondMet;
2225 }
2226
2227 return false;
2228}
2229
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002230/// ParseDirectiveElseIf
2231/// ::= .elseif expression
2232bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2233 if (TheCondState.TheCond != AsmCond::IfCond &&
2234 TheCondState.TheCond != AsmCond::ElseIfCond)
2235 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2236 " an .elseif");
2237 TheCondState.TheCond = AsmCond::ElseIfCond;
2238
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002239 bool LastIgnoreState = false;
2240 if (!TheCondStack.empty())
2241 LastIgnoreState = TheCondStack.back().Ignore;
2242 if (LastIgnoreState || TheCondState.CondMet) {
2243 TheCondState.Ignore = true;
2244 EatToEndOfStatement();
2245 }
2246 else {
2247 int64_t ExprValue;
2248 if (ParseAbsoluteExpression(ExprValue))
2249 return true;
2250
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002251 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002252 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002253
Sean Callanan79ed1a82010-01-19 20:22:31 +00002254 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002255 TheCondState.CondMet = ExprValue;
2256 TheCondState.Ignore = !TheCondState.CondMet;
2257 }
2258
2259 return false;
2260}
2261
2262/// ParseDirectiveElse
2263/// ::= .else
2264bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002265 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002266 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002267
Sean Callanan79ed1a82010-01-19 20:22:31 +00002268 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002269
2270 if (TheCondState.TheCond != AsmCond::IfCond &&
2271 TheCondState.TheCond != AsmCond::ElseIfCond)
2272 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2273 ".elseif");
2274 TheCondState.TheCond = AsmCond::ElseCond;
2275 bool LastIgnoreState = false;
2276 if (!TheCondStack.empty())
2277 LastIgnoreState = TheCondStack.back().Ignore;
2278 if (LastIgnoreState || TheCondState.CondMet)
2279 TheCondState.Ignore = true;
2280 else
2281 TheCondState.Ignore = false;
2282
2283 return false;
2284}
2285
2286/// ParseDirectiveEndIf
2287/// ::= .endif
2288bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002289 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002290 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002291
Sean Callanan79ed1a82010-01-19 20:22:31 +00002292 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002293
2294 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2295 TheCondStack.empty())
2296 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2297 ".else");
2298 if (!TheCondStack.empty()) {
2299 TheCondState = TheCondStack.back();
2300 TheCondStack.pop_back();
2301 }
2302
2303 return false;
2304}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002305
2306/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002307/// ::= .file [number] filename
2308/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002309bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002310 // FIXME: I'm not sure what this is.
2311 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002312 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002313 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002314 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002315 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002316
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002317 if (FileNumber < 1)
2318 return TokError("file number less than one");
2319 }
2320
Daniel Dunbareceec052010-07-12 17:45:27 +00002321 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002322 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002323
Nick Lewycky44d798d2011-10-17 23:05:28 +00002324 // Usually the directory and filename together, otherwise just the directory.
2325 StringRef Path = getTok().getString();
2326 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002327 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002328
Nick Lewycky44d798d2011-10-17 23:05:28 +00002329 StringRef Directory;
2330 StringRef Filename;
2331 if (getLexer().is(AsmToken::String)) {
2332 if (FileNumber == -1)
2333 return TokError("explicit path specified, but no file number");
2334 Filename = getTok().getString();
2335 Filename = Filename.substr(1, Filename.size()-2);
2336 Directory = Path;
2337 Lex();
2338 } else {
2339 Filename = Path;
2340 }
2341
Daniel Dunbareceec052010-07-12 17:45:27 +00002342 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002343 return TokError("unexpected token in '.file' directive");
2344
Chris Lattnerd32e8032010-01-25 19:02:58 +00002345 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002346 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002347 else {
Nick Lewycky44d798d2011-10-17 23:05:28 +00002348 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002349 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002350 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002351
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002352 return false;
2353}
2354
2355/// ParseDirectiveLine
2356/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002357bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002358 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2359 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002360 return TokError("unexpected token in '.line' directive");
2361
Sean Callanan18b83232010-01-19 21:44:56 +00002362 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002363 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002364 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002365
2366 // FIXME: Do something with the .line.
2367 }
2368
Daniel Dunbareceec052010-07-12 17:45:27 +00002369 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002370 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002371
2372 return false;
2373}
2374
2375
2376/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002377/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002378/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2379/// The first number is a file number, must have been previously assigned with
2380/// a .file directive, the second number is the line number and optionally the
2381/// third number is a column position (zero if not specified). The remaining
2382/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002383bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002384
Daniel Dunbareceec052010-07-12 17:45:27 +00002385 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002386 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002387 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002388 if (FileNumber < 1)
2389 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002390 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002391 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002392 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002393
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002394 int64_t LineNumber = 0;
2395 if (getLexer().is(AsmToken::Integer)) {
2396 LineNumber = getTok().getIntVal();
2397 if (LineNumber < 1)
2398 return TokError("line number less than one in '.loc' directive");
2399 Lex();
2400 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002401
2402 int64_t ColumnPos = 0;
2403 if (getLexer().is(AsmToken::Integer)) {
2404 ColumnPos = getTok().getIntVal();
2405 if (ColumnPos < 0)
2406 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002407 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002408 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002409
Kevin Enderbyc0957932010-09-30 16:52:03 +00002410 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002411 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002412 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002413 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2414 for (;;) {
2415 if (getLexer().is(AsmToken::EndOfStatement))
2416 break;
2417
2418 StringRef Name;
2419 SMLoc Loc = getTok().getLoc();
2420 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002421 return TokError("unexpected token in '.loc' directive");
2422
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002423 if (Name == "basic_block")
2424 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2425 else if (Name == "prologue_end")
2426 Flags |= DWARF2_FLAG_PROLOGUE_END;
2427 else if (Name == "epilogue_begin")
2428 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2429 else if (Name == "is_stmt") {
2430 SMLoc Loc = getTok().getLoc();
2431 const MCExpr *Value;
2432 if (getParser().ParseExpression(Value))
2433 return true;
2434 // The expression must be the constant 0 or 1.
2435 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2436 int Value = MCE->getValue();
2437 if (Value == 0)
2438 Flags &= ~DWARF2_FLAG_IS_STMT;
2439 else if (Value == 1)
2440 Flags |= DWARF2_FLAG_IS_STMT;
2441 else
2442 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002443 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002444 else {
2445 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2446 }
2447 }
2448 else if (Name == "isa") {
2449 SMLoc Loc = getTok().getLoc();
2450 const MCExpr *Value;
2451 if (getParser().ParseExpression(Value))
2452 return true;
2453 // The expression must be a constant greater or equal to 0.
2454 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2455 int Value = MCE->getValue();
2456 if (Value < 0)
2457 return Error(Loc, "isa number less than zero");
2458 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002459 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002460 else {
2461 return Error(Loc, "isa number not a constant value");
2462 }
2463 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002464 else if (Name == "discriminator") {
2465 if (getParser().ParseAbsoluteExpression(Discriminator))
2466 return true;
2467 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002468 else {
2469 return Error(Loc, "unknown sub-directive in '.loc' directive");
2470 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002471
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002472 if (getLexer().is(AsmToken::EndOfStatement))
2473 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002474 }
2475 }
2476
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002477 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002478 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002479
2480 return false;
2481}
2482
Daniel Dunbar138abae2010-10-16 04:56:42 +00002483/// ParseDirectiveStabs
2484/// ::= .stabs string, number, number, number
2485bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2486 SMLoc DirectiveLoc) {
2487 return TokError("unsupported directive '" + Directive + "'");
2488}
2489
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002490/// ParseDirectiveCFISections
2491/// ::= .cfi_sections section [, section]
2492bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2493 SMLoc DirectiveLoc) {
2494 StringRef Name;
2495 bool EH = false;
2496 bool Debug = false;
2497
2498 if (getParser().ParseIdentifier(Name))
2499 return TokError("Expected an identifier");
2500
2501 if (Name == ".eh_frame")
2502 EH = true;
2503 else if (Name == ".debug_frame")
2504 Debug = true;
2505
2506 if (getLexer().is(AsmToken::Comma)) {
2507 Lex();
2508
2509 if (getParser().ParseIdentifier(Name))
2510 return TokError("Expected an identifier");
2511
2512 if (Name == ".eh_frame")
2513 EH = true;
2514 else if (Name == ".debug_frame")
2515 Debug = true;
2516 }
2517
2518 getStreamer().EmitCFISections(EH, Debug);
2519
2520 return false;
2521}
2522
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002523/// ParseDirectiveCFIStartProc
2524/// ::= .cfi_startproc
2525bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2526 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002527 getStreamer().EmitCFIStartProc();
2528 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002529}
2530
2531/// ParseDirectiveCFIEndProc
2532/// ::= .cfi_endproc
2533bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002534 getStreamer().EmitCFIEndProc();
2535 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002536}
2537
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002538/// ParseRegisterOrRegisterNumber - parse register name or number.
2539bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2540 SMLoc DirectiveLoc) {
2541 unsigned RegNo;
2542
Jim Grosbach6f888a82011-06-02 17:14:04 +00002543 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002544 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2545 DirectiveLoc))
2546 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002547 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002548 } else
2549 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002550
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002551 return false;
2552}
2553
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002554/// ParseDirectiveCFIDefCfa
2555/// ::= .cfi_def_cfa register, offset
2556bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2557 SMLoc DirectiveLoc) {
2558 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002559 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002560 return true;
2561
2562 if (getLexer().isNot(AsmToken::Comma))
2563 return TokError("unexpected token in directive");
2564 Lex();
2565
2566 int64_t Offset = 0;
2567 if (getParser().ParseAbsoluteExpression(Offset))
2568 return true;
2569
Rafael Espindola066c2f42011-04-12 23:59:07 +00002570 getStreamer().EmitCFIDefCfa(Register, Offset);
2571 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002572}
2573
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002574/// ParseDirectiveCFIDefCfaOffset
2575/// ::= .cfi_def_cfa_offset offset
2576bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2577 SMLoc DirectiveLoc) {
2578 int64_t Offset = 0;
2579 if (getParser().ParseAbsoluteExpression(Offset))
2580 return true;
2581
Rafael Espindola066c2f42011-04-12 23:59:07 +00002582 getStreamer().EmitCFIDefCfaOffset(Offset);
2583 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002584}
2585
2586/// ParseDirectiveCFIAdjustCfaOffset
2587/// ::= .cfi_adjust_cfa_offset adjustment
2588bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2589 SMLoc DirectiveLoc) {
2590 int64_t Adjustment = 0;
2591 if (getParser().ParseAbsoluteExpression(Adjustment))
2592 return true;
2593
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002594 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2595 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002596}
2597
2598/// ParseDirectiveCFIDefCfaRegister
2599/// ::= .cfi_def_cfa_register register
2600bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2601 SMLoc DirectiveLoc) {
2602 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002603 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002604 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002605
Rafael Espindola066c2f42011-04-12 23:59:07 +00002606 getStreamer().EmitCFIDefCfaRegister(Register);
2607 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002608}
2609
2610/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002611/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002612bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2613 int64_t Register = 0;
2614 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002615
2616 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002617 return true;
2618
2619 if (getLexer().isNot(AsmToken::Comma))
2620 return TokError("unexpected token in directive");
2621 Lex();
2622
2623 if (getParser().ParseAbsoluteExpression(Offset))
2624 return true;
2625
Rafael Espindola066c2f42011-04-12 23:59:07 +00002626 getStreamer().EmitCFIOffset(Register, Offset);
2627 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002628}
2629
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002630/// ParseDirectiveCFIRelOffset
2631/// ::= .cfi_rel_offset register, offset
2632bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2633 SMLoc DirectiveLoc) {
2634 int64_t Register = 0;
2635
2636 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2637 return true;
2638
2639 if (getLexer().isNot(AsmToken::Comma))
2640 return TokError("unexpected token in directive");
2641 Lex();
2642
2643 int64_t Offset = 0;
2644 if (getParser().ParseAbsoluteExpression(Offset))
2645 return true;
2646
Rafael Espindola25f492e2011-04-12 16:12:03 +00002647 getStreamer().EmitCFIRelOffset(Register, Offset);
2648 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002649}
2650
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002651static bool isValidEncoding(int64_t Encoding) {
2652 if (Encoding & ~0xff)
2653 return false;
2654
2655 if (Encoding == dwarf::DW_EH_PE_omit)
2656 return true;
2657
2658 const unsigned Format = Encoding & 0xf;
2659 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2660 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2661 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2662 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2663 return false;
2664
Rafael Espindolacaf11582010-12-29 04:31:26 +00002665 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002666 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002667 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002668 return false;
2669
2670 return true;
2671}
2672
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002673/// ParseDirectiveCFIPersonalityOrLsda
2674/// ::= .cfi_personality encoding, [symbol_name]
2675/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002676bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002677 SMLoc DirectiveLoc) {
2678 int64_t Encoding = 0;
2679 if (getParser().ParseAbsoluteExpression(Encoding))
2680 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002681 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002682 return false;
2683
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002684 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002685 return TokError("unsupported encoding.");
2686
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002687 if (getLexer().isNot(AsmToken::Comma))
2688 return TokError("unexpected token in directive");
2689 Lex();
2690
2691 StringRef Name;
2692 if (getParser().ParseIdentifier(Name))
2693 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002694
2695 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2696
2697 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002698 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002699 else {
2700 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002701 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002702 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002703 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002704}
2705
Rafael Espindolafe024d02010-12-28 18:36:23 +00002706/// ParseDirectiveCFIRememberState
2707/// ::= .cfi_remember_state
2708bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2709 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002710 getStreamer().EmitCFIRememberState();
2711 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002712}
2713
2714/// ParseDirectiveCFIRestoreState
2715/// ::= .cfi_remember_state
2716bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2717 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002718 getStreamer().EmitCFIRestoreState();
2719 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002720}
2721
Rafael Espindolac5754392011-04-12 15:31:05 +00002722/// ParseDirectiveCFISameValue
2723/// ::= .cfi_same_value register
2724bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2725 SMLoc DirectiveLoc) {
2726 int64_t Register = 0;
2727
2728 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2729 return true;
2730
2731 getStreamer().EmitCFISameValue(Register);
2732
2733 return false;
2734}
2735
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002736/// ParseDirectiveMacrosOnOff
2737/// ::= .macros_on
2738/// ::= .macros_off
2739bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2740 SMLoc DirectiveLoc) {
2741 if (getLexer().isNot(AsmToken::EndOfStatement))
2742 return Error(getLexer().getLoc(),
2743 "unexpected token in '" + Directive + "' directive");
2744
2745 getParser().MacrosEnabled = Directive == ".macros_on";
2746
2747 return false;
2748}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002749
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002750/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002751/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002752bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2753 SMLoc DirectiveLoc) {
2754 StringRef Name;
2755 if (getParser().ParseIdentifier(Name))
2756 return TokError("expected identifier in directive");
2757
Rafael Espindola65366442011-06-05 02:43:45 +00002758 std::vector<StringRef> Parameters;
2759 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2760 for(;;) {
2761 StringRef Parameter;
2762 if (getParser().ParseIdentifier(Parameter))
2763 return TokError("expected identifier in directive");
2764 Parameters.push_back(Parameter);
2765
2766 if (getLexer().isNot(AsmToken::Comma))
2767 break;
2768 Lex();
2769 }
2770 }
2771
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002772 if (getLexer().isNot(AsmToken::EndOfStatement))
2773 return TokError("unexpected token in '.macro' directive");
2774
2775 // Eat the end of statement.
2776 Lex();
2777
2778 AsmToken EndToken, StartToken = getTok();
2779
2780 // Lex the macro definition.
2781 for (;;) {
2782 // Check whether we have reached the end of the file.
2783 if (getLexer().is(AsmToken::Eof))
2784 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2785
2786 // Otherwise, check whether we have reach the .endmacro.
2787 if (getLexer().is(AsmToken::Identifier) &&
2788 (getTok().getIdentifier() == ".endm" ||
2789 getTok().getIdentifier() == ".endmacro")) {
2790 EndToken = getTok();
2791 Lex();
2792 if (getLexer().isNot(AsmToken::EndOfStatement))
2793 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2794 "' directive");
2795 break;
2796 }
2797
2798 // Otherwise, scan til the end of the statement.
2799 getParser().EatToEndOfStatement();
2800 }
2801
2802 if (getParser().MacroMap.lookup(Name)) {
2803 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2804 }
2805
2806 const char *BodyStart = StartToken.getLoc().getPointer();
2807 const char *BodyEnd = EndToken.getLoc().getPointer();
2808 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002809 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002810 return false;
2811}
2812
2813/// ParseDirectiveEndMacro
2814/// ::= .endm
2815/// ::= .endmacro
2816bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2817 SMLoc DirectiveLoc) {
2818 if (getLexer().isNot(AsmToken::EndOfStatement))
2819 return TokError("unexpected token in '" + Directive + "' directive");
2820
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002821 // If we are inside a macro instantiation, terminate the current
2822 // instantiation.
2823 if (!getParser().ActiveMacros.empty()) {
2824 getParser().HandleMacroExit();
2825 return false;
2826 }
2827
2828 // Otherwise, this .endmacro is a stray entry in the file; well formed
2829 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002830 return TokError("unexpected '" + Directive + "' in file, "
2831 "no current macro definition");
2832}
2833
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002834bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002835 getParser().CheckForValidSection();
2836
2837 const MCExpr *Value;
2838
2839 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002840 return true;
2841
2842 if (getLexer().isNot(AsmToken::EndOfStatement))
2843 return TokError("unexpected token in directive");
2844
2845 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002846 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002847 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002848 getStreamer().EmitULEB128Value(Value);
2849
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002850 return false;
2851}
2852
2853
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002854/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002855MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002856 MCContext &C, MCStreamer &Out,
2857 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002858 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002859}