blob: 50fa4d4d7ba2bcf8b72981b329abf05638c466a0 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000028#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000032#include "llvm/Support/CommandLine.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000090 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000094
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095 /// This is the current buffer index we're lexing from as managed by the
96 /// SourceMgr object.
97 int CurBuffer;
98
99 AsmCond TheCondState;
100 std::vector<AsmCond> TheCondStack;
101
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000107
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
110
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
113
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
116
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
119
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
123 SMLoc CppHashLoc;
124
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000125public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000126 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000127 const MCAsmInfo &MAI);
128 ~AsmParser();
129
130 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
131
132 void AddDirectiveHandler(MCAsmParserExtension *Object,
133 StringRef Directive,
134 DirectiveHandler Handler) {
135 DirectiveMap[Directive] = std::make_pair(Object, Handler);
136 }
137
138public:
139 /// @name MCAsmParser Interface
140 /// {
141
142 virtual SourceMgr &getSourceManager() { return SrcMgr; }
143 virtual MCAsmLexer &getLexer() { return Lexer; }
144 virtual MCContext &getContext() { return Ctx; }
145 virtual MCStreamer &getStreamer() { return Out; }
146
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000147 virtual bool Warning(SMLoc L, const Twine &Msg,
148 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
149 virtual bool Error(SMLoc L, const Twine &Msg,
150 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000151
152 const AsmToken &Lex();
153
154 bool ParseExpression(const MCExpr *&Res);
155 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
156 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
157 virtual bool ParseAbsoluteExpression(int64_t &Res);
158
159 /// }
160
161private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000162 void CheckForValidSection();
163
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000165 void EatToEndOfLine();
166 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000167
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000168 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000169 bool expandMacro(SmallString<256> &Buf, StringRef Body,
170 const std::vector<StringRef> &Parameters,
171 const std::vector<std::vector<AsmToken> > &A,
172 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000173 void HandleMacroExit();
174
175 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000176 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000177 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
178 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000179 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000180 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000181
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000182 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
183 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000184
185 /// \brief Reset the current lexer position to that given by \arg Loc. The
186 /// current token is not set; clients should ensure Lex() is called
187 /// subsequently.
188 void JumpToLoc(SMLoc Loc);
189
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000191
192 /// \brief Parse up to the end of statement and a return the contents from the
193 /// current token until the end of the statement; the current token on exit
194 /// will be either the EndOfStatement or EOF.
195 StringRef ParseStringToEndOfStatement();
196
Nico Weber4c4c7322011-01-28 03:04:41 +0000197 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198
199 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
200 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
201 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000202 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000203
204 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
205 /// and set \arg Res to the identifier contents.
206 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000207
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000209
210 // ".ascii", ".asciiz", ".string"
211 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000213 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214 bool ParseDirectiveFill(); // ".fill"
215 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000216 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000217 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218 bool ParseDirectiveOrg(); // ".org"
219 // ".align{,32}", ".p2align{,w,l}"
220 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
221
222 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
223 /// accepts a single symbol (which should be a label or an external).
224 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000225
226 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
227
228 bool ParseDirectiveAbort(); // ".abort"
229 bool ParseDirectiveInclude(); // ".include"
230
231 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000232 // ".ifdef" or ".ifndef", depending on expect_defined
233 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
235 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
236 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
237
238 /// ParseEscapedString - Parse the current token as a string which may include
239 /// escaped characters and return the string contents.
240 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000241
242 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
243 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244};
245
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000246/// \brief Generic implementations of directive handling, etc. which is shared
247/// (or the default, at least) for all assembler parser.
248class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000249 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
250 void AddDirectiveHandler(StringRef Directive) {
251 getParser().AddDirectiveHandler(this, Directive,
252 HandleDirective<GenericAsmParser, Handler>);
253 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000254public:
255 GenericAsmParser() {}
256
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257 AsmParser &getParser() {
258 return (AsmParser&) this->MCAsmParserExtension::getParser();
259 }
260
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000261 virtual void Initialize(MCAsmParser &Parser) {
262 // Call the base implementation.
263 this->MCAsmParserExtension::Initialize(Parser);
264
265 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000270
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000271 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
273 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
275 ".cfi_startproc");
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
277 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
279 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
281 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
283 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
285 ".cfi_def_cfa_register");
286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
287 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000288 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
289 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000290 AddDirectiveHandler<
291 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
292 AddDirectiveHandler<
293 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000294 AddDirectiveHandler<
295 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
296 AddDirectiveHandler<
297 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000298 AddDirectiveHandler<
299 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000300
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000301 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
303 ".macros_on");
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
305 ".macros_off");
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000309
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000312 }
313
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000314 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
315
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000316 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
317 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000319 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000320 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000321 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
322 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000323 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000324 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000325 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000326 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
327 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000328 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000329 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000330 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
331 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000332 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000333
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000334 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000335 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
336 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000337
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000338 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000339};
340
341}
342
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000343namespace llvm {
344
345extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000346extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000347extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000348
349}
350
Chris Lattneraaec2052010-01-19 19:46:13 +0000351enum { DEFAULT_ADDRSPACE = 0 };
352
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000353AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000354 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000355 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000356 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000357 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000358 // Save the old handler.
359 SavedDiagHandler = SrcMgr.getDiagHandler();
360 SavedDiagContext = SrcMgr.getDiagContext();
361 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000362 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000363 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000364
365 // Initialize the generic parser.
366 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000367
368 // Initialize the platform / file format parser.
369 //
370 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
371 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000372 if (_MAI.hasMicrosoftFastStdCallMangling()) {
373 PlatformParser = createCOFFAsmParser();
374 PlatformParser->Initialize(*this);
375 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000376 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000377 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000378 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000379 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000380 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000381 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000382}
383
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000384AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000385 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
386
387 // Destroy any macros.
388 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
389 ie = MacroMap.end(); it != ie; ++it)
390 delete it->getValue();
391
Daniel Dunbare4749702010-07-12 18:12:02 +0000392 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000393 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000394}
395
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000396void AsmParser::PrintMacroInstantiations() {
397 // Print the active macro instantiation stack.
398 for (std::vector<MacroInstantiation*>::const_reverse_iterator
399 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000400 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
401 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000402}
403
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000404bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000405 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000406 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000407 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000408 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000409 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000410}
411
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000412bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000413 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000414 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000415 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000416 return true;
417}
418
Sean Callananfd0b0282010-01-21 00:19:58 +0000419bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000420 std::string IncludedFile;
421 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000422 if (NewBuf == -1)
423 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000424
Sean Callananfd0b0282010-01-21 00:19:58 +0000425 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000426
Sean Callananfd0b0282010-01-21 00:19:58 +0000427 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000428
Sean Callananfd0b0282010-01-21 00:19:58 +0000429 return false;
430}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000431
432void AsmParser::JumpToLoc(SMLoc Loc) {
433 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
434 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
435}
436
Sean Callananfd0b0282010-01-21 00:19:58 +0000437const AsmToken &AsmParser::Lex() {
438 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000439
Sean Callananfd0b0282010-01-21 00:19:58 +0000440 if (tok->is(AsmToken::Eof)) {
441 // If this is the end of an included file, pop the parent file off the
442 // include stack.
443 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
444 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000445 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000446 tok = &Lexer.Lex();
447 }
448 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000449
Sean Callananfd0b0282010-01-21 00:19:58 +0000450 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000451 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000452
Sean Callananfd0b0282010-01-21 00:19:58 +0000453 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000454}
455
Chris Lattner79180e22010-04-05 23:15:42 +0000456bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000457 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000458 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000459 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000460
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000461 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000462 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000463
464 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000465 AsmCond StartingCondState = TheCondState;
466
Chris Lattnerb717fb02009-07-02 21:53:43 +0000467 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000468 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000469 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000470
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000471 // We had an error, validate that one was emitted and recover by skipping to
472 // the next line.
473 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000474 EatToEndOfStatement();
475 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000476
477 if (TheCondState.TheCond != StartingCondState.TheCond ||
478 TheCondState.Ignore != StartingCondState.Ignore)
479 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000480
481 // Check to see there are no empty DwarfFile slots.
482 const std::vector<MCDwarfFile *> &MCDwarfFiles =
483 getContext().getMCDwarfFiles();
484 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000485 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000486 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000487 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000488
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000489 // Check to see that all assembler local symbols were actually defined.
490 // Targets that don't do subsections via symbols may not want this, though,
491 // so conservatively exclude them. Only do this if we're finalizing, though,
492 // as otherwise we won't necessarilly have seen everything yet.
493 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
494 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
495 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
496 e = Symbols.end();
497 i != e; ++i) {
498 MCSymbol *Sym = i->getValue();
499 // Variable symbols may not be marked as defined, so check those
500 // explicitly. If we know it's a variable, we have a definition for
501 // the purposes of this check.
502 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
503 // FIXME: We would really like to refer back to where the symbol was
504 // first referenced for a source location. We need to add something
505 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000506 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
507 "assembler local symbol '" + Sym->getName() +
508 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000509 }
510 }
511
512
Chris Lattner79180e22010-04-05 23:15:42 +0000513 // Finalize the output stream if there are no errors and if the client wants
514 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000515 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000516 Out.Finish();
517
Chris Lattnerb717fb02009-07-02 21:53:43 +0000518 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000519}
520
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000521void AsmParser::CheckForValidSection() {
522 if (!getStreamer().getCurrentSection()) {
523 TokError("expected section directive before assembly directive");
524 Out.SwitchSection(Ctx.getMachOSection(
525 "__TEXT", "__text",
526 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
527 0, SectionKind::getText()));
528 }
529}
530
Chris Lattner2cf5f142009-06-22 01:29:09 +0000531/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
532void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000533 while (Lexer.isNot(AsmToken::EndOfStatement) &&
534 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000535 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000536
Chris Lattner2cf5f142009-06-22 01:29:09 +0000537 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000538 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000539 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000540}
541
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000542StringRef AsmParser::ParseStringToEndOfStatement() {
543 const char *Start = getTok().getLoc().getPointer();
544
545 while (Lexer.isNot(AsmToken::EndOfStatement) &&
546 Lexer.isNot(AsmToken::Eof))
547 Lex();
548
549 const char *End = getTok().getLoc().getPointer();
550 return StringRef(Start, End - Start);
551}
Chris Lattnerc4193832009-06-22 05:51:26 +0000552
Chris Lattner74ec1a32009-06-22 06:32:03 +0000553/// ParseParenExpr - Parse a paren expression and return it.
554/// NOTE: This assumes the leading '(' has already been consumed.
555///
556/// parenexpr ::= expr)
557///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000558bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000559 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000561 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000562 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000563 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000564 return false;
565}
Chris Lattnerc4193832009-06-22 05:51:26 +0000566
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000567/// ParseBracketExpr - Parse a bracket expression and return it.
568/// NOTE: This assumes the leading '[' has already been consumed.
569///
570/// bracketexpr ::= expr]
571///
572bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
573 if (ParseExpression(Res)) return true;
574 if (Lexer.isNot(AsmToken::RBrac))
575 return TokError("expected ']' in brackets expression");
576 EndLoc = Lexer.getLoc();
577 Lex();
578 return false;
579}
580
Chris Lattner74ec1a32009-06-22 06:32:03 +0000581/// ParsePrimaryExpr - Parse a primary expression and return it.
582/// primaryexpr ::= (parenexpr
583/// primaryexpr ::= symbol
584/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000585/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000586/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000587bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000588 switch (Lexer.getKind()) {
589 default:
590 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000591 // If we have an error assume that we've already handled it.
592 case AsmToken::Error:
593 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000594 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000595 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000596 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000597 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000598 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000599 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000600 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000601 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000602 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000603 EndLoc = Lexer.getLoc();
604
605 StringRef Identifier;
606 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000607 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000608
Daniel Dunbarfffff912009-10-16 01:34:54 +0000609 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000610 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000611 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000612
613 // Lookup the symbol variant if used.
614 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000615 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000616 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000617 if (Variant == MCSymbolRefExpr::VK_Invalid) {
618 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000619 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000620 }
621 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000622
Daniel Dunbarfffff912009-10-16 01:34:54 +0000623 // If this is an absolute variable reference, substitute it now to preserve
624 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000625 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000626 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000627 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000628
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000629 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000630 return false;
631 }
632
633 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000634 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000635 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000636 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000637 case AsmToken::Integer: {
638 SMLoc Loc = getTok().getLoc();
639 int64_t IntVal = getTok().getIntVal();
640 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000641 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000642 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000643 // Look for 'b' or 'f' following an Integer as a directional label
644 if (Lexer.getKind() == AsmToken::Identifier) {
645 StringRef IDVal = getTok().getString();
646 if (IDVal == "f" || IDVal == "b"){
647 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
648 IDVal == "f" ? 1 : 0);
649 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
650 getContext());
651 if(IDVal == "b" && Sym->isUndefined())
652 return Error(Loc, "invalid reference to undefined symbol");
653 EndLoc = Lexer.getLoc();
654 Lex(); // Eat identifier.
655 }
656 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000657 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000658 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000659 case AsmToken::Real: {
660 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000661 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000662 Res = MCConstantExpr::Create(IntVal, getContext());
663 Lex(); // Eat token.
664 return false;
665 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000666 case AsmToken::Dot: {
667 // This is a '.' reference, which references the current PC. Emit a
668 // temporary label to the streamer and refer to it.
669 MCSymbol *Sym = Ctx.CreateTempSymbol();
670 Out.EmitLabel(Sym);
671 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
672 EndLoc = Lexer.getLoc();
673 Lex(); // Eat identifier.
674 return false;
675 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000676 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000677 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000678 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000679 case AsmToken::LBrac:
680 if (!PlatformParser->HasBracketExpressions())
681 return TokError("brackets expression not supported on this target");
682 Lex(); // Eat the '['.
683 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000684 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000685 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000686 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000687 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000688 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000689 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000690 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000691 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000692 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000693 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000694 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000695 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000696 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000697 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000698 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000699 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000700 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000701 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000702 }
703}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000704
Chris Lattnerb4307b32010-01-15 19:28:38 +0000705bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000706 SMLoc EndLoc;
707 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000708}
709
Daniel Dunbarcceba832010-09-17 02:47:07 +0000710const MCExpr *
711AsmParser::ApplyModifierToExpr(const MCExpr *E,
712 MCSymbolRefExpr::VariantKind Variant) {
713 // Recurse over the given expression, rebuilding it to apply the given variant
714 // if there is exactly one symbol.
715 switch (E->getKind()) {
716 case MCExpr::Target:
717 case MCExpr::Constant:
718 return 0;
719
720 case MCExpr::SymbolRef: {
721 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
722
723 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
724 TokError("invalid variant on expression '" +
725 getTok().getIdentifier() + "' (already modified)");
726 return E;
727 }
728
729 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
730 }
731
732 case MCExpr::Unary: {
733 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
734 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
735 if (!Sub)
736 return 0;
737 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
738 }
739
740 case MCExpr::Binary: {
741 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
742 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
743 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
744
745 if (!LHS && !RHS)
746 return 0;
747
748 if (!LHS) LHS = BE->getLHS();
749 if (!RHS) RHS = BE->getRHS();
750
751 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
752 }
753 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000754
755 assert(0 && "Invalid expression kind!");
756 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000757}
758
Chris Lattner74ec1a32009-06-22 06:32:03 +0000759/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000760///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000761/// expr ::= expr &&,|| expr -> lowest.
762/// expr ::= expr |,^,&,! expr
763/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
764/// expr ::= expr <<,>> expr
765/// expr ::= expr +,- expr
766/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000767/// expr ::= primaryexpr
768///
Chris Lattner54482b42010-01-15 19:39:23 +0000769bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000770 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000771 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000772 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
773 return true;
774
Daniel Dunbarcceba832010-09-17 02:47:07 +0000775 // As a special case, we support 'a op b @ modifier' by rewriting the
776 // expression to include the modifier. This is inefficient, but in general we
777 // expect users to use 'a@modifier op b'.
778 if (Lexer.getKind() == AsmToken::At) {
779 Lex();
780
781 if (Lexer.isNot(AsmToken::Identifier))
782 return TokError("unexpected symbol modifier following '@'");
783
784 MCSymbolRefExpr::VariantKind Variant =
785 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
786 if (Variant == MCSymbolRefExpr::VK_Invalid)
787 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
788
789 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
790 if (!ModifiedRes) {
791 return TokError("invalid modifier '" + getTok().getIdentifier() +
792 "' (no symbols present)");
793 return true;
794 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000795
Daniel Dunbarcceba832010-09-17 02:47:07 +0000796 Res = ModifiedRes;
797 Lex();
798 }
799
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000800 // Try to constant fold it up front, if possible.
801 int64_t Value;
802 if (Res->EvaluateAsAbsolute(Value))
803 Res = MCConstantExpr::Create(Value, getContext());
804
805 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000806}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000807
Chris Lattnerb4307b32010-01-15 19:28:38 +0000808bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000809 Res = 0;
810 return ParseParenExpr(Res, EndLoc) ||
811 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000812}
813
Daniel Dunbar475839e2009-06-29 20:37:27 +0000814bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000815 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000816
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000817 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000818 if (ParseExpression(Expr))
819 return true;
820
Daniel Dunbare00b0112009-10-16 01:57:52 +0000821 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000822 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000823
824 return false;
825}
826
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000827static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000828 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000829 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000830 default:
831 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000832
Jim Grosbachfbe16812011-08-20 16:24:13 +0000833 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000834 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000835 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000836 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000837 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000838 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000839 return 1;
840
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000841
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000842 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000843 //
844 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000846 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000847 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000848 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000849 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000850 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000851 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000852 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000853 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000854
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000855 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000856 case AsmToken::EqualEqual:
857 Kind = MCBinaryExpr::EQ;
858 return 3;
859 case AsmToken::ExclaimEqual:
860 case AsmToken::LessGreater:
861 Kind = MCBinaryExpr::NE;
862 return 3;
863 case AsmToken::Less:
864 Kind = MCBinaryExpr::LT;
865 return 3;
866 case AsmToken::LessEqual:
867 Kind = MCBinaryExpr::LTE;
868 return 3;
869 case AsmToken::Greater:
870 Kind = MCBinaryExpr::GT;
871 return 3;
872 case AsmToken::GreaterEqual:
873 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000874 return 3;
875
Jim Grosbachfbe16812011-08-20 16:24:13 +0000876 // Intermediate Precedence: <<, >>
877 case AsmToken::LessLess:
878 Kind = MCBinaryExpr::Shl;
879 return 4;
880 case AsmToken::GreaterGreater:
881 Kind = MCBinaryExpr::Shr;
882 return 4;
883
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000884 // High Intermediate Precedence: +, -
885 case AsmToken::Plus:
886 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000887 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000888 case AsmToken::Minus:
889 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000890 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000891
Jim Grosbachfbe16812011-08-20 16:24:13 +0000892 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000893 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000894 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000895 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000896 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000897 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000898 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000899 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000900 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000901 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000902 }
903}
904
905
906/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
907/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000908bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
909 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000910 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000911 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000912 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000913
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000914 // If the next token is lower precedence than we are allowed to eat, return
915 // successfully with what we ate already.
916 if (TokPrec < Precedence)
917 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000918
Sean Callanan79ed1a82010-01-19 20:22:31 +0000919 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000920
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000921 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000922 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000923 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000924
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000925 // If BinOp binds less tightly with RHS than the operator after RHS, let
926 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000927 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000928 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000929 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000930 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000931 }
932
Daniel Dunbar475839e2009-06-29 20:37:27 +0000933 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000934 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000935 }
936}
937
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000938
939
940
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000941/// ParseStatement:
942/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000943/// ::= Label* Directive ...Operands... EndOfStatement
944/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000945bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000946 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000947 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000948 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000949 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000950 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000951
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000952 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000953 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000954 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000955 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000956 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000957 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000958 if (Lexer.is(AsmToken::Hash))
959 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000960
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000961 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000962 if (Lexer.is(AsmToken::Integer)) {
963 LocalLabelVal = getTok().getIntVal();
964 if (LocalLabelVal < 0) {
965 if (!TheCondState.Ignore)
966 return TokError("unexpected token at start of statement");
967 IDVal = "";
968 }
969 else {
970 IDVal = getTok().getString();
971 Lex(); // Consume the integer token to be used as an identifier token.
972 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000973 if (!TheCondState.Ignore)
974 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000975 }
976 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000977
978 } else if (Lexer.is(AsmToken::Dot)) {
979 // Treat '.' as a valid identifier in this context.
980 Lex();
981 IDVal = ".";
982
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000983 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000984 if (!TheCondState.Ignore)
985 return TokError("unexpected token at start of statement");
986 IDVal = "";
987 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000988
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000989
Chris Lattner7834fac2010-04-17 18:14:27 +0000990 // Handle conditional assembly here before checking for skipping. We
991 // have to do this so that .endif isn't skipped in a ".if 0" block for
992 // example.
993 if (IDVal == ".if")
994 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000995 if (IDVal == ".ifdef")
996 return ParseDirectiveIfdef(IDLoc, true);
997 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
998 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000999 if (IDVal == ".elseif")
1000 return ParseDirectiveElseIf(IDLoc);
1001 if (IDVal == ".else")
1002 return ParseDirectiveElse(IDLoc);
1003 if (IDVal == ".endif")
1004 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001005
Chris Lattner7834fac2010-04-17 18:14:27 +00001006 // If we are in a ".if 0" block, ignore this statement.
1007 if (TheCondState.Ignore) {
1008 EatToEndOfStatement();
1009 return false;
1010 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001011
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001012 // FIXME: Recurse on local labels?
1013
1014 // See what kind of statement we have.
1015 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001016 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001017 CheckForValidSection();
1018
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001019 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001020 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001021
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001022 // Diagnose attempt to use '.' as a label.
1023 if (IDVal == ".")
1024 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1025
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001026 // Diagnose attempt to use a variable as a label.
1027 //
1028 // FIXME: Diagnostics. Note the location of the definition as a label.
1029 // FIXME: This doesn't diagnose assignment to a symbol which has been
1030 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001031 MCSymbol *Sym;
1032 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001033 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001034 else
1035 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001036 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001037 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001038
Daniel Dunbar959fd882009-08-26 22:13:22 +00001039 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001040 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001041
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001042 // Consume any end of statement token, if present, to avoid spurious
1043 // AddBlankLine calls().
1044 if (Lexer.is(AsmToken::EndOfStatement)) {
1045 Lex();
1046 if (Lexer.is(AsmToken::Eof))
1047 return false;
1048 }
1049
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001050 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001051 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001052
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001054 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001055 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001056
Nico Weber4c4c7322011-01-28 03:04:41 +00001057 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001058
1059 default: // Normal instruction or directive.
1060 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001061 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001062
1063 // If macros are enabled, check to see if this is a macro instantiation.
1064 if (MacrosEnabled)
1065 if (const Macro *M = MacroMap.lookup(IDVal))
1066 return HandleMacroEntry(IDVal, IDLoc, M);
1067
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001068 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001069 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001070 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001071 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001072 return ParseDirectiveSet(IDVal, true);
1073 if (IDVal == ".equiv")
1074 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001075
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076 // Data directives
1077
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001078 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001079 return ParseDirectiveAscii(IDVal, false);
1080 if (IDVal == ".asciz" || IDVal == ".string")
1081 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001083 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001085 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001087 if (IDVal == ".value")
1088 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001089 if (IDVal == ".2byte")
1090 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001091 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001093 if (IDVal == ".int")
1094 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001095 if (IDVal == ".4byte")
1096 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001098 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001099 if (IDVal == ".8byte")
1100 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001101 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001102 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1103 if (IDVal == ".double")
1104 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001105
Eli Friedman5d68ec22010-07-19 04:17:25 +00001106 if (IDVal == ".align") {
1107 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1108 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1109 }
1110 if (IDVal == ".align32") {
1111 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1112 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1113 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001114 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001115 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001116 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001117 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001118 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001119 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001120 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001121 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001123 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001125 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1126
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001127 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001128 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001129
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001130 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001132 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001133 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001134 if (IDVal == ".zero")
1135 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001136
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001137 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001138
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001139 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001140 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001141 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001142 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001143 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001144 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001145 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001146 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001147 if (IDVal == ".symbol_resolver")
1148 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001149 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001150 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001151 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001152 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001153 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001154 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001155 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001156 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001157 if (IDVal == ".weak_def_can_be_hidden")
1158 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001159
Hans Wennborg5cc64912011-06-18 13:51:54 +00001160 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001161 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001162 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001163 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001164
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001165 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001166 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001167 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001168 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001169
Evan Chengbd27f5a2011-07-27 00:38:12 +00001170 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001171 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001172
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001173 // Look up the handler in the handler table.
1174 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1175 DirectiveMap.lookup(IDVal);
1176 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001177 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001178
Kevin Enderby9c656452009-09-10 20:51:44 +00001179 // Target hook for parsing target specific directives.
1180 if (!getTargetParser().ParseDirective(ID))
1181 return false;
1182
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001183 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001184 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001185 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001186 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001187
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001188 CheckForValidSection();
1189
Chris Lattnera7f13542010-05-19 23:34:33 +00001190 // Canonicalize the opcode to lower case.
1191 SmallString<128> Opcode;
1192 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1193 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001194
Chris Lattner98986712010-01-14 22:21:20 +00001195 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001196 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001197 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001198
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001199 // Dump the parsed representation, if requested.
1200 if (getShowParsedOperands()) {
1201 SmallString<256> Str;
1202 raw_svector_ostream OS(Str);
1203 OS << "parsed instruction: [";
1204 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1205 if (i != 0)
1206 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001207 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001208 }
1209 OS << "]";
1210
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001211 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001212 }
1213
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001214 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001215 if (!HadError)
1216 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1217 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001218
Chris Lattner98986712010-01-14 22:21:20 +00001219 // Free any parsed operands.
1220 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1221 delete ParsedOperands[i];
1222
Chris Lattnercbf8a982010-09-11 16:18:25 +00001223 // Don't skip the rest of the line, the instruction parser is responsible for
1224 // that.
1225 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001226}
Chris Lattner9a023f72009-06-24 04:43:34 +00001227
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001228/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1229/// since they may not be able to be tokenized to get to the end of line token.
1230void AsmParser::EatToEndOfLine() {
1231 Lexer.LexUntilEndOfLine();
1232 // Eat EOL.
1233 Lex();
1234}
1235
1236/// ParseCppHashLineFilenameComment as this:
1237/// ::= # number "filename"
1238/// or just as a full line comment if it doesn't have a number and a string.
1239bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1240 Lex(); // Eat the hash token.
1241
1242 if (getLexer().isNot(AsmToken::Integer)) {
1243 // Consume the line since in cases it is not a well-formed line directive,
1244 // as if were simply a full line comment.
1245 EatToEndOfLine();
1246 return false;
1247 }
1248
1249 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001250 Lex();
1251
1252 if (getLexer().isNot(AsmToken::String)) {
1253 EatToEndOfLine();
1254 return false;
1255 }
1256
1257 StringRef Filename = getTok().getString();
1258 // Get rid of the enclosing quotes.
1259 Filename = Filename.substr(1, Filename.size()-2);
1260
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001261 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1262 CppHashLoc = L;
1263 CppHashFilename = Filename;
1264 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001265
1266 // Ignore any trailing characters, they're just comment.
1267 EatToEndOfLine();
1268 return false;
1269}
1270
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001271/// DiagHandler - will use the the last parsed cpp hash line filename comment
1272/// for the Filename and LineNo if any in the diagnostic.
1273void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1274 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1275 raw_ostream &OS = errs();
1276
1277 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1278 const SMLoc &DiagLoc = Diag.getLoc();
1279 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1280 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1281
1282 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1283 // before printing the message.
1284 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001285 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001286 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1287 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1288 }
1289
1290 // If we have not parsed a cpp hash line filename comment or the source
1291 // manager changed or buffer changed (like in a nested include) then just
1292 // print the normal diagnostic using its Filename and LineNo.
1293 if (!Parser->CppHashLineNumber ||
1294 &DiagSrcMgr != &Parser->SrcMgr ||
1295 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001296 if (Parser->SavedDiagHandler)
1297 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1298 else
1299 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001300 return;
1301 }
1302
1303 // Use the CppHashFilename and calculate a line number based on the
1304 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1305 // the diagnostic.
1306 const std::string Filename = Parser->CppHashFilename;
1307
1308 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1309 int CppHashLocLineNo =
1310 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1311 int LineNo = Parser->CppHashLineNumber - 1 +
1312 (DiagLocLineNo - CppHashLocLineNo);
1313
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001314 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1315 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001316 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001317 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001318
Benjamin Kramer04a04262011-10-16 10:48:29 +00001319 if (Parser->SavedDiagHandler)
1320 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1321 else
1322 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001323}
1324
Rafael Espindola65366442011-06-05 02:43:45 +00001325bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1326 const std::vector<StringRef> &Parameters,
1327 const std::vector<std::vector<AsmToken> > &A,
1328 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001329 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001330 unsigned NParameters = Parameters.size();
1331 if (NParameters != 0 && NParameters != A.size())
1332 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001333
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001334 while (!Body.empty()) {
1335 // Scan for the next substitution.
1336 std::size_t End = Body.size(), Pos = 0;
1337 for (; Pos != End; ++Pos) {
1338 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001339 if (!NParameters) {
1340 // This macro has no parameters, look for $0, $1, etc.
1341 if (Body[Pos] != '$' || Pos + 1 == End)
1342 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001343
Rafael Espindola65366442011-06-05 02:43:45 +00001344 char Next = Body[Pos + 1];
1345 if (Next == '$' || Next == 'n' || isdigit(Next))
1346 break;
1347 } else {
1348 // This macro has parameters, look for \foo, \bar, etc.
1349 if (Body[Pos] == '\\' && Pos + 1 != End)
1350 break;
1351 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001352 }
1353
1354 // Add the prefix.
1355 OS << Body.slice(0, Pos);
1356
1357 // Check if we reached the end.
1358 if (Pos == End)
1359 break;
1360
Rafael Espindola65366442011-06-05 02:43:45 +00001361 if (!NParameters) {
1362 switch (Body[Pos+1]) {
1363 // $$ => $
1364 case '$':
1365 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001366 break;
1367
Rafael Espindola65366442011-06-05 02:43:45 +00001368 // $n => number of arguments
1369 case 'n':
1370 OS << A.size();
1371 break;
1372
1373 // $[0-9] => argument
1374 default: {
1375 // Missing arguments are ignored.
1376 unsigned Index = Body[Pos+1] - '0';
1377 if (Index >= A.size())
1378 break;
1379
1380 // Otherwise substitute with the token values, with spaces eliminated.
1381 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1382 ie = A[Index].end(); it != ie; ++it)
1383 OS << it->getString();
1384 break;
1385 }
1386 }
1387 Pos += 2;
1388 } else {
1389 unsigned I = Pos + 1;
1390 while (isalnum(Body[I]) && I + 1 != End)
1391 ++I;
1392
1393 const char *Begin = Body.data() + Pos +1;
1394 StringRef Argument(Begin, I - (Pos +1));
1395 unsigned Index = 0;
1396 for (; Index < NParameters; ++Index)
1397 if (Parameters[Index] == Argument)
1398 break;
1399
1400 // FIXME: We should error at the macro definition.
1401 if (Index == NParameters)
1402 return Error(L, "Parameter not found");
1403
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001404 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1405 ie = A[Index].end(); it != ie; ++it)
1406 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001407
Rafael Espindola65366442011-06-05 02:43:45 +00001408 Pos += 1 + Argument.size();
1409 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001410 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001411 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001412 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001413
1414 // We include the .endmacro in the buffer as our queue to exit the macro
1415 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001416 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001417 return false;
1418}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001419
Rafael Espindola65366442011-06-05 02:43:45 +00001420MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1421 MemoryBuffer *I)
1422 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1423{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001424}
1425
1426bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1427 const Macro *M) {
1428 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1429 // this, although we should protect against infinite loops.
1430 if (ActiveMacros.size() == 20)
1431 return TokError("macros cannot be nested more than 20 levels deep");
1432
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001433 // Parse the macro instantiation arguments.
1434 std::vector<std::vector<AsmToken> > MacroArguments;
1435 MacroArguments.push_back(std::vector<AsmToken>());
1436 unsigned ParenLevel = 0;
1437 for (;;) {
1438 if (Lexer.is(AsmToken::Eof))
1439 return TokError("unexpected token in macro instantiation");
1440 if (Lexer.is(AsmToken::EndOfStatement))
1441 break;
1442
1443 // If we aren't inside parentheses and this is a comma, start a new token
1444 // list.
1445 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1446 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001447 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001448 // Adjust the current parentheses level.
1449 if (Lexer.is(AsmToken::LParen))
1450 ++ParenLevel;
1451 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1452 --ParenLevel;
1453
1454 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001455 MacroArguments.back().push_back(getTok());
1456 }
1457 Lex();
1458 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001459
Rafael Espindola65366442011-06-05 02:43:45 +00001460 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1461 // to hold the macro body with substitutions.
1462 SmallString<256> Buf;
1463 StringRef Body = M->Body;
1464
1465 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1466 return true;
1467
1468 MemoryBuffer *Instantiation =
1469 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1470
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001471 // Create the macro instantiation object and add to the current macro
1472 // instantiation stack.
1473 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001474 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001475 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001476 ActiveMacros.push_back(MI);
1477
1478 // Jump to the macro instantiation and prime the lexer.
1479 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1480 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1481 Lex();
1482
1483 return false;
1484}
1485
1486void AsmParser::HandleMacroExit() {
1487 // Jump to the EndOfStatement we should return to, and consume it.
1488 JumpToLoc(ActiveMacros.back()->ExitLoc);
1489 Lex();
1490
1491 // Pop the instantiation entry.
1492 delete ActiveMacros.back();
1493 ActiveMacros.pop_back();
1494}
1495
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001496static void MarkUsed(const MCExpr *Value) {
1497 switch (Value->getKind()) {
1498 case MCExpr::Binary:
1499 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1500 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1501 break;
1502 case MCExpr::Target:
1503 case MCExpr::Constant:
1504 break;
1505 case MCExpr::SymbolRef: {
1506 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1507 break;
1508 }
1509 case MCExpr::Unary:
1510 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1511 break;
1512 }
1513}
1514
Nico Weber4c4c7322011-01-28 03:04:41 +00001515bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001516 // FIXME: Use better location, we should use proper tokens.
1517 SMLoc EqualLoc = Lexer.getLoc();
1518
Daniel Dunbar821e3332009-08-31 08:09:28 +00001519 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001520 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001521 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001522
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001523 MarkUsed(Value);
1524
Daniel Dunbar3f872332009-07-28 16:08:33 +00001525 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001526 return TokError("unexpected token in assignment");
1527
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001528 // Error on assignment to '.'.
1529 if (Name == ".") {
1530 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1531 "(use '.space' or '.org').)"));
1532 }
1533
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001534 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001535 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001536
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001537 // Validate that the LHS is allowed to be a variable (either it has not been
1538 // used as a symbol, or it is an absolute symbol).
1539 MCSymbol *Sym = getContext().LookupSymbol(Name);
1540 if (Sym) {
1541 // Diagnose assignment to a label.
1542 //
1543 // FIXME: Diagnostics. Note the location of the definition as a label.
1544 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001545 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001546 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001547 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001548 return Error(EqualLoc, "redefinition of '" + Name + "'");
1549 else if (!Sym->isVariable())
1550 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001551 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001552 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1553 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001554
1555 // Don't count these checks as uses.
1556 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001557 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001558 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001559
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001560 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001561
1562 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001563 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001564
1565 return false;
1566}
1567
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001568/// ParseIdentifier:
1569/// ::= identifier
1570/// ::= string
1571bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001572 // The assembler has relaxed rules for accepting identifiers, in particular we
1573 // allow things like '.globl $foo', which would normally be separate
1574 // tokens. At this level, we have already lexed so we cannot (currently)
1575 // handle this as a context dependent token, instead we detect adjacent tokens
1576 // and return the combined identifier.
1577 if (Lexer.is(AsmToken::Dollar)) {
1578 SMLoc DollarLoc = getLexer().getLoc();
1579
1580 // Consume the dollar sign, and check for a following identifier.
1581 Lex();
1582 if (Lexer.isNot(AsmToken::Identifier))
1583 return true;
1584
1585 // We have a '$' followed by an identifier, make sure they are adjacent.
1586 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1587 return true;
1588
1589 // Construct the joined identifier and consume the token.
1590 Res = StringRef(DollarLoc.getPointer(),
1591 getTok().getIdentifier().size() + 1);
1592 Lex();
1593 return false;
1594 }
1595
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001596 if (Lexer.isNot(AsmToken::Identifier) &&
1597 Lexer.isNot(AsmToken::String))
1598 return true;
1599
Sean Callanan18b83232010-01-19 21:44:56 +00001600 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001601
Sean Callanan79ed1a82010-01-19 20:22:31 +00001602 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001603
1604 return false;
1605}
1606
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001607/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001608/// ::= .equ identifier ',' expression
1609/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001610/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001611bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001612 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001613
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001614 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001615 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001616
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001617 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001618 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001619 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001620
Nico Weber4c4c7322011-01-28 03:04:41 +00001621 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001622}
1623
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001624bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001625 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001626
1627 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001628 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001629 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1630 if (Str[i] != '\\') {
1631 Data += Str[i];
1632 continue;
1633 }
1634
1635 // Recognize escaped characters. Note that this escape semantics currently
1636 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1637 ++i;
1638 if (i == e)
1639 return TokError("unexpected backslash at end of string");
1640
1641 // Recognize octal sequences.
1642 if ((unsigned) (Str[i] - '0') <= 7) {
1643 // Consume up to three octal characters.
1644 unsigned Value = Str[i] - '0';
1645
1646 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1647 ++i;
1648 Value = Value * 8 + (Str[i] - '0');
1649
1650 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1651 ++i;
1652 Value = Value * 8 + (Str[i] - '0');
1653 }
1654 }
1655
1656 if (Value > 255)
1657 return TokError("invalid octal escape sequence (out of range)");
1658
1659 Data += (unsigned char) Value;
1660 continue;
1661 }
1662
1663 // Otherwise recognize individual escapes.
1664 switch (Str[i]) {
1665 default:
1666 // Just reject invalid escape sequences for now.
1667 return TokError("invalid escape sequence (unrecognized character)");
1668
1669 case 'b': Data += '\b'; break;
1670 case 'f': Data += '\f'; break;
1671 case 'n': Data += '\n'; break;
1672 case 'r': Data += '\r'; break;
1673 case 't': Data += '\t'; break;
1674 case '"': Data += '"'; break;
1675 case '\\': Data += '\\'; break;
1676 }
1677 }
1678
1679 return false;
1680}
1681
Daniel Dunbara0d14262009-06-24 23:30:00 +00001682/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001683/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1684bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001685 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001686 CheckForValidSection();
1687
Daniel Dunbara0d14262009-06-24 23:30:00 +00001688 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001689 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001690 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001691
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001692 std::string Data;
1693 if (ParseEscapedString(Data))
1694 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695
1696 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001697 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001698 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1699
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701
1702 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001703 break;
1704
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001705 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001706 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001707 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001708 }
1709 }
1710
Sean Callanan79ed1a82010-01-19 20:22:31 +00001711 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001712 return false;
1713}
1714
1715/// ParseDirectiveValue
1716/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1717bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001718 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001719 CheckForValidSection();
1720
Daniel Dunbara0d14262009-06-24 23:30:00 +00001721 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001722 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001723 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001724 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001725 return true;
1726
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001727 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001728 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1729 assert(Size <= 8 && "Invalid size");
1730 uint64_t IntValue = MCE->getValue();
1731 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1732 return Error(ExprLoc, "literal value out of range for directive");
1733 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1734 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001735 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001736
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001737 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001738 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001739
Daniel Dunbara0d14262009-06-24 23:30:00 +00001740 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001741 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001742 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001743 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001744 }
1745 }
1746
Sean Callanan79ed1a82010-01-19 20:22:31 +00001747 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001748 return false;
1749}
1750
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001751/// ParseDirectiveRealValue
1752/// ::= (.single | .double) [ expression (, expression)* ]
1753bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1754 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1755 CheckForValidSection();
1756
1757 for (;;) {
1758 // We don't truly support arithmetic on floating point expressions, so we
1759 // have to manually parse unary prefixes.
1760 bool IsNeg = false;
1761 if (getLexer().is(AsmToken::Minus)) {
1762 Lex();
1763 IsNeg = true;
1764 } else if (getLexer().is(AsmToken::Plus))
1765 Lex();
1766
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001767 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001768 getLexer().isNot(AsmToken::Real) &&
1769 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001770 return TokError("unexpected token in directive");
1771
1772 // Convert to an APFloat.
1773 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001774 StringRef IDVal = getTok().getString();
1775 if (getLexer().is(AsmToken::Identifier)) {
1776 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1777 Value = APFloat::getInf(Semantics);
1778 else if (!IDVal.compare_lower("nan"))
1779 Value = APFloat::getNaN(Semantics, false, ~0);
1780 else
1781 return TokError("invalid floating point literal");
1782 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001783 APFloat::opInvalidOp)
1784 return TokError("invalid floating point literal");
1785 if (IsNeg)
1786 Value.changeSign();
1787
1788 // Consume the numeric token.
1789 Lex();
1790
1791 // Emit the value as an integer.
1792 APInt AsInt = Value.bitcastToAPInt();
1793 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1794 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1795
1796 if (getLexer().is(AsmToken::EndOfStatement))
1797 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001798
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001799 if (getLexer().isNot(AsmToken::Comma))
1800 return TokError("unexpected token in directive");
1801 Lex();
1802 }
1803 }
1804
1805 Lex();
1806 return false;
1807}
1808
Daniel Dunbara0d14262009-06-24 23:30:00 +00001809/// ParseDirectiveSpace
1810/// ::= .space expression [ , expression ]
1811bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001812 CheckForValidSection();
1813
Daniel Dunbara0d14262009-06-24 23:30:00 +00001814 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001815 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001816 return true;
1817
1818 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001819 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1820 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001821 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001822 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001823
Daniel Dunbar475839e2009-06-29 20:37:27 +00001824 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001825 return true;
1826
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001827 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001828 return TokError("unexpected token in '.space' directive");
1829 }
1830
Sean Callanan79ed1a82010-01-19 20:22:31 +00001831 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001832
1833 if (NumBytes <= 0)
1834 return TokError("invalid number of bytes in '.space' directive");
1835
1836 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001837 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001838
1839 return false;
1840}
1841
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001842/// ParseDirectiveZero
1843/// ::= .zero expression
1844bool AsmParser::ParseDirectiveZero() {
1845 CheckForValidSection();
1846
1847 int64_t NumBytes;
1848 if (ParseAbsoluteExpression(NumBytes))
1849 return true;
1850
Rafael Espindolae452b172010-10-05 19:42:57 +00001851 int64_t Val = 0;
1852 if (getLexer().is(AsmToken::Comma)) {
1853 Lex();
1854 if (ParseAbsoluteExpression(Val))
1855 return true;
1856 }
1857
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001858 if (getLexer().isNot(AsmToken::EndOfStatement))
1859 return TokError("unexpected token in '.zero' directive");
1860
1861 Lex();
1862
Rafael Espindolae452b172010-10-05 19:42:57 +00001863 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001864
1865 return false;
1866}
1867
Daniel Dunbara0d14262009-06-24 23:30:00 +00001868/// ParseDirectiveFill
1869/// ::= .fill expression , expression , expression
1870bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001871 CheckForValidSection();
1872
Daniel Dunbara0d14262009-06-24 23:30:00 +00001873 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001874 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001875 return true;
1876
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001877 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001878 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001879 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001880
Daniel Dunbara0d14262009-06-24 23:30:00 +00001881 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001882 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001883 return true;
1884
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001885 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001886 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001887 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001888
Daniel Dunbara0d14262009-06-24 23:30:00 +00001889 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001890 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001891 return true;
1892
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001893 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001894 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001895
Sean Callanan79ed1a82010-01-19 20:22:31 +00001896 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001897
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001898 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1899 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001900
1901 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001902 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001903
1904 return false;
1905}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001906
1907/// ParseDirectiveOrg
1908/// ::= .org expression [ , expression ]
1909bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001910 CheckForValidSection();
1911
Daniel Dunbar821e3332009-08-31 08:09:28 +00001912 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001913 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001914 return true;
1915
1916 // Parse optional fill expression.
1917 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001918 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1919 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001920 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001921 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001922
Daniel Dunbar475839e2009-06-29 20:37:27 +00001923 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001924 return true;
1925
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001926 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001927 return TokError("unexpected token in '.org' directive");
1928 }
1929
Sean Callanan79ed1a82010-01-19 20:22:31 +00001930 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001931
1932 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1933 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001934 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001935
1936 return false;
1937}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001938
1939/// ParseDirectiveAlign
1940/// ::= {.align, ...} expression [ , expression [ , expression ]]
1941bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001942 CheckForValidSection();
1943
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001944 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001945 int64_t Alignment;
1946 if (ParseAbsoluteExpression(Alignment))
1947 return true;
1948
1949 SMLoc MaxBytesLoc;
1950 bool HasFillExpr = false;
1951 int64_t FillExpr = 0;
1952 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001953 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1954 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001955 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001956 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001957
1958 // The fill expression can be omitted while specifying a maximum number of
1959 // alignment bytes, e.g:
1960 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001961 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001962 HasFillExpr = true;
1963 if (ParseAbsoluteExpression(FillExpr))
1964 return true;
1965 }
1966
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001967 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1968 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001969 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001970 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001971
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001972 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001973 if (ParseAbsoluteExpression(MaxBytesToFill))
1974 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001975
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001976 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001977 return TokError("unexpected token in directive");
1978 }
1979 }
1980
Sean Callanan79ed1a82010-01-19 20:22:31 +00001981 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001982
Daniel Dunbar648ac512010-05-17 21:54:30 +00001983 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001984 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001985
1986 // Compute alignment in bytes.
1987 if (IsPow2) {
1988 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001989 if (Alignment >= 32) {
1990 Error(AlignmentLoc, "invalid alignment value");
1991 Alignment = 31;
1992 }
1993
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001994 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001995 }
1996
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001997 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001998 if (MaxBytesLoc.isValid()) {
1999 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002000 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2001 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002002 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002003 }
2004
2005 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002006 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2007 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002008 MaxBytesToFill = 0;
2009 }
2010 }
2011
Daniel Dunbar648ac512010-05-17 21:54:30 +00002012 // Check whether we should use optimal code alignment for this .align
2013 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002014 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002015 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2016 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002017 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002018 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002019 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002020 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2021 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002022 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002023
2024 return false;
2025}
2026
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002027/// ParseDirectiveSymbolAttribute
2028/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002029bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002030 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002031 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002032 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002033 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002034
2035 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002036 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002037
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002038 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002039
Jim Grosbach10ec6502011-09-15 17:56:49 +00002040 // Assembler local symbols don't make any sense here. Complain loudly.
2041 if (Sym->isTemporary())
2042 return Error(Loc, "non-local symbol required in directive");
2043
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002044 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002045
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002046 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002047 break;
2048
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002049 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002050 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002051 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002052 }
2053 }
2054
Sean Callanan79ed1a82010-01-19 20:22:31 +00002055 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002056 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002057}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002058
2059/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002060/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2061bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002062 CheckForValidSection();
2063
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002064 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002065 StringRef Name;
2066 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002067 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002068
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002069 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002070 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002071
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002072 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002073 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002074 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002075
2076 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002077 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002078 if (ParseAbsoluteExpression(Size))
2079 return true;
2080
2081 int64_t Pow2Alignment = 0;
2082 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002083 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002084 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002085 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002086 if (ParseAbsoluteExpression(Pow2Alignment))
2087 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002088
Chris Lattner258281d2010-01-19 06:22:22 +00002089 // If this target takes alignments in bytes (not log) validate and convert.
2090 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2091 if (!isPowerOf2_64(Pow2Alignment))
2092 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2093 Pow2Alignment = Log2_64(Pow2Alignment);
2094 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002095 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002096
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002097 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002098 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002099
Sean Callanan79ed1a82010-01-19 20:22:31 +00002100 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002101
Chris Lattner1fc3d752009-07-09 17:25:12 +00002102 // NOTE: a size of zero for a .comm should create a undefined symbol
2103 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002104 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002105 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2106 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002107
Eric Christopherc260a3e2010-05-14 01:38:54 +00002108 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002109 // may internally end up wanting an alignment in bytes.
2110 // FIXME: Diagnose overflow.
2111 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002112 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2113 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002114
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002115 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002116 return Error(IDLoc, "invalid symbol redefinition");
2117
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002118 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002119 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002120 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002121 getStreamer().EmitZerofill(Ctx.getMachOSection(
2122 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2123 0, SectionKind::getBSS()),
2124 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002125 return false;
2126 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002127
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002128 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002129 return false;
2130}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002131
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002132/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002133/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002134bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002135 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002137
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002138 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002139 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002140 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002141
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002143
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002144 if (Str.empty())
2145 Error(Loc, ".abort detected. Assembly stopping.");
2146 else
2147 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002148 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002149
2150 return false;
2151}
Kevin Enderby71148242009-07-14 21:35:03 +00002152
Kevin Enderby1f049b22009-07-14 23:21:55 +00002153/// ParseDirectiveInclude
2154/// ::= .include "filename"
2155bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002156 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002157 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002158
Sean Callanan18b83232010-01-19 21:44:56 +00002159 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002160 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002161 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002162
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002163 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002164 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002165
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002166 // Strip the quotes.
2167 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002168
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002169 // Attempt to switch the lexer to the included file before consuming the end
2170 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002171 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002172 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002173 return true;
2174 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002175
2176 return false;
2177}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002178
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002179/// ParseDirectiveIf
2180/// ::= .if expression
2181bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002182 TheCondStack.push_back(TheCondState);
2183 TheCondState.TheCond = AsmCond::IfCond;
2184 if(TheCondState.Ignore) {
2185 EatToEndOfStatement();
2186 }
2187 else {
2188 int64_t ExprValue;
2189 if (ParseAbsoluteExpression(ExprValue))
2190 return true;
2191
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002192 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002193 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002194
Sean Callanan79ed1a82010-01-19 20:22:31 +00002195 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002196
2197 TheCondState.CondMet = ExprValue;
2198 TheCondState.Ignore = !TheCondState.CondMet;
2199 }
2200
2201 return false;
2202}
2203
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002204bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2205 StringRef Name;
2206 TheCondStack.push_back(TheCondState);
2207 TheCondState.TheCond = AsmCond::IfCond;
2208
2209 if (TheCondState.Ignore) {
2210 EatToEndOfStatement();
2211 } else {
2212 if (ParseIdentifier(Name))
2213 return TokError("expected identifier after '.ifdef'");
2214
2215 Lex();
2216
2217 MCSymbol *Sym = getContext().LookupSymbol(Name);
2218
2219 if (expect_defined)
2220 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2221 else
2222 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2223 TheCondState.Ignore = !TheCondState.CondMet;
2224 }
2225
2226 return false;
2227}
2228
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002229/// ParseDirectiveElseIf
2230/// ::= .elseif expression
2231bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2232 if (TheCondState.TheCond != AsmCond::IfCond &&
2233 TheCondState.TheCond != AsmCond::ElseIfCond)
2234 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2235 " an .elseif");
2236 TheCondState.TheCond = AsmCond::ElseIfCond;
2237
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002238 bool LastIgnoreState = false;
2239 if (!TheCondStack.empty())
2240 LastIgnoreState = TheCondStack.back().Ignore;
2241 if (LastIgnoreState || TheCondState.CondMet) {
2242 TheCondState.Ignore = true;
2243 EatToEndOfStatement();
2244 }
2245 else {
2246 int64_t ExprValue;
2247 if (ParseAbsoluteExpression(ExprValue))
2248 return true;
2249
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002250 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002251 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002252
Sean Callanan79ed1a82010-01-19 20:22:31 +00002253 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002254 TheCondState.CondMet = ExprValue;
2255 TheCondState.Ignore = !TheCondState.CondMet;
2256 }
2257
2258 return false;
2259}
2260
2261/// ParseDirectiveElse
2262/// ::= .else
2263bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002264 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002265 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002266
Sean Callanan79ed1a82010-01-19 20:22:31 +00002267 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002268
2269 if (TheCondState.TheCond != AsmCond::IfCond &&
2270 TheCondState.TheCond != AsmCond::ElseIfCond)
2271 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2272 ".elseif");
2273 TheCondState.TheCond = AsmCond::ElseCond;
2274 bool LastIgnoreState = false;
2275 if (!TheCondStack.empty())
2276 LastIgnoreState = TheCondStack.back().Ignore;
2277 if (LastIgnoreState || TheCondState.CondMet)
2278 TheCondState.Ignore = true;
2279 else
2280 TheCondState.Ignore = false;
2281
2282 return false;
2283}
2284
2285/// ParseDirectiveEndIf
2286/// ::= .endif
2287bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002288 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002289 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002290
Sean Callanan79ed1a82010-01-19 20:22:31 +00002291 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002292
2293 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2294 TheCondStack.empty())
2295 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2296 ".else");
2297 if (!TheCondStack.empty()) {
2298 TheCondState = TheCondStack.back();
2299 TheCondStack.pop_back();
2300 }
2301
2302 return false;
2303}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002304
2305/// ParseDirectiveFile
2306/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002307bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002308 // FIXME: I'm not sure what this is.
2309 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002310 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002311 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002312 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002313 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002314
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002315 if (FileNumber < 1)
2316 return TokError("file number less than one");
2317 }
2318
Daniel Dunbareceec052010-07-12 17:45:27 +00002319 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002320 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002321
Chris Lattnerd32e8032010-01-25 19:02:58 +00002322 StringRef Filename = getTok().getString();
2323 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002324 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002325
Daniel Dunbareceec052010-07-12 17:45:27 +00002326 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002327 return TokError("unexpected token in '.file' directive");
2328
Chris Lattnerd32e8032010-01-25 19:02:58 +00002329 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002330 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002331 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002332 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002333 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002334 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002335
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002336 return false;
2337}
2338
2339/// ParseDirectiveLine
2340/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002341bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002342 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2343 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002344 return TokError("unexpected token in '.line' directive");
2345
Sean Callanan18b83232010-01-19 21:44:56 +00002346 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002347 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002348 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002349
2350 // FIXME: Do something with the .line.
2351 }
2352
Daniel Dunbareceec052010-07-12 17:45:27 +00002353 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002354 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002355
2356 return false;
2357}
2358
2359
2360/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002361/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002362/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2363/// The first number is a file number, must have been previously assigned with
2364/// a .file directive, the second number is the line number and optionally the
2365/// third number is a column position (zero if not specified). The remaining
2366/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002367bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002368
Daniel Dunbareceec052010-07-12 17:45:27 +00002369 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002370 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002371 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002372 if (FileNumber < 1)
2373 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002374 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002375 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002376 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002377
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002378 int64_t LineNumber = 0;
2379 if (getLexer().is(AsmToken::Integer)) {
2380 LineNumber = getTok().getIntVal();
2381 if (LineNumber < 1)
2382 return TokError("line number less than one in '.loc' directive");
2383 Lex();
2384 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002385
2386 int64_t ColumnPos = 0;
2387 if (getLexer().is(AsmToken::Integer)) {
2388 ColumnPos = getTok().getIntVal();
2389 if (ColumnPos < 0)
2390 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002391 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002392 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002393
Kevin Enderbyc0957932010-09-30 16:52:03 +00002394 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002395 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002396 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002397 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2398 for (;;) {
2399 if (getLexer().is(AsmToken::EndOfStatement))
2400 break;
2401
2402 StringRef Name;
2403 SMLoc Loc = getTok().getLoc();
2404 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002405 return TokError("unexpected token in '.loc' directive");
2406
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002407 if (Name == "basic_block")
2408 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2409 else if (Name == "prologue_end")
2410 Flags |= DWARF2_FLAG_PROLOGUE_END;
2411 else if (Name == "epilogue_begin")
2412 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2413 else if (Name == "is_stmt") {
2414 SMLoc Loc = getTok().getLoc();
2415 const MCExpr *Value;
2416 if (getParser().ParseExpression(Value))
2417 return true;
2418 // The expression must be the constant 0 or 1.
2419 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2420 int Value = MCE->getValue();
2421 if (Value == 0)
2422 Flags &= ~DWARF2_FLAG_IS_STMT;
2423 else if (Value == 1)
2424 Flags |= DWARF2_FLAG_IS_STMT;
2425 else
2426 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002427 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002428 else {
2429 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2430 }
2431 }
2432 else if (Name == "isa") {
2433 SMLoc Loc = getTok().getLoc();
2434 const MCExpr *Value;
2435 if (getParser().ParseExpression(Value))
2436 return true;
2437 // The expression must be a constant greater or equal to 0.
2438 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2439 int Value = MCE->getValue();
2440 if (Value < 0)
2441 return Error(Loc, "isa number less than zero");
2442 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002443 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002444 else {
2445 return Error(Loc, "isa number not a constant value");
2446 }
2447 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002448 else if (Name == "discriminator") {
2449 if (getParser().ParseAbsoluteExpression(Discriminator))
2450 return true;
2451 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002452 else {
2453 return Error(Loc, "unknown sub-directive in '.loc' directive");
2454 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002455
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002456 if (getLexer().is(AsmToken::EndOfStatement))
2457 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002458 }
2459 }
2460
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002461 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002462 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002463
2464 return false;
2465}
2466
Daniel Dunbar138abae2010-10-16 04:56:42 +00002467/// ParseDirectiveStabs
2468/// ::= .stabs string, number, number, number
2469bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2470 SMLoc DirectiveLoc) {
2471 return TokError("unsupported directive '" + Directive + "'");
2472}
2473
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002474/// ParseDirectiveCFISections
2475/// ::= .cfi_sections section [, section]
2476bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2477 SMLoc DirectiveLoc) {
2478 StringRef Name;
2479 bool EH = false;
2480 bool Debug = false;
2481
2482 if (getParser().ParseIdentifier(Name))
2483 return TokError("Expected an identifier");
2484
2485 if (Name == ".eh_frame")
2486 EH = true;
2487 else if (Name == ".debug_frame")
2488 Debug = true;
2489
2490 if (getLexer().is(AsmToken::Comma)) {
2491 Lex();
2492
2493 if (getParser().ParseIdentifier(Name))
2494 return TokError("Expected an identifier");
2495
2496 if (Name == ".eh_frame")
2497 EH = true;
2498 else if (Name == ".debug_frame")
2499 Debug = true;
2500 }
2501
2502 getStreamer().EmitCFISections(EH, Debug);
2503
2504 return false;
2505}
2506
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002507/// ParseDirectiveCFIStartProc
2508/// ::= .cfi_startproc
2509bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2510 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002511 getStreamer().EmitCFIStartProc();
2512 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002513}
2514
2515/// ParseDirectiveCFIEndProc
2516/// ::= .cfi_endproc
2517bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002518 getStreamer().EmitCFIEndProc();
2519 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002520}
2521
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002522/// ParseRegisterOrRegisterNumber - parse register name or number.
2523bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2524 SMLoc DirectiveLoc) {
2525 unsigned RegNo;
2526
Jim Grosbach6f888a82011-06-02 17:14:04 +00002527 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002528 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2529 DirectiveLoc))
2530 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002531 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002532 } else
2533 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002534
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002535 return false;
2536}
2537
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002538/// ParseDirectiveCFIDefCfa
2539/// ::= .cfi_def_cfa register, offset
2540bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2541 SMLoc DirectiveLoc) {
2542 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002543 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002544 return true;
2545
2546 if (getLexer().isNot(AsmToken::Comma))
2547 return TokError("unexpected token in directive");
2548 Lex();
2549
2550 int64_t Offset = 0;
2551 if (getParser().ParseAbsoluteExpression(Offset))
2552 return true;
2553
Rafael Espindola066c2f42011-04-12 23:59:07 +00002554 getStreamer().EmitCFIDefCfa(Register, Offset);
2555 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002556}
2557
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002558/// ParseDirectiveCFIDefCfaOffset
2559/// ::= .cfi_def_cfa_offset offset
2560bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2561 SMLoc DirectiveLoc) {
2562 int64_t Offset = 0;
2563 if (getParser().ParseAbsoluteExpression(Offset))
2564 return true;
2565
Rafael Espindola066c2f42011-04-12 23:59:07 +00002566 getStreamer().EmitCFIDefCfaOffset(Offset);
2567 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002568}
2569
2570/// ParseDirectiveCFIAdjustCfaOffset
2571/// ::= .cfi_adjust_cfa_offset adjustment
2572bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2573 SMLoc DirectiveLoc) {
2574 int64_t Adjustment = 0;
2575 if (getParser().ParseAbsoluteExpression(Adjustment))
2576 return true;
2577
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002578 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2579 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002580}
2581
2582/// ParseDirectiveCFIDefCfaRegister
2583/// ::= .cfi_def_cfa_register register
2584bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2585 SMLoc DirectiveLoc) {
2586 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002587 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002588 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002589
Rafael Espindola066c2f42011-04-12 23:59:07 +00002590 getStreamer().EmitCFIDefCfaRegister(Register);
2591 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002592}
2593
2594/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002595/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002596bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2597 int64_t Register = 0;
2598 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002599
2600 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002601 return true;
2602
2603 if (getLexer().isNot(AsmToken::Comma))
2604 return TokError("unexpected token in directive");
2605 Lex();
2606
2607 if (getParser().ParseAbsoluteExpression(Offset))
2608 return true;
2609
Rafael Espindola066c2f42011-04-12 23:59:07 +00002610 getStreamer().EmitCFIOffset(Register, Offset);
2611 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002612}
2613
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002614/// ParseDirectiveCFIRelOffset
2615/// ::= .cfi_rel_offset register, offset
2616bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2617 SMLoc DirectiveLoc) {
2618 int64_t Register = 0;
2619
2620 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2621 return true;
2622
2623 if (getLexer().isNot(AsmToken::Comma))
2624 return TokError("unexpected token in directive");
2625 Lex();
2626
2627 int64_t Offset = 0;
2628 if (getParser().ParseAbsoluteExpression(Offset))
2629 return true;
2630
Rafael Espindola25f492e2011-04-12 16:12:03 +00002631 getStreamer().EmitCFIRelOffset(Register, Offset);
2632 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002633}
2634
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002635static bool isValidEncoding(int64_t Encoding) {
2636 if (Encoding & ~0xff)
2637 return false;
2638
2639 if (Encoding == dwarf::DW_EH_PE_omit)
2640 return true;
2641
2642 const unsigned Format = Encoding & 0xf;
2643 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2644 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2645 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2646 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2647 return false;
2648
Rafael Espindolacaf11582010-12-29 04:31:26 +00002649 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002650 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002651 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002652 return false;
2653
2654 return true;
2655}
2656
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002657/// ParseDirectiveCFIPersonalityOrLsda
2658/// ::= .cfi_personality encoding, [symbol_name]
2659/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002660bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002661 SMLoc DirectiveLoc) {
2662 int64_t Encoding = 0;
2663 if (getParser().ParseAbsoluteExpression(Encoding))
2664 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002665 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002666 return false;
2667
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002668 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002669 return TokError("unsupported encoding.");
2670
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002671 if (getLexer().isNot(AsmToken::Comma))
2672 return TokError("unexpected token in directive");
2673 Lex();
2674
2675 StringRef Name;
2676 if (getParser().ParseIdentifier(Name))
2677 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002678
2679 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2680
2681 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002682 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002683 else {
2684 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002685 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002686 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002687 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002688}
2689
Rafael Espindolafe024d02010-12-28 18:36:23 +00002690/// ParseDirectiveCFIRememberState
2691/// ::= .cfi_remember_state
2692bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2693 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002694 getStreamer().EmitCFIRememberState();
2695 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002696}
2697
2698/// ParseDirectiveCFIRestoreState
2699/// ::= .cfi_remember_state
2700bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2701 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002702 getStreamer().EmitCFIRestoreState();
2703 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002704}
2705
Rafael Espindolac5754392011-04-12 15:31:05 +00002706/// ParseDirectiveCFISameValue
2707/// ::= .cfi_same_value register
2708bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2709 SMLoc DirectiveLoc) {
2710 int64_t Register = 0;
2711
2712 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2713 return true;
2714
2715 getStreamer().EmitCFISameValue(Register);
2716
2717 return false;
2718}
2719
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002720/// ParseDirectiveMacrosOnOff
2721/// ::= .macros_on
2722/// ::= .macros_off
2723bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2724 SMLoc DirectiveLoc) {
2725 if (getLexer().isNot(AsmToken::EndOfStatement))
2726 return Error(getLexer().getLoc(),
2727 "unexpected token in '" + Directive + "' directive");
2728
2729 getParser().MacrosEnabled = Directive == ".macros_on";
2730
2731 return false;
2732}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002733
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002734/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002735/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002736bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2737 SMLoc DirectiveLoc) {
2738 StringRef Name;
2739 if (getParser().ParseIdentifier(Name))
2740 return TokError("expected identifier in directive");
2741
Rafael Espindola65366442011-06-05 02:43:45 +00002742 std::vector<StringRef> Parameters;
2743 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2744 for(;;) {
2745 StringRef Parameter;
2746 if (getParser().ParseIdentifier(Parameter))
2747 return TokError("expected identifier in directive");
2748 Parameters.push_back(Parameter);
2749
2750 if (getLexer().isNot(AsmToken::Comma))
2751 break;
2752 Lex();
2753 }
2754 }
2755
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002756 if (getLexer().isNot(AsmToken::EndOfStatement))
2757 return TokError("unexpected token in '.macro' directive");
2758
2759 // Eat the end of statement.
2760 Lex();
2761
2762 AsmToken EndToken, StartToken = getTok();
2763
2764 // Lex the macro definition.
2765 for (;;) {
2766 // Check whether we have reached the end of the file.
2767 if (getLexer().is(AsmToken::Eof))
2768 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2769
2770 // Otherwise, check whether we have reach the .endmacro.
2771 if (getLexer().is(AsmToken::Identifier) &&
2772 (getTok().getIdentifier() == ".endm" ||
2773 getTok().getIdentifier() == ".endmacro")) {
2774 EndToken = getTok();
2775 Lex();
2776 if (getLexer().isNot(AsmToken::EndOfStatement))
2777 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2778 "' directive");
2779 break;
2780 }
2781
2782 // Otherwise, scan til the end of the statement.
2783 getParser().EatToEndOfStatement();
2784 }
2785
2786 if (getParser().MacroMap.lookup(Name)) {
2787 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2788 }
2789
2790 const char *BodyStart = StartToken.getLoc().getPointer();
2791 const char *BodyEnd = EndToken.getLoc().getPointer();
2792 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002793 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002794 return false;
2795}
2796
2797/// ParseDirectiveEndMacro
2798/// ::= .endm
2799/// ::= .endmacro
2800bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2801 SMLoc DirectiveLoc) {
2802 if (getLexer().isNot(AsmToken::EndOfStatement))
2803 return TokError("unexpected token in '" + Directive + "' directive");
2804
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002805 // If we are inside a macro instantiation, terminate the current
2806 // instantiation.
2807 if (!getParser().ActiveMacros.empty()) {
2808 getParser().HandleMacroExit();
2809 return false;
2810 }
2811
2812 // Otherwise, this .endmacro is a stray entry in the file; well formed
2813 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002814 return TokError("unexpected '" + Directive + "' in file, "
2815 "no current macro definition");
2816}
2817
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002818bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002819 getParser().CheckForValidSection();
2820
2821 const MCExpr *Value;
2822
2823 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002824 return true;
2825
2826 if (getLexer().isNot(AsmToken::EndOfStatement))
2827 return TokError("unexpected token in directive");
2828
2829 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002830 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002831 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002832 getStreamer().EmitULEB128Value(Value);
2833
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002834 return false;
2835}
2836
2837
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002838/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002839MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002840 MCContext &C, MCStreamer &Out,
2841 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002842 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002843}