blob: 943f270a0a719af8daf3f452dacdf41f78e48449 [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
Kevin Enderby613b7572011-11-01 22:27:22 +0000467 // If we are generating dwarf for assembly source files save the initial text
468 // section and generate a .file directive.
469 if (getContext().getGenDwarfForAssembly()) {
470 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000471 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
472 getStreamer().EmitLabel(SectionStartSym);
473 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000474 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
475 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
476 }
477
Chris Lattnerb717fb02009-07-02 21:53:43 +0000478 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000479 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000480 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000481
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000482 // We had an error, validate that one was emitted and recover by skipping to
483 // the next line.
484 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000485 EatToEndOfStatement();
486 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000487
488 if (TheCondState.TheCond != StartingCondState.TheCond ||
489 TheCondState.Ignore != StartingCondState.Ignore)
490 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000491
492 // Check to see there are no empty DwarfFile slots.
493 const std::vector<MCDwarfFile *> &MCDwarfFiles =
494 getContext().getMCDwarfFiles();
495 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000496 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000497 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000498 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000499
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000500 // Check to see that all assembler local symbols were actually defined.
501 // Targets that don't do subsections via symbols may not want this, though,
502 // so conservatively exclude them. Only do this if we're finalizing, though,
503 // as otherwise we won't necessarilly have seen everything yet.
504 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
505 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
506 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
507 e = Symbols.end();
508 i != e; ++i) {
509 MCSymbol *Sym = i->getValue();
510 // Variable symbols may not be marked as defined, so check those
511 // explicitly. If we know it's a variable, we have a definition for
512 // the purposes of this check.
513 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
514 // FIXME: We would really like to refer back to where the symbol was
515 // first referenced for a source location. We need to add something
516 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000517 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
518 "assembler local symbol '" + Sym->getName() +
519 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000520 }
521 }
522
523
Chris Lattner79180e22010-04-05 23:15:42 +0000524 // Finalize the output stream if there are no errors and if the client wants
525 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000526 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000527 Out.Finish();
528
Chris Lattnerb717fb02009-07-02 21:53:43 +0000529 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000530}
531
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000532void AsmParser::CheckForValidSection() {
533 if (!getStreamer().getCurrentSection()) {
534 TokError("expected section directive before assembly directive");
535 Out.SwitchSection(Ctx.getMachOSection(
536 "__TEXT", "__text",
537 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
538 0, SectionKind::getText()));
539 }
540}
541
Chris Lattner2cf5f142009-06-22 01:29:09 +0000542/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
543void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000544 while (Lexer.isNot(AsmToken::EndOfStatement) &&
545 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000546 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000547
Chris Lattner2cf5f142009-06-22 01:29:09 +0000548 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000549 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000550 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000551}
552
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000553StringRef AsmParser::ParseStringToEndOfStatement() {
554 const char *Start = getTok().getLoc().getPointer();
555
556 while (Lexer.isNot(AsmToken::EndOfStatement) &&
557 Lexer.isNot(AsmToken::Eof))
558 Lex();
559
560 const char *End = getTok().getLoc().getPointer();
561 return StringRef(Start, End - Start);
562}
Chris Lattnerc4193832009-06-22 05:51:26 +0000563
Chris Lattner74ec1a32009-06-22 06:32:03 +0000564/// ParseParenExpr - Parse a paren expression and return it.
565/// NOTE: This assumes the leading '(' has already been consumed.
566///
567/// parenexpr ::= expr)
568///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000569bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000570 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000571 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000572 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000573 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000574 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000575 return false;
576}
Chris Lattnerc4193832009-06-22 05:51:26 +0000577
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000578/// ParseBracketExpr - Parse a bracket expression and return it.
579/// NOTE: This assumes the leading '[' has already been consumed.
580///
581/// bracketexpr ::= expr]
582///
583bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
584 if (ParseExpression(Res)) return true;
585 if (Lexer.isNot(AsmToken::RBrac))
586 return TokError("expected ']' in brackets expression");
587 EndLoc = Lexer.getLoc();
588 Lex();
589 return false;
590}
591
Chris Lattner74ec1a32009-06-22 06:32:03 +0000592/// ParsePrimaryExpr - Parse a primary expression and return it.
593/// primaryexpr ::= (parenexpr
594/// primaryexpr ::= symbol
595/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000596/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000597/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000598bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000599 switch (Lexer.getKind()) {
600 default:
601 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000602 // If we have an error assume that we've already handled it.
603 case AsmToken::Error:
604 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000605 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000606 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000607 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000608 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000609 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000610 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000611 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000612 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000613 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000614 EndLoc = Lexer.getLoc();
615
616 StringRef Identifier;
617 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000618 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000619
Daniel Dunbarfffff912009-10-16 01:34:54 +0000620 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000621 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000622 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000623
624 // Lookup the symbol variant if used.
625 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000626 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000627 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000628 if (Variant == MCSymbolRefExpr::VK_Invalid) {
629 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000630 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000631 }
632 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000633
Daniel Dunbarfffff912009-10-16 01:34:54 +0000634 // If this is an absolute variable reference, substitute it now to preserve
635 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000636 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000637 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000638 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000639
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000640 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000641 return false;
642 }
643
644 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000645 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000646 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000647 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000648 case AsmToken::Integer: {
649 SMLoc Loc = getTok().getLoc();
650 int64_t IntVal = getTok().getIntVal();
651 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000652 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000653 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000654 // Look for 'b' or 'f' following an Integer as a directional label
655 if (Lexer.getKind() == AsmToken::Identifier) {
656 StringRef IDVal = getTok().getString();
657 if (IDVal == "f" || IDVal == "b"){
658 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
659 IDVal == "f" ? 1 : 0);
660 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
661 getContext());
662 if(IDVal == "b" && Sym->isUndefined())
663 return Error(Loc, "invalid reference to undefined symbol");
664 EndLoc = Lexer.getLoc();
665 Lex(); // Eat identifier.
666 }
667 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000668 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000669 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000670 case AsmToken::Real: {
671 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000672 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000673 Res = MCConstantExpr::Create(IntVal, getContext());
674 Lex(); // Eat token.
675 return false;
676 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000677 case AsmToken::Dot: {
678 // This is a '.' reference, which references the current PC. Emit a
679 // temporary label to the streamer and refer to it.
680 MCSymbol *Sym = Ctx.CreateTempSymbol();
681 Out.EmitLabel(Sym);
682 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
683 EndLoc = Lexer.getLoc();
684 Lex(); // Eat identifier.
685 return false;
686 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000687 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000688 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000689 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000690 case AsmToken::LBrac:
691 if (!PlatformParser->HasBracketExpressions())
692 return TokError("brackets expression not supported on this target");
693 Lex(); // Eat the '['.
694 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000695 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000696 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000697 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000698 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000699 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000700 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000701 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000702 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000703 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000704 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000705 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000706 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000707 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000708 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000709 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000710 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000711 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000712 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000713 }
714}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000715
Chris Lattnerb4307b32010-01-15 19:28:38 +0000716bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000717 SMLoc EndLoc;
718 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000719}
720
Daniel Dunbarcceba832010-09-17 02:47:07 +0000721const MCExpr *
722AsmParser::ApplyModifierToExpr(const MCExpr *E,
723 MCSymbolRefExpr::VariantKind Variant) {
724 // Recurse over the given expression, rebuilding it to apply the given variant
725 // if there is exactly one symbol.
726 switch (E->getKind()) {
727 case MCExpr::Target:
728 case MCExpr::Constant:
729 return 0;
730
731 case MCExpr::SymbolRef: {
732 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
733
734 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
735 TokError("invalid variant on expression '" +
736 getTok().getIdentifier() + "' (already modified)");
737 return E;
738 }
739
740 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
741 }
742
743 case MCExpr::Unary: {
744 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
745 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
746 if (!Sub)
747 return 0;
748 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
749 }
750
751 case MCExpr::Binary: {
752 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
753 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
754 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
755
756 if (!LHS && !RHS)
757 return 0;
758
759 if (!LHS) LHS = BE->getLHS();
760 if (!RHS) RHS = BE->getRHS();
761
762 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
763 }
764 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000765
766 assert(0 && "Invalid expression kind!");
767 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000768}
769
Chris Lattner74ec1a32009-06-22 06:32:03 +0000770/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000771///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000772/// expr ::= expr &&,|| expr -> lowest.
773/// expr ::= expr |,^,&,! expr
774/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
775/// expr ::= expr <<,>> expr
776/// expr ::= expr +,- expr
777/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000778/// expr ::= primaryexpr
779///
Chris Lattner54482b42010-01-15 19:39:23 +0000780bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000781 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000782 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000783 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
784 return true;
785
Daniel Dunbarcceba832010-09-17 02:47:07 +0000786 // As a special case, we support 'a op b @ modifier' by rewriting the
787 // expression to include the modifier. This is inefficient, but in general we
788 // expect users to use 'a@modifier op b'.
789 if (Lexer.getKind() == AsmToken::At) {
790 Lex();
791
792 if (Lexer.isNot(AsmToken::Identifier))
793 return TokError("unexpected symbol modifier following '@'");
794
795 MCSymbolRefExpr::VariantKind Variant =
796 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
797 if (Variant == MCSymbolRefExpr::VK_Invalid)
798 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
799
800 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
801 if (!ModifiedRes) {
802 return TokError("invalid modifier '" + getTok().getIdentifier() +
803 "' (no symbols present)");
804 return true;
805 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000806
Daniel Dunbarcceba832010-09-17 02:47:07 +0000807 Res = ModifiedRes;
808 Lex();
809 }
810
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000811 // Try to constant fold it up front, if possible.
812 int64_t Value;
813 if (Res->EvaluateAsAbsolute(Value))
814 Res = MCConstantExpr::Create(Value, getContext());
815
816 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000817}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000818
Chris Lattnerb4307b32010-01-15 19:28:38 +0000819bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000820 Res = 0;
821 return ParseParenExpr(Res, EndLoc) ||
822 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000823}
824
Daniel Dunbar475839e2009-06-29 20:37:27 +0000825bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000826 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000827
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000828 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000829 if (ParseExpression(Expr))
830 return true;
831
Daniel Dunbare00b0112009-10-16 01:57:52 +0000832 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000833 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000834
835 return false;
836}
837
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000838static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000839 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000840 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000841 default:
842 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000843
Jim Grosbachfbe16812011-08-20 16:24:13 +0000844 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000846 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000847 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000848 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000849 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000850 return 1;
851
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000852
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000853 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000854 //
855 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000856 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000857 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000858 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000859 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000860 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000861 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000862 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000863 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000864 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000865
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000866 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000867 case AsmToken::EqualEqual:
868 Kind = MCBinaryExpr::EQ;
869 return 3;
870 case AsmToken::ExclaimEqual:
871 case AsmToken::LessGreater:
872 Kind = MCBinaryExpr::NE;
873 return 3;
874 case AsmToken::Less:
875 Kind = MCBinaryExpr::LT;
876 return 3;
877 case AsmToken::LessEqual:
878 Kind = MCBinaryExpr::LTE;
879 return 3;
880 case AsmToken::Greater:
881 Kind = MCBinaryExpr::GT;
882 return 3;
883 case AsmToken::GreaterEqual:
884 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000885 return 3;
886
Jim Grosbachfbe16812011-08-20 16:24:13 +0000887 // Intermediate Precedence: <<, >>
888 case AsmToken::LessLess:
889 Kind = MCBinaryExpr::Shl;
890 return 4;
891 case AsmToken::GreaterGreater:
892 Kind = MCBinaryExpr::Shr;
893 return 4;
894
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000895 // High Intermediate Precedence: +, -
896 case AsmToken::Plus:
897 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000898 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000899 case AsmToken::Minus:
900 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000901 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000902
Jim Grosbachfbe16812011-08-20 16:24:13 +0000903 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000904 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000905 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000906 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000908 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000909 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000910 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000911 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000912 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000913 }
914}
915
916
917/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
918/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000919bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
920 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000921 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000922 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000923 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000924
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000925 // If the next token is lower precedence than we are allowed to eat, return
926 // successfully with what we ate already.
927 if (TokPrec < Precedence)
928 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000929
Sean Callanan79ed1a82010-01-19 20:22:31 +0000930 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000931
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000932 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000933 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000934 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000935
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000936 // If BinOp binds less tightly with RHS than the operator after RHS, let
937 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000938 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000939 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000940 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000941 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000942 }
943
Daniel Dunbar475839e2009-06-29 20:37:27 +0000944 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000945 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000946 }
947}
948
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000949
950
951
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000952/// ParseStatement:
953/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000954/// ::= Label* Directive ...Operands... EndOfStatement
955/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000956bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000957 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000958 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000959 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000960 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000961 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000962
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000963 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000964 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000965 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000966 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000967 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000968 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000969 if (Lexer.is(AsmToken::Hash))
970 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000971
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000972 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000973 if (Lexer.is(AsmToken::Integer)) {
974 LocalLabelVal = getTok().getIntVal();
975 if (LocalLabelVal < 0) {
976 if (!TheCondState.Ignore)
977 return TokError("unexpected token at start of statement");
978 IDVal = "";
979 }
980 else {
981 IDVal = getTok().getString();
982 Lex(); // Consume the integer token to be used as an identifier token.
983 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000984 if (!TheCondState.Ignore)
985 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000986 }
987 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000988
989 } else if (Lexer.is(AsmToken::Dot)) {
990 // Treat '.' as a valid identifier in this context.
991 Lex();
992 IDVal = ".";
993
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000994 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000995 if (!TheCondState.Ignore)
996 return TokError("unexpected token at start of statement");
997 IDVal = "";
998 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000999
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001000
Chris Lattner7834fac2010-04-17 18:14:27 +00001001 // Handle conditional assembly here before checking for skipping. We
1002 // have to do this so that .endif isn't skipped in a ".if 0" block for
1003 // example.
1004 if (IDVal == ".if")
1005 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001006 if (IDVal == ".ifdef")
1007 return ParseDirectiveIfdef(IDLoc, true);
1008 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1009 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001010 if (IDVal == ".elseif")
1011 return ParseDirectiveElseIf(IDLoc);
1012 if (IDVal == ".else")
1013 return ParseDirectiveElse(IDLoc);
1014 if (IDVal == ".endif")
1015 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001016
Chris Lattner7834fac2010-04-17 18:14:27 +00001017 // If we are in a ".if 0" block, ignore this statement.
1018 if (TheCondState.Ignore) {
1019 EatToEndOfStatement();
1020 return false;
1021 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001022
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001023 // FIXME: Recurse on local labels?
1024
1025 // See what kind of statement we have.
1026 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001027 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001028 CheckForValidSection();
1029
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001030 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001031 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001032
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001033 // Diagnose attempt to use '.' as a label.
1034 if (IDVal == ".")
1035 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1036
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001037 // Diagnose attempt to use a variable as a label.
1038 //
1039 // FIXME: Diagnostics. Note the location of the definition as a label.
1040 // FIXME: This doesn't diagnose assignment to a symbol which has been
1041 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001042 MCSymbol *Sym;
1043 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001044 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001045 else
1046 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001047 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001048 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001049
Daniel Dunbar959fd882009-08-26 22:13:22 +00001050 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001051 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001052
Kevin Enderby94c2e852011-12-09 18:09:40 +00001053 // If we are generating dwarf for assembly source files then gather the
1054 // info to make a dwarf subprogram entry for this label if needed.
1055 if (getContext().getGenDwarfForAssembly())
1056 MCGenDwarfSubprogramEntry::Make(Sym, &getStreamer(), getSourceManager(),
1057 IDLoc);
1058
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001059 // Consume any end of statement token, if present, to avoid spurious
1060 // AddBlankLine calls().
1061 if (Lexer.is(AsmToken::EndOfStatement)) {
1062 Lex();
1063 if (Lexer.is(AsmToken::Eof))
1064 return false;
1065 }
1066
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001067 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001068 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001069
Daniel Dunbar3f872332009-07-28 16:08:33 +00001070 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001071 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001072 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001073
Nico Weber4c4c7322011-01-28 03:04:41 +00001074 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001075
1076 default: // Normal instruction or directive.
1077 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001078 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001079
1080 // If macros are enabled, check to see if this is a macro instantiation.
1081 if (MacrosEnabled)
1082 if (const Macro *M = MacroMap.lookup(IDVal))
1083 return HandleMacroEntry(IDVal, IDLoc, M);
1084
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001085 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001086 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001087 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001088 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001089 return ParseDirectiveSet(IDVal, true);
1090 if (IDVal == ".equiv")
1091 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001092
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093 // Data directives
1094
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001095 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001096 return ParseDirectiveAscii(IDVal, false);
1097 if (IDVal == ".asciz" || IDVal == ".string")
1098 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001099
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001100 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001102 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001103 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001104 if (IDVal == ".value")
1105 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001106 if (IDVal == ".2byte")
1107 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001108 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001109 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001110 if (IDVal == ".int")
1111 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001112 if (IDVal == ".4byte")
1113 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001114 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001116 if (IDVal == ".8byte")
1117 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001118 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001119 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1120 if (IDVal == ".double")
1121 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001122
Eli Friedman5d68ec22010-07-19 04:17:25 +00001123 if (IDVal == ".align") {
1124 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1125 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1126 }
1127 if (IDVal == ".align32") {
1128 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1129 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1130 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001131 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001132 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001133 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001134 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001135 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001136 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001137 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001138 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001139 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001140 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001141 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001142 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1143
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001144 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001145 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001146
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001147 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001148 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001149 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001150 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001151 if (IDVal == ".zero")
1152 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001153
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001154 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001155
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001156 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001157 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001158 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001159 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001160 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001161 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001162 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001163 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001164 if (IDVal == ".symbol_resolver")
1165 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001166 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001167 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001168 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001169 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001170 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001171 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001172 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001173 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001174 if (IDVal == ".weak_def_can_be_hidden")
1175 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001176
Hans Wennborg5cc64912011-06-18 13:51:54 +00001177 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001178 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001179 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001180 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001181
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001182 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001183 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001184 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001185 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001186
Evan Chengbd27f5a2011-07-27 00:38:12 +00001187 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001188 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001189
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001190 // Look up the handler in the handler table.
1191 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1192 DirectiveMap.lookup(IDVal);
1193 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001194 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001195
Kevin Enderby9c656452009-09-10 20:51:44 +00001196 // Target hook for parsing target specific directives.
1197 if (!getTargetParser().ParseDirective(ID))
1198 return false;
1199
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001200 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001201 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001202 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001203 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001204
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001205 CheckForValidSection();
1206
Chris Lattnera7f13542010-05-19 23:34:33 +00001207 // Canonicalize the opcode to lower case.
1208 SmallString<128> Opcode;
1209 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1210 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001211
Chris Lattner98986712010-01-14 22:21:20 +00001212 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001213 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001214 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001215
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001216 // Dump the parsed representation, if requested.
1217 if (getShowParsedOperands()) {
1218 SmallString<256> Str;
1219 raw_svector_ostream OS(Str);
1220 OS << "parsed instruction: [";
1221 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1222 if (i != 0)
1223 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001224 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001225 }
1226 OS << "]";
1227
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001228 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001229 }
1230
Kevin Enderby613b7572011-11-01 22:27:22 +00001231 // If we are generating dwarf for assembly source files and the current
1232 // section is the initial text section then generate a .loc directive for
1233 // the instruction.
1234 if (!HadError && getContext().getGenDwarfForAssembly() &&
1235 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1236 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1237 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1238 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001239 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001240 StringRef());
1241 }
1242
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001243 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001244 if (!HadError)
1245 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1246 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001247
Chris Lattner98986712010-01-14 22:21:20 +00001248 // Free any parsed operands.
1249 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1250 delete ParsedOperands[i];
1251
Chris Lattnercbf8a982010-09-11 16:18:25 +00001252 // Don't skip the rest of the line, the instruction parser is responsible for
1253 // that.
1254 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001255}
Chris Lattner9a023f72009-06-24 04:43:34 +00001256
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001257/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1258/// since they may not be able to be tokenized to get to the end of line token.
1259void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001260 if (!Lexer.is(AsmToken::EndOfStatement))
1261 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001262 // Eat EOL.
1263 Lex();
1264}
1265
1266/// ParseCppHashLineFilenameComment as this:
1267/// ::= # number "filename"
1268/// or just as a full line comment if it doesn't have a number and a string.
1269bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1270 Lex(); // Eat the hash token.
1271
1272 if (getLexer().isNot(AsmToken::Integer)) {
1273 // Consume the line since in cases it is not a well-formed line directive,
1274 // as if were simply a full line comment.
1275 EatToEndOfLine();
1276 return false;
1277 }
1278
1279 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001280 Lex();
1281
1282 if (getLexer().isNot(AsmToken::String)) {
1283 EatToEndOfLine();
1284 return false;
1285 }
1286
1287 StringRef Filename = getTok().getString();
1288 // Get rid of the enclosing quotes.
1289 Filename = Filename.substr(1, Filename.size()-2);
1290
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001291 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1292 CppHashLoc = L;
1293 CppHashFilename = Filename;
1294 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001295
1296 // Ignore any trailing characters, they're just comment.
1297 EatToEndOfLine();
1298 return false;
1299}
1300
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001301/// DiagHandler - will use the the last parsed cpp hash line filename comment
1302/// for the Filename and LineNo if any in the diagnostic.
1303void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1304 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1305 raw_ostream &OS = errs();
1306
1307 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1308 const SMLoc &DiagLoc = Diag.getLoc();
1309 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1310 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1311
1312 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1313 // before printing the message.
1314 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001315 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001316 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1317 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1318 }
1319
1320 // If we have not parsed a cpp hash line filename comment or the source
1321 // manager changed or buffer changed (like in a nested include) then just
1322 // print the normal diagnostic using its Filename and LineNo.
1323 if (!Parser->CppHashLineNumber ||
1324 &DiagSrcMgr != &Parser->SrcMgr ||
1325 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001326 if (Parser->SavedDiagHandler)
1327 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1328 else
1329 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001330 return;
1331 }
1332
1333 // Use the CppHashFilename and calculate a line number based on the
1334 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1335 // the diagnostic.
1336 const std::string Filename = Parser->CppHashFilename;
1337
1338 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1339 int CppHashLocLineNo =
1340 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1341 int LineNo = Parser->CppHashLineNumber - 1 +
1342 (DiagLocLineNo - CppHashLocLineNo);
1343
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001344 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1345 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001346 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001347 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001348
Benjamin Kramer04a04262011-10-16 10:48:29 +00001349 if (Parser->SavedDiagHandler)
1350 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1351 else
1352 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001353}
1354
Rafael Espindola65366442011-06-05 02:43:45 +00001355bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1356 const std::vector<StringRef> &Parameters,
1357 const std::vector<std::vector<AsmToken> > &A,
1358 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001359 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001360 unsigned NParameters = Parameters.size();
1361 if (NParameters != 0 && NParameters != A.size())
1362 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001363
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001364 while (!Body.empty()) {
1365 // Scan for the next substitution.
1366 std::size_t End = Body.size(), Pos = 0;
1367 for (; Pos != End; ++Pos) {
1368 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001369 if (!NParameters) {
1370 // This macro has no parameters, look for $0, $1, etc.
1371 if (Body[Pos] != '$' || Pos + 1 == End)
1372 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001373
Rafael Espindola65366442011-06-05 02:43:45 +00001374 char Next = Body[Pos + 1];
1375 if (Next == '$' || Next == 'n' || isdigit(Next))
1376 break;
1377 } else {
1378 // This macro has parameters, look for \foo, \bar, etc.
1379 if (Body[Pos] == '\\' && Pos + 1 != End)
1380 break;
1381 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001382 }
1383
1384 // Add the prefix.
1385 OS << Body.slice(0, Pos);
1386
1387 // Check if we reached the end.
1388 if (Pos == End)
1389 break;
1390
Rafael Espindola65366442011-06-05 02:43:45 +00001391 if (!NParameters) {
1392 switch (Body[Pos+1]) {
1393 // $$ => $
1394 case '$':
1395 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001396 break;
1397
Rafael Espindola65366442011-06-05 02:43:45 +00001398 // $n => number of arguments
1399 case 'n':
1400 OS << A.size();
1401 break;
1402
1403 // $[0-9] => argument
1404 default: {
1405 // Missing arguments are ignored.
1406 unsigned Index = Body[Pos+1] - '0';
1407 if (Index >= A.size())
1408 break;
1409
1410 // Otherwise substitute with the token values, with spaces eliminated.
1411 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1412 ie = A[Index].end(); it != ie; ++it)
1413 OS << it->getString();
1414 break;
1415 }
1416 }
1417 Pos += 2;
1418 } else {
1419 unsigned I = Pos + 1;
1420 while (isalnum(Body[I]) && I + 1 != End)
1421 ++I;
1422
1423 const char *Begin = Body.data() + Pos +1;
1424 StringRef Argument(Begin, I - (Pos +1));
1425 unsigned Index = 0;
1426 for (; Index < NParameters; ++Index)
1427 if (Parameters[Index] == Argument)
1428 break;
1429
1430 // FIXME: We should error at the macro definition.
1431 if (Index == NParameters)
1432 return Error(L, "Parameter not found");
1433
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001434 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1435 ie = A[Index].end(); it != ie; ++it)
1436 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001437
Rafael Espindola65366442011-06-05 02:43:45 +00001438 Pos += 1 + Argument.size();
1439 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001440 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001441 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001442 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001443
1444 // We include the .endmacro in the buffer as our queue to exit the macro
1445 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001446 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001447 return false;
1448}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001449
Rafael Espindola65366442011-06-05 02:43:45 +00001450MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1451 MemoryBuffer *I)
1452 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1453{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001454}
1455
1456bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1457 const Macro *M) {
1458 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1459 // this, although we should protect against infinite loops.
1460 if (ActiveMacros.size() == 20)
1461 return TokError("macros cannot be nested more than 20 levels deep");
1462
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001463 // Parse the macro instantiation arguments.
1464 std::vector<std::vector<AsmToken> > MacroArguments;
1465 MacroArguments.push_back(std::vector<AsmToken>());
1466 unsigned ParenLevel = 0;
1467 for (;;) {
1468 if (Lexer.is(AsmToken::Eof))
1469 return TokError("unexpected token in macro instantiation");
1470 if (Lexer.is(AsmToken::EndOfStatement))
1471 break;
1472
1473 // If we aren't inside parentheses and this is a comma, start a new token
1474 // list.
1475 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1476 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001477 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001478 // Adjust the current parentheses level.
1479 if (Lexer.is(AsmToken::LParen))
1480 ++ParenLevel;
1481 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1482 --ParenLevel;
1483
1484 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001485 MacroArguments.back().push_back(getTok());
1486 }
1487 Lex();
1488 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001489
Rafael Espindola65366442011-06-05 02:43:45 +00001490 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1491 // to hold the macro body with substitutions.
1492 SmallString<256> Buf;
1493 StringRef Body = M->Body;
1494
1495 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1496 return true;
1497
1498 MemoryBuffer *Instantiation =
1499 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1500
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001501 // Create the macro instantiation object and add to the current macro
1502 // instantiation stack.
1503 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001504 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001505 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001506 ActiveMacros.push_back(MI);
1507
1508 // Jump to the macro instantiation and prime the lexer.
1509 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1510 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1511 Lex();
1512
1513 return false;
1514}
1515
1516void AsmParser::HandleMacroExit() {
1517 // Jump to the EndOfStatement we should return to, and consume it.
1518 JumpToLoc(ActiveMacros.back()->ExitLoc);
1519 Lex();
1520
1521 // Pop the instantiation entry.
1522 delete ActiveMacros.back();
1523 ActiveMacros.pop_back();
1524}
1525
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001526static void MarkUsed(const MCExpr *Value) {
1527 switch (Value->getKind()) {
1528 case MCExpr::Binary:
1529 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1530 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1531 break;
1532 case MCExpr::Target:
1533 case MCExpr::Constant:
1534 break;
1535 case MCExpr::SymbolRef: {
1536 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1537 break;
1538 }
1539 case MCExpr::Unary:
1540 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1541 break;
1542 }
1543}
1544
Nico Weber4c4c7322011-01-28 03:04:41 +00001545bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001546 // FIXME: Use better location, we should use proper tokens.
1547 SMLoc EqualLoc = Lexer.getLoc();
1548
Daniel Dunbar821e3332009-08-31 08:09:28 +00001549 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001550 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001551 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001552
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001553 MarkUsed(Value);
1554
Daniel Dunbar3f872332009-07-28 16:08:33 +00001555 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001556 return TokError("unexpected token in assignment");
1557
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001558 // Error on assignment to '.'.
1559 if (Name == ".") {
1560 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1561 "(use '.space' or '.org').)"));
1562 }
1563
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001564 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001565 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001566
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001567 // Validate that the LHS is allowed to be a variable (either it has not been
1568 // used as a symbol, or it is an absolute symbol).
1569 MCSymbol *Sym = getContext().LookupSymbol(Name);
1570 if (Sym) {
1571 // Diagnose assignment to a label.
1572 //
1573 // FIXME: Diagnostics. Note the location of the definition as a label.
1574 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001575 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001576 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001577 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001578 return Error(EqualLoc, "redefinition of '" + Name + "'");
1579 else if (!Sym->isVariable())
1580 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001581 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001582 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1583 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001584
1585 // Don't count these checks as uses.
1586 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001587 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001588 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001589
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001590 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001591
1592 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001593 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001594
1595 return false;
1596}
1597
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001598/// ParseIdentifier:
1599/// ::= identifier
1600/// ::= string
1601bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001602 // The assembler has relaxed rules for accepting identifiers, in particular we
1603 // allow things like '.globl $foo', which would normally be separate
1604 // tokens. At this level, we have already lexed so we cannot (currently)
1605 // handle this as a context dependent token, instead we detect adjacent tokens
1606 // and return the combined identifier.
1607 if (Lexer.is(AsmToken::Dollar)) {
1608 SMLoc DollarLoc = getLexer().getLoc();
1609
1610 // Consume the dollar sign, and check for a following identifier.
1611 Lex();
1612 if (Lexer.isNot(AsmToken::Identifier))
1613 return true;
1614
1615 // We have a '$' followed by an identifier, make sure they are adjacent.
1616 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1617 return true;
1618
1619 // Construct the joined identifier and consume the token.
1620 Res = StringRef(DollarLoc.getPointer(),
1621 getTok().getIdentifier().size() + 1);
1622 Lex();
1623 return false;
1624 }
1625
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001626 if (Lexer.isNot(AsmToken::Identifier) &&
1627 Lexer.isNot(AsmToken::String))
1628 return true;
1629
Sean Callanan18b83232010-01-19 21:44:56 +00001630 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001631
Sean Callanan79ed1a82010-01-19 20:22:31 +00001632 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001633
1634 return false;
1635}
1636
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001637/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001638/// ::= .equ identifier ',' expression
1639/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001640/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001641bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001642 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001643
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001644 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001645 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001646
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001647 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001648 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001649 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001650
Nico Weber4c4c7322011-01-28 03:04:41 +00001651 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001652}
1653
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001654bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001655 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001656
1657 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001658 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001659 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1660 if (Str[i] != '\\') {
1661 Data += Str[i];
1662 continue;
1663 }
1664
1665 // Recognize escaped characters. Note that this escape semantics currently
1666 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1667 ++i;
1668 if (i == e)
1669 return TokError("unexpected backslash at end of string");
1670
1671 // Recognize octal sequences.
1672 if ((unsigned) (Str[i] - '0') <= 7) {
1673 // Consume up to three octal characters.
1674 unsigned Value = Str[i] - '0';
1675
1676 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1677 ++i;
1678 Value = Value * 8 + (Str[i] - '0');
1679
1680 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1681 ++i;
1682 Value = Value * 8 + (Str[i] - '0');
1683 }
1684 }
1685
1686 if (Value > 255)
1687 return TokError("invalid octal escape sequence (out of range)");
1688
1689 Data += (unsigned char) Value;
1690 continue;
1691 }
1692
1693 // Otherwise recognize individual escapes.
1694 switch (Str[i]) {
1695 default:
1696 // Just reject invalid escape sequences for now.
1697 return TokError("invalid escape sequence (unrecognized character)");
1698
1699 case 'b': Data += '\b'; break;
1700 case 'f': Data += '\f'; break;
1701 case 'n': Data += '\n'; break;
1702 case 'r': Data += '\r'; break;
1703 case 't': Data += '\t'; break;
1704 case '"': Data += '"'; break;
1705 case '\\': Data += '\\'; break;
1706 }
1707 }
1708
1709 return false;
1710}
1711
Daniel Dunbara0d14262009-06-24 23:30:00 +00001712/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001713/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1714bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001715 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001716 CheckForValidSection();
1717
Daniel Dunbara0d14262009-06-24 23:30:00 +00001718 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001719 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001720 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001721
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001722 std::string Data;
1723 if (ParseEscapedString(Data))
1724 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001725
1726 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001727 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001728 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1729
Sean Callanan79ed1a82010-01-19 20:22:31 +00001730 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001731
1732 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001733 break;
1734
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001735 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001736 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001737 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001738 }
1739 }
1740
Sean Callanan79ed1a82010-01-19 20:22:31 +00001741 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001742 return false;
1743}
1744
1745/// ParseDirectiveValue
1746/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1747bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001748 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001749 CheckForValidSection();
1750
Daniel Dunbara0d14262009-06-24 23:30:00 +00001751 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001752 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001753 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001754 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001755 return true;
1756
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001757 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001758 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1759 assert(Size <= 8 && "Invalid size");
1760 uint64_t IntValue = MCE->getValue();
1761 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1762 return Error(ExprLoc, "literal value out of range for directive");
1763 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1764 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001765 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001766
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001767 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001768 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001769
Daniel Dunbara0d14262009-06-24 23:30:00 +00001770 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001772 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001773 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001774 }
1775 }
1776
Sean Callanan79ed1a82010-01-19 20:22:31 +00001777 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001778 return false;
1779}
1780
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001781/// ParseDirectiveRealValue
1782/// ::= (.single | .double) [ expression (, expression)* ]
1783bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1784 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1785 CheckForValidSection();
1786
1787 for (;;) {
1788 // We don't truly support arithmetic on floating point expressions, so we
1789 // have to manually parse unary prefixes.
1790 bool IsNeg = false;
1791 if (getLexer().is(AsmToken::Minus)) {
1792 Lex();
1793 IsNeg = true;
1794 } else if (getLexer().is(AsmToken::Plus))
1795 Lex();
1796
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001797 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001798 getLexer().isNot(AsmToken::Real) &&
1799 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001800 return TokError("unexpected token in directive");
1801
1802 // Convert to an APFloat.
1803 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001804 StringRef IDVal = getTok().getString();
1805 if (getLexer().is(AsmToken::Identifier)) {
1806 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1807 Value = APFloat::getInf(Semantics);
1808 else if (!IDVal.compare_lower("nan"))
1809 Value = APFloat::getNaN(Semantics, false, ~0);
1810 else
1811 return TokError("invalid floating point literal");
1812 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001813 APFloat::opInvalidOp)
1814 return TokError("invalid floating point literal");
1815 if (IsNeg)
1816 Value.changeSign();
1817
1818 // Consume the numeric token.
1819 Lex();
1820
1821 // Emit the value as an integer.
1822 APInt AsInt = Value.bitcastToAPInt();
1823 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1824 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1825
1826 if (getLexer().is(AsmToken::EndOfStatement))
1827 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001828
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001829 if (getLexer().isNot(AsmToken::Comma))
1830 return TokError("unexpected token in directive");
1831 Lex();
1832 }
1833 }
1834
1835 Lex();
1836 return false;
1837}
1838
Daniel Dunbara0d14262009-06-24 23:30:00 +00001839/// ParseDirectiveSpace
1840/// ::= .space expression [ , expression ]
1841bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001842 CheckForValidSection();
1843
Daniel Dunbara0d14262009-06-24 23:30:00 +00001844 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001845 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001846 return true;
1847
1848 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001849 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1850 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001851 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001852 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001853
Daniel Dunbar475839e2009-06-29 20:37:27 +00001854 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001855 return true;
1856
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001857 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001858 return TokError("unexpected token in '.space' directive");
1859 }
1860
Sean Callanan79ed1a82010-01-19 20:22:31 +00001861 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001862
1863 if (NumBytes <= 0)
1864 return TokError("invalid number of bytes in '.space' directive");
1865
1866 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001867 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001868
1869 return false;
1870}
1871
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001872/// ParseDirectiveZero
1873/// ::= .zero expression
1874bool AsmParser::ParseDirectiveZero() {
1875 CheckForValidSection();
1876
1877 int64_t NumBytes;
1878 if (ParseAbsoluteExpression(NumBytes))
1879 return true;
1880
Rafael Espindolae452b172010-10-05 19:42:57 +00001881 int64_t Val = 0;
1882 if (getLexer().is(AsmToken::Comma)) {
1883 Lex();
1884 if (ParseAbsoluteExpression(Val))
1885 return true;
1886 }
1887
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001888 if (getLexer().isNot(AsmToken::EndOfStatement))
1889 return TokError("unexpected token in '.zero' directive");
1890
1891 Lex();
1892
Rafael Espindolae452b172010-10-05 19:42:57 +00001893 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001894
1895 return false;
1896}
1897
Daniel Dunbara0d14262009-06-24 23:30:00 +00001898/// ParseDirectiveFill
1899/// ::= .fill expression , expression , expression
1900bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001901 CheckForValidSection();
1902
Daniel Dunbara0d14262009-06-24 23:30:00 +00001903 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001904 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001905 return true;
1906
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001907 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001908 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001909 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001910
Daniel Dunbara0d14262009-06-24 23:30:00 +00001911 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001912 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001913 return true;
1914
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001915 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001916 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001917 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001918
Daniel Dunbara0d14262009-06-24 23:30:00 +00001919 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001920 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001921 return true;
1922
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001923 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001924 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001925
Sean Callanan79ed1a82010-01-19 20:22:31 +00001926 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001927
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001928 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1929 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001930
1931 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001933
1934 return false;
1935}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001936
1937/// ParseDirectiveOrg
1938/// ::= .org expression [ , expression ]
1939bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001940 CheckForValidSection();
1941
Daniel Dunbar821e3332009-08-31 08:09:28 +00001942 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001943 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001944 return true;
1945
1946 // Parse optional fill expression.
1947 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001948 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1949 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001950 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001951 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001952
Daniel Dunbar475839e2009-06-29 20:37:27 +00001953 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001954 return true;
1955
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001956 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001957 return TokError("unexpected token in '.org' directive");
1958 }
1959
Sean Callanan79ed1a82010-01-19 20:22:31 +00001960 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001961
1962 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1963 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001964 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001965
1966 return false;
1967}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001968
1969/// ParseDirectiveAlign
1970/// ::= {.align, ...} expression [ , expression [ , expression ]]
1971bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001972 CheckForValidSection();
1973
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001974 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001975 int64_t Alignment;
1976 if (ParseAbsoluteExpression(Alignment))
1977 return true;
1978
1979 SMLoc MaxBytesLoc;
1980 bool HasFillExpr = false;
1981 int64_t FillExpr = 0;
1982 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001983 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1984 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001985 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001986 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001987
1988 // The fill expression can be omitted while specifying a maximum number of
1989 // alignment bytes, e.g:
1990 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001991 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001992 HasFillExpr = true;
1993 if (ParseAbsoluteExpression(FillExpr))
1994 return true;
1995 }
1996
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001997 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1998 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001999 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002000 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002001
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002002 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002003 if (ParseAbsoluteExpression(MaxBytesToFill))
2004 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002005
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002006 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002007 return TokError("unexpected token in directive");
2008 }
2009 }
2010
Sean Callanan79ed1a82010-01-19 20:22:31 +00002011 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002012
Daniel Dunbar648ac512010-05-17 21:54:30 +00002013 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002014 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002015
2016 // Compute alignment in bytes.
2017 if (IsPow2) {
2018 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002019 if (Alignment >= 32) {
2020 Error(AlignmentLoc, "invalid alignment value");
2021 Alignment = 31;
2022 }
2023
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002024 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002025 }
2026
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002027 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002028 if (MaxBytesLoc.isValid()) {
2029 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002030 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2031 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002032 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002033 }
2034
2035 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002036 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2037 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002038 MaxBytesToFill = 0;
2039 }
2040 }
2041
Daniel Dunbar648ac512010-05-17 21:54:30 +00002042 // Check whether we should use optimal code alignment for this .align
2043 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002044 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002045 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2046 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002047 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002048 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002049 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002050 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2051 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002052 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002053
2054 return false;
2055}
2056
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002057/// ParseDirectiveSymbolAttribute
2058/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002059bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002061 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002062 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002063 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002064
2065 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002066 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002067
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002068 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002069
Jim Grosbach10ec6502011-09-15 17:56:49 +00002070 // Assembler local symbols don't make any sense here. Complain loudly.
2071 if (Sym->isTemporary())
2072 return Error(Loc, "non-local symbol required in directive");
2073
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002074 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002075
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002076 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002077 break;
2078
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002079 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002080 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002081 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002082 }
2083 }
2084
Sean Callanan79ed1a82010-01-19 20:22:31 +00002085 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002086 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002087}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002088
2089/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002090/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2091bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002092 CheckForValidSection();
2093
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002094 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002095 StringRef Name;
2096 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002097 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002098
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002099 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002100 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002101
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002102 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002103 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002104 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002105
2106 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002107 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002108 if (ParseAbsoluteExpression(Size))
2109 return true;
2110
2111 int64_t Pow2Alignment = 0;
2112 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002113 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002114 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002115 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002116 if (ParseAbsoluteExpression(Pow2Alignment))
2117 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002118
Chris Lattner258281d2010-01-19 06:22:22 +00002119 // If this target takes alignments in bytes (not log) validate and convert.
2120 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2121 if (!isPowerOf2_64(Pow2Alignment))
2122 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2123 Pow2Alignment = Log2_64(Pow2Alignment);
2124 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002125 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002126
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002127 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002128 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002129
Sean Callanan79ed1a82010-01-19 20:22:31 +00002130 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002131
Chris Lattner1fc3d752009-07-09 17:25:12 +00002132 // NOTE: a size of zero for a .comm should create a undefined symbol
2133 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002134 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002135 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2136 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002137
Eric Christopherc260a3e2010-05-14 01:38:54 +00002138 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002139 // may internally end up wanting an alignment in bytes.
2140 // FIXME: Diagnose overflow.
2141 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002142 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2143 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002144
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002145 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002146 return Error(IDLoc, "invalid symbol redefinition");
2147
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002148 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002149 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002150 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002151 getStreamer().EmitZerofill(Ctx.getMachOSection(
2152 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2153 0, SectionKind::getBSS()),
2154 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002155 return false;
2156 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002157
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002158 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002159 return false;
2160}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002161
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002162/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002163/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002164bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002165 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002166 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002167
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002168 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002169 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002170 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002171
Sean Callanan79ed1a82010-01-19 20:22:31 +00002172 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002173
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002174 if (Str.empty())
2175 Error(Loc, ".abort detected. Assembly stopping.");
2176 else
2177 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002178 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002179
2180 return false;
2181}
Kevin Enderby71148242009-07-14 21:35:03 +00002182
Kevin Enderby1f049b22009-07-14 23:21:55 +00002183/// ParseDirectiveInclude
2184/// ::= .include "filename"
2185bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002186 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002187 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002188
Sean Callanan18b83232010-01-19 21:44:56 +00002189 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002190 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002191 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002192
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002193 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002194 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002195
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002196 // Strip the quotes.
2197 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002198
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002199 // Attempt to switch the lexer to the included file before consuming the end
2200 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002201 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002202 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002203 return true;
2204 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002205
2206 return false;
2207}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002208
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002209/// ParseDirectiveIf
2210/// ::= .if expression
2211bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002212 TheCondStack.push_back(TheCondState);
2213 TheCondState.TheCond = AsmCond::IfCond;
2214 if(TheCondState.Ignore) {
2215 EatToEndOfStatement();
2216 }
2217 else {
2218 int64_t ExprValue;
2219 if (ParseAbsoluteExpression(ExprValue))
2220 return true;
2221
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002222 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002223 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002224
Sean Callanan79ed1a82010-01-19 20:22:31 +00002225 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002226
2227 TheCondState.CondMet = ExprValue;
2228 TheCondState.Ignore = !TheCondState.CondMet;
2229 }
2230
2231 return false;
2232}
2233
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002234bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2235 StringRef Name;
2236 TheCondStack.push_back(TheCondState);
2237 TheCondState.TheCond = AsmCond::IfCond;
2238
2239 if (TheCondState.Ignore) {
2240 EatToEndOfStatement();
2241 } else {
2242 if (ParseIdentifier(Name))
2243 return TokError("expected identifier after '.ifdef'");
2244
2245 Lex();
2246
2247 MCSymbol *Sym = getContext().LookupSymbol(Name);
2248
2249 if (expect_defined)
2250 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2251 else
2252 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2253 TheCondState.Ignore = !TheCondState.CondMet;
2254 }
2255
2256 return false;
2257}
2258
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002259/// ParseDirectiveElseIf
2260/// ::= .elseif expression
2261bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2262 if (TheCondState.TheCond != AsmCond::IfCond &&
2263 TheCondState.TheCond != AsmCond::ElseIfCond)
2264 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2265 " an .elseif");
2266 TheCondState.TheCond = AsmCond::ElseIfCond;
2267
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002268 bool LastIgnoreState = false;
2269 if (!TheCondStack.empty())
2270 LastIgnoreState = TheCondStack.back().Ignore;
2271 if (LastIgnoreState || TheCondState.CondMet) {
2272 TheCondState.Ignore = true;
2273 EatToEndOfStatement();
2274 }
2275 else {
2276 int64_t ExprValue;
2277 if (ParseAbsoluteExpression(ExprValue))
2278 return true;
2279
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002280 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002281 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002282
Sean Callanan79ed1a82010-01-19 20:22:31 +00002283 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002284 TheCondState.CondMet = ExprValue;
2285 TheCondState.Ignore = !TheCondState.CondMet;
2286 }
2287
2288 return false;
2289}
2290
2291/// ParseDirectiveElse
2292/// ::= .else
2293bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002294 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002295 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002296
Sean Callanan79ed1a82010-01-19 20:22:31 +00002297 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002298
2299 if (TheCondState.TheCond != AsmCond::IfCond &&
2300 TheCondState.TheCond != AsmCond::ElseIfCond)
2301 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2302 ".elseif");
2303 TheCondState.TheCond = AsmCond::ElseCond;
2304 bool LastIgnoreState = false;
2305 if (!TheCondStack.empty())
2306 LastIgnoreState = TheCondStack.back().Ignore;
2307 if (LastIgnoreState || TheCondState.CondMet)
2308 TheCondState.Ignore = true;
2309 else
2310 TheCondState.Ignore = false;
2311
2312 return false;
2313}
2314
2315/// ParseDirectiveEndIf
2316/// ::= .endif
2317bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002318 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002319 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002320
Sean Callanan79ed1a82010-01-19 20:22:31 +00002321 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002322
2323 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2324 TheCondStack.empty())
2325 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2326 ".else");
2327 if (!TheCondStack.empty()) {
2328 TheCondState = TheCondStack.back();
2329 TheCondStack.pop_back();
2330 }
2331
2332 return false;
2333}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002334
2335/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002336/// ::= .file [number] filename
2337/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002338bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002339 // FIXME: I'm not sure what this is.
2340 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002341 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002342 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002343 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002344 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002345
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002346 if (FileNumber < 1)
2347 return TokError("file number less than one");
2348 }
2349
Daniel Dunbareceec052010-07-12 17:45:27 +00002350 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002351 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002352
Nick Lewycky44d798d2011-10-17 23:05:28 +00002353 // Usually the directory and filename together, otherwise just the directory.
2354 StringRef Path = getTok().getString();
2355 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002356 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002357
Nick Lewycky44d798d2011-10-17 23:05:28 +00002358 StringRef Directory;
2359 StringRef Filename;
2360 if (getLexer().is(AsmToken::String)) {
2361 if (FileNumber == -1)
2362 return TokError("explicit path specified, but no file number");
2363 Filename = getTok().getString();
2364 Filename = Filename.substr(1, Filename.size()-2);
2365 Directory = Path;
2366 Lex();
2367 } else {
2368 Filename = Path;
2369 }
2370
Daniel Dunbareceec052010-07-12 17:45:27 +00002371 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002372 return TokError("unexpected token in '.file' directive");
2373
Kevin Enderby613b7572011-11-01 22:27:22 +00002374 if (getContext().getGenDwarfForAssembly() == true)
2375 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2376 "used to generate dwarf debug info for assembly code");
2377
Chris Lattnerd32e8032010-01-25 19:02:58 +00002378 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002379 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002380 else {
Nick Lewycky44d798d2011-10-17 23:05:28 +00002381 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002382 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002383 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002384
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002385 return false;
2386}
2387
2388/// ParseDirectiveLine
2389/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002390bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002391 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2392 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002393 return TokError("unexpected token in '.line' directive");
2394
Sean Callanan18b83232010-01-19 21:44:56 +00002395 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002396 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002397 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002398
2399 // FIXME: Do something with the .line.
2400 }
2401
Daniel Dunbareceec052010-07-12 17:45:27 +00002402 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002403 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002404
2405 return false;
2406}
2407
2408
2409/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002410/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002411/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2412/// The first number is a file number, must have been previously assigned with
2413/// a .file directive, the second number is the line number and optionally the
2414/// third number is a column position (zero if not specified). The remaining
2415/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002416bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002417
Daniel Dunbareceec052010-07-12 17:45:27 +00002418 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002419 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002420 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002421 if (FileNumber < 1)
2422 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002423 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002424 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002425 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002426
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002427 int64_t LineNumber = 0;
2428 if (getLexer().is(AsmToken::Integer)) {
2429 LineNumber = getTok().getIntVal();
2430 if (LineNumber < 1)
2431 return TokError("line number less than one in '.loc' directive");
2432 Lex();
2433 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002434
2435 int64_t ColumnPos = 0;
2436 if (getLexer().is(AsmToken::Integer)) {
2437 ColumnPos = getTok().getIntVal();
2438 if (ColumnPos < 0)
2439 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002440 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002441 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002442
Kevin Enderbyc0957932010-09-30 16:52:03 +00002443 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002444 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002445 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002446 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2447 for (;;) {
2448 if (getLexer().is(AsmToken::EndOfStatement))
2449 break;
2450
2451 StringRef Name;
2452 SMLoc Loc = getTok().getLoc();
2453 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002454 return TokError("unexpected token in '.loc' directive");
2455
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002456 if (Name == "basic_block")
2457 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2458 else if (Name == "prologue_end")
2459 Flags |= DWARF2_FLAG_PROLOGUE_END;
2460 else if (Name == "epilogue_begin")
2461 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2462 else if (Name == "is_stmt") {
2463 SMLoc Loc = getTok().getLoc();
2464 const MCExpr *Value;
2465 if (getParser().ParseExpression(Value))
2466 return true;
2467 // The expression must be the constant 0 or 1.
2468 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2469 int Value = MCE->getValue();
2470 if (Value == 0)
2471 Flags &= ~DWARF2_FLAG_IS_STMT;
2472 else if (Value == 1)
2473 Flags |= DWARF2_FLAG_IS_STMT;
2474 else
2475 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002476 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002477 else {
2478 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2479 }
2480 }
2481 else if (Name == "isa") {
2482 SMLoc Loc = getTok().getLoc();
2483 const MCExpr *Value;
2484 if (getParser().ParseExpression(Value))
2485 return true;
2486 // The expression must be a constant greater or equal to 0.
2487 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2488 int Value = MCE->getValue();
2489 if (Value < 0)
2490 return Error(Loc, "isa number less than zero");
2491 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002492 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002493 else {
2494 return Error(Loc, "isa number not a constant value");
2495 }
2496 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002497 else if (Name == "discriminator") {
2498 if (getParser().ParseAbsoluteExpression(Discriminator))
2499 return true;
2500 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002501 else {
2502 return Error(Loc, "unknown sub-directive in '.loc' directive");
2503 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002504
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002505 if (getLexer().is(AsmToken::EndOfStatement))
2506 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002507 }
2508 }
2509
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002510 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002511 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002512
2513 return false;
2514}
2515
Daniel Dunbar138abae2010-10-16 04:56:42 +00002516/// ParseDirectiveStabs
2517/// ::= .stabs string, number, number, number
2518bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2519 SMLoc DirectiveLoc) {
2520 return TokError("unsupported directive '" + Directive + "'");
2521}
2522
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002523/// ParseDirectiveCFISections
2524/// ::= .cfi_sections section [, section]
2525bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2526 SMLoc DirectiveLoc) {
2527 StringRef Name;
2528 bool EH = false;
2529 bool Debug = false;
2530
2531 if (getParser().ParseIdentifier(Name))
2532 return TokError("Expected an identifier");
2533
2534 if (Name == ".eh_frame")
2535 EH = true;
2536 else if (Name == ".debug_frame")
2537 Debug = true;
2538
2539 if (getLexer().is(AsmToken::Comma)) {
2540 Lex();
2541
2542 if (getParser().ParseIdentifier(Name))
2543 return TokError("Expected an identifier");
2544
2545 if (Name == ".eh_frame")
2546 EH = true;
2547 else if (Name == ".debug_frame")
2548 Debug = true;
2549 }
2550
2551 getStreamer().EmitCFISections(EH, Debug);
2552
2553 return false;
2554}
2555
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002556/// ParseDirectiveCFIStartProc
2557/// ::= .cfi_startproc
2558bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2559 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002560 getStreamer().EmitCFIStartProc();
2561 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002562}
2563
2564/// ParseDirectiveCFIEndProc
2565/// ::= .cfi_endproc
2566bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002567 getStreamer().EmitCFIEndProc();
2568 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002569}
2570
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002571/// ParseRegisterOrRegisterNumber - parse register name or number.
2572bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2573 SMLoc DirectiveLoc) {
2574 unsigned RegNo;
2575
Jim Grosbach6f888a82011-06-02 17:14:04 +00002576 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002577 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2578 DirectiveLoc))
2579 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002580 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002581 } else
2582 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002583
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002584 return false;
2585}
2586
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002587/// ParseDirectiveCFIDefCfa
2588/// ::= .cfi_def_cfa register, offset
2589bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2590 SMLoc DirectiveLoc) {
2591 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002592 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002593 return true;
2594
2595 if (getLexer().isNot(AsmToken::Comma))
2596 return TokError("unexpected token in directive");
2597 Lex();
2598
2599 int64_t Offset = 0;
2600 if (getParser().ParseAbsoluteExpression(Offset))
2601 return true;
2602
Rafael Espindola066c2f42011-04-12 23:59:07 +00002603 getStreamer().EmitCFIDefCfa(Register, Offset);
2604 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002605}
2606
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002607/// ParseDirectiveCFIDefCfaOffset
2608/// ::= .cfi_def_cfa_offset offset
2609bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2610 SMLoc DirectiveLoc) {
2611 int64_t Offset = 0;
2612 if (getParser().ParseAbsoluteExpression(Offset))
2613 return true;
2614
Rafael Espindola066c2f42011-04-12 23:59:07 +00002615 getStreamer().EmitCFIDefCfaOffset(Offset);
2616 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002617}
2618
2619/// ParseDirectiveCFIAdjustCfaOffset
2620/// ::= .cfi_adjust_cfa_offset adjustment
2621bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2622 SMLoc DirectiveLoc) {
2623 int64_t Adjustment = 0;
2624 if (getParser().ParseAbsoluteExpression(Adjustment))
2625 return true;
2626
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002627 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2628 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002629}
2630
2631/// ParseDirectiveCFIDefCfaRegister
2632/// ::= .cfi_def_cfa_register register
2633bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2634 SMLoc DirectiveLoc) {
2635 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002636 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002637 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002638
Rafael Espindola066c2f42011-04-12 23:59:07 +00002639 getStreamer().EmitCFIDefCfaRegister(Register);
2640 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002641}
2642
2643/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002644/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002645bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2646 int64_t Register = 0;
2647 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002648
2649 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002650 return true;
2651
2652 if (getLexer().isNot(AsmToken::Comma))
2653 return TokError("unexpected token in directive");
2654 Lex();
2655
2656 if (getParser().ParseAbsoluteExpression(Offset))
2657 return true;
2658
Rafael Espindola066c2f42011-04-12 23:59:07 +00002659 getStreamer().EmitCFIOffset(Register, Offset);
2660 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002661}
2662
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002663/// ParseDirectiveCFIRelOffset
2664/// ::= .cfi_rel_offset register, offset
2665bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2666 SMLoc DirectiveLoc) {
2667 int64_t Register = 0;
2668
2669 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2670 return true;
2671
2672 if (getLexer().isNot(AsmToken::Comma))
2673 return TokError("unexpected token in directive");
2674 Lex();
2675
2676 int64_t Offset = 0;
2677 if (getParser().ParseAbsoluteExpression(Offset))
2678 return true;
2679
Rafael Espindola25f492e2011-04-12 16:12:03 +00002680 getStreamer().EmitCFIRelOffset(Register, Offset);
2681 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002682}
2683
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002684static bool isValidEncoding(int64_t Encoding) {
2685 if (Encoding & ~0xff)
2686 return false;
2687
2688 if (Encoding == dwarf::DW_EH_PE_omit)
2689 return true;
2690
2691 const unsigned Format = Encoding & 0xf;
2692 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2693 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2694 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2695 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2696 return false;
2697
Rafael Espindolacaf11582010-12-29 04:31:26 +00002698 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002699 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002700 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002701 return false;
2702
2703 return true;
2704}
2705
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002706/// ParseDirectiveCFIPersonalityOrLsda
2707/// ::= .cfi_personality encoding, [symbol_name]
2708/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002709bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002710 SMLoc DirectiveLoc) {
2711 int64_t Encoding = 0;
2712 if (getParser().ParseAbsoluteExpression(Encoding))
2713 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002714 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002715 return false;
2716
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002717 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002718 return TokError("unsupported encoding.");
2719
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002720 if (getLexer().isNot(AsmToken::Comma))
2721 return TokError("unexpected token in directive");
2722 Lex();
2723
2724 StringRef Name;
2725 if (getParser().ParseIdentifier(Name))
2726 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002727
2728 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2729
2730 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002731 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002732 else {
2733 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002734 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002735 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002736 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002737}
2738
Rafael Espindolafe024d02010-12-28 18:36:23 +00002739/// ParseDirectiveCFIRememberState
2740/// ::= .cfi_remember_state
2741bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2742 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002743 getStreamer().EmitCFIRememberState();
2744 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002745}
2746
2747/// ParseDirectiveCFIRestoreState
2748/// ::= .cfi_remember_state
2749bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2750 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002751 getStreamer().EmitCFIRestoreState();
2752 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002753}
2754
Rafael Espindolac5754392011-04-12 15:31:05 +00002755/// ParseDirectiveCFISameValue
2756/// ::= .cfi_same_value register
2757bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2758 SMLoc DirectiveLoc) {
2759 int64_t Register = 0;
2760
2761 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2762 return true;
2763
2764 getStreamer().EmitCFISameValue(Register);
2765
2766 return false;
2767}
2768
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002769/// ParseDirectiveMacrosOnOff
2770/// ::= .macros_on
2771/// ::= .macros_off
2772bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2773 SMLoc DirectiveLoc) {
2774 if (getLexer().isNot(AsmToken::EndOfStatement))
2775 return Error(getLexer().getLoc(),
2776 "unexpected token in '" + Directive + "' directive");
2777
2778 getParser().MacrosEnabled = Directive == ".macros_on";
2779
2780 return false;
2781}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002782
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002783/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002784/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002785bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2786 SMLoc DirectiveLoc) {
2787 StringRef Name;
2788 if (getParser().ParseIdentifier(Name))
2789 return TokError("expected identifier in directive");
2790
Rafael Espindola65366442011-06-05 02:43:45 +00002791 std::vector<StringRef> Parameters;
2792 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2793 for(;;) {
2794 StringRef Parameter;
2795 if (getParser().ParseIdentifier(Parameter))
2796 return TokError("expected identifier in directive");
2797 Parameters.push_back(Parameter);
2798
2799 if (getLexer().isNot(AsmToken::Comma))
2800 break;
2801 Lex();
2802 }
2803 }
2804
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002805 if (getLexer().isNot(AsmToken::EndOfStatement))
2806 return TokError("unexpected token in '.macro' directive");
2807
2808 // Eat the end of statement.
2809 Lex();
2810
2811 AsmToken EndToken, StartToken = getTok();
2812
2813 // Lex the macro definition.
2814 for (;;) {
2815 // Check whether we have reached the end of the file.
2816 if (getLexer().is(AsmToken::Eof))
2817 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2818
2819 // Otherwise, check whether we have reach the .endmacro.
2820 if (getLexer().is(AsmToken::Identifier) &&
2821 (getTok().getIdentifier() == ".endm" ||
2822 getTok().getIdentifier() == ".endmacro")) {
2823 EndToken = getTok();
2824 Lex();
2825 if (getLexer().isNot(AsmToken::EndOfStatement))
2826 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2827 "' directive");
2828 break;
2829 }
2830
2831 // Otherwise, scan til the end of the statement.
2832 getParser().EatToEndOfStatement();
2833 }
2834
2835 if (getParser().MacroMap.lookup(Name)) {
2836 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2837 }
2838
2839 const char *BodyStart = StartToken.getLoc().getPointer();
2840 const char *BodyEnd = EndToken.getLoc().getPointer();
2841 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002842 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002843 return false;
2844}
2845
2846/// ParseDirectiveEndMacro
2847/// ::= .endm
2848/// ::= .endmacro
2849bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2850 SMLoc DirectiveLoc) {
2851 if (getLexer().isNot(AsmToken::EndOfStatement))
2852 return TokError("unexpected token in '" + Directive + "' directive");
2853
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002854 // If we are inside a macro instantiation, terminate the current
2855 // instantiation.
2856 if (!getParser().ActiveMacros.empty()) {
2857 getParser().HandleMacroExit();
2858 return false;
2859 }
2860
2861 // Otherwise, this .endmacro is a stray entry in the file; well formed
2862 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002863 return TokError("unexpected '" + Directive + "' in file, "
2864 "no current macro definition");
2865}
2866
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002867bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002868 getParser().CheckForValidSection();
2869
2870 const MCExpr *Value;
2871
2872 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002873 return true;
2874
2875 if (getLexer().isNot(AsmToken::EndOfStatement))
2876 return TokError("unexpected token in directive");
2877
2878 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002879 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002880 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002881 getStreamer().EmitULEB128Value(Value);
2882
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002883 return false;
2884}
2885
2886
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002887/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002888MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002889 MCContext &C, MCStreamer &Out,
2890 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002891 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002892}