blob: 0be8f51f0c6cdaeb03a2c9b93171198715a754f8 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000028#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000032#include "llvm/Support/CommandLine.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
90 MCAsmParserExtension *GenericParser;
91 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000092
Daniel Dunbaraef87e32010-07-18 18:31:38 +000093 /// This is the current buffer index we're lexing from as managed by the
94 /// SourceMgr object.
95 int CurBuffer;
96
97 AsmCond TheCondState;
98 std::vector<AsmCond> TheCondStack;
99
100 /// DirectiveMap - This is a table handlers for directives. Each handler is
101 /// invoked after the directive identifier is read and is responsible for
102 /// parsing and validating the rest of the directive. The handler is passed
103 /// in the directive name and the location of the directive keyword.
104 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000105
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000106 /// MacroMap - Map of currently defined macros.
107 StringMap<Macro*> MacroMap;
108
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000109 /// ActiveMacros - Stack of active macro instantiations.
110 std::vector<MacroInstantiation*> ActiveMacros;
111
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000112 /// Boolean tracking whether macro substitution is enabled.
113 unsigned MacrosEnabled : 1;
114
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000115 /// Flag tracking whether any errors have been encountered.
116 unsigned HadError : 1;
117
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000118 /// The values from the last parsed cpp hash file line comment if any.
119 StringRef CppHashFilename;
120 int64_t CppHashLineNumber;
121 SMLoc CppHashLoc;
122
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000123public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000124 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000125 const MCAsmInfo &MAI);
126 ~AsmParser();
127
128 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
129
130 void AddDirectiveHandler(MCAsmParserExtension *Object,
131 StringRef Directive,
132 DirectiveHandler Handler) {
133 DirectiveMap[Directive] = std::make_pair(Object, Handler);
134 }
135
136public:
137 /// @name MCAsmParser Interface
138 /// {
139
140 virtual SourceMgr &getSourceManager() { return SrcMgr; }
141 virtual MCAsmLexer &getLexer() { return Lexer; }
142 virtual MCContext &getContext() { return Ctx; }
143 virtual MCStreamer &getStreamer() { return Out; }
144
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000145 virtual bool Warning(SMLoc L, const Twine &Msg,
146 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
147 virtual bool Error(SMLoc L, const Twine &Msg,
148 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000149
150 const AsmToken &Lex();
151
152 bool ParseExpression(const MCExpr *&Res);
153 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
154 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
155 virtual bool ParseAbsoluteExpression(int64_t &Res);
156
157 /// }
158
159private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000160 void CheckForValidSection();
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000163 void EatToEndOfLine();
164 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000165
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000166 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000167 bool expandMacro(SmallString<256> &Buf, StringRef Body,
168 const std::vector<StringRef> &Parameters,
169 const std::vector<std::vector<AsmToken> > &A,
170 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000171 void HandleMacroExit();
172
173 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000174 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000175 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000176 bool ShowLine = true) const {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000177 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges, ShowLine);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000178 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000179 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000180
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000181 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
182 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000183
184 /// \brief Reset the current lexer position to that given by \arg Loc. The
185 /// current token is not set; clients should ensure Lex() is called
186 /// subsequently.
187 void JumpToLoc(SMLoc Loc);
188
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000189 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000190
191 /// \brief Parse up to the end of statement and a return the contents from the
192 /// current token until the end of the statement; the current token on exit
193 /// will be either the EndOfStatement or EOF.
194 StringRef ParseStringToEndOfStatement();
195
Nico Weber4c4c7322011-01-28 03:04:41 +0000196 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197
198 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
199 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
200 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000201 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202
203 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
204 /// and set \arg Res to the identifier contents.
205 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000206
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000208
209 // ".ascii", ".asciiz", ".string"
210 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000211 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000212 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213 bool ParseDirectiveFill(); // ".fill"
214 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000215 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000216 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000217 bool ParseDirectiveOrg(); // ".org"
218 // ".align{,32}", ".p2align{,w,l}"
219 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
220
221 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
222 /// accepts a single symbol (which should be a label or an external).
223 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224
225 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
226
227 bool ParseDirectiveAbort(); // ".abort"
228 bool ParseDirectiveInclude(); // ".include"
229
230 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000231 // ".ifdef" or ".ifndef", depending on expect_defined
232 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000233 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
234 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
235 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
236
237 /// ParseEscapedString - Parse the current token as a string which may include
238 /// escaped characters and return the string contents.
239 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000240
241 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
242 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000243};
244
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000245/// \brief Generic implementations of directive handling, etc. which is shared
246/// (or the default, at least) for all assembler parser.
247class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000248 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
249 void AddDirectiveHandler(StringRef Directive) {
250 getParser().AddDirectiveHandler(this, Directive,
251 HandleDirective<GenericAsmParser, Handler>);
252 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000253public:
254 GenericAsmParser() {}
255
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000256 AsmParser &getParser() {
257 return (AsmParser&) this->MCAsmParserExtension::getParser();
258 }
259
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000260 virtual void Initialize(MCAsmParser &Parser) {
261 // Call the base implementation.
262 this->MCAsmParserExtension::Initialize(Parser);
263
264 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000269
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000270 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
272 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
274 ".cfi_startproc");
275 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
276 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000277 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
278 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000279 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
280 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000281 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
282 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000283 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
284 ".cfi_def_cfa_register");
285 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
286 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000287 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
288 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000289 AddDirectiveHandler<
290 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
291 AddDirectiveHandler<
292 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000293 AddDirectiveHandler<
294 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
295 AddDirectiveHandler<
296 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000297 AddDirectiveHandler<
298 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000299
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000300 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000301 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
302 ".macros_on");
303 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
304 ".macros_off");
305 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000308
309 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000311 }
312
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000313 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
314
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000315 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
316 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
317 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000318 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000319 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000320 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
321 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000322 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000323 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000324 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000325 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
326 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000327 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000328 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000329 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
330 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000331 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000332
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000333 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000334 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
335 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000336
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000337 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000338};
339
340}
341
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000342namespace llvm {
343
344extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000345extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000346extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000347
348}
349
Chris Lattneraaec2052010-01-19 19:46:13 +0000350enum { DEFAULT_ADDRSPACE = 0 };
351
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000352AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000353 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000354 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000355 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000356 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
357 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000358 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000359
360 // Initialize the generic parser.
361 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000362
363 // Initialize the platform / file format parser.
364 //
365 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
366 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000367 if (_MAI.hasMicrosoftFastStdCallMangling()) {
368 PlatformParser = createCOFFAsmParser();
369 PlatformParser->Initialize(*this);
370 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000371 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000372 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000373 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000374 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000375 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000376 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000377}
378
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000379AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000380 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
381
382 // Destroy any macros.
383 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
384 ie = MacroMap.end(); it != ie; ++it)
385 delete it->getValue();
386
Daniel Dunbare4749702010-07-12 18:12:02 +0000387 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000388 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000389}
390
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000391void AsmParser::PrintMacroInstantiations() {
392 // Print the active macro instantiation stack.
393 for (std::vector<MacroInstantiation*>::const_reverse_iterator
394 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000395 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
396 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000397}
398
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000399bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000400 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000401 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000402 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000403 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000404 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000405}
406
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000407bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000408 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000409 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000410 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000411 return true;
412}
413
Sean Callananfd0b0282010-01-21 00:19:58 +0000414bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000415 std::string IncludedFile;
416 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000417 if (NewBuf == -1)
418 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000419
Sean Callananfd0b0282010-01-21 00:19:58 +0000420 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000421
Sean Callananfd0b0282010-01-21 00:19:58 +0000422 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000423
Sean Callananfd0b0282010-01-21 00:19:58 +0000424 return false;
425}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000426
427void AsmParser::JumpToLoc(SMLoc Loc) {
428 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
429 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
430}
431
Sean Callananfd0b0282010-01-21 00:19:58 +0000432const AsmToken &AsmParser::Lex() {
433 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000434
Sean Callananfd0b0282010-01-21 00:19:58 +0000435 if (tok->is(AsmToken::Eof)) {
436 // If this is the end of an included file, pop the parent file off the
437 // include stack.
438 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
439 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000440 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000441 tok = &Lexer.Lex();
442 }
443 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000444
Sean Callananfd0b0282010-01-21 00:19:58 +0000445 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000446 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000447
Sean Callananfd0b0282010-01-21 00:19:58 +0000448 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000449}
450
Chris Lattner79180e22010-04-05 23:15:42 +0000451bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000452 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000453 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000454 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000455
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000456 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000457 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000458
459 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000460 AsmCond StartingCondState = TheCondState;
461
Chris Lattnerb717fb02009-07-02 21:53:43 +0000462 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000463 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000464 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000465
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000466 // We had an error, validate that one was emitted and recover by skipping to
467 // the next line.
468 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000469 EatToEndOfStatement();
470 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000471
472 if (TheCondState.TheCond != StartingCondState.TheCond ||
473 TheCondState.Ignore != StartingCondState.Ignore)
474 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000475
476 // Check to see there are no empty DwarfFile slots.
477 const std::vector<MCDwarfFile *> &MCDwarfFiles =
478 getContext().getMCDwarfFiles();
479 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000480 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000481 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000482 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000483
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000484 // Check to see that all assembler local symbols were actually defined.
485 // Targets that don't do subsections via symbols may not want this, though,
486 // so conservatively exclude them. Only do this if we're finalizing, though,
487 // as otherwise we won't necessarilly have seen everything yet.
488 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
489 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
490 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
491 e = Symbols.end();
492 i != e; ++i) {
493 MCSymbol *Sym = i->getValue();
494 // Variable symbols may not be marked as defined, so check those
495 // explicitly. If we know it's a variable, we have a definition for
496 // the purposes of this check.
497 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
498 // FIXME: We would really like to refer back to where the symbol was
499 // first referenced for a source location. We need to add something
500 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000501 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
502 "assembler local symbol '" + Sym->getName() +
503 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000504 }
505 }
506
507
Chris Lattner79180e22010-04-05 23:15:42 +0000508 // Finalize the output stream if there are no errors and if the client wants
509 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000510 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000511 Out.Finish();
512
Chris Lattnerb717fb02009-07-02 21:53:43 +0000513 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000514}
515
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000516void AsmParser::CheckForValidSection() {
517 if (!getStreamer().getCurrentSection()) {
518 TokError("expected section directive before assembly directive");
519 Out.SwitchSection(Ctx.getMachOSection(
520 "__TEXT", "__text",
521 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
522 0, SectionKind::getText()));
523 }
524}
525
Chris Lattner2cf5f142009-06-22 01:29:09 +0000526/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
527void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000528 while (Lexer.isNot(AsmToken::EndOfStatement) &&
529 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000530 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000531
Chris Lattner2cf5f142009-06-22 01:29:09 +0000532 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000533 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000534 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000535}
536
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000537StringRef AsmParser::ParseStringToEndOfStatement() {
538 const char *Start = getTok().getLoc().getPointer();
539
540 while (Lexer.isNot(AsmToken::EndOfStatement) &&
541 Lexer.isNot(AsmToken::Eof))
542 Lex();
543
544 const char *End = getTok().getLoc().getPointer();
545 return StringRef(Start, End - Start);
546}
Chris Lattnerc4193832009-06-22 05:51:26 +0000547
Chris Lattner74ec1a32009-06-22 06:32:03 +0000548/// ParseParenExpr - Parse a paren expression and return it.
549/// NOTE: This assumes the leading '(' has already been consumed.
550///
551/// parenexpr ::= expr)
552///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000553bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000554 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000555 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000556 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000557 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000558 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000559 return false;
560}
Chris Lattnerc4193832009-06-22 05:51:26 +0000561
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000562/// ParseBracketExpr - Parse a bracket expression and return it.
563/// NOTE: This assumes the leading '[' has already been consumed.
564///
565/// bracketexpr ::= expr]
566///
567bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
568 if (ParseExpression(Res)) return true;
569 if (Lexer.isNot(AsmToken::RBrac))
570 return TokError("expected ']' in brackets expression");
571 EndLoc = Lexer.getLoc();
572 Lex();
573 return false;
574}
575
Chris Lattner74ec1a32009-06-22 06:32:03 +0000576/// ParsePrimaryExpr - Parse a primary expression and return it.
577/// primaryexpr ::= (parenexpr
578/// primaryexpr ::= symbol
579/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000580/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000581/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000582bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000583 switch (Lexer.getKind()) {
584 default:
585 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000586 // If we have an error assume that we've already handled it.
587 case AsmToken::Error:
588 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000589 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000590 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000591 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000592 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000593 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000594 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000595 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000596 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000597 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000598 EndLoc = Lexer.getLoc();
599
600 StringRef Identifier;
601 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000602 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000603
Daniel Dunbarfffff912009-10-16 01:34:54 +0000604 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000605 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000606 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000607
608 // Lookup the symbol variant if used.
609 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000610 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000611 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000612 if (Variant == MCSymbolRefExpr::VK_Invalid) {
613 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000614 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000615 }
616 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000617
Daniel Dunbarfffff912009-10-16 01:34:54 +0000618 // If this is an absolute variable reference, substitute it now to preserve
619 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000620 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000621 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000622 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000623
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000624 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000625 return false;
626 }
627
628 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000629 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000630 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000631 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000632 case AsmToken::Integer: {
633 SMLoc Loc = getTok().getLoc();
634 int64_t IntVal = getTok().getIntVal();
635 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000636 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000637 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000638 // Look for 'b' or 'f' following an Integer as a directional label
639 if (Lexer.getKind() == AsmToken::Identifier) {
640 StringRef IDVal = getTok().getString();
641 if (IDVal == "f" || IDVal == "b"){
642 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
643 IDVal == "f" ? 1 : 0);
644 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
645 getContext());
646 if(IDVal == "b" && Sym->isUndefined())
647 return Error(Loc, "invalid reference to undefined symbol");
648 EndLoc = Lexer.getLoc();
649 Lex(); // Eat identifier.
650 }
651 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000652 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000653 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000654 case AsmToken::Real: {
655 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000656 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000657 Res = MCConstantExpr::Create(IntVal, getContext());
658 Lex(); // Eat token.
659 return false;
660 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000661 case AsmToken::Dot: {
662 // This is a '.' reference, which references the current PC. Emit a
663 // temporary label to the streamer and refer to it.
664 MCSymbol *Sym = Ctx.CreateTempSymbol();
665 Out.EmitLabel(Sym);
666 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
667 EndLoc = Lexer.getLoc();
668 Lex(); // Eat identifier.
669 return false;
670 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000671 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000672 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000673 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000674 case AsmToken::LBrac:
675 if (!PlatformParser->HasBracketExpressions())
676 return TokError("brackets expression not supported on this target");
677 Lex(); // Eat the '['.
678 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000679 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000680 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000681 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000682 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000683 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000684 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000685 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000686 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000687 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000688 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000689 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000690 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000691 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000692 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000693 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000694 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000695 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000696 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000697 }
698}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000699
Chris Lattnerb4307b32010-01-15 19:28:38 +0000700bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000701 SMLoc EndLoc;
702 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000703}
704
Daniel Dunbarcceba832010-09-17 02:47:07 +0000705const MCExpr *
706AsmParser::ApplyModifierToExpr(const MCExpr *E,
707 MCSymbolRefExpr::VariantKind Variant) {
708 // Recurse over the given expression, rebuilding it to apply the given variant
709 // if there is exactly one symbol.
710 switch (E->getKind()) {
711 case MCExpr::Target:
712 case MCExpr::Constant:
713 return 0;
714
715 case MCExpr::SymbolRef: {
716 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
717
718 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
719 TokError("invalid variant on expression '" +
720 getTok().getIdentifier() + "' (already modified)");
721 return E;
722 }
723
724 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
725 }
726
727 case MCExpr::Unary: {
728 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
729 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
730 if (!Sub)
731 return 0;
732 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
733 }
734
735 case MCExpr::Binary: {
736 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
737 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
738 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
739
740 if (!LHS && !RHS)
741 return 0;
742
743 if (!LHS) LHS = BE->getLHS();
744 if (!RHS) RHS = BE->getRHS();
745
746 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
747 }
748 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000749
750 assert(0 && "Invalid expression kind!");
751 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000752}
753
Chris Lattner74ec1a32009-06-22 06:32:03 +0000754/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000755///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000756/// expr ::= expr &&,|| expr -> lowest.
757/// expr ::= expr |,^,&,! expr
758/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
759/// expr ::= expr <<,>> expr
760/// expr ::= expr +,- expr
761/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000762/// expr ::= primaryexpr
763///
Chris Lattner54482b42010-01-15 19:39:23 +0000764bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000765 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000766 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000767 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
768 return true;
769
Daniel Dunbarcceba832010-09-17 02:47:07 +0000770 // As a special case, we support 'a op b @ modifier' by rewriting the
771 // expression to include the modifier. This is inefficient, but in general we
772 // expect users to use 'a@modifier op b'.
773 if (Lexer.getKind() == AsmToken::At) {
774 Lex();
775
776 if (Lexer.isNot(AsmToken::Identifier))
777 return TokError("unexpected symbol modifier following '@'");
778
779 MCSymbolRefExpr::VariantKind Variant =
780 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
781 if (Variant == MCSymbolRefExpr::VK_Invalid)
782 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
783
784 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
785 if (!ModifiedRes) {
786 return TokError("invalid modifier '" + getTok().getIdentifier() +
787 "' (no symbols present)");
788 return true;
789 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000790
Daniel Dunbarcceba832010-09-17 02:47:07 +0000791 Res = ModifiedRes;
792 Lex();
793 }
794
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000795 // Try to constant fold it up front, if possible.
796 int64_t Value;
797 if (Res->EvaluateAsAbsolute(Value))
798 Res = MCConstantExpr::Create(Value, getContext());
799
800 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000801}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000802
Chris Lattnerb4307b32010-01-15 19:28:38 +0000803bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000804 Res = 0;
805 return ParseParenExpr(Res, EndLoc) ||
806 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000807}
808
Daniel Dunbar475839e2009-06-29 20:37:27 +0000809bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000810 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000811
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000812 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000813 if (ParseExpression(Expr))
814 return true;
815
Daniel Dunbare00b0112009-10-16 01:57:52 +0000816 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000817 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000818
819 return false;
820}
821
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000822static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000823 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000824 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000825 default:
826 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000827
Jim Grosbachfbe16812011-08-20 16:24:13 +0000828 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000829 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000830 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000831 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000832 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000833 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000834 return 1;
835
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000836
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000837 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000838 //
839 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000840 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000841 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000842 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000843 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000844 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000845 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000846 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000847 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000848 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000849
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000850 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000851 case AsmToken::EqualEqual:
852 Kind = MCBinaryExpr::EQ;
853 return 3;
854 case AsmToken::ExclaimEqual:
855 case AsmToken::LessGreater:
856 Kind = MCBinaryExpr::NE;
857 return 3;
858 case AsmToken::Less:
859 Kind = MCBinaryExpr::LT;
860 return 3;
861 case AsmToken::LessEqual:
862 Kind = MCBinaryExpr::LTE;
863 return 3;
864 case AsmToken::Greater:
865 Kind = MCBinaryExpr::GT;
866 return 3;
867 case AsmToken::GreaterEqual:
868 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000869 return 3;
870
Jim Grosbachfbe16812011-08-20 16:24:13 +0000871 // Intermediate Precedence: <<, >>
872 case AsmToken::LessLess:
873 Kind = MCBinaryExpr::Shl;
874 return 4;
875 case AsmToken::GreaterGreater:
876 Kind = MCBinaryExpr::Shr;
877 return 4;
878
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000879 // High Intermediate Precedence: +, -
880 case AsmToken::Plus:
881 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000882 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000883 case AsmToken::Minus:
884 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000885 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000886
Jim Grosbachfbe16812011-08-20 16:24:13 +0000887 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000888 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000889 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000890 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000891 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000892 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000893 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000894 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000895 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000896 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000897 }
898}
899
900
901/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
902/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000903bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
904 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000905 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000906 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000907 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000908
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000909 // If the next token is lower precedence than we are allowed to eat, return
910 // successfully with what we ate already.
911 if (TokPrec < Precedence)
912 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000913
Sean Callanan79ed1a82010-01-19 20:22:31 +0000914 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000915
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000916 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000917 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000918 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000919
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000920 // If BinOp binds less tightly with RHS than the operator after RHS, let
921 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000922 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000923 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000924 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000925 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000926 }
927
Daniel Dunbar475839e2009-06-29 20:37:27 +0000928 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000929 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000930 }
931}
932
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000933
934
935
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000936/// ParseStatement:
937/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000938/// ::= Label* Directive ...Operands... EndOfStatement
939/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000940bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000941 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000942 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000943 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000944 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000945 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000946
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000947 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000948 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000949 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000950 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000951 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000952 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000953 if (Lexer.is(AsmToken::Hash))
954 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000955
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000956 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000957 if (Lexer.is(AsmToken::Integer)) {
958 LocalLabelVal = getTok().getIntVal();
959 if (LocalLabelVal < 0) {
960 if (!TheCondState.Ignore)
961 return TokError("unexpected token at start of statement");
962 IDVal = "";
963 }
964 else {
965 IDVal = getTok().getString();
966 Lex(); // Consume the integer token to be used as an identifier token.
967 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000968 if (!TheCondState.Ignore)
969 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000970 }
971 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000972
973 } else if (Lexer.is(AsmToken::Dot)) {
974 // Treat '.' as a valid identifier in this context.
975 Lex();
976 IDVal = ".";
977
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000978 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000979 if (!TheCondState.Ignore)
980 return TokError("unexpected token at start of statement");
981 IDVal = "";
982 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000983
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000984
Chris Lattner7834fac2010-04-17 18:14:27 +0000985 // Handle conditional assembly here before checking for skipping. We
986 // have to do this so that .endif isn't skipped in a ".if 0" block for
987 // example.
988 if (IDVal == ".if")
989 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000990 if (IDVal == ".ifdef")
991 return ParseDirectiveIfdef(IDLoc, true);
992 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
993 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000994 if (IDVal == ".elseif")
995 return ParseDirectiveElseIf(IDLoc);
996 if (IDVal == ".else")
997 return ParseDirectiveElse(IDLoc);
998 if (IDVal == ".endif")
999 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001000
Chris Lattner7834fac2010-04-17 18:14:27 +00001001 // If we are in a ".if 0" block, ignore this statement.
1002 if (TheCondState.Ignore) {
1003 EatToEndOfStatement();
1004 return false;
1005 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001006
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001007 // FIXME: Recurse on local labels?
1008
1009 // See what kind of statement we have.
1010 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001011 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001012 CheckForValidSection();
1013
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001014 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001015 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001016
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001017 // Diagnose attempt to use '.' as a label.
1018 if (IDVal == ".")
1019 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1020
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001021 // Diagnose attempt to use a variable as a label.
1022 //
1023 // FIXME: Diagnostics. Note the location of the definition as a label.
1024 // FIXME: This doesn't diagnose assignment to a symbol which has been
1025 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001026 MCSymbol *Sym;
1027 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001028 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001029 else
1030 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001031 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001032 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001033
Daniel Dunbar959fd882009-08-26 22:13:22 +00001034 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001035 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001036
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001037 // Consume any end of statement token, if present, to avoid spurious
1038 // AddBlankLine calls().
1039 if (Lexer.is(AsmToken::EndOfStatement)) {
1040 Lex();
1041 if (Lexer.is(AsmToken::Eof))
1042 return false;
1043 }
1044
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001045 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001046 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001047
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001049 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001050 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001051
Nico Weber4c4c7322011-01-28 03:04:41 +00001052 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001053
1054 default: // Normal instruction or directive.
1055 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001056 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001057
1058 // If macros are enabled, check to see if this is a macro instantiation.
1059 if (MacrosEnabled)
1060 if (const Macro *M = MacroMap.lookup(IDVal))
1061 return HandleMacroEntry(IDVal, IDLoc, M);
1062
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001063 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001064 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001065 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001066 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001067 return ParseDirectiveSet(IDVal, true);
1068 if (IDVal == ".equiv")
1069 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001070
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 // Data directives
1072
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001073 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001074 return ParseDirectiveAscii(IDVal, false);
1075 if (IDVal == ".asciz" || IDVal == ".string")
1076 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001078 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001080 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001082 if (IDVal == ".value")
1083 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001084 if (IDVal == ".2byte")
1085 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001086 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001088 if (IDVal == ".int")
1089 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001090 if (IDVal == ".4byte")
1091 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001092 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001094 if (IDVal == ".8byte")
1095 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001096 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001097 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1098 if (IDVal == ".double")
1099 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001100
Eli Friedman5d68ec22010-07-19 04:17:25 +00001101 if (IDVal == ".align") {
1102 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1103 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1104 }
1105 if (IDVal == ".align32") {
1106 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1107 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1108 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001110 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001112 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001113 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001114 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001115 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001116 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001117 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001118 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001119 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001120 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1121
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001123 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001124
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001125 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001126 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001127 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001128 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001129 if (IDVal == ".zero")
1130 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001132 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001133
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001134 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001135 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001136 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001137 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001138 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001139 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001140 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001141 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001142 if (IDVal == ".symbol_resolver")
1143 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001144 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001145 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001146 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001147 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001148 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001149 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001150 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001151 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001152 if (IDVal == ".weak_def_can_be_hidden")
1153 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001154
Hans Wennborg5cc64912011-06-18 13:51:54 +00001155 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001156 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001157 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001158 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001159
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001160 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001161 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001162 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001163 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001164
Evan Chengbd27f5a2011-07-27 00:38:12 +00001165 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001166 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001167
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001168 // Look up the handler in the handler table.
1169 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1170 DirectiveMap.lookup(IDVal);
1171 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001172 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001173
Kevin Enderby9c656452009-09-10 20:51:44 +00001174 // Target hook for parsing target specific directives.
1175 if (!getTargetParser().ParseDirective(ID))
1176 return false;
1177
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001178 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001179 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001180 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001181 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001182
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001183 CheckForValidSection();
1184
Chris Lattnera7f13542010-05-19 23:34:33 +00001185 // Canonicalize the opcode to lower case.
1186 SmallString<128> Opcode;
1187 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1188 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001189
Chris Lattner98986712010-01-14 22:21:20 +00001190 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001191 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001192 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001193
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001194 // Dump the parsed representation, if requested.
1195 if (getShowParsedOperands()) {
1196 SmallString<256> Str;
1197 raw_svector_ostream OS(Str);
1198 OS << "parsed instruction: [";
1199 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1200 if (i != 0)
1201 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001202 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001203 }
1204 OS << "]";
1205
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001206 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001207 }
1208
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001209 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001210 if (!HadError)
1211 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1212 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001213
Chris Lattner98986712010-01-14 22:21:20 +00001214 // Free any parsed operands.
1215 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1216 delete ParsedOperands[i];
1217
Chris Lattnercbf8a982010-09-11 16:18:25 +00001218 // Don't skip the rest of the line, the instruction parser is responsible for
1219 // that.
1220 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001221}
Chris Lattner9a023f72009-06-24 04:43:34 +00001222
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001223/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1224/// since they may not be able to be tokenized to get to the end of line token.
1225void AsmParser::EatToEndOfLine() {
1226 Lexer.LexUntilEndOfLine();
1227 // Eat EOL.
1228 Lex();
1229}
1230
1231/// ParseCppHashLineFilenameComment as this:
1232/// ::= # number "filename"
1233/// or just as a full line comment if it doesn't have a number and a string.
1234bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1235 Lex(); // Eat the hash token.
1236
1237 if (getLexer().isNot(AsmToken::Integer)) {
1238 // Consume the line since in cases it is not a well-formed line directive,
1239 // as if were simply a full line comment.
1240 EatToEndOfLine();
1241 return false;
1242 }
1243
1244 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001245 Lex();
1246
1247 if (getLexer().isNot(AsmToken::String)) {
1248 EatToEndOfLine();
1249 return false;
1250 }
1251
1252 StringRef Filename = getTok().getString();
1253 // Get rid of the enclosing quotes.
1254 Filename = Filename.substr(1, Filename.size()-2);
1255
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001256 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1257 CppHashLoc = L;
1258 CppHashFilename = Filename;
1259 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001260
1261 // Ignore any trailing characters, they're just comment.
1262 EatToEndOfLine();
1263 return false;
1264}
1265
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001266/// DiagHandler - will use the the last parsed cpp hash line filename comment
1267/// for the Filename and LineNo if any in the diagnostic.
1268void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1269 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1270 raw_ostream &OS = errs();
1271
1272 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1273 const SMLoc &DiagLoc = Diag.getLoc();
1274 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1275 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1276
1277 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1278 // before printing the message.
1279 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1280 if (DiagCurBuffer > 0) {
1281 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1282 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1283 }
1284
1285 // If we have not parsed a cpp hash line filename comment or the source
1286 // manager changed or buffer changed (like in a nested include) then just
1287 // print the normal diagnostic using its Filename and LineNo.
1288 if (!Parser->CppHashLineNumber ||
1289 &DiagSrcMgr != &Parser->SrcMgr ||
1290 DiagBuf != CppHashBuf) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001291 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001292 return;
1293 }
1294
1295 // Use the CppHashFilename and calculate a line number based on the
1296 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1297 // the diagnostic.
1298 const std::string Filename = Parser->CppHashFilename;
1299
1300 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1301 int CppHashLocLineNo =
1302 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1303 int LineNo = Parser->CppHashLineNumber - 1 +
1304 (DiagLocLineNo - CppHashLocLineNo);
1305
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001306 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1307 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001308 Diag.getKind(), Diag.getMessage(),
1309 Diag.getLineContents(),
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001310 Diag.getRanges(), Diag.getShowLine());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001311
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001312 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001313}
1314
Rafael Espindola65366442011-06-05 02:43:45 +00001315bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1316 const std::vector<StringRef> &Parameters,
1317 const std::vector<std::vector<AsmToken> > &A,
1318 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001319 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001320 unsigned NParameters = Parameters.size();
1321 if (NParameters != 0 && NParameters != A.size())
1322 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001323
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001324 while (!Body.empty()) {
1325 // Scan for the next substitution.
1326 std::size_t End = Body.size(), Pos = 0;
1327 for (; Pos != End; ++Pos) {
1328 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001329 if (!NParameters) {
1330 // This macro has no parameters, look for $0, $1, etc.
1331 if (Body[Pos] != '$' || Pos + 1 == End)
1332 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001333
Rafael Espindola65366442011-06-05 02:43:45 +00001334 char Next = Body[Pos + 1];
1335 if (Next == '$' || Next == 'n' || isdigit(Next))
1336 break;
1337 } else {
1338 // This macro has parameters, look for \foo, \bar, etc.
1339 if (Body[Pos] == '\\' && Pos + 1 != End)
1340 break;
1341 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001342 }
1343
1344 // Add the prefix.
1345 OS << Body.slice(0, Pos);
1346
1347 // Check if we reached the end.
1348 if (Pos == End)
1349 break;
1350
Rafael Espindola65366442011-06-05 02:43:45 +00001351 if (!NParameters) {
1352 switch (Body[Pos+1]) {
1353 // $$ => $
1354 case '$':
1355 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001356 break;
1357
Rafael Espindola65366442011-06-05 02:43:45 +00001358 // $n => number of arguments
1359 case 'n':
1360 OS << A.size();
1361 break;
1362
1363 // $[0-9] => argument
1364 default: {
1365 // Missing arguments are ignored.
1366 unsigned Index = Body[Pos+1] - '0';
1367 if (Index >= A.size())
1368 break;
1369
1370 // Otherwise substitute with the token values, with spaces eliminated.
1371 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1372 ie = A[Index].end(); it != ie; ++it)
1373 OS << it->getString();
1374 break;
1375 }
1376 }
1377 Pos += 2;
1378 } else {
1379 unsigned I = Pos + 1;
1380 while (isalnum(Body[I]) && I + 1 != End)
1381 ++I;
1382
1383 const char *Begin = Body.data() + Pos +1;
1384 StringRef Argument(Begin, I - (Pos +1));
1385 unsigned Index = 0;
1386 for (; Index < NParameters; ++Index)
1387 if (Parameters[Index] == Argument)
1388 break;
1389
1390 // FIXME: We should error at the macro definition.
1391 if (Index == NParameters)
1392 return Error(L, "Parameter not found");
1393
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001394 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1395 ie = A[Index].end(); it != ie; ++it)
1396 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001397
Rafael Espindola65366442011-06-05 02:43:45 +00001398 Pos += 1 + Argument.size();
1399 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001400 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001401 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001402 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001403
1404 // We include the .endmacro in the buffer as our queue to exit the macro
1405 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001406 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001407 return false;
1408}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001409
Rafael Espindola65366442011-06-05 02:43:45 +00001410MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1411 MemoryBuffer *I)
1412 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1413{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001414}
1415
1416bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1417 const Macro *M) {
1418 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1419 // this, although we should protect against infinite loops.
1420 if (ActiveMacros.size() == 20)
1421 return TokError("macros cannot be nested more than 20 levels deep");
1422
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001423 // Parse the macro instantiation arguments.
1424 std::vector<std::vector<AsmToken> > MacroArguments;
1425 MacroArguments.push_back(std::vector<AsmToken>());
1426 unsigned ParenLevel = 0;
1427 for (;;) {
1428 if (Lexer.is(AsmToken::Eof))
1429 return TokError("unexpected token in macro instantiation");
1430 if (Lexer.is(AsmToken::EndOfStatement))
1431 break;
1432
1433 // If we aren't inside parentheses and this is a comma, start a new token
1434 // list.
1435 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1436 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001437 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001438 // Adjust the current parentheses level.
1439 if (Lexer.is(AsmToken::LParen))
1440 ++ParenLevel;
1441 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1442 --ParenLevel;
1443
1444 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001445 MacroArguments.back().push_back(getTok());
1446 }
1447 Lex();
1448 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001449
Rafael Espindola65366442011-06-05 02:43:45 +00001450 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1451 // to hold the macro body with substitutions.
1452 SmallString<256> Buf;
1453 StringRef Body = M->Body;
1454
1455 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1456 return true;
1457
1458 MemoryBuffer *Instantiation =
1459 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1460
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001461 // Create the macro instantiation object and add to the current macro
1462 // instantiation stack.
1463 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001464 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001465 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001466 ActiveMacros.push_back(MI);
1467
1468 // Jump to the macro instantiation and prime the lexer.
1469 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1470 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1471 Lex();
1472
1473 return false;
1474}
1475
1476void AsmParser::HandleMacroExit() {
1477 // Jump to the EndOfStatement we should return to, and consume it.
1478 JumpToLoc(ActiveMacros.back()->ExitLoc);
1479 Lex();
1480
1481 // Pop the instantiation entry.
1482 delete ActiveMacros.back();
1483 ActiveMacros.pop_back();
1484}
1485
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001486static void MarkUsed(const MCExpr *Value) {
1487 switch (Value->getKind()) {
1488 case MCExpr::Binary:
1489 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1490 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1491 break;
1492 case MCExpr::Target:
1493 case MCExpr::Constant:
1494 break;
1495 case MCExpr::SymbolRef: {
1496 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1497 break;
1498 }
1499 case MCExpr::Unary:
1500 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1501 break;
1502 }
1503}
1504
Nico Weber4c4c7322011-01-28 03:04:41 +00001505bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001506 // FIXME: Use better location, we should use proper tokens.
1507 SMLoc EqualLoc = Lexer.getLoc();
1508
Daniel Dunbar821e3332009-08-31 08:09:28 +00001509 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001510 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001511 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001512
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001513 MarkUsed(Value);
1514
Daniel Dunbar3f872332009-07-28 16:08:33 +00001515 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001516 return TokError("unexpected token in assignment");
1517
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001518 // Error on assignment to '.'.
1519 if (Name == ".") {
1520 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1521 "(use '.space' or '.org').)"));
1522 }
1523
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001524 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001525 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001526
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001527 // Validate that the LHS is allowed to be a variable (either it has not been
1528 // used as a symbol, or it is an absolute symbol).
1529 MCSymbol *Sym = getContext().LookupSymbol(Name);
1530 if (Sym) {
1531 // Diagnose assignment to a label.
1532 //
1533 // FIXME: Diagnostics. Note the location of the definition as a label.
1534 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001535 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001536 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001537 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001538 return Error(EqualLoc, "redefinition of '" + Name + "'");
1539 else if (!Sym->isVariable())
1540 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001541 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001542 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1543 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001544
1545 // Don't count these checks as uses.
1546 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001547 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001548 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001549
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001550 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001551
1552 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001553 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001554
1555 return false;
1556}
1557
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001558/// ParseIdentifier:
1559/// ::= identifier
1560/// ::= string
1561bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001562 // The assembler has relaxed rules for accepting identifiers, in particular we
1563 // allow things like '.globl $foo', which would normally be separate
1564 // tokens. At this level, we have already lexed so we cannot (currently)
1565 // handle this as a context dependent token, instead we detect adjacent tokens
1566 // and return the combined identifier.
1567 if (Lexer.is(AsmToken::Dollar)) {
1568 SMLoc DollarLoc = getLexer().getLoc();
1569
1570 // Consume the dollar sign, and check for a following identifier.
1571 Lex();
1572 if (Lexer.isNot(AsmToken::Identifier))
1573 return true;
1574
1575 // We have a '$' followed by an identifier, make sure they are adjacent.
1576 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1577 return true;
1578
1579 // Construct the joined identifier and consume the token.
1580 Res = StringRef(DollarLoc.getPointer(),
1581 getTok().getIdentifier().size() + 1);
1582 Lex();
1583 return false;
1584 }
1585
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001586 if (Lexer.isNot(AsmToken::Identifier) &&
1587 Lexer.isNot(AsmToken::String))
1588 return true;
1589
Sean Callanan18b83232010-01-19 21:44:56 +00001590 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001591
Sean Callanan79ed1a82010-01-19 20:22:31 +00001592 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001593
1594 return false;
1595}
1596
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001597/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001598/// ::= .equ identifier ',' expression
1599/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001600/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001601bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001602 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001603
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001604 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001605 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001606
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001607 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001608 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001609 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001610
Nico Weber4c4c7322011-01-28 03:04:41 +00001611 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001612}
1613
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001614bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001615 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001616
1617 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001618 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001619 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1620 if (Str[i] != '\\') {
1621 Data += Str[i];
1622 continue;
1623 }
1624
1625 // Recognize escaped characters. Note that this escape semantics currently
1626 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1627 ++i;
1628 if (i == e)
1629 return TokError("unexpected backslash at end of string");
1630
1631 // Recognize octal sequences.
1632 if ((unsigned) (Str[i] - '0') <= 7) {
1633 // Consume up to three octal characters.
1634 unsigned Value = Str[i] - '0';
1635
1636 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1637 ++i;
1638 Value = Value * 8 + (Str[i] - '0');
1639
1640 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1641 ++i;
1642 Value = Value * 8 + (Str[i] - '0');
1643 }
1644 }
1645
1646 if (Value > 255)
1647 return TokError("invalid octal escape sequence (out of range)");
1648
1649 Data += (unsigned char) Value;
1650 continue;
1651 }
1652
1653 // Otherwise recognize individual escapes.
1654 switch (Str[i]) {
1655 default:
1656 // Just reject invalid escape sequences for now.
1657 return TokError("invalid escape sequence (unrecognized character)");
1658
1659 case 'b': Data += '\b'; break;
1660 case 'f': Data += '\f'; break;
1661 case 'n': Data += '\n'; break;
1662 case 'r': Data += '\r'; break;
1663 case 't': Data += '\t'; break;
1664 case '"': Data += '"'; break;
1665 case '\\': Data += '\\'; break;
1666 }
1667 }
1668
1669 return false;
1670}
1671
Daniel Dunbara0d14262009-06-24 23:30:00 +00001672/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001673/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1674bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001675 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001676 CheckForValidSection();
1677
Daniel Dunbara0d14262009-06-24 23:30:00 +00001678 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001680 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001681
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001682 std::string Data;
1683 if (ParseEscapedString(Data))
1684 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001685
1686 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001687 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001688 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1689
Sean Callanan79ed1a82010-01-19 20:22:31 +00001690 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001691
1692 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001693 break;
1694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001696 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001697 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001698 }
1699 }
1700
Sean Callanan79ed1a82010-01-19 20:22:31 +00001701 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001702 return false;
1703}
1704
1705/// ParseDirectiveValue
1706/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1707bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001708 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001709 CheckForValidSection();
1710
Daniel Dunbara0d14262009-06-24 23:30:00 +00001711 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001712 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001713 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001714 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001715 return true;
1716
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001717 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001718 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1719 assert(Size <= 8 && "Invalid size");
1720 uint64_t IntValue = MCE->getValue();
1721 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1722 return Error(ExprLoc, "literal value out of range for directive");
1723 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1724 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001725 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001726
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001727 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001728 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001729
Daniel Dunbara0d14262009-06-24 23:30:00 +00001730 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001731 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001732 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001733 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001734 }
1735 }
1736
Sean Callanan79ed1a82010-01-19 20:22:31 +00001737 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001738 return false;
1739}
1740
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001741/// ParseDirectiveRealValue
1742/// ::= (.single | .double) [ expression (, expression)* ]
1743bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1744 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1745 CheckForValidSection();
1746
1747 for (;;) {
1748 // We don't truly support arithmetic on floating point expressions, so we
1749 // have to manually parse unary prefixes.
1750 bool IsNeg = false;
1751 if (getLexer().is(AsmToken::Minus)) {
1752 Lex();
1753 IsNeg = true;
1754 } else if (getLexer().is(AsmToken::Plus))
1755 Lex();
1756
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001757 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001758 getLexer().isNot(AsmToken::Real) &&
1759 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001760 return TokError("unexpected token in directive");
1761
1762 // Convert to an APFloat.
1763 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001764 StringRef IDVal = getTok().getString();
1765 if (getLexer().is(AsmToken::Identifier)) {
1766 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1767 Value = APFloat::getInf(Semantics);
1768 else if (!IDVal.compare_lower("nan"))
1769 Value = APFloat::getNaN(Semantics, false, ~0);
1770 else
1771 return TokError("invalid floating point literal");
1772 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001773 APFloat::opInvalidOp)
1774 return TokError("invalid floating point literal");
1775 if (IsNeg)
1776 Value.changeSign();
1777
1778 // Consume the numeric token.
1779 Lex();
1780
1781 // Emit the value as an integer.
1782 APInt AsInt = Value.bitcastToAPInt();
1783 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1784 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1785
1786 if (getLexer().is(AsmToken::EndOfStatement))
1787 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001788
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001789 if (getLexer().isNot(AsmToken::Comma))
1790 return TokError("unexpected token in directive");
1791 Lex();
1792 }
1793 }
1794
1795 Lex();
1796 return false;
1797}
1798
Daniel Dunbara0d14262009-06-24 23:30:00 +00001799/// ParseDirectiveSpace
1800/// ::= .space expression [ , expression ]
1801bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001802 CheckForValidSection();
1803
Daniel Dunbara0d14262009-06-24 23:30:00 +00001804 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001805 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001806 return true;
1807
1808 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001809 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1810 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001811 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001812 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001813
Daniel Dunbar475839e2009-06-29 20:37:27 +00001814 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001815 return true;
1816
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001817 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001818 return TokError("unexpected token in '.space' directive");
1819 }
1820
Sean Callanan79ed1a82010-01-19 20:22:31 +00001821 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001822
1823 if (NumBytes <= 0)
1824 return TokError("invalid number of bytes in '.space' directive");
1825
1826 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001827 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001828
1829 return false;
1830}
1831
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001832/// ParseDirectiveZero
1833/// ::= .zero expression
1834bool AsmParser::ParseDirectiveZero() {
1835 CheckForValidSection();
1836
1837 int64_t NumBytes;
1838 if (ParseAbsoluteExpression(NumBytes))
1839 return true;
1840
Rafael Espindolae452b172010-10-05 19:42:57 +00001841 int64_t Val = 0;
1842 if (getLexer().is(AsmToken::Comma)) {
1843 Lex();
1844 if (ParseAbsoluteExpression(Val))
1845 return true;
1846 }
1847
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001848 if (getLexer().isNot(AsmToken::EndOfStatement))
1849 return TokError("unexpected token in '.zero' directive");
1850
1851 Lex();
1852
Rafael Espindolae452b172010-10-05 19:42:57 +00001853 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001854
1855 return false;
1856}
1857
Daniel Dunbara0d14262009-06-24 23:30:00 +00001858/// ParseDirectiveFill
1859/// ::= .fill expression , expression , expression
1860bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001861 CheckForValidSection();
1862
Daniel Dunbara0d14262009-06-24 23:30:00 +00001863 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001864 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001865 return true;
1866
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001867 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001868 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001869 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001870
Daniel Dunbara0d14262009-06-24 23:30:00 +00001871 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001872 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001873 return true;
1874
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001875 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001876 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001877 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001878
Daniel Dunbara0d14262009-06-24 23:30:00 +00001879 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001880 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001881 return true;
1882
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001883 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001884 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001885
Sean Callanan79ed1a82010-01-19 20:22:31 +00001886 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001887
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001888 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1889 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001890
1891 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001892 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001893
1894 return false;
1895}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001896
1897/// ParseDirectiveOrg
1898/// ::= .org expression [ , expression ]
1899bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001900 CheckForValidSection();
1901
Daniel Dunbar821e3332009-08-31 08:09:28 +00001902 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001903 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001904 return true;
1905
1906 // Parse optional fill expression.
1907 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001908 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1909 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001910 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001911 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001912
Daniel Dunbar475839e2009-06-29 20:37:27 +00001913 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001914 return true;
1915
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001916 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001917 return TokError("unexpected token in '.org' directive");
1918 }
1919
Sean Callanan79ed1a82010-01-19 20:22:31 +00001920 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001921
1922 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1923 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001924 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001925
1926 return false;
1927}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001928
1929/// ParseDirectiveAlign
1930/// ::= {.align, ...} expression [ , expression [ , expression ]]
1931bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001932 CheckForValidSection();
1933
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001934 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001935 int64_t Alignment;
1936 if (ParseAbsoluteExpression(Alignment))
1937 return true;
1938
1939 SMLoc MaxBytesLoc;
1940 bool HasFillExpr = false;
1941 int64_t FillExpr = 0;
1942 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001943 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1944 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001945 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001946 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001947
1948 // The fill expression can be omitted while specifying a maximum number of
1949 // alignment bytes, e.g:
1950 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001951 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001952 HasFillExpr = true;
1953 if (ParseAbsoluteExpression(FillExpr))
1954 return true;
1955 }
1956
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001957 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1958 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001959 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001960 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001961
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001962 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001963 if (ParseAbsoluteExpression(MaxBytesToFill))
1964 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001965
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001966 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001967 return TokError("unexpected token in directive");
1968 }
1969 }
1970
Sean Callanan79ed1a82010-01-19 20:22:31 +00001971 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001972
Daniel Dunbar648ac512010-05-17 21:54:30 +00001973 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001974 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001975
1976 // Compute alignment in bytes.
1977 if (IsPow2) {
1978 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001979 if (Alignment >= 32) {
1980 Error(AlignmentLoc, "invalid alignment value");
1981 Alignment = 31;
1982 }
1983
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001984 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001985 }
1986
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001987 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001988 if (MaxBytesLoc.isValid()) {
1989 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001990 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1991 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001992 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001993 }
1994
1995 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001996 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1997 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001998 MaxBytesToFill = 0;
1999 }
2000 }
2001
Daniel Dunbar648ac512010-05-17 21:54:30 +00002002 // Check whether we should use optimal code alignment for this .align
2003 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002004 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002005 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2006 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002007 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002008 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002009 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002010 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2011 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002012 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002013
2014 return false;
2015}
2016
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002017/// ParseDirectiveSymbolAttribute
2018/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002019bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002020 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002021 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002022 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002023 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002024
2025 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002026 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002027
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002028 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002029
Jim Grosbach10ec6502011-09-15 17:56:49 +00002030 // Assembler local symbols don't make any sense here. Complain loudly.
2031 if (Sym->isTemporary())
2032 return Error(Loc, "non-local symbol required in directive");
2033
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002034 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002035
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002036 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002037 break;
2038
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002039 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002040 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002041 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002042 }
2043 }
2044
Sean Callanan79ed1a82010-01-19 20:22:31 +00002045 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002046 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002047}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002048
2049/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002050/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2051bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002052 CheckForValidSection();
2053
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002054 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002055 StringRef Name;
2056 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002057 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002058
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002059 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002060 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002061
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002062 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002063 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002064 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002065
2066 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002067 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002068 if (ParseAbsoluteExpression(Size))
2069 return true;
2070
2071 int64_t Pow2Alignment = 0;
2072 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002073 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002074 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002075 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002076 if (ParseAbsoluteExpression(Pow2Alignment))
2077 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002078
Chris Lattner258281d2010-01-19 06:22:22 +00002079 // If this target takes alignments in bytes (not log) validate and convert.
2080 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2081 if (!isPowerOf2_64(Pow2Alignment))
2082 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2083 Pow2Alignment = Log2_64(Pow2Alignment);
2084 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002085 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002086
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002087 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002088 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002089
Sean Callanan79ed1a82010-01-19 20:22:31 +00002090 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002091
Chris Lattner1fc3d752009-07-09 17:25:12 +00002092 // NOTE: a size of zero for a .comm should create a undefined symbol
2093 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002094 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002095 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2096 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002097
Eric Christopherc260a3e2010-05-14 01:38:54 +00002098 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002099 // may internally end up wanting an alignment in bytes.
2100 // FIXME: Diagnose overflow.
2101 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002102 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2103 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002104
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002105 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002106 return Error(IDLoc, "invalid symbol redefinition");
2107
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002108 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002109 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002110 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002111 getStreamer().EmitZerofill(Ctx.getMachOSection(
2112 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2113 0, SectionKind::getBSS()),
2114 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002115 return false;
2116 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002117
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002118 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002119 return false;
2120}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002121
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002122/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002123/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002124bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002125 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002126 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002127
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002128 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002129 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002130 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002131
Sean Callanan79ed1a82010-01-19 20:22:31 +00002132 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002133
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002134 if (Str.empty())
2135 Error(Loc, ".abort detected. Assembly stopping.");
2136 else
2137 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002138 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002139
2140 return false;
2141}
Kevin Enderby71148242009-07-14 21:35:03 +00002142
Kevin Enderby1f049b22009-07-14 23:21:55 +00002143/// ParseDirectiveInclude
2144/// ::= .include "filename"
2145bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002146 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002147 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002148
Sean Callanan18b83232010-01-19 21:44:56 +00002149 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002150 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002151 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002152
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002153 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002154 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002155
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002156 // Strip the quotes.
2157 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002158
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002159 // Attempt to switch the lexer to the included file before consuming the end
2160 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002161 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002162 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002163 return true;
2164 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002165
2166 return false;
2167}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002168
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002169/// ParseDirectiveIf
2170/// ::= .if expression
2171bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002172 TheCondStack.push_back(TheCondState);
2173 TheCondState.TheCond = AsmCond::IfCond;
2174 if(TheCondState.Ignore) {
2175 EatToEndOfStatement();
2176 }
2177 else {
2178 int64_t ExprValue;
2179 if (ParseAbsoluteExpression(ExprValue))
2180 return true;
2181
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002182 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002183 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002184
Sean Callanan79ed1a82010-01-19 20:22:31 +00002185 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002186
2187 TheCondState.CondMet = ExprValue;
2188 TheCondState.Ignore = !TheCondState.CondMet;
2189 }
2190
2191 return false;
2192}
2193
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002194bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2195 StringRef Name;
2196 TheCondStack.push_back(TheCondState);
2197 TheCondState.TheCond = AsmCond::IfCond;
2198
2199 if (TheCondState.Ignore) {
2200 EatToEndOfStatement();
2201 } else {
2202 if (ParseIdentifier(Name))
2203 return TokError("expected identifier after '.ifdef'");
2204
2205 Lex();
2206
2207 MCSymbol *Sym = getContext().LookupSymbol(Name);
2208
2209 if (expect_defined)
2210 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2211 else
2212 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2213 TheCondState.Ignore = !TheCondState.CondMet;
2214 }
2215
2216 return false;
2217}
2218
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002219/// ParseDirectiveElseIf
2220/// ::= .elseif expression
2221bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2222 if (TheCondState.TheCond != AsmCond::IfCond &&
2223 TheCondState.TheCond != AsmCond::ElseIfCond)
2224 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2225 " an .elseif");
2226 TheCondState.TheCond = AsmCond::ElseIfCond;
2227
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002228 bool LastIgnoreState = false;
2229 if (!TheCondStack.empty())
2230 LastIgnoreState = TheCondStack.back().Ignore;
2231 if (LastIgnoreState || TheCondState.CondMet) {
2232 TheCondState.Ignore = true;
2233 EatToEndOfStatement();
2234 }
2235 else {
2236 int64_t ExprValue;
2237 if (ParseAbsoluteExpression(ExprValue))
2238 return true;
2239
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002240 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002241 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002242
Sean Callanan79ed1a82010-01-19 20:22:31 +00002243 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002244 TheCondState.CondMet = ExprValue;
2245 TheCondState.Ignore = !TheCondState.CondMet;
2246 }
2247
2248 return false;
2249}
2250
2251/// ParseDirectiveElse
2252/// ::= .else
2253bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002254 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002255 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002256
Sean Callanan79ed1a82010-01-19 20:22:31 +00002257 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002258
2259 if (TheCondState.TheCond != AsmCond::IfCond &&
2260 TheCondState.TheCond != AsmCond::ElseIfCond)
2261 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2262 ".elseif");
2263 TheCondState.TheCond = AsmCond::ElseCond;
2264 bool LastIgnoreState = false;
2265 if (!TheCondStack.empty())
2266 LastIgnoreState = TheCondStack.back().Ignore;
2267 if (LastIgnoreState || TheCondState.CondMet)
2268 TheCondState.Ignore = true;
2269 else
2270 TheCondState.Ignore = false;
2271
2272 return false;
2273}
2274
2275/// ParseDirectiveEndIf
2276/// ::= .endif
2277bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002278 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002279 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002280
Sean Callanan79ed1a82010-01-19 20:22:31 +00002281 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002282
2283 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2284 TheCondStack.empty())
2285 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2286 ".else");
2287 if (!TheCondStack.empty()) {
2288 TheCondState = TheCondStack.back();
2289 TheCondStack.pop_back();
2290 }
2291
2292 return false;
2293}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002294
2295/// ParseDirectiveFile
2296/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002297bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002298 // FIXME: I'm not sure what this is.
2299 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002300 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002301 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002302 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002303 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002304
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002305 if (FileNumber < 1)
2306 return TokError("file number less than one");
2307 }
2308
Daniel Dunbareceec052010-07-12 17:45:27 +00002309 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002310 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002311
Chris Lattnerd32e8032010-01-25 19:02:58 +00002312 StringRef Filename = getTok().getString();
2313 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002314 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002315
Daniel Dunbareceec052010-07-12 17:45:27 +00002316 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002317 return TokError("unexpected token in '.file' directive");
2318
Chris Lattnerd32e8032010-01-25 19:02:58 +00002319 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002320 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002321 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002322 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002323 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002324 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002325
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002326 return false;
2327}
2328
2329/// ParseDirectiveLine
2330/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002331bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002332 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2333 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002334 return TokError("unexpected token in '.line' directive");
2335
Sean Callanan18b83232010-01-19 21:44:56 +00002336 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002337 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002338 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002339
2340 // FIXME: Do something with the .line.
2341 }
2342
Daniel Dunbareceec052010-07-12 17:45:27 +00002343 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002344 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002345
2346 return false;
2347}
2348
2349
2350/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002351/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002352/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2353/// The first number is a file number, must have been previously assigned with
2354/// a .file directive, the second number is the line number and optionally the
2355/// third number is a column position (zero if not specified). The remaining
2356/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002357bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002358
Daniel Dunbareceec052010-07-12 17:45:27 +00002359 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002360 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002361 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002362 if (FileNumber < 1)
2363 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002364 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002365 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002366 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002367
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002368 int64_t LineNumber = 0;
2369 if (getLexer().is(AsmToken::Integer)) {
2370 LineNumber = getTok().getIntVal();
2371 if (LineNumber < 1)
2372 return TokError("line number less than one in '.loc' directive");
2373 Lex();
2374 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002375
2376 int64_t ColumnPos = 0;
2377 if (getLexer().is(AsmToken::Integer)) {
2378 ColumnPos = getTok().getIntVal();
2379 if (ColumnPos < 0)
2380 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002381 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002382 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002383
Kevin Enderbyc0957932010-09-30 16:52:03 +00002384 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002385 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002386 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002387 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2388 for (;;) {
2389 if (getLexer().is(AsmToken::EndOfStatement))
2390 break;
2391
2392 StringRef Name;
2393 SMLoc Loc = getTok().getLoc();
2394 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002395 return TokError("unexpected token in '.loc' directive");
2396
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002397 if (Name == "basic_block")
2398 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2399 else if (Name == "prologue_end")
2400 Flags |= DWARF2_FLAG_PROLOGUE_END;
2401 else if (Name == "epilogue_begin")
2402 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2403 else if (Name == "is_stmt") {
2404 SMLoc Loc = getTok().getLoc();
2405 const MCExpr *Value;
2406 if (getParser().ParseExpression(Value))
2407 return true;
2408 // The expression must be the constant 0 or 1.
2409 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2410 int Value = MCE->getValue();
2411 if (Value == 0)
2412 Flags &= ~DWARF2_FLAG_IS_STMT;
2413 else if (Value == 1)
2414 Flags |= DWARF2_FLAG_IS_STMT;
2415 else
2416 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002417 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002418 else {
2419 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2420 }
2421 }
2422 else if (Name == "isa") {
2423 SMLoc Loc = getTok().getLoc();
2424 const MCExpr *Value;
2425 if (getParser().ParseExpression(Value))
2426 return true;
2427 // The expression must be a constant greater or equal to 0.
2428 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2429 int Value = MCE->getValue();
2430 if (Value < 0)
2431 return Error(Loc, "isa number less than zero");
2432 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002433 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002434 else {
2435 return Error(Loc, "isa number not a constant value");
2436 }
2437 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002438 else if (Name == "discriminator") {
2439 if (getParser().ParseAbsoluteExpression(Discriminator))
2440 return true;
2441 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002442 else {
2443 return Error(Loc, "unknown sub-directive in '.loc' directive");
2444 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002445
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002446 if (getLexer().is(AsmToken::EndOfStatement))
2447 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002448 }
2449 }
2450
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002451 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002452 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002453
2454 return false;
2455}
2456
Daniel Dunbar138abae2010-10-16 04:56:42 +00002457/// ParseDirectiveStabs
2458/// ::= .stabs string, number, number, number
2459bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2460 SMLoc DirectiveLoc) {
2461 return TokError("unsupported directive '" + Directive + "'");
2462}
2463
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002464/// ParseDirectiveCFISections
2465/// ::= .cfi_sections section [, section]
2466bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2467 SMLoc DirectiveLoc) {
2468 StringRef Name;
2469 bool EH = false;
2470 bool Debug = false;
2471
2472 if (getParser().ParseIdentifier(Name))
2473 return TokError("Expected an identifier");
2474
2475 if (Name == ".eh_frame")
2476 EH = true;
2477 else if (Name == ".debug_frame")
2478 Debug = true;
2479
2480 if (getLexer().is(AsmToken::Comma)) {
2481 Lex();
2482
2483 if (getParser().ParseIdentifier(Name))
2484 return TokError("Expected an identifier");
2485
2486 if (Name == ".eh_frame")
2487 EH = true;
2488 else if (Name == ".debug_frame")
2489 Debug = true;
2490 }
2491
2492 getStreamer().EmitCFISections(EH, Debug);
2493
2494 return false;
2495}
2496
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002497/// ParseDirectiveCFIStartProc
2498/// ::= .cfi_startproc
2499bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2500 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002501 getStreamer().EmitCFIStartProc();
2502 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002503}
2504
2505/// ParseDirectiveCFIEndProc
2506/// ::= .cfi_endproc
2507bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002508 getStreamer().EmitCFIEndProc();
2509 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002510}
2511
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002512/// ParseRegisterOrRegisterNumber - parse register name or number.
2513bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2514 SMLoc DirectiveLoc) {
2515 unsigned RegNo;
2516
Jim Grosbach6f888a82011-06-02 17:14:04 +00002517 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002518 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2519 DirectiveLoc))
2520 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002521 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002522 } else
2523 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002524
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002525 return false;
2526}
2527
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002528/// ParseDirectiveCFIDefCfa
2529/// ::= .cfi_def_cfa register, offset
2530bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2531 SMLoc DirectiveLoc) {
2532 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002533 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002534 return true;
2535
2536 if (getLexer().isNot(AsmToken::Comma))
2537 return TokError("unexpected token in directive");
2538 Lex();
2539
2540 int64_t Offset = 0;
2541 if (getParser().ParseAbsoluteExpression(Offset))
2542 return true;
2543
Rafael Espindola066c2f42011-04-12 23:59:07 +00002544 getStreamer().EmitCFIDefCfa(Register, Offset);
2545 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002546}
2547
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002548/// ParseDirectiveCFIDefCfaOffset
2549/// ::= .cfi_def_cfa_offset offset
2550bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2551 SMLoc DirectiveLoc) {
2552 int64_t Offset = 0;
2553 if (getParser().ParseAbsoluteExpression(Offset))
2554 return true;
2555
Rafael Espindola066c2f42011-04-12 23:59:07 +00002556 getStreamer().EmitCFIDefCfaOffset(Offset);
2557 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002558}
2559
2560/// ParseDirectiveCFIAdjustCfaOffset
2561/// ::= .cfi_adjust_cfa_offset adjustment
2562bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2563 SMLoc DirectiveLoc) {
2564 int64_t Adjustment = 0;
2565 if (getParser().ParseAbsoluteExpression(Adjustment))
2566 return true;
2567
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002568 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2569 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002570}
2571
2572/// ParseDirectiveCFIDefCfaRegister
2573/// ::= .cfi_def_cfa_register register
2574bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2575 SMLoc DirectiveLoc) {
2576 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002577 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002578 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002579
Rafael Espindola066c2f42011-04-12 23:59:07 +00002580 getStreamer().EmitCFIDefCfaRegister(Register);
2581 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002582}
2583
2584/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002585/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002586bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2587 int64_t Register = 0;
2588 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002589
2590 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002591 return true;
2592
2593 if (getLexer().isNot(AsmToken::Comma))
2594 return TokError("unexpected token in directive");
2595 Lex();
2596
2597 if (getParser().ParseAbsoluteExpression(Offset))
2598 return true;
2599
Rafael Espindola066c2f42011-04-12 23:59:07 +00002600 getStreamer().EmitCFIOffset(Register, Offset);
2601 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002602}
2603
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002604/// ParseDirectiveCFIRelOffset
2605/// ::= .cfi_rel_offset register, offset
2606bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2607 SMLoc DirectiveLoc) {
2608 int64_t Register = 0;
2609
2610 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2611 return true;
2612
2613 if (getLexer().isNot(AsmToken::Comma))
2614 return TokError("unexpected token in directive");
2615 Lex();
2616
2617 int64_t Offset = 0;
2618 if (getParser().ParseAbsoluteExpression(Offset))
2619 return true;
2620
Rafael Espindola25f492e2011-04-12 16:12:03 +00002621 getStreamer().EmitCFIRelOffset(Register, Offset);
2622 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002623}
2624
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002625static bool isValidEncoding(int64_t Encoding) {
2626 if (Encoding & ~0xff)
2627 return false;
2628
2629 if (Encoding == dwarf::DW_EH_PE_omit)
2630 return true;
2631
2632 const unsigned Format = Encoding & 0xf;
2633 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2634 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2635 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2636 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2637 return false;
2638
Rafael Espindolacaf11582010-12-29 04:31:26 +00002639 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002640 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002641 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002642 return false;
2643
2644 return true;
2645}
2646
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002647/// ParseDirectiveCFIPersonalityOrLsda
2648/// ::= .cfi_personality encoding, [symbol_name]
2649/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002650bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002651 SMLoc DirectiveLoc) {
2652 int64_t Encoding = 0;
2653 if (getParser().ParseAbsoluteExpression(Encoding))
2654 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002655 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002656 return false;
2657
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002658 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002659 return TokError("unsupported encoding.");
2660
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002661 if (getLexer().isNot(AsmToken::Comma))
2662 return TokError("unexpected token in directive");
2663 Lex();
2664
2665 StringRef Name;
2666 if (getParser().ParseIdentifier(Name))
2667 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002668
2669 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2670
2671 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002672 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002673 else {
2674 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002675 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002676 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002677 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002678}
2679
Rafael Espindolafe024d02010-12-28 18:36:23 +00002680/// ParseDirectiveCFIRememberState
2681/// ::= .cfi_remember_state
2682bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2683 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002684 getStreamer().EmitCFIRememberState();
2685 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002686}
2687
2688/// ParseDirectiveCFIRestoreState
2689/// ::= .cfi_remember_state
2690bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2691 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002692 getStreamer().EmitCFIRestoreState();
2693 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002694}
2695
Rafael Espindolac5754392011-04-12 15:31:05 +00002696/// ParseDirectiveCFISameValue
2697/// ::= .cfi_same_value register
2698bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2699 SMLoc DirectiveLoc) {
2700 int64_t Register = 0;
2701
2702 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2703 return true;
2704
2705 getStreamer().EmitCFISameValue(Register);
2706
2707 return false;
2708}
2709
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002710/// ParseDirectiveMacrosOnOff
2711/// ::= .macros_on
2712/// ::= .macros_off
2713bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2714 SMLoc DirectiveLoc) {
2715 if (getLexer().isNot(AsmToken::EndOfStatement))
2716 return Error(getLexer().getLoc(),
2717 "unexpected token in '" + Directive + "' directive");
2718
2719 getParser().MacrosEnabled = Directive == ".macros_on";
2720
2721 return false;
2722}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002723
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002724/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002725/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002726bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2727 SMLoc DirectiveLoc) {
2728 StringRef Name;
2729 if (getParser().ParseIdentifier(Name))
2730 return TokError("expected identifier in directive");
2731
Rafael Espindola65366442011-06-05 02:43:45 +00002732 std::vector<StringRef> Parameters;
2733 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2734 for(;;) {
2735 StringRef Parameter;
2736 if (getParser().ParseIdentifier(Parameter))
2737 return TokError("expected identifier in directive");
2738 Parameters.push_back(Parameter);
2739
2740 if (getLexer().isNot(AsmToken::Comma))
2741 break;
2742 Lex();
2743 }
2744 }
2745
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002746 if (getLexer().isNot(AsmToken::EndOfStatement))
2747 return TokError("unexpected token in '.macro' directive");
2748
2749 // Eat the end of statement.
2750 Lex();
2751
2752 AsmToken EndToken, StartToken = getTok();
2753
2754 // Lex the macro definition.
2755 for (;;) {
2756 // Check whether we have reached the end of the file.
2757 if (getLexer().is(AsmToken::Eof))
2758 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2759
2760 // Otherwise, check whether we have reach the .endmacro.
2761 if (getLexer().is(AsmToken::Identifier) &&
2762 (getTok().getIdentifier() == ".endm" ||
2763 getTok().getIdentifier() == ".endmacro")) {
2764 EndToken = getTok();
2765 Lex();
2766 if (getLexer().isNot(AsmToken::EndOfStatement))
2767 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2768 "' directive");
2769 break;
2770 }
2771
2772 // Otherwise, scan til the end of the statement.
2773 getParser().EatToEndOfStatement();
2774 }
2775
2776 if (getParser().MacroMap.lookup(Name)) {
2777 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2778 }
2779
2780 const char *BodyStart = StartToken.getLoc().getPointer();
2781 const char *BodyEnd = EndToken.getLoc().getPointer();
2782 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002783 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002784 return false;
2785}
2786
2787/// ParseDirectiveEndMacro
2788/// ::= .endm
2789/// ::= .endmacro
2790bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2791 SMLoc DirectiveLoc) {
2792 if (getLexer().isNot(AsmToken::EndOfStatement))
2793 return TokError("unexpected token in '" + Directive + "' directive");
2794
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002795 // If we are inside a macro instantiation, terminate the current
2796 // instantiation.
2797 if (!getParser().ActiveMacros.empty()) {
2798 getParser().HandleMacroExit();
2799 return false;
2800 }
2801
2802 // Otherwise, this .endmacro is a stray entry in the file; well formed
2803 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002804 return TokError("unexpected '" + Directive + "' in file, "
2805 "no current macro definition");
2806}
2807
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002808bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002809 getParser().CheckForValidSection();
2810
2811 const MCExpr *Value;
2812
2813 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002814 return true;
2815
2816 if (getLexer().isNot(AsmToken::EndOfStatement))
2817 return TokError("unexpected token in directive");
2818
2819 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002820 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002821 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002822 getStreamer().EmitULEB128Value(Value);
2823
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002824 return false;
2825}
2826
2827
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002828/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002829MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002830 MCContext &C, MCStreamer &Out,
2831 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002832 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002833}