blob: 7b62db2e69d5f8e3469249c8907b642eeb849d2b [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"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000030#include "llvm/Support/CommandLine.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000031#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000032#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000033#include "llvm/Support/raw_ostream.h"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000034#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000035#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000036#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000037#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000038using namespace llvm;
39
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000040static cl::opt<bool>
41FatalAssemblerWarnings("fatal-assembler-warnings",
42 cl::desc("Consider warnings as error"));
43
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000044namespace {
45
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000046/// \brief Helper class for tracking macro definitions.
47struct Macro {
48 StringRef Name;
49 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000050 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000051
52public:
Rafael Espindola65366442011-06-05 02:43:45 +000053 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
54 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000055};
56
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000057/// \brief Helper class for storing information about an active macro
58/// instantiation.
59struct MacroInstantiation {
60 /// The macro being instantiated.
61 const Macro *TheMacro;
62
63 /// The macro instantiation with substitutions.
64 MemoryBuffer *Instantiation;
65
66 /// The location of the instantiation.
67 SMLoc InstantiationLoc;
68
69 /// The location where parsing should resume upon instantiation completion.
70 SMLoc ExitLoc;
71
72public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000073 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000074 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000075};
76
Daniel Dunbaraef87e32010-07-18 18:31:38 +000077/// \brief The concrete assembly parser instance.
78class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000079 friend class GenericAsmParser;
80
Daniel Dunbaraef87e32010-07-18 18:31:38 +000081 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
82 void operator=(const AsmParser &); // DO NOT IMPLEMENT
83private:
84 AsmLexer Lexer;
85 MCContext &Ctx;
86 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000087 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000088 SourceMgr &SrcMgr;
89 MCAsmParserExtension *GenericParser;
90 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000091
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 /// This is the current buffer index we're lexing from as managed by the
93 /// SourceMgr object.
94 int CurBuffer;
95
96 AsmCond TheCondState;
97 std::vector<AsmCond> TheCondStack;
98
99 /// DirectiveMap - This is a table handlers for directives. Each handler is
100 /// invoked after the directive identifier is read and is responsible for
101 /// parsing and validating the rest of the directive. The handler is passed
102 /// in the directive name and the location of the directive keyword.
103 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000104
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000105 /// MacroMap - Map of currently defined macros.
106 StringMap<Macro*> MacroMap;
107
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000108 /// ActiveMacros - Stack of active macro instantiations.
109 std::vector<MacroInstantiation*> ActiveMacros;
110
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000111 /// Boolean tracking whether macro substitution is enabled.
112 unsigned MacrosEnabled : 1;
113
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000114 /// Flag tracking whether any errors have been encountered.
115 unsigned HadError : 1;
116
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000117public:
118 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
119 const MCAsmInfo &MAI);
120 ~AsmParser();
121
122 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
123
124 void AddDirectiveHandler(MCAsmParserExtension *Object,
125 StringRef Directive,
126 DirectiveHandler Handler) {
127 DirectiveMap[Directive] = std::make_pair(Object, Handler);
128 }
129
130public:
131 /// @name MCAsmParser Interface
132 /// {
133
134 virtual SourceMgr &getSourceManager() { return SrcMgr; }
135 virtual MCAsmLexer &getLexer() { return Lexer; }
136 virtual MCContext &getContext() { return Ctx; }
137 virtual MCStreamer &getStreamer() { return Out; }
138
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000139 virtual bool Warning(SMLoc L, const Twine &Msg);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000140 virtual bool Error(SMLoc L, const Twine &Msg);
141
142 const AsmToken &Lex();
143
144 bool ParseExpression(const MCExpr *&Res);
145 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
146 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
147 virtual bool ParseAbsoluteExpression(int64_t &Res);
148
149 /// }
150
151private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000152 void CheckForValidSection();
153
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000154 bool ParseStatement();
155
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000156 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000157 bool expandMacro(SmallString<256> &Buf, StringRef Body,
158 const std::vector<StringRef> &Parameters,
159 const std::vector<std::vector<AsmToken> > &A,
160 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000161 void HandleMacroExit();
162
163 void PrintMacroInstantiations();
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000164 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
165 bool ShowLine = true) const {
166 SrcMgr.PrintMessage(Loc, Msg, Type, ShowLine);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000167 }
168
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000169 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
170 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000171
172 /// \brief Reset the current lexer position to that given by \arg Loc. The
173 /// current token is not set; clients should ensure Lex() is called
174 /// subsequently.
175 void JumpToLoc(SMLoc Loc);
176
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000178
179 /// \brief Parse up to the end of statement and a return the contents from the
180 /// current token until the end of the statement; the current token on exit
181 /// will be either the EndOfStatement or EOF.
182 StringRef ParseStringToEndOfStatement();
183
Nico Weber4c4c7322011-01-28 03:04:41 +0000184 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185
186 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
187 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
188 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000189 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190
191 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
192 /// and set \arg Res to the identifier contents.
193 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000194
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000195 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000196
197 // ".ascii", ".asciiz", ".string"
198 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000200 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000201 bool ParseDirectiveFill(); // ".fill"
202 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000203 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000204 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000205 bool ParseDirectiveOrg(); // ".org"
206 // ".align{,32}", ".p2align{,w,l}"
207 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
208
209 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
210 /// accepts a single symbol (which should be a label or an external).
211 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212
213 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
214
215 bool ParseDirectiveAbort(); // ".abort"
216 bool ParseDirectiveInclude(); // ".include"
217
218 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000219 // ".ifdef" or ".ifndef", depending on expect_defined
220 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000221 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
222 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
223 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
224
225 /// ParseEscapedString - Parse the current token as a string which may include
226 /// escaped characters and return the string contents.
227 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000228
229 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
230 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000231};
232
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000233/// \brief Generic implementations of directive handling, etc. which is shared
234/// (or the default, at least) for all assembler parser.
235class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000236 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
237 void AddDirectiveHandler(StringRef Directive) {
238 getParser().AddDirectiveHandler(this, Directive,
239 HandleDirective<GenericAsmParser, Handler>);
240 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000241public:
242 GenericAsmParser() {}
243
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000244 AsmParser &getParser() {
245 return (AsmParser&) this->MCAsmParserExtension::getParser();
246 }
247
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000248 virtual void Initialize(MCAsmParser &Parser) {
249 // Call the base implementation.
250 this->MCAsmParserExtension::Initialize(Parser);
251
252 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000253 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
255 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000258 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
260 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000261 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
262 ".cfi_startproc");
263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
264 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
266 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
268 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
270 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
272 ".cfi_def_cfa_register");
273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
274 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000275 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
276 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000277 AddDirectiveHandler<
278 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
279 AddDirectiveHandler<
280 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000281 AddDirectiveHandler<
282 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
283 AddDirectiveHandler<
284 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000285 AddDirectiveHandler<
286 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000287
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000288 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000289 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
290 ".macros_on");
291 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
292 ".macros_off");
293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
294 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
295 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000296
297 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000299 }
300
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000301 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
302
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000303 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
304 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
305 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000306 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000307 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000308 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
309 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000310 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000311 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000312 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000313 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
314 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000315 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000316 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000317 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000319 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000320
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000321 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000322 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
323 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000324
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000325 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000326};
327
328}
329
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000330namespace llvm {
331
332extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000333extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000334extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000335
336}
337
Chris Lattneraaec2052010-01-19 19:46:13 +0000338enum { DEFAULT_ADDRSPACE = 0 };
339
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000340AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
341 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000342 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000343 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000344 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000345 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000346
347 // Initialize the generic parser.
348 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000349
350 // Initialize the platform / file format parser.
351 //
352 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
353 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000354 if (_MAI.hasMicrosoftFastStdCallMangling()) {
355 PlatformParser = createCOFFAsmParser();
356 PlatformParser->Initialize(*this);
357 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000358 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000359 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000360 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000361 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000362 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000363 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000364}
365
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000366AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000367 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
368
369 // Destroy any macros.
370 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
371 ie = MacroMap.end(); it != ie; ++it)
372 delete it->getValue();
373
Daniel Dunbare4749702010-07-12 18:12:02 +0000374 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000375 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000376}
377
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000378void AsmParser::PrintMacroInstantiations() {
379 // Print the active macro instantiation stack.
380 for (std::vector<MacroInstantiation*>::const_reverse_iterator
381 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
382 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
383 "note");
384}
385
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000386bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
387 if (FatalAssemblerWarnings)
388 return Error(L, Msg);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000389 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000390 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000391 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000392}
393
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000394bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000395 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000396 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000397 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000398 return true;
399}
400
Sean Callananfd0b0282010-01-21 00:19:58 +0000401bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000402 std::string IncludedFile;
403 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000404 if (NewBuf == -1)
405 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000406
Sean Callananfd0b0282010-01-21 00:19:58 +0000407 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000408
Sean Callananfd0b0282010-01-21 00:19:58 +0000409 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000410
Sean Callananfd0b0282010-01-21 00:19:58 +0000411 return false;
412}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000413
414void AsmParser::JumpToLoc(SMLoc Loc) {
415 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
416 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
417}
418
Sean Callananfd0b0282010-01-21 00:19:58 +0000419const AsmToken &AsmParser::Lex() {
420 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000421
Sean Callananfd0b0282010-01-21 00:19:58 +0000422 if (tok->is(AsmToken::Eof)) {
423 // If this is the end of an included file, pop the parent file off the
424 // include stack.
425 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
426 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000427 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000428 tok = &Lexer.Lex();
429 }
430 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000431
Sean Callananfd0b0282010-01-21 00:19:58 +0000432 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000433 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000434
Sean Callananfd0b0282010-01-21 00:19:58 +0000435 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000436}
437
Chris Lattner79180e22010-04-05 23:15:42 +0000438bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000439 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000440 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000441 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000442
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000443 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000444 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000445
446 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000447 AsmCond StartingCondState = TheCondState;
448
Chris Lattnerb717fb02009-07-02 21:53:43 +0000449 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000451 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000452
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000453 // We had an error, validate that one was emitted and recover by skipping to
454 // the next line.
455 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000456 EatToEndOfStatement();
457 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000458
459 if (TheCondState.TheCond != StartingCondState.TheCond ||
460 TheCondState.Ignore != StartingCondState.Ignore)
461 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000462
463 // Check to see there are no empty DwarfFile slots.
464 const std::vector<MCDwarfFile *> &MCDwarfFiles =
465 getContext().getMCDwarfFiles();
466 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000467 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000468 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000469 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000470
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000471 // Check to see that all assembler local symbols were actually defined.
472 // Targets that don't do subsections via symbols may not want this, though,
473 // so conservatively exclude them. Only do this if we're finalizing, though,
474 // as otherwise we won't necessarilly have seen everything yet.
475 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
476 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
477 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
478 e = Symbols.end();
479 i != e; ++i) {
480 MCSymbol *Sym = i->getValue();
481 // Variable symbols may not be marked as defined, so check those
482 // explicitly. If we know it's a variable, we have a definition for
483 // the purposes of this check.
484 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
485 // FIXME: We would really like to refer back to where the symbol was
486 // first referenced for a source location. We need to add something
487 // to track that. Currently, we just point to the end of the file.
488 PrintMessage(getLexer().getLoc(), "assembler local symbol '" +
489 Sym->getName() + "' not defined", "error", false);
490 }
491 }
492
493
Chris Lattner79180e22010-04-05 23:15:42 +0000494 // Finalize the output stream if there are no errors and if the client wants
495 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000496 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000497 Out.Finish();
498
Chris Lattnerb717fb02009-07-02 21:53:43 +0000499 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000500}
501
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000502void AsmParser::CheckForValidSection() {
503 if (!getStreamer().getCurrentSection()) {
504 TokError("expected section directive before assembly directive");
505 Out.SwitchSection(Ctx.getMachOSection(
506 "__TEXT", "__text",
507 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
508 0, SectionKind::getText()));
509 }
510}
511
Chris Lattner2cf5f142009-06-22 01:29:09 +0000512/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
513void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000514 while (Lexer.isNot(AsmToken::EndOfStatement) &&
515 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000516 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000517
Chris Lattner2cf5f142009-06-22 01:29:09 +0000518 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000519 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000520 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000521}
522
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000523StringRef AsmParser::ParseStringToEndOfStatement() {
524 const char *Start = getTok().getLoc().getPointer();
525
526 while (Lexer.isNot(AsmToken::EndOfStatement) &&
527 Lexer.isNot(AsmToken::Eof))
528 Lex();
529
530 const char *End = getTok().getLoc().getPointer();
531 return StringRef(Start, End - Start);
532}
Chris Lattnerc4193832009-06-22 05:51:26 +0000533
Chris Lattner74ec1a32009-06-22 06:32:03 +0000534/// ParseParenExpr - Parse a paren expression and return it.
535/// NOTE: This assumes the leading '(' has already been consumed.
536///
537/// parenexpr ::= expr)
538///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000539bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000540 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000541 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000542 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000543 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000544 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000545 return false;
546}
Chris Lattnerc4193832009-06-22 05:51:26 +0000547
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000548/// ParseBracketExpr - Parse a bracket expression and return it.
549/// NOTE: This assumes the leading '[' has already been consumed.
550///
551/// bracketexpr ::= expr]
552///
553bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
554 if (ParseExpression(Res)) return true;
555 if (Lexer.isNot(AsmToken::RBrac))
556 return TokError("expected ']' in brackets expression");
557 EndLoc = Lexer.getLoc();
558 Lex();
559 return false;
560}
561
Chris Lattner74ec1a32009-06-22 06:32:03 +0000562/// ParsePrimaryExpr - Parse a primary expression and return it.
563/// primaryexpr ::= (parenexpr
564/// primaryexpr ::= symbol
565/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000566/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000567/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000568bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000569 switch (Lexer.getKind()) {
570 default:
571 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000572 // If we have an error assume that we've already handled it.
573 case AsmToken::Error:
574 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000575 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000576 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000577 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000578 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000579 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000581 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000582 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000583 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000584 EndLoc = Lexer.getLoc();
585
586 StringRef Identifier;
587 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000588 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000589
Daniel Dunbarfffff912009-10-16 01:34:54 +0000590 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000591 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000592 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000593
594 // Lookup the symbol variant if used.
595 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000596 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000597 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000598 if (Variant == MCSymbolRefExpr::VK_Invalid) {
599 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000600 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000601 }
602 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000603
Daniel Dunbarfffff912009-10-16 01:34:54 +0000604 // If this is an absolute variable reference, substitute it now to preserve
605 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000606 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000607 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000608 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000609
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000610 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000611 return false;
612 }
613
614 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000615 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000616 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000617 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000618 case AsmToken::Integer: {
619 SMLoc Loc = getTok().getLoc();
620 int64_t IntVal = getTok().getIntVal();
621 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000622 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000623 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000624 // Look for 'b' or 'f' following an Integer as a directional label
625 if (Lexer.getKind() == AsmToken::Identifier) {
626 StringRef IDVal = getTok().getString();
627 if (IDVal == "f" || IDVal == "b"){
628 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
629 IDVal == "f" ? 1 : 0);
630 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
631 getContext());
632 if(IDVal == "b" && Sym->isUndefined())
633 return Error(Loc, "invalid reference to undefined symbol");
634 EndLoc = Lexer.getLoc();
635 Lex(); // Eat identifier.
636 }
637 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000638 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000639 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000640 case AsmToken::Real: {
641 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000642 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000643 Res = MCConstantExpr::Create(IntVal, getContext());
644 Lex(); // Eat token.
645 return false;
646 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000647 case AsmToken::Dot: {
648 // This is a '.' reference, which references the current PC. Emit a
649 // temporary label to the streamer and refer to it.
650 MCSymbol *Sym = Ctx.CreateTempSymbol();
651 Out.EmitLabel(Sym);
652 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
653 EndLoc = Lexer.getLoc();
654 Lex(); // Eat identifier.
655 return false;
656 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000657 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000658 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000659 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000660 case AsmToken::LBrac:
661 if (!PlatformParser->HasBracketExpressions())
662 return TokError("brackets expression not supported on this target");
663 Lex(); // Eat the '['.
664 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000665 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000666 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000667 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000668 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000669 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000670 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000671 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000672 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000673 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000674 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000675 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000677 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000678 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000679 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000680 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000681 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000682 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000683 }
684}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000685
Chris Lattnerb4307b32010-01-15 19:28:38 +0000686bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000687 SMLoc EndLoc;
688 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000689}
690
Daniel Dunbarcceba832010-09-17 02:47:07 +0000691const MCExpr *
692AsmParser::ApplyModifierToExpr(const MCExpr *E,
693 MCSymbolRefExpr::VariantKind Variant) {
694 // Recurse over the given expression, rebuilding it to apply the given variant
695 // if there is exactly one symbol.
696 switch (E->getKind()) {
697 case MCExpr::Target:
698 case MCExpr::Constant:
699 return 0;
700
701 case MCExpr::SymbolRef: {
702 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
703
704 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
705 TokError("invalid variant on expression '" +
706 getTok().getIdentifier() + "' (already modified)");
707 return E;
708 }
709
710 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
711 }
712
713 case MCExpr::Unary: {
714 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
715 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
716 if (!Sub)
717 return 0;
718 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
719 }
720
721 case MCExpr::Binary: {
722 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
723 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
724 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
725
726 if (!LHS && !RHS)
727 return 0;
728
729 if (!LHS) LHS = BE->getLHS();
730 if (!RHS) RHS = BE->getRHS();
731
732 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
733 }
734 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000735
736 assert(0 && "Invalid expression kind!");
737 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000738}
739
Chris Lattner74ec1a32009-06-22 06:32:03 +0000740/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000741///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000742/// expr ::= expr +,- expr -> lowest.
743/// expr ::= expr |,^,&,! expr -> middle.
744/// expr ::= expr *,/,%,<<,>> expr -> highest.
745/// expr ::= primaryexpr
746///
Chris Lattner54482b42010-01-15 19:39:23 +0000747bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000748 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000750 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
751 return true;
752
Daniel Dunbarcceba832010-09-17 02:47:07 +0000753 // As a special case, we support 'a op b @ modifier' by rewriting the
754 // expression to include the modifier. This is inefficient, but in general we
755 // expect users to use 'a@modifier op b'.
756 if (Lexer.getKind() == AsmToken::At) {
757 Lex();
758
759 if (Lexer.isNot(AsmToken::Identifier))
760 return TokError("unexpected symbol modifier following '@'");
761
762 MCSymbolRefExpr::VariantKind Variant =
763 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
764 if (Variant == MCSymbolRefExpr::VK_Invalid)
765 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
766
767 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
768 if (!ModifiedRes) {
769 return TokError("invalid modifier '" + getTok().getIdentifier() +
770 "' (no symbols present)");
771 return true;
772 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000773
Daniel Dunbarcceba832010-09-17 02:47:07 +0000774 Res = ModifiedRes;
775 Lex();
776 }
777
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000778 // Try to constant fold it up front, if possible.
779 int64_t Value;
780 if (Res->EvaluateAsAbsolute(Value))
781 Res = MCConstantExpr::Create(Value, getContext());
782
783 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000784}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000785
Chris Lattnerb4307b32010-01-15 19:28:38 +0000786bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000787 Res = 0;
788 return ParseParenExpr(Res, EndLoc) ||
789 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000790}
791
Daniel Dunbar475839e2009-06-29 20:37:27 +0000792bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000793 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000794
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000795 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 if (ParseExpression(Expr))
797 return true;
798
Daniel Dunbare00b0112009-10-16 01:57:52 +0000799 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000800 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000801
802 return false;
803}
804
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000805static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000806 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000807 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000808 default:
809 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000810
Daniel Dunbarcceba832010-09-17 02:47:07 +0000811 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000812 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000813 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000814 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000815 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000816 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000817 return 1;
818
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000819
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000820 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000821 //
822 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000823 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000824 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000825 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000826 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000827 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000828 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000829 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000830 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000831 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000832
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000833 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000834 case AsmToken::EqualEqual:
835 Kind = MCBinaryExpr::EQ;
836 return 3;
837 case AsmToken::ExclaimEqual:
838 case AsmToken::LessGreater:
839 Kind = MCBinaryExpr::NE;
840 return 3;
841 case AsmToken::Less:
842 Kind = MCBinaryExpr::LT;
843 return 3;
844 case AsmToken::LessEqual:
845 Kind = MCBinaryExpr::LTE;
846 return 3;
847 case AsmToken::Greater:
848 Kind = MCBinaryExpr::GT;
849 return 3;
850 case AsmToken::GreaterEqual:
851 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000852 return 3;
853
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000854 // High Intermediate Precedence: +, -
855 case AsmToken::Plus:
856 Kind = MCBinaryExpr::Add;
857 return 4;
858 case AsmToken::Minus:
859 Kind = MCBinaryExpr::Sub;
860 return 4;
861
Daniel Dunbar475839e2009-06-29 20:37:27 +0000862 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000863 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000864 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000865 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000866 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000867 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000868 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000869 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000870 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000871 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000872 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000873 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000874 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000875 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000876 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000877 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000878 }
879}
880
881
882/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
883/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000884bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
885 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000886 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000887 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000888 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000889
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000890 // If the next token is lower precedence than we are allowed to eat, return
891 // successfully with what we ate already.
892 if (TokPrec < Precedence)
893 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000894
Sean Callanan79ed1a82010-01-19 20:22:31 +0000895 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000896
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000897 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000898 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000899 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000900
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000901 // If BinOp binds less tightly with RHS than the operator after RHS, let
902 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000903 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000904 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000905 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000906 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000907 }
908
Daniel Dunbar475839e2009-06-29 20:37:27 +0000909 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000910 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000911 }
912}
913
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000914
915
916
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000917/// ParseStatement:
918/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000919/// ::= Label* Directive ...Operands... EndOfStatement
920/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000921bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000922 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000923 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000924 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000925 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000926 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000927
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000928 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000929 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000930 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000931 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000932 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000933 // A full line comment is a '#' as the first token.
934 if (Lexer.is(AsmToken::Hash)) {
935 EatToEndOfStatement();
936 return false;
937 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000938
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000939 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000940 if (Lexer.is(AsmToken::Integer)) {
941 LocalLabelVal = getTok().getIntVal();
942 if (LocalLabelVal < 0) {
943 if (!TheCondState.Ignore)
944 return TokError("unexpected token at start of statement");
945 IDVal = "";
946 }
947 else {
948 IDVal = getTok().getString();
949 Lex(); // Consume the integer token to be used as an identifier token.
950 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000951 if (!TheCondState.Ignore)
952 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000953 }
954 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000955
956 } else if (Lexer.is(AsmToken::Dot)) {
957 // Treat '.' as a valid identifier in this context.
958 Lex();
959 IDVal = ".";
960
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000961 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000962 if (!TheCondState.Ignore)
963 return TokError("unexpected token at start of statement");
964 IDVal = "";
965 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000966
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000967
Chris Lattner7834fac2010-04-17 18:14:27 +0000968 // Handle conditional assembly here before checking for skipping. We
969 // have to do this so that .endif isn't skipped in a ".if 0" block for
970 // example.
971 if (IDVal == ".if")
972 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000973 if (IDVal == ".ifdef")
974 return ParseDirectiveIfdef(IDLoc, true);
975 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
976 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000977 if (IDVal == ".elseif")
978 return ParseDirectiveElseIf(IDLoc);
979 if (IDVal == ".else")
980 return ParseDirectiveElse(IDLoc);
981 if (IDVal == ".endif")
982 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000983
Chris Lattner7834fac2010-04-17 18:14:27 +0000984 // If we are in a ".if 0" block, ignore this statement.
985 if (TheCondState.Ignore) {
986 EatToEndOfStatement();
987 return false;
988 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000989
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000990 // FIXME: Recurse on local labels?
991
992 // See what kind of statement we have.
993 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000994 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000995 CheckForValidSection();
996
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000997 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000998 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000999
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001000 // Diagnose attempt to use '.' as a label.
1001 if (IDVal == ".")
1002 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1003
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001004 // Diagnose attempt to use a variable as a label.
1005 //
1006 // FIXME: Diagnostics. Note the location of the definition as a label.
1007 // FIXME: This doesn't diagnose assignment to a symbol which has been
1008 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001009 MCSymbol *Sym;
1010 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001011 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001012 else
1013 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001014 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001015 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001016
Daniel Dunbar959fd882009-08-26 22:13:22 +00001017 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001018 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001019
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001020 // Consume any end of statement token, if present, to avoid spurious
1021 // AddBlankLine calls().
1022 if (Lexer.is(AsmToken::EndOfStatement)) {
1023 Lex();
1024 if (Lexer.is(AsmToken::Eof))
1025 return false;
1026 }
1027
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001028 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001029 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001030
Daniel Dunbar3f872332009-07-28 16:08:33 +00001031 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001032 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001033 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001034
Nico Weber4c4c7322011-01-28 03:04:41 +00001035 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001036
1037 default: // Normal instruction or directive.
1038 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001039 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001040
1041 // If macros are enabled, check to see if this is a macro instantiation.
1042 if (MacrosEnabled)
1043 if (const Macro *M = MacroMap.lookup(IDVal))
1044 return HandleMacroEntry(IDVal, IDLoc, M);
1045
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001046 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001047 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001048 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001049 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001050 return ParseDirectiveSet(IDVal, true);
1051 if (IDVal == ".equiv")
1052 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001053
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054 // Data directives
1055
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001056 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001057 return ParseDirectiveAscii(IDVal, false);
1058 if (IDVal == ".asciz" || IDVal == ".string")
1059 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001061 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001063 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001065 if (IDVal == ".value")
1066 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001067 if (IDVal == ".2byte")
1068 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001069 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001070 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001071 if (IDVal == ".int")
1072 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001073 if (IDVal == ".4byte")
1074 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001075 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001077 if (IDVal == ".8byte")
1078 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001079 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001080 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1081 if (IDVal == ".double")
1082 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001083
Eli Friedman5d68ec22010-07-19 04:17:25 +00001084 if (IDVal == ".align") {
1085 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1086 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1087 }
1088 if (IDVal == ".align32") {
1089 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1090 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1091 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001092 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001093 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001094 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001095 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001096 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001097 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001098 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001099 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001100 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001101 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001102 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001103 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1104
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001105 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001106 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001107
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001108 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001109 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001110 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001111 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001112 if (IDVal == ".zero")
1113 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001115 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001116
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001117 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001118 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001119 // ELF only? Should it be here?
1120 if (IDVal == ".local")
1121 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001123 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001125 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001126 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001127 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001128 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001129 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001130 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001131 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001132 if (IDVal == ".symbol_resolver")
1133 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001134 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001135 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001136 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001137 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001138 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001139 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001140 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001141 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001142 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001143 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001144 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001145 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001146 if (IDVal == ".weak_def_can_be_hidden")
1147 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001148
Hans Wennborg5cc64912011-06-18 13:51:54 +00001149 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001150 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001151 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001152 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001153
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001154 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001155 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001156 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001157 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001158
Roman Divackybb6d14f2011-01-31 21:19:43 +00001159 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001160 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001161
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001162 // Look up the handler in the handler table.
1163 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1164 DirectiveMap.lookup(IDVal);
1165 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001166 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001167
Kevin Enderby9c656452009-09-10 20:51:44 +00001168 // Target hook for parsing target specific directives.
1169 if (!getTargetParser().ParseDirective(ID))
1170 return false;
1171
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001172 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001173 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001174 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001175 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001176
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001177 CheckForValidSection();
1178
Chris Lattnera7f13542010-05-19 23:34:33 +00001179 // Canonicalize the opcode to lower case.
1180 SmallString<128> Opcode;
1181 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1182 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001183
Chris Lattner98986712010-01-14 22:21:20 +00001184 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001185 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001186 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001187
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001188 // Dump the parsed representation, if requested.
1189 if (getShowParsedOperands()) {
1190 SmallString<256> Str;
1191 raw_svector_ostream OS(Str);
1192 OS << "parsed instruction: [";
1193 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1194 if (i != 0)
1195 OS << ", ";
1196 ParsedOperands[i]->dump(OS);
1197 }
1198 OS << "]";
1199
1200 PrintMessage(IDLoc, OS.str(), "note");
1201 }
1202
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001203 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001204 if (!HadError)
1205 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1206 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001207
Chris Lattner98986712010-01-14 22:21:20 +00001208 // Free any parsed operands.
1209 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1210 delete ParsedOperands[i];
1211
Chris Lattnercbf8a982010-09-11 16:18:25 +00001212 // Don't skip the rest of the line, the instruction parser is responsible for
1213 // that.
1214 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001215}
Chris Lattner9a023f72009-06-24 04:43:34 +00001216
Rafael Espindola65366442011-06-05 02:43:45 +00001217bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1218 const std::vector<StringRef> &Parameters,
1219 const std::vector<std::vector<AsmToken> > &A,
1220 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001221 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001222 unsigned NParameters = Parameters.size();
1223 if (NParameters != 0 && NParameters != A.size())
1224 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001225
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001226 while (!Body.empty()) {
1227 // Scan for the next substitution.
1228 std::size_t End = Body.size(), Pos = 0;
1229 for (; Pos != End; ++Pos) {
1230 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001231 if (!NParameters) {
1232 // This macro has no parameters, look for $0, $1, etc.
1233 if (Body[Pos] != '$' || Pos + 1 == End)
1234 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001235
Rafael Espindola65366442011-06-05 02:43:45 +00001236 char Next = Body[Pos + 1];
1237 if (Next == '$' || Next == 'n' || isdigit(Next))
1238 break;
1239 } else {
1240 // This macro has parameters, look for \foo, \bar, etc.
1241 if (Body[Pos] == '\\' && Pos + 1 != End)
1242 break;
1243 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001244 }
1245
1246 // Add the prefix.
1247 OS << Body.slice(0, Pos);
1248
1249 // Check if we reached the end.
1250 if (Pos == End)
1251 break;
1252
Rafael Espindola65366442011-06-05 02:43:45 +00001253 if (!NParameters) {
1254 switch (Body[Pos+1]) {
1255 // $$ => $
1256 case '$':
1257 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001258 break;
1259
Rafael Espindola65366442011-06-05 02:43:45 +00001260 // $n => number of arguments
1261 case 'n':
1262 OS << A.size();
1263 break;
1264
1265 // $[0-9] => argument
1266 default: {
1267 // Missing arguments are ignored.
1268 unsigned Index = Body[Pos+1] - '0';
1269 if (Index >= A.size())
1270 break;
1271
1272 // Otherwise substitute with the token values, with spaces eliminated.
1273 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1274 ie = A[Index].end(); it != ie; ++it)
1275 OS << it->getString();
1276 break;
1277 }
1278 }
1279 Pos += 2;
1280 } else {
1281 unsigned I = Pos + 1;
1282 while (isalnum(Body[I]) && I + 1 != End)
1283 ++I;
1284
1285 const char *Begin = Body.data() + Pos +1;
1286 StringRef Argument(Begin, I - (Pos +1));
1287 unsigned Index = 0;
1288 for (; Index < NParameters; ++Index)
1289 if (Parameters[Index] == Argument)
1290 break;
1291
1292 // FIXME: We should error at the macro definition.
1293 if (Index == NParameters)
1294 return Error(L, "Parameter not found");
1295
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001296 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1297 ie = A[Index].end(); it != ie; ++it)
1298 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001299
Rafael Espindola65366442011-06-05 02:43:45 +00001300 Pos += 1 + Argument.size();
1301 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001302 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001303 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001304 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001305
1306 // We include the .endmacro in the buffer as our queue to exit the macro
1307 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001308 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001309 return false;
1310}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001311
Rafael Espindola65366442011-06-05 02:43:45 +00001312MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1313 MemoryBuffer *I)
1314 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1315{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001316}
1317
1318bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1319 const Macro *M) {
1320 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1321 // this, although we should protect against infinite loops.
1322 if (ActiveMacros.size() == 20)
1323 return TokError("macros cannot be nested more than 20 levels deep");
1324
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001325 // Parse the macro instantiation arguments.
1326 std::vector<std::vector<AsmToken> > MacroArguments;
1327 MacroArguments.push_back(std::vector<AsmToken>());
1328 unsigned ParenLevel = 0;
1329 for (;;) {
1330 if (Lexer.is(AsmToken::Eof))
1331 return TokError("unexpected token in macro instantiation");
1332 if (Lexer.is(AsmToken::EndOfStatement))
1333 break;
1334
1335 // If we aren't inside parentheses and this is a comma, start a new token
1336 // list.
1337 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1338 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001339 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001340 // Adjust the current parentheses level.
1341 if (Lexer.is(AsmToken::LParen))
1342 ++ParenLevel;
1343 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1344 --ParenLevel;
1345
1346 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001347 MacroArguments.back().push_back(getTok());
1348 }
1349 Lex();
1350 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001351
Rafael Espindola65366442011-06-05 02:43:45 +00001352 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1353 // to hold the macro body with substitutions.
1354 SmallString<256> Buf;
1355 StringRef Body = M->Body;
1356
1357 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1358 return true;
1359
1360 MemoryBuffer *Instantiation =
1361 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1362
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001363 // Create the macro instantiation object and add to the current macro
1364 // instantiation stack.
1365 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001366 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001367 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001368 ActiveMacros.push_back(MI);
1369
1370 // Jump to the macro instantiation and prime the lexer.
1371 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1372 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1373 Lex();
1374
1375 return false;
1376}
1377
1378void AsmParser::HandleMacroExit() {
1379 // Jump to the EndOfStatement we should return to, and consume it.
1380 JumpToLoc(ActiveMacros.back()->ExitLoc);
1381 Lex();
1382
1383 // Pop the instantiation entry.
1384 delete ActiveMacros.back();
1385 ActiveMacros.pop_back();
1386}
1387
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001388static void MarkUsed(const MCExpr *Value) {
1389 switch (Value->getKind()) {
1390 case MCExpr::Binary:
1391 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1392 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1393 break;
1394 case MCExpr::Target:
1395 case MCExpr::Constant:
1396 break;
1397 case MCExpr::SymbolRef: {
1398 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1399 break;
1400 }
1401 case MCExpr::Unary:
1402 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1403 break;
1404 }
1405}
1406
Nico Weber4c4c7322011-01-28 03:04:41 +00001407bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001408 // FIXME: Use better location, we should use proper tokens.
1409 SMLoc EqualLoc = Lexer.getLoc();
1410
Daniel Dunbar821e3332009-08-31 08:09:28 +00001411 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001412 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001413 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001414
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001415 MarkUsed(Value);
1416
Daniel Dunbar3f872332009-07-28 16:08:33 +00001417 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001418 return TokError("unexpected token in assignment");
1419
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001420 // Error on assignment to '.'.
1421 if (Name == ".") {
1422 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1423 "(use '.space' or '.org').)"));
1424 }
1425
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001426 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001427 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001428
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001429 // Validate that the LHS is allowed to be a variable (either it has not been
1430 // used as a symbol, or it is an absolute symbol).
1431 MCSymbol *Sym = getContext().LookupSymbol(Name);
1432 if (Sym) {
1433 // Diagnose assignment to a label.
1434 //
1435 // FIXME: Diagnostics. Note the location of the definition as a label.
1436 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001437 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001438 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001439 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001440 return Error(EqualLoc, "redefinition of '" + Name + "'");
1441 else if (!Sym->isVariable())
1442 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001443 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001444 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1445 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001446
1447 // Don't count these checks as uses.
1448 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001449 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001450 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001451
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001452 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001453
1454 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001455 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001456
1457 return false;
1458}
1459
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001460/// ParseIdentifier:
1461/// ::= identifier
1462/// ::= string
1463bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001464 // The assembler has relaxed rules for accepting identifiers, in particular we
1465 // allow things like '.globl $foo', which would normally be separate
1466 // tokens. At this level, we have already lexed so we cannot (currently)
1467 // handle this as a context dependent token, instead we detect adjacent tokens
1468 // and return the combined identifier.
1469 if (Lexer.is(AsmToken::Dollar)) {
1470 SMLoc DollarLoc = getLexer().getLoc();
1471
1472 // Consume the dollar sign, and check for a following identifier.
1473 Lex();
1474 if (Lexer.isNot(AsmToken::Identifier))
1475 return true;
1476
1477 // We have a '$' followed by an identifier, make sure they are adjacent.
1478 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1479 return true;
1480
1481 // Construct the joined identifier and consume the token.
1482 Res = StringRef(DollarLoc.getPointer(),
1483 getTok().getIdentifier().size() + 1);
1484 Lex();
1485 return false;
1486 }
1487
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001488 if (Lexer.isNot(AsmToken::Identifier) &&
1489 Lexer.isNot(AsmToken::String))
1490 return true;
1491
Sean Callanan18b83232010-01-19 21:44:56 +00001492 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001493
Sean Callanan79ed1a82010-01-19 20:22:31 +00001494 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001495
1496 return false;
1497}
1498
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001499/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001500/// ::= .equ identifier ',' expression
1501/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001502/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001503bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001504 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001505
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001506 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001507 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001508
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001509 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001510 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001511 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001512
Nico Weber4c4c7322011-01-28 03:04:41 +00001513 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001514}
1515
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001516bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001517 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001518
1519 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001520 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001521 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1522 if (Str[i] != '\\') {
1523 Data += Str[i];
1524 continue;
1525 }
1526
1527 // Recognize escaped characters. Note that this escape semantics currently
1528 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1529 ++i;
1530 if (i == e)
1531 return TokError("unexpected backslash at end of string");
1532
1533 // Recognize octal sequences.
1534 if ((unsigned) (Str[i] - '0') <= 7) {
1535 // Consume up to three octal characters.
1536 unsigned Value = Str[i] - '0';
1537
1538 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1539 ++i;
1540 Value = Value * 8 + (Str[i] - '0');
1541
1542 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1543 ++i;
1544 Value = Value * 8 + (Str[i] - '0');
1545 }
1546 }
1547
1548 if (Value > 255)
1549 return TokError("invalid octal escape sequence (out of range)");
1550
1551 Data += (unsigned char) Value;
1552 continue;
1553 }
1554
1555 // Otherwise recognize individual escapes.
1556 switch (Str[i]) {
1557 default:
1558 // Just reject invalid escape sequences for now.
1559 return TokError("invalid escape sequence (unrecognized character)");
1560
1561 case 'b': Data += '\b'; break;
1562 case 'f': Data += '\f'; break;
1563 case 'n': Data += '\n'; break;
1564 case 'r': Data += '\r'; break;
1565 case 't': Data += '\t'; break;
1566 case '"': Data += '"'; break;
1567 case '\\': Data += '\\'; break;
1568 }
1569 }
1570
1571 return false;
1572}
1573
Daniel Dunbara0d14262009-06-24 23:30:00 +00001574/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001575/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1576bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001577 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001578 CheckForValidSection();
1579
Daniel Dunbara0d14262009-06-24 23:30:00 +00001580 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001581 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001582 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001584 std::string Data;
1585 if (ParseEscapedString(Data))
1586 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001587
1588 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001589 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001590 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1591
Sean Callanan79ed1a82010-01-19 20:22:31 +00001592 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001593
1594 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001595 break;
1596
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001597 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001598 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001599 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001600 }
1601 }
1602
Sean Callanan79ed1a82010-01-19 20:22:31 +00001603 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001604 return false;
1605}
1606
1607/// ParseDirectiveValue
1608/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1609bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001611 CheckForValidSection();
1612
Daniel Dunbara0d14262009-06-24 23:30:00 +00001613 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001614 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001615 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001616 return true;
1617
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001618 // Special case constant expressions to match code generator.
1619 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001620 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001621 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001622 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001623
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001624 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001625 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001626
Daniel Dunbara0d14262009-06-24 23:30:00 +00001627 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001628 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001629 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001631 }
1632 }
1633
Sean Callanan79ed1a82010-01-19 20:22:31 +00001634 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001635 return false;
1636}
1637
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001638/// ParseDirectiveRealValue
1639/// ::= (.single | .double) [ expression (, expression)* ]
1640bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1641 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1642 CheckForValidSection();
1643
1644 for (;;) {
1645 // We don't truly support arithmetic on floating point expressions, so we
1646 // have to manually parse unary prefixes.
1647 bool IsNeg = false;
1648 if (getLexer().is(AsmToken::Minus)) {
1649 Lex();
1650 IsNeg = true;
1651 } else if (getLexer().is(AsmToken::Plus))
1652 Lex();
1653
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001654 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001655 getLexer().isNot(AsmToken::Real) &&
1656 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001657 return TokError("unexpected token in directive");
1658
1659 // Convert to an APFloat.
1660 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001661 StringRef IDVal = getTok().getString();
1662 if (getLexer().is(AsmToken::Identifier)) {
1663 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1664 Value = APFloat::getInf(Semantics);
1665 else if (!IDVal.compare_lower("nan"))
1666 Value = APFloat::getNaN(Semantics, false, ~0);
1667 else
1668 return TokError("invalid floating point literal");
1669 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001670 APFloat::opInvalidOp)
1671 return TokError("invalid floating point literal");
1672 if (IsNeg)
1673 Value.changeSign();
1674
1675 // Consume the numeric token.
1676 Lex();
1677
1678 // Emit the value as an integer.
1679 APInt AsInt = Value.bitcastToAPInt();
1680 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1681 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1682
1683 if (getLexer().is(AsmToken::EndOfStatement))
1684 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001685
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001686 if (getLexer().isNot(AsmToken::Comma))
1687 return TokError("unexpected token in directive");
1688 Lex();
1689 }
1690 }
1691
1692 Lex();
1693 return false;
1694}
1695
Daniel Dunbara0d14262009-06-24 23:30:00 +00001696/// ParseDirectiveSpace
1697/// ::= .space expression [ , expression ]
1698bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001699 CheckForValidSection();
1700
Daniel Dunbara0d14262009-06-24 23:30:00 +00001701 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001702 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001703 return true;
1704
1705 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1707 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001708 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001709 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001710
Daniel Dunbar475839e2009-06-29 20:37:27 +00001711 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001712 return true;
1713
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001714 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001715 return TokError("unexpected token in '.space' directive");
1716 }
1717
Sean Callanan79ed1a82010-01-19 20:22:31 +00001718 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001719
1720 if (NumBytes <= 0)
1721 return TokError("invalid number of bytes in '.space' directive");
1722
1723 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001724 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001725
1726 return false;
1727}
1728
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001729/// ParseDirectiveZero
1730/// ::= .zero expression
1731bool AsmParser::ParseDirectiveZero() {
1732 CheckForValidSection();
1733
1734 int64_t NumBytes;
1735 if (ParseAbsoluteExpression(NumBytes))
1736 return true;
1737
Rafael Espindolae452b172010-10-05 19:42:57 +00001738 int64_t Val = 0;
1739 if (getLexer().is(AsmToken::Comma)) {
1740 Lex();
1741 if (ParseAbsoluteExpression(Val))
1742 return true;
1743 }
1744
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001745 if (getLexer().isNot(AsmToken::EndOfStatement))
1746 return TokError("unexpected token in '.zero' directive");
1747
1748 Lex();
1749
Rafael Espindolae452b172010-10-05 19:42:57 +00001750 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001751
1752 return false;
1753}
1754
Daniel Dunbara0d14262009-06-24 23:30:00 +00001755/// ParseDirectiveFill
1756/// ::= .fill expression , expression , expression
1757bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001758 CheckForValidSection();
1759
Daniel Dunbara0d14262009-06-24 23:30:00 +00001760 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001761 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001762 return true;
1763
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001764 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001765 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001766 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001767
Daniel Dunbara0d14262009-06-24 23:30:00 +00001768 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001769 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001770 return true;
1771
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001772 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001773 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001774 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001775
Daniel Dunbara0d14262009-06-24 23:30:00 +00001776 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001777 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001778 return true;
1779
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001780 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001781 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001782
Sean Callanan79ed1a82010-01-19 20:22:31 +00001783 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001784
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001785 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1786 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001787
1788 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001789 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001790
1791 return false;
1792}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001793
1794/// ParseDirectiveOrg
1795/// ::= .org expression [ , expression ]
1796bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001797 CheckForValidSection();
1798
Daniel Dunbar821e3332009-08-31 08:09:28 +00001799 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001800 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001801 return true;
1802
1803 // Parse optional fill expression.
1804 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001805 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1806 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001807 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001808 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001809
Daniel Dunbar475839e2009-06-29 20:37:27 +00001810 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001811 return true;
1812
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001813 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001814 return TokError("unexpected token in '.org' directive");
1815 }
1816
Sean Callanan79ed1a82010-01-19 20:22:31 +00001817 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001818
1819 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1820 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001821 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001822
1823 return false;
1824}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001825
1826/// ParseDirectiveAlign
1827/// ::= {.align, ...} expression [ , expression [ , expression ]]
1828bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001829 CheckForValidSection();
1830
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001831 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001832 int64_t Alignment;
1833 if (ParseAbsoluteExpression(Alignment))
1834 return true;
1835
1836 SMLoc MaxBytesLoc;
1837 bool HasFillExpr = false;
1838 int64_t FillExpr = 0;
1839 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001840 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1841 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001842 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001843 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001844
1845 // The fill expression can be omitted while specifying a maximum number of
1846 // alignment bytes, e.g:
1847 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001848 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001849 HasFillExpr = true;
1850 if (ParseAbsoluteExpression(FillExpr))
1851 return true;
1852 }
1853
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001854 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1855 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001856 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001857 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001858
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001859 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001860 if (ParseAbsoluteExpression(MaxBytesToFill))
1861 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001862
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001863 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001864 return TokError("unexpected token in directive");
1865 }
1866 }
1867
Sean Callanan79ed1a82010-01-19 20:22:31 +00001868 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001869
Daniel Dunbar648ac512010-05-17 21:54:30 +00001870 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001871 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001872
1873 // Compute alignment in bytes.
1874 if (IsPow2) {
1875 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001876 if (Alignment >= 32) {
1877 Error(AlignmentLoc, "invalid alignment value");
1878 Alignment = 31;
1879 }
1880
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001881 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001882 }
1883
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001884 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001885 if (MaxBytesLoc.isValid()) {
1886 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001887 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1888 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001889 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001890 }
1891
1892 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001893 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1894 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001895 MaxBytesToFill = 0;
1896 }
1897 }
1898
Daniel Dunbar648ac512010-05-17 21:54:30 +00001899 // Check whether we should use optimal code alignment for this .align
1900 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001901 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001902 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1903 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001904 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001905 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001906 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001907 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1908 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001909 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001910
1911 return false;
1912}
1913
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001914/// ParseDirectiveSymbolAttribute
1915/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001916bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001917 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001918 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001919 StringRef Name;
1920
1921 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001922 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001923
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001924 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001925
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001926 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001927
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001928 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001929 break;
1930
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001931 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001932 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001933 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001934 }
1935 }
1936
Sean Callanan79ed1a82010-01-19 20:22:31 +00001937 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001938 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001939}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001940
1941/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001942/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1943bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001944 CheckForValidSection();
1945
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001946 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001947 StringRef Name;
1948 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001949 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001950
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001951 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001952 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001953
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001954 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001955 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001956 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001957
1958 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001959 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001960 if (ParseAbsoluteExpression(Size))
1961 return true;
1962
1963 int64_t Pow2Alignment = 0;
1964 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001965 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001966 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001967 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001968 if (ParseAbsoluteExpression(Pow2Alignment))
1969 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001970
Chris Lattner258281d2010-01-19 06:22:22 +00001971 // If this target takes alignments in bytes (not log) validate and convert.
1972 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1973 if (!isPowerOf2_64(Pow2Alignment))
1974 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1975 Pow2Alignment = Log2_64(Pow2Alignment);
1976 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001977 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001978
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001979 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001980 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001981
Sean Callanan79ed1a82010-01-19 20:22:31 +00001982 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001983
Chris Lattner1fc3d752009-07-09 17:25:12 +00001984 // NOTE: a size of zero for a .comm should create a undefined symbol
1985 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001986 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001987 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1988 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001989
Eric Christopherc260a3e2010-05-14 01:38:54 +00001990 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001991 // may internally end up wanting an alignment in bytes.
1992 // FIXME: Diagnose overflow.
1993 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001994 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1995 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001996
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001997 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001998 return Error(IDLoc, "invalid symbol redefinition");
1999
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002000 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002001 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002002 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002003 getStreamer().EmitZerofill(Ctx.getMachOSection(
2004 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2005 0, SectionKind::getBSS()),
2006 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002007 return false;
2008 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002009
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002010 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002011 return false;
2012}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002013
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002014/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002015/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002016bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002017 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002018 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002019
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002020 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002021 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002022 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002023
Sean Callanan79ed1a82010-01-19 20:22:31 +00002024 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002025
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002026 if (Str.empty())
2027 Error(Loc, ".abort detected. Assembly stopping.");
2028 else
2029 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002030 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002031
2032 return false;
2033}
Kevin Enderby71148242009-07-14 21:35:03 +00002034
Kevin Enderby1f049b22009-07-14 23:21:55 +00002035/// ParseDirectiveInclude
2036/// ::= .include "filename"
2037bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002038 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002039 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002040
Sean Callanan18b83232010-01-19 21:44:56 +00002041 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002042 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002043 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002044
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002045 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002046 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002047
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002048 // Strip the quotes.
2049 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002050
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002051 // Attempt to switch the lexer to the included file before consuming the end
2052 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002053 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002054 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002055 return true;
2056 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002057
2058 return false;
2059}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002060
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002061/// ParseDirectiveIf
2062/// ::= .if expression
2063bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002064 TheCondStack.push_back(TheCondState);
2065 TheCondState.TheCond = AsmCond::IfCond;
2066 if(TheCondState.Ignore) {
2067 EatToEndOfStatement();
2068 }
2069 else {
2070 int64_t ExprValue;
2071 if (ParseAbsoluteExpression(ExprValue))
2072 return true;
2073
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002074 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002075 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002076
Sean Callanan79ed1a82010-01-19 20:22:31 +00002077 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002078
2079 TheCondState.CondMet = ExprValue;
2080 TheCondState.Ignore = !TheCondState.CondMet;
2081 }
2082
2083 return false;
2084}
2085
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002086bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2087 StringRef Name;
2088 TheCondStack.push_back(TheCondState);
2089 TheCondState.TheCond = AsmCond::IfCond;
2090
2091 if (TheCondState.Ignore) {
2092 EatToEndOfStatement();
2093 } else {
2094 if (ParseIdentifier(Name))
2095 return TokError("expected identifier after '.ifdef'");
2096
2097 Lex();
2098
2099 MCSymbol *Sym = getContext().LookupSymbol(Name);
2100
2101 if (expect_defined)
2102 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2103 else
2104 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2105 TheCondState.Ignore = !TheCondState.CondMet;
2106 }
2107
2108 return false;
2109}
2110
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002111/// ParseDirectiveElseIf
2112/// ::= .elseif expression
2113bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2114 if (TheCondState.TheCond != AsmCond::IfCond &&
2115 TheCondState.TheCond != AsmCond::ElseIfCond)
2116 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2117 " an .elseif");
2118 TheCondState.TheCond = AsmCond::ElseIfCond;
2119
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002120 bool LastIgnoreState = false;
2121 if (!TheCondStack.empty())
2122 LastIgnoreState = TheCondStack.back().Ignore;
2123 if (LastIgnoreState || TheCondState.CondMet) {
2124 TheCondState.Ignore = true;
2125 EatToEndOfStatement();
2126 }
2127 else {
2128 int64_t ExprValue;
2129 if (ParseAbsoluteExpression(ExprValue))
2130 return true;
2131
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002132 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002133 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002134
Sean Callanan79ed1a82010-01-19 20:22:31 +00002135 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002136 TheCondState.CondMet = ExprValue;
2137 TheCondState.Ignore = !TheCondState.CondMet;
2138 }
2139
2140 return false;
2141}
2142
2143/// ParseDirectiveElse
2144/// ::= .else
2145bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002146 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002147 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002148
Sean Callanan79ed1a82010-01-19 20:22:31 +00002149 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002150
2151 if (TheCondState.TheCond != AsmCond::IfCond &&
2152 TheCondState.TheCond != AsmCond::ElseIfCond)
2153 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2154 ".elseif");
2155 TheCondState.TheCond = AsmCond::ElseCond;
2156 bool LastIgnoreState = false;
2157 if (!TheCondStack.empty())
2158 LastIgnoreState = TheCondStack.back().Ignore;
2159 if (LastIgnoreState || TheCondState.CondMet)
2160 TheCondState.Ignore = true;
2161 else
2162 TheCondState.Ignore = false;
2163
2164 return false;
2165}
2166
2167/// ParseDirectiveEndIf
2168/// ::= .endif
2169bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002170 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002171 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002172
Sean Callanan79ed1a82010-01-19 20:22:31 +00002173 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002174
2175 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2176 TheCondStack.empty())
2177 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2178 ".else");
2179 if (!TheCondStack.empty()) {
2180 TheCondState = TheCondStack.back();
2181 TheCondStack.pop_back();
2182 }
2183
2184 return false;
2185}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002186
2187/// ParseDirectiveFile
2188/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002189bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002190 // FIXME: I'm not sure what this is.
2191 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002192 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002193 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002194 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002195 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002196
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002197 if (FileNumber < 1)
2198 return TokError("file number less than one");
2199 }
2200
Daniel Dunbareceec052010-07-12 17:45:27 +00002201 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002202 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002203
Chris Lattnerd32e8032010-01-25 19:02:58 +00002204 StringRef Filename = getTok().getString();
2205 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002206 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002207
Daniel Dunbareceec052010-07-12 17:45:27 +00002208 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002209 return TokError("unexpected token in '.file' directive");
2210
Chris Lattnerd32e8032010-01-25 19:02:58 +00002211 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002212 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002213 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002214 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002215 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002216 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002217
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002218 return false;
2219}
2220
2221/// ParseDirectiveLine
2222/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002223bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002224 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2225 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002226 return TokError("unexpected token in '.line' directive");
2227
Sean Callanan18b83232010-01-19 21:44:56 +00002228 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002229 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002230 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002231
2232 // FIXME: Do something with the .line.
2233 }
2234
Daniel Dunbareceec052010-07-12 17:45:27 +00002235 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002236 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002237
2238 return false;
2239}
2240
2241
2242/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002243/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002244/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2245/// The first number is a file number, must have been previously assigned with
2246/// a .file directive, the second number is the line number and optionally the
2247/// third number is a column position (zero if not specified). The remaining
2248/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002249bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002250
Daniel Dunbareceec052010-07-12 17:45:27 +00002251 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002252 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002253 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002254 if (FileNumber < 1)
2255 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002256 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002257 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002258 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002259
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002260 int64_t LineNumber = 0;
2261 if (getLexer().is(AsmToken::Integer)) {
2262 LineNumber = getTok().getIntVal();
2263 if (LineNumber < 1)
2264 return TokError("line number less than one in '.loc' directive");
2265 Lex();
2266 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002267
2268 int64_t ColumnPos = 0;
2269 if (getLexer().is(AsmToken::Integer)) {
2270 ColumnPos = getTok().getIntVal();
2271 if (ColumnPos < 0)
2272 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002273 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002274 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002275
Kevin Enderbyc0957932010-09-30 16:52:03 +00002276 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002277 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002278 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002279 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2280 for (;;) {
2281 if (getLexer().is(AsmToken::EndOfStatement))
2282 break;
2283
2284 StringRef Name;
2285 SMLoc Loc = getTok().getLoc();
2286 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002287 return TokError("unexpected token in '.loc' directive");
2288
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002289 if (Name == "basic_block")
2290 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2291 else if (Name == "prologue_end")
2292 Flags |= DWARF2_FLAG_PROLOGUE_END;
2293 else if (Name == "epilogue_begin")
2294 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2295 else if (Name == "is_stmt") {
2296 SMLoc Loc = getTok().getLoc();
2297 const MCExpr *Value;
2298 if (getParser().ParseExpression(Value))
2299 return true;
2300 // The expression must be the constant 0 or 1.
2301 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2302 int Value = MCE->getValue();
2303 if (Value == 0)
2304 Flags &= ~DWARF2_FLAG_IS_STMT;
2305 else if (Value == 1)
2306 Flags |= DWARF2_FLAG_IS_STMT;
2307 else
2308 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002309 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002310 else {
2311 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2312 }
2313 }
2314 else if (Name == "isa") {
2315 SMLoc Loc = getTok().getLoc();
2316 const MCExpr *Value;
2317 if (getParser().ParseExpression(Value))
2318 return true;
2319 // The expression must be a constant greater or equal to 0.
2320 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2321 int Value = MCE->getValue();
2322 if (Value < 0)
2323 return Error(Loc, "isa number less than zero");
2324 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002325 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002326 else {
2327 return Error(Loc, "isa number not a constant value");
2328 }
2329 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002330 else if (Name == "discriminator") {
2331 if (getParser().ParseAbsoluteExpression(Discriminator))
2332 return true;
2333 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002334 else {
2335 return Error(Loc, "unknown sub-directive in '.loc' directive");
2336 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002337
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002338 if (getLexer().is(AsmToken::EndOfStatement))
2339 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002340 }
2341 }
2342
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002343 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002344 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002345
2346 return false;
2347}
2348
Daniel Dunbar138abae2010-10-16 04:56:42 +00002349/// ParseDirectiveStabs
2350/// ::= .stabs string, number, number, number
2351bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2352 SMLoc DirectiveLoc) {
2353 return TokError("unsupported directive '" + Directive + "'");
2354}
2355
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002356/// ParseDirectiveCFISections
2357/// ::= .cfi_sections section [, section]
2358bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2359 SMLoc DirectiveLoc) {
2360 StringRef Name;
2361 bool EH = false;
2362 bool Debug = false;
2363
2364 if (getParser().ParseIdentifier(Name))
2365 return TokError("Expected an identifier");
2366
2367 if (Name == ".eh_frame")
2368 EH = true;
2369 else if (Name == ".debug_frame")
2370 Debug = true;
2371
2372 if (getLexer().is(AsmToken::Comma)) {
2373 Lex();
2374
2375 if (getParser().ParseIdentifier(Name))
2376 return TokError("Expected an identifier");
2377
2378 if (Name == ".eh_frame")
2379 EH = true;
2380 else if (Name == ".debug_frame")
2381 Debug = true;
2382 }
2383
2384 getStreamer().EmitCFISections(EH, Debug);
2385
2386 return false;
2387}
2388
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002389/// ParseDirectiveCFIStartProc
2390/// ::= .cfi_startproc
2391bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2392 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002393 getStreamer().EmitCFIStartProc();
2394 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002395}
2396
2397/// ParseDirectiveCFIEndProc
2398/// ::= .cfi_endproc
2399bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002400 getStreamer().EmitCFIEndProc();
2401 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002402}
2403
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002404/// ParseRegisterOrRegisterNumber - parse register name or number.
2405bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2406 SMLoc DirectiveLoc) {
2407 unsigned RegNo;
2408
Jim Grosbach6f888a82011-06-02 17:14:04 +00002409 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002410 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2411 DirectiveLoc))
2412 return true;
2413 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2414 } else
2415 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002416
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002417 return false;
2418}
2419
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002420/// ParseDirectiveCFIDefCfa
2421/// ::= .cfi_def_cfa register, offset
2422bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2423 SMLoc DirectiveLoc) {
2424 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002425 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002426 return true;
2427
2428 if (getLexer().isNot(AsmToken::Comma))
2429 return TokError("unexpected token in directive");
2430 Lex();
2431
2432 int64_t Offset = 0;
2433 if (getParser().ParseAbsoluteExpression(Offset))
2434 return true;
2435
Rafael Espindola066c2f42011-04-12 23:59:07 +00002436 getStreamer().EmitCFIDefCfa(Register, Offset);
2437 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002438}
2439
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002440/// ParseDirectiveCFIDefCfaOffset
2441/// ::= .cfi_def_cfa_offset offset
2442bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2443 SMLoc DirectiveLoc) {
2444 int64_t Offset = 0;
2445 if (getParser().ParseAbsoluteExpression(Offset))
2446 return true;
2447
Rafael Espindola066c2f42011-04-12 23:59:07 +00002448 getStreamer().EmitCFIDefCfaOffset(Offset);
2449 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002450}
2451
2452/// ParseDirectiveCFIAdjustCfaOffset
2453/// ::= .cfi_adjust_cfa_offset adjustment
2454bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2455 SMLoc DirectiveLoc) {
2456 int64_t Adjustment = 0;
2457 if (getParser().ParseAbsoluteExpression(Adjustment))
2458 return true;
2459
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002460 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2461 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002462}
2463
2464/// ParseDirectiveCFIDefCfaRegister
2465/// ::= .cfi_def_cfa_register register
2466bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2467 SMLoc DirectiveLoc) {
2468 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002469 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002470 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002471
Rafael Espindola066c2f42011-04-12 23:59:07 +00002472 getStreamer().EmitCFIDefCfaRegister(Register);
2473 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002474}
2475
2476/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002477/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002478bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2479 int64_t Register = 0;
2480 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002481
2482 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002483 return true;
2484
2485 if (getLexer().isNot(AsmToken::Comma))
2486 return TokError("unexpected token in directive");
2487 Lex();
2488
2489 if (getParser().ParseAbsoluteExpression(Offset))
2490 return true;
2491
Rafael Espindola066c2f42011-04-12 23:59:07 +00002492 getStreamer().EmitCFIOffset(Register, Offset);
2493 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002494}
2495
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002496/// ParseDirectiveCFIRelOffset
2497/// ::= .cfi_rel_offset register, offset
2498bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2499 SMLoc DirectiveLoc) {
2500 int64_t Register = 0;
2501
2502 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2503 return true;
2504
2505 if (getLexer().isNot(AsmToken::Comma))
2506 return TokError("unexpected token in directive");
2507 Lex();
2508
2509 int64_t Offset = 0;
2510 if (getParser().ParseAbsoluteExpression(Offset))
2511 return true;
2512
Rafael Espindola25f492e2011-04-12 16:12:03 +00002513 getStreamer().EmitCFIRelOffset(Register, Offset);
2514 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002515}
2516
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002517static bool isValidEncoding(int64_t Encoding) {
2518 if (Encoding & ~0xff)
2519 return false;
2520
2521 if (Encoding == dwarf::DW_EH_PE_omit)
2522 return true;
2523
2524 const unsigned Format = Encoding & 0xf;
2525 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2526 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2527 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2528 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2529 return false;
2530
Rafael Espindolacaf11582010-12-29 04:31:26 +00002531 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002532 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002533 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002534 return false;
2535
2536 return true;
2537}
2538
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002539/// ParseDirectiveCFIPersonalityOrLsda
2540/// ::= .cfi_personality encoding, [symbol_name]
2541/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002542bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002543 SMLoc DirectiveLoc) {
2544 int64_t Encoding = 0;
2545 if (getParser().ParseAbsoluteExpression(Encoding))
2546 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002547 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002548 return false;
2549
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002550 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002551 return TokError("unsupported encoding.");
2552
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002553 if (getLexer().isNot(AsmToken::Comma))
2554 return TokError("unexpected token in directive");
2555 Lex();
2556
2557 StringRef Name;
2558 if (getParser().ParseIdentifier(Name))
2559 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002560
2561 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2562
2563 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002564 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002565 else {
2566 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002567 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002568 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002569 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002570}
2571
Rafael Espindolafe024d02010-12-28 18:36:23 +00002572/// ParseDirectiveCFIRememberState
2573/// ::= .cfi_remember_state
2574bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2575 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002576 getStreamer().EmitCFIRememberState();
2577 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002578}
2579
2580/// ParseDirectiveCFIRestoreState
2581/// ::= .cfi_remember_state
2582bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2583 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002584 getStreamer().EmitCFIRestoreState();
2585 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002586}
2587
Rafael Espindolac5754392011-04-12 15:31:05 +00002588/// ParseDirectiveCFISameValue
2589/// ::= .cfi_same_value register
2590bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2591 SMLoc DirectiveLoc) {
2592 int64_t Register = 0;
2593
2594 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2595 return true;
2596
2597 getStreamer().EmitCFISameValue(Register);
2598
2599 return false;
2600}
2601
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002602/// ParseDirectiveMacrosOnOff
2603/// ::= .macros_on
2604/// ::= .macros_off
2605bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2606 SMLoc DirectiveLoc) {
2607 if (getLexer().isNot(AsmToken::EndOfStatement))
2608 return Error(getLexer().getLoc(),
2609 "unexpected token in '" + Directive + "' directive");
2610
2611 getParser().MacrosEnabled = Directive == ".macros_on";
2612
2613 return false;
2614}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002615
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002616/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002617/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002618bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2619 SMLoc DirectiveLoc) {
2620 StringRef Name;
2621 if (getParser().ParseIdentifier(Name))
2622 return TokError("expected identifier in directive");
2623
Rafael Espindola65366442011-06-05 02:43:45 +00002624 std::vector<StringRef> Parameters;
2625 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2626 for(;;) {
2627 StringRef Parameter;
2628 if (getParser().ParseIdentifier(Parameter))
2629 return TokError("expected identifier in directive");
2630 Parameters.push_back(Parameter);
2631
2632 if (getLexer().isNot(AsmToken::Comma))
2633 break;
2634 Lex();
2635 }
2636 }
2637
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002638 if (getLexer().isNot(AsmToken::EndOfStatement))
2639 return TokError("unexpected token in '.macro' directive");
2640
2641 // Eat the end of statement.
2642 Lex();
2643
2644 AsmToken EndToken, StartToken = getTok();
2645
2646 // Lex the macro definition.
2647 for (;;) {
2648 // Check whether we have reached the end of the file.
2649 if (getLexer().is(AsmToken::Eof))
2650 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2651
2652 // Otherwise, check whether we have reach the .endmacro.
2653 if (getLexer().is(AsmToken::Identifier) &&
2654 (getTok().getIdentifier() == ".endm" ||
2655 getTok().getIdentifier() == ".endmacro")) {
2656 EndToken = getTok();
2657 Lex();
2658 if (getLexer().isNot(AsmToken::EndOfStatement))
2659 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2660 "' directive");
2661 break;
2662 }
2663
2664 // Otherwise, scan til the end of the statement.
2665 getParser().EatToEndOfStatement();
2666 }
2667
2668 if (getParser().MacroMap.lookup(Name)) {
2669 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2670 }
2671
2672 const char *BodyStart = StartToken.getLoc().getPointer();
2673 const char *BodyEnd = EndToken.getLoc().getPointer();
2674 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002675 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002676 return false;
2677}
2678
2679/// ParseDirectiveEndMacro
2680/// ::= .endm
2681/// ::= .endmacro
2682bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2683 SMLoc DirectiveLoc) {
2684 if (getLexer().isNot(AsmToken::EndOfStatement))
2685 return TokError("unexpected token in '" + Directive + "' directive");
2686
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002687 // If we are inside a macro instantiation, terminate the current
2688 // instantiation.
2689 if (!getParser().ActiveMacros.empty()) {
2690 getParser().HandleMacroExit();
2691 return false;
2692 }
2693
2694 // Otherwise, this .endmacro is a stray entry in the file; well formed
2695 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002696 return TokError("unexpected '" + Directive + "' in file, "
2697 "no current macro definition");
2698}
2699
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002700bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002701 getParser().CheckForValidSection();
2702
2703 const MCExpr *Value;
2704
2705 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002706 return true;
2707
2708 if (getLexer().isNot(AsmToken::EndOfStatement))
2709 return TokError("unexpected token in directive");
2710
2711 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002712 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002713 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002714 getStreamer().EmitULEB128Value(Value);
2715
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002716 return false;
2717}
2718
2719
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002720/// \brief Create an MCAsmParser instance.
2721MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2722 MCContext &C, MCStreamer &Out,
2723 const MCAsmInfo &MAI) {
2724 return new AsmParser(T, SM, C, Out, MAI);
2725}