blob: 4f55cea7bc5e26109f09802fefb5202014e05259 [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;
87 SourceMgr &SrcMgr;
88 MCAsmParserExtension *GenericParser;
89 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000090
Daniel Dunbaraef87e32010-07-18 18:31:38 +000091 /// This is the current buffer index we're lexing from as managed by the
92 /// SourceMgr object.
93 int CurBuffer;
94
95 AsmCond TheCondState;
96 std::vector<AsmCond> TheCondStack;
97
98 /// DirectiveMap - This is a table handlers for directives. Each handler is
99 /// invoked after the directive identifier is read and is responsible for
100 /// parsing and validating the rest of the directive. The handler is passed
101 /// in the directive name and the location of the directive keyword.
102 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000103
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000104 /// MacroMap - Map of currently defined macros.
105 StringMap<Macro*> MacroMap;
106
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000107 /// ActiveMacros - Stack of active macro instantiations.
108 std::vector<MacroInstantiation*> ActiveMacros;
109
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000110 /// Boolean tracking whether macro substitution is enabled.
111 unsigned MacrosEnabled : 1;
112
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000113 /// Flag tracking whether any errors have been encountered.
114 unsigned HadError : 1;
115
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000116public:
117 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
118 const MCAsmInfo &MAI);
119 ~AsmParser();
120
121 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
122
123 void AddDirectiveHandler(MCAsmParserExtension *Object,
124 StringRef Directive,
125 DirectiveHandler Handler) {
126 DirectiveMap[Directive] = std::make_pair(Object, Handler);
127 }
128
129public:
130 /// @name MCAsmParser Interface
131 /// {
132
133 virtual SourceMgr &getSourceManager() { return SrcMgr; }
134 virtual MCAsmLexer &getLexer() { return Lexer; }
135 virtual MCContext &getContext() { return Ctx; }
136 virtual MCStreamer &getStreamer() { return Out; }
137
Jim Grosbach6c3044d2011-06-14 21:51:20 +0000138 virtual bool Warning(SMLoc L, const Twine &Meg);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000139 virtual bool Error(SMLoc L, const Twine &Msg);
140
141 const AsmToken &Lex();
142
143 bool ParseExpression(const MCExpr *&Res);
144 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
145 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
146 virtual bool ParseAbsoluteExpression(int64_t &Res);
147
148 /// }
149
150private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000151 void CheckForValidSection();
152
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000153 bool ParseStatement();
154
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000155 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000156 bool expandMacro(SmallString<256> &Buf, StringRef Body,
157 const std::vector<StringRef> &Parameters,
158 const std::vector<std::vector<AsmToken> > &A,
159 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000160 void HandleMacroExit();
161
162 void PrintMacroInstantiations();
Jim Grosbach6c3044d2011-06-14 21:51:20 +0000163 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
164 SrcMgr.PrintMessage(Loc, Msg, Type);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000165 }
166
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000167 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
168 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000169
170 /// \brief Reset the current lexer position to that given by \arg Loc. The
171 /// current token is not set; clients should ensure Lex() is called
172 /// subsequently.
173 void JumpToLoc(SMLoc Loc);
174
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000175 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000176
177 /// \brief Parse up to the end of statement and a return the contents from the
178 /// current token until the end of the statement; the current token on exit
179 /// will be either the EndOfStatement or EOF.
180 StringRef ParseStringToEndOfStatement();
181
Nico Weber4c4c7322011-01-28 03:04:41 +0000182 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183
184 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
185 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
186 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000187 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188
189 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
190 /// and set \arg Res to the identifier contents.
191 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000192
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000193 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000194
195 // ".ascii", ".asciiz", ".string"
196 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000198 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199 bool ParseDirectiveFill(); // ".fill"
200 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000201 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000202 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000203 bool ParseDirectiveOrg(); // ".org"
204 // ".align{,32}", ".p2align{,w,l}"
205 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
206
207 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
208 /// accepts a single symbol (which should be a label or an external).
209 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210
211 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
212
213 bool ParseDirectiveAbort(); // ".abort"
214 bool ParseDirectiveInclude(); // ".include"
215
216 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000217 // ".ifdef" or ".ifndef", depending on expect_defined
218 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000219 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
220 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
221 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
222
223 /// ParseEscapedString - Parse the current token as a string which may include
224 /// escaped characters and return the string contents.
225 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000226
227 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
228 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000229};
230
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000231/// \brief Generic implementations of directive handling, etc. which is shared
232/// (or the default, at least) for all assembler parser.
233class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000234 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
235 void AddDirectiveHandler(StringRef Directive) {
236 getParser().AddDirectiveHandler(this, Directive,
237 HandleDirective<GenericAsmParser, Handler>);
238 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000239public:
240 GenericAsmParser() {}
241
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000242 AsmParser &getParser() {
243 return (AsmParser&) this->MCAsmParserExtension::getParser();
244 }
245
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000246 virtual void Initialize(MCAsmParser &Parser) {
247 // Call the base implementation.
248 this->MCAsmParserExtension::Initialize(Parser);
249
250 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
252 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
253 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000255
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000256 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000257 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
258 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
260 ".cfi_startproc");
261 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
262 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
264 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
266 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
268 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
270 ".cfi_def_cfa_register");
271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
272 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
274 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000275 AddDirectiveHandler<
276 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
277 AddDirectiveHandler<
278 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000279 AddDirectiveHandler<
280 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
281 AddDirectiveHandler<
282 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000283 AddDirectiveHandler<
284 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000285
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000286 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000287 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
288 ".macros_on");
289 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
290 ".macros_off");
291 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000294
295 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000297 }
298
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000299 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
300
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000301 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
302 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
303 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000304 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000305 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000306 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
307 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000308 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000309 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000310 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000311 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
312 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000313 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000314 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000315 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
316 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000317 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000318
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000319 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000320 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
321 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000322
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000323 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000324};
325
326}
327
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000328namespace llvm {
329
330extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000331extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000332extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000333
334}
335
Chris Lattneraaec2052010-01-19 19:46:13 +0000336enum { DEFAULT_ADDRSPACE = 0 };
337
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000338AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
339 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbach6c3044d2011-06-14 21:51:20 +0000340 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000341 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000342 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000343 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000344
345 // Initialize the generic parser.
346 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000347
348 // Initialize the platform / file format parser.
349 //
350 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
351 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000352 if (_MAI.hasMicrosoftFastStdCallMangling()) {
353 PlatformParser = createCOFFAsmParser();
354 PlatformParser->Initialize(*this);
355 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000356 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000357 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000358 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000359 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000360 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000361 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000362}
363
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000364AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000365 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
366
367 // Destroy any macros.
368 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
369 ie = MacroMap.end(); it != ie; ++it)
370 delete it->getValue();
371
Daniel Dunbare4749702010-07-12 18:12:02 +0000372 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000373 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000374}
375
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000376void AsmParser::PrintMacroInstantiations() {
377 // Print the active macro instantiation stack.
378 for (std::vector<MacroInstantiation*>::const_reverse_iterator
379 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
380 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
381 "note");
382}
383
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000384bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
385 if (FatalAssemblerWarnings)
386 return Error(L, Msg);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000387 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000388 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000389 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000390}
391
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000392bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000393 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000394 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000395 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000396 return true;
397}
398
Sean Callananfd0b0282010-01-21 00:19:58 +0000399bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000400 std::string IncludedFile;
401 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000402 if (NewBuf == -1)
403 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000404
Sean Callananfd0b0282010-01-21 00:19:58 +0000405 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000406
Sean Callananfd0b0282010-01-21 00:19:58 +0000407 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000408
Sean Callananfd0b0282010-01-21 00:19:58 +0000409 return false;
410}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000411
412void AsmParser::JumpToLoc(SMLoc Loc) {
413 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
414 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
415}
416
Sean Callananfd0b0282010-01-21 00:19:58 +0000417const AsmToken &AsmParser::Lex() {
418 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000419
Sean Callananfd0b0282010-01-21 00:19:58 +0000420 if (tok->is(AsmToken::Eof)) {
421 // If this is the end of an included file, pop the parent file off the
422 // include stack.
423 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
424 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000425 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000426 tok = &Lexer.Lex();
427 }
428 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000429
Sean Callananfd0b0282010-01-21 00:19:58 +0000430 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000431 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000432
Sean Callananfd0b0282010-01-21 00:19:58 +0000433 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000434}
435
Chris Lattner79180e22010-04-05 23:15:42 +0000436bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000437 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000438 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000439 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000440
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000441 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000442 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000443
444 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000445 AsmCond StartingCondState = TheCondState;
446
Chris Lattnerb717fb02009-07-02 21:53:43 +0000447 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000449 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000450
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000451 // We had an error, validate that one was emitted and recover by skipping to
452 // the next line.
453 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000454 EatToEndOfStatement();
455 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000456
457 if (TheCondState.TheCond != StartingCondState.TheCond ||
458 TheCondState.Ignore != StartingCondState.Ignore)
459 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000460
461 // Check to see there are no empty DwarfFile slots.
462 const std::vector<MCDwarfFile *> &MCDwarfFiles =
463 getContext().getMCDwarfFiles();
464 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000465 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000466 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000467 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000468
Chris Lattner79180e22010-04-05 23:15:42 +0000469 // Finalize the output stream if there are no errors and if the client wants
470 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000471 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000472 Out.Finish();
473
Chris Lattnerb717fb02009-07-02 21:53:43 +0000474 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000475}
476
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000477void AsmParser::CheckForValidSection() {
478 if (!getStreamer().getCurrentSection()) {
479 TokError("expected section directive before assembly directive");
480 Out.SwitchSection(Ctx.getMachOSection(
481 "__TEXT", "__text",
482 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
483 0, SectionKind::getText()));
484 }
485}
486
Chris Lattner2cf5f142009-06-22 01:29:09 +0000487/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
488void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000489 while (Lexer.isNot(AsmToken::EndOfStatement) &&
490 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000491 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000492
Chris Lattner2cf5f142009-06-22 01:29:09 +0000493 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000494 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000495 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000496}
497
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000498StringRef AsmParser::ParseStringToEndOfStatement() {
499 const char *Start = getTok().getLoc().getPointer();
500
501 while (Lexer.isNot(AsmToken::EndOfStatement) &&
502 Lexer.isNot(AsmToken::Eof))
503 Lex();
504
505 const char *End = getTok().getLoc().getPointer();
506 return StringRef(Start, End - Start);
507}
Chris Lattnerc4193832009-06-22 05:51:26 +0000508
Chris Lattner74ec1a32009-06-22 06:32:03 +0000509/// ParseParenExpr - Parse a paren expression and return it.
510/// NOTE: This assumes the leading '(' has already been consumed.
511///
512/// parenexpr ::= expr)
513///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000514bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000515 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000516 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000517 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000518 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000519 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000520 return false;
521}
Chris Lattnerc4193832009-06-22 05:51:26 +0000522
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000523/// ParseBracketExpr - Parse a bracket expression and return it.
524/// NOTE: This assumes the leading '[' has already been consumed.
525///
526/// bracketexpr ::= expr]
527///
528bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
529 if (ParseExpression(Res)) return true;
530 if (Lexer.isNot(AsmToken::RBrac))
531 return TokError("expected ']' in brackets expression");
532 EndLoc = Lexer.getLoc();
533 Lex();
534 return false;
535}
536
Chris Lattner74ec1a32009-06-22 06:32:03 +0000537/// ParsePrimaryExpr - Parse a primary expression and return it.
538/// primaryexpr ::= (parenexpr
539/// primaryexpr ::= symbol
540/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000541/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000542/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000543bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000544 switch (Lexer.getKind()) {
545 default:
546 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000547 // If we have an error assume that we've already handled it.
548 case AsmToken::Error:
549 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000550 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000551 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000552 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000553 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000554 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000555 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000556 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000557 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000558 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000559 EndLoc = Lexer.getLoc();
560
561 StringRef Identifier;
562 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000563 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000564
Daniel Dunbarfffff912009-10-16 01:34:54 +0000565 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000566 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000567 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000568
569 // Lookup the symbol variant if used.
570 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000571 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000572 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000573 if (Variant == MCSymbolRefExpr::VK_Invalid) {
574 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000575 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000576 }
577 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000578
Daniel Dunbarfffff912009-10-16 01:34:54 +0000579 // If this is an absolute variable reference, substitute it now to preserve
580 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000581 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000582 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000583 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000584
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000585 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000586 return false;
587 }
588
589 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000590 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000591 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000592 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000593 case AsmToken::Integer: {
594 SMLoc Loc = getTok().getLoc();
595 int64_t IntVal = getTok().getIntVal();
596 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000597 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000598 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000599 // Look for 'b' or 'f' following an Integer as a directional label
600 if (Lexer.getKind() == AsmToken::Identifier) {
601 StringRef IDVal = getTok().getString();
602 if (IDVal == "f" || IDVal == "b"){
603 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
604 IDVal == "f" ? 1 : 0);
605 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
606 getContext());
607 if(IDVal == "b" && Sym->isUndefined())
608 return Error(Loc, "invalid reference to undefined symbol");
609 EndLoc = Lexer.getLoc();
610 Lex(); // Eat identifier.
611 }
612 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000613 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000614 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000615 case AsmToken::Real: {
616 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000617 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000618 Res = MCConstantExpr::Create(IntVal, getContext());
619 Lex(); // Eat token.
620 return false;
621 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000622 case AsmToken::Dot: {
623 // This is a '.' reference, which references the current PC. Emit a
624 // temporary label to the streamer and refer to it.
625 MCSymbol *Sym = Ctx.CreateTempSymbol();
626 Out.EmitLabel(Sym);
627 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
628 EndLoc = Lexer.getLoc();
629 Lex(); // Eat identifier.
630 return false;
631 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000632 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000633 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000634 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000635 case AsmToken::LBrac:
636 if (!PlatformParser->HasBracketExpressions())
637 return TokError("brackets expression not supported on this target");
638 Lex(); // Eat the '['.
639 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000640 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000641 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000642 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000644 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000645 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000646 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000647 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000648 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000650 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000651 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000652 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000653 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000654 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000655 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000656 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000657 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000658 }
659}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000660
Chris Lattnerb4307b32010-01-15 19:28:38 +0000661bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000662 SMLoc EndLoc;
663 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000664}
665
Daniel Dunbarcceba832010-09-17 02:47:07 +0000666const MCExpr *
667AsmParser::ApplyModifierToExpr(const MCExpr *E,
668 MCSymbolRefExpr::VariantKind Variant) {
669 // Recurse over the given expression, rebuilding it to apply the given variant
670 // if there is exactly one symbol.
671 switch (E->getKind()) {
672 case MCExpr::Target:
673 case MCExpr::Constant:
674 return 0;
675
676 case MCExpr::SymbolRef: {
677 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
678
679 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
680 TokError("invalid variant on expression '" +
681 getTok().getIdentifier() + "' (already modified)");
682 return E;
683 }
684
685 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
686 }
687
688 case MCExpr::Unary: {
689 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
690 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
691 if (!Sub)
692 return 0;
693 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
694 }
695
696 case MCExpr::Binary: {
697 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
698 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
699 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
700
701 if (!LHS && !RHS)
702 return 0;
703
704 if (!LHS) LHS = BE->getLHS();
705 if (!RHS) RHS = BE->getRHS();
706
707 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
708 }
709 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000710
711 assert(0 && "Invalid expression kind!");
712 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000713}
714
Chris Lattner74ec1a32009-06-22 06:32:03 +0000715/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000716///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000717/// expr ::= expr +,- expr -> lowest.
718/// expr ::= expr |,^,&,! expr -> middle.
719/// expr ::= expr *,/,%,<<,>> expr -> highest.
720/// expr ::= primaryexpr
721///
Chris Lattner54482b42010-01-15 19:39:23 +0000722bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000723 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000724 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000725 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
726 return true;
727
Daniel Dunbarcceba832010-09-17 02:47:07 +0000728 // As a special case, we support 'a op b @ modifier' by rewriting the
729 // expression to include the modifier. This is inefficient, but in general we
730 // expect users to use 'a@modifier op b'.
731 if (Lexer.getKind() == AsmToken::At) {
732 Lex();
733
734 if (Lexer.isNot(AsmToken::Identifier))
735 return TokError("unexpected symbol modifier following '@'");
736
737 MCSymbolRefExpr::VariantKind Variant =
738 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
739 if (Variant == MCSymbolRefExpr::VK_Invalid)
740 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
741
742 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
743 if (!ModifiedRes) {
744 return TokError("invalid modifier '" + getTok().getIdentifier() +
745 "' (no symbols present)");
746 return true;
747 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000748
Daniel Dunbarcceba832010-09-17 02:47:07 +0000749 Res = ModifiedRes;
750 Lex();
751 }
752
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000753 // Try to constant fold it up front, if possible.
754 int64_t Value;
755 if (Res->EvaluateAsAbsolute(Value))
756 Res = MCConstantExpr::Create(Value, getContext());
757
758 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000759}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000760
Chris Lattnerb4307b32010-01-15 19:28:38 +0000761bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000762 Res = 0;
763 return ParseParenExpr(Res, EndLoc) ||
764 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000765}
766
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000768 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000769
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000770 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000771 if (ParseExpression(Expr))
772 return true;
773
Daniel Dunbare00b0112009-10-16 01:57:52 +0000774 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000775 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000776
777 return false;
778}
779
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000780static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000781 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000782 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000783 default:
784 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000785
Daniel Dunbarcceba832010-09-17 02:47:07 +0000786 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000788 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000789 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000791 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000792 return 1;
793
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000794
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000795 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 //
797 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000798 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000799 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000800 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000801 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000802 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000803 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000804 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000805 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000806 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000807
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000808 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000809 case AsmToken::EqualEqual:
810 Kind = MCBinaryExpr::EQ;
811 return 3;
812 case AsmToken::ExclaimEqual:
813 case AsmToken::LessGreater:
814 Kind = MCBinaryExpr::NE;
815 return 3;
816 case AsmToken::Less:
817 Kind = MCBinaryExpr::LT;
818 return 3;
819 case AsmToken::LessEqual:
820 Kind = MCBinaryExpr::LTE;
821 return 3;
822 case AsmToken::Greater:
823 Kind = MCBinaryExpr::GT;
824 return 3;
825 case AsmToken::GreaterEqual:
826 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000827 return 3;
828
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000829 // High Intermediate Precedence: +, -
830 case AsmToken::Plus:
831 Kind = MCBinaryExpr::Add;
832 return 4;
833 case AsmToken::Minus:
834 Kind = MCBinaryExpr::Sub;
835 return 4;
836
Daniel Dunbar475839e2009-06-29 20:37:27 +0000837 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000838 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000839 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000840 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000841 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000842 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000843 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000844 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000845 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000846 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000847 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000848 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000849 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000850 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000851 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000852 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000853 }
854}
855
856
857/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
858/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000859bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
860 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000861 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000862 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000863 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000864
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000865 // If the next token is lower precedence than we are allowed to eat, return
866 // successfully with what we ate already.
867 if (TokPrec < Precedence)
868 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000869
Sean Callanan79ed1a82010-01-19 20:22:31 +0000870 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000871
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000872 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000873 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000874 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000875
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000876 // If BinOp binds less tightly with RHS than the operator after RHS, let
877 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000878 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000879 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000880 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000881 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000882 }
883
Daniel Dunbar475839e2009-06-29 20:37:27 +0000884 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000885 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000886 }
887}
888
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000889
890
891
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000892/// ParseStatement:
893/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000894/// ::= Label* Directive ...Operands... EndOfStatement
895/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000896bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000897 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000898 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000899 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000900 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000901 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000902
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000903 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000904 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000905 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000906 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000907 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000908 // A full line comment is a '#' as the first token.
909 if (Lexer.is(AsmToken::Hash)) {
910 EatToEndOfStatement();
911 return false;
912 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000913
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000914 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000915 if (Lexer.is(AsmToken::Integer)) {
916 LocalLabelVal = getTok().getIntVal();
917 if (LocalLabelVal < 0) {
918 if (!TheCondState.Ignore)
919 return TokError("unexpected token at start of statement");
920 IDVal = "";
921 }
922 else {
923 IDVal = getTok().getString();
924 Lex(); // Consume the integer token to be used as an identifier token.
925 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000926 if (!TheCondState.Ignore)
927 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000928 }
929 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000930
931 } else if (Lexer.is(AsmToken::Dot)) {
932 // Treat '.' as a valid identifier in this context.
933 Lex();
934 IDVal = ".";
935
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000936 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000937 if (!TheCondState.Ignore)
938 return TokError("unexpected token at start of statement");
939 IDVal = "";
940 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000941
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000942
Chris Lattner7834fac2010-04-17 18:14:27 +0000943 // Handle conditional assembly here before checking for skipping. We
944 // have to do this so that .endif isn't skipped in a ".if 0" block for
945 // example.
946 if (IDVal == ".if")
947 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000948 if (IDVal == ".ifdef")
949 return ParseDirectiveIfdef(IDLoc, true);
950 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
951 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000952 if (IDVal == ".elseif")
953 return ParseDirectiveElseIf(IDLoc);
954 if (IDVal == ".else")
955 return ParseDirectiveElse(IDLoc);
956 if (IDVal == ".endif")
957 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000958
Chris Lattner7834fac2010-04-17 18:14:27 +0000959 // If we are in a ".if 0" block, ignore this statement.
960 if (TheCondState.Ignore) {
961 EatToEndOfStatement();
962 return false;
963 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000964
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000965 // FIXME: Recurse on local labels?
966
967 // See what kind of statement we have.
968 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000969 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000970 CheckForValidSection();
971
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000972 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000973 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000974
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000975 // Diagnose attempt to use '.' as a label.
976 if (IDVal == ".")
977 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
978
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000979 // Diagnose attempt to use a variable as a label.
980 //
981 // FIXME: Diagnostics. Note the location of the definition as a label.
982 // FIXME: This doesn't diagnose assignment to a symbol which has been
983 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000984 MCSymbol *Sym;
985 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000986 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000987 else
988 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000989 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000990 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000991
Daniel Dunbar959fd882009-08-26 22:13:22 +0000992 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000993 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000994
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000995 // Consume any end of statement token, if present, to avoid spurious
996 // AddBlankLine calls().
997 if (Lexer.is(AsmToken::EndOfStatement)) {
998 Lex();
999 if (Lexer.is(AsmToken::Eof))
1000 return false;
1001 }
1002
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001003 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001004 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001005
Daniel Dunbar3f872332009-07-28 16:08:33 +00001006 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001007 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001008 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001009
Nico Weber4c4c7322011-01-28 03:04:41 +00001010 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001011
1012 default: // Normal instruction or directive.
1013 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001014 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001015
1016 // If macros are enabled, check to see if this is a macro instantiation.
1017 if (MacrosEnabled)
1018 if (const Macro *M = MacroMap.lookup(IDVal))
1019 return HandleMacroEntry(IDVal, IDLoc, M);
1020
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001021 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001022 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001023 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001024 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001025 return ParseDirectiveSet(IDVal, true);
1026 if (IDVal == ".equiv")
1027 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001028
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 // Data directives
1030
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001032 return ParseDirectiveAscii(IDVal, false);
1033 if (IDVal == ".asciz" || IDVal == ".string")
1034 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001035
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001036 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001038 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001039 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001040 if (IDVal == ".value")
1041 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001042 if (IDVal == ".2byte")
1043 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001044 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001046 if (IDVal == ".int")
1047 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001048 if (IDVal == ".4byte")
1049 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001050 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001052 if (IDVal == ".8byte")
1053 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001054 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001055 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1056 if (IDVal == ".double")
1057 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001058
Eli Friedman5d68ec22010-07-19 04:17:25 +00001059 if (IDVal == ".align") {
1060 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1061 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1062 }
1063 if (IDVal == ".align32") {
1064 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1065 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1066 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001067 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001068 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001069 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001070 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001071 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001072 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001073 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001074 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001075 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001076 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001077 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001078 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1079
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001080 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001081 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001082
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001083 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001084 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001085 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001087 if (IDVal == ".zero")
1088 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001089
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001090 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001091
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001092 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001093 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001094 // ELF only? Should it be here?
1095 if (IDVal == ".local")
1096 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001098 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001099 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001100 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001101 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001102 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001103 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001104 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001105 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001106 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001107 if (IDVal == ".symbol_resolver")
1108 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001110 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001112 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001113 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001114 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001115 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001116 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001117 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001118 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001119 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001120 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001121 if (IDVal == ".weak_def_can_be_hidden")
1122 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001123
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001125 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001126 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001127 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001128
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001129 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001130 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001131 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001132 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001133
Roman Divackybb6d14f2011-01-31 21:19:43 +00001134 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001135 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001136
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001137 // Look up the handler in the handler table.
1138 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1139 DirectiveMap.lookup(IDVal);
1140 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001141 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001142
Kevin Enderby9c656452009-09-10 20:51:44 +00001143 // Target hook for parsing target specific directives.
1144 if (!getTargetParser().ParseDirective(ID))
1145 return false;
1146
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001147 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001148 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001149 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001150 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001151
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001152 CheckForValidSection();
1153
Chris Lattnera7f13542010-05-19 23:34:33 +00001154 // Canonicalize the opcode to lower case.
1155 SmallString<128> Opcode;
1156 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1157 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001158
Chris Lattner98986712010-01-14 22:21:20 +00001159 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001160 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001161 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001162
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001163 // Dump the parsed representation, if requested.
1164 if (getShowParsedOperands()) {
1165 SmallString<256> Str;
1166 raw_svector_ostream OS(Str);
1167 OS << "parsed instruction: [";
1168 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1169 if (i != 0)
1170 OS << ", ";
1171 ParsedOperands[i]->dump(OS);
1172 }
1173 OS << "]";
1174
1175 PrintMessage(IDLoc, OS.str(), "note");
1176 }
1177
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001178 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001179 if (!HadError)
1180 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1181 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001182
Chris Lattner98986712010-01-14 22:21:20 +00001183 // Free any parsed operands.
1184 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1185 delete ParsedOperands[i];
1186
Chris Lattnercbf8a982010-09-11 16:18:25 +00001187 // Don't skip the rest of the line, the instruction parser is responsible for
1188 // that.
1189 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001190}
Chris Lattner9a023f72009-06-24 04:43:34 +00001191
Rafael Espindola65366442011-06-05 02:43:45 +00001192bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1193 const std::vector<StringRef> &Parameters,
1194 const std::vector<std::vector<AsmToken> > &A,
1195 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001196 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001197 unsigned NParameters = Parameters.size();
1198 if (NParameters != 0 && NParameters != A.size())
1199 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001200
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001201 while (!Body.empty()) {
1202 // Scan for the next substitution.
1203 std::size_t End = Body.size(), Pos = 0;
1204 for (; Pos != End; ++Pos) {
1205 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001206 if (!NParameters) {
1207 // This macro has no parameters, look for $0, $1, etc.
1208 if (Body[Pos] != '$' || Pos + 1 == End)
1209 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001210
Rafael Espindola65366442011-06-05 02:43:45 +00001211 char Next = Body[Pos + 1];
1212 if (Next == '$' || Next == 'n' || isdigit(Next))
1213 break;
1214 } else {
1215 // This macro has parameters, look for \foo, \bar, etc.
1216 if (Body[Pos] == '\\' && Pos + 1 != End)
1217 break;
1218 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001219 }
1220
1221 // Add the prefix.
1222 OS << Body.slice(0, Pos);
1223
1224 // Check if we reached the end.
1225 if (Pos == End)
1226 break;
1227
Rafael Espindola65366442011-06-05 02:43:45 +00001228 if (!NParameters) {
1229 switch (Body[Pos+1]) {
1230 // $$ => $
1231 case '$':
1232 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001233 break;
1234
Rafael Espindola65366442011-06-05 02:43:45 +00001235 // $n => number of arguments
1236 case 'n':
1237 OS << A.size();
1238 break;
1239
1240 // $[0-9] => argument
1241 default: {
1242 // Missing arguments are ignored.
1243 unsigned Index = Body[Pos+1] - '0';
1244 if (Index >= A.size())
1245 break;
1246
1247 // Otherwise substitute with the token values, with spaces eliminated.
1248 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1249 ie = A[Index].end(); it != ie; ++it)
1250 OS << it->getString();
1251 break;
1252 }
1253 }
1254 Pos += 2;
1255 } else {
1256 unsigned I = Pos + 1;
1257 while (isalnum(Body[I]) && I + 1 != End)
1258 ++I;
1259
1260 const char *Begin = Body.data() + Pos +1;
1261 StringRef Argument(Begin, I - (Pos +1));
1262 unsigned Index = 0;
1263 for (; Index < NParameters; ++Index)
1264 if (Parameters[Index] == Argument)
1265 break;
1266
1267 // FIXME: We should error at the macro definition.
1268 if (Index == NParameters)
1269 return Error(L, "Parameter not found");
1270
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001271 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1272 ie = A[Index].end(); it != ie; ++it)
1273 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001274
Rafael Espindola65366442011-06-05 02:43:45 +00001275 Pos += 1 + Argument.size();
1276 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001277 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001278 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001279 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001280
1281 // We include the .endmacro in the buffer as our queue to exit the macro
1282 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001283 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001284 return false;
1285}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001286
Rafael Espindola65366442011-06-05 02:43:45 +00001287MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1288 MemoryBuffer *I)
1289 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1290{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001291}
1292
1293bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1294 const Macro *M) {
1295 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1296 // this, although we should protect against infinite loops.
1297 if (ActiveMacros.size() == 20)
1298 return TokError("macros cannot be nested more than 20 levels deep");
1299
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001300 // Parse the macro instantiation arguments.
1301 std::vector<std::vector<AsmToken> > MacroArguments;
1302 MacroArguments.push_back(std::vector<AsmToken>());
1303 unsigned ParenLevel = 0;
1304 for (;;) {
1305 if (Lexer.is(AsmToken::Eof))
1306 return TokError("unexpected token in macro instantiation");
1307 if (Lexer.is(AsmToken::EndOfStatement))
1308 break;
1309
1310 // If we aren't inside parentheses and this is a comma, start a new token
1311 // list.
1312 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1313 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001314 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001315 // Adjust the current parentheses level.
1316 if (Lexer.is(AsmToken::LParen))
1317 ++ParenLevel;
1318 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1319 --ParenLevel;
1320
1321 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001322 MacroArguments.back().push_back(getTok());
1323 }
1324 Lex();
1325 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001326
Rafael Espindola65366442011-06-05 02:43:45 +00001327 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1328 // to hold the macro body with substitutions.
1329 SmallString<256> Buf;
1330 StringRef Body = M->Body;
1331
1332 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1333 return true;
1334
1335 MemoryBuffer *Instantiation =
1336 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1337
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001338 // Create the macro instantiation object and add to the current macro
1339 // instantiation stack.
1340 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001341 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001342 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001343 ActiveMacros.push_back(MI);
1344
1345 // Jump to the macro instantiation and prime the lexer.
1346 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1347 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1348 Lex();
1349
1350 return false;
1351}
1352
1353void AsmParser::HandleMacroExit() {
1354 // Jump to the EndOfStatement we should return to, and consume it.
1355 JumpToLoc(ActiveMacros.back()->ExitLoc);
1356 Lex();
1357
1358 // Pop the instantiation entry.
1359 delete ActiveMacros.back();
1360 ActiveMacros.pop_back();
1361}
1362
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001363static void MarkUsed(const MCExpr *Value) {
1364 switch (Value->getKind()) {
1365 case MCExpr::Binary:
1366 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1367 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1368 break;
1369 case MCExpr::Target:
1370 case MCExpr::Constant:
1371 break;
1372 case MCExpr::SymbolRef: {
1373 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1374 break;
1375 }
1376 case MCExpr::Unary:
1377 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1378 break;
1379 }
1380}
1381
Nico Weber4c4c7322011-01-28 03:04:41 +00001382bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001383 // FIXME: Use better location, we should use proper tokens.
1384 SMLoc EqualLoc = Lexer.getLoc();
1385
Daniel Dunbar821e3332009-08-31 08:09:28 +00001386 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001387 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001388 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001389
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001390 MarkUsed(Value);
1391
Daniel Dunbar3f872332009-07-28 16:08:33 +00001392 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001393 return TokError("unexpected token in assignment");
1394
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001395 // Error on assignment to '.'.
1396 if (Name == ".") {
1397 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1398 "(use '.space' or '.org').)"));
1399 }
1400
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001401 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001402 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001403
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001404 // Validate that the LHS is allowed to be a variable (either it has not been
1405 // used as a symbol, or it is an absolute symbol).
1406 MCSymbol *Sym = getContext().LookupSymbol(Name);
1407 if (Sym) {
1408 // Diagnose assignment to a label.
1409 //
1410 // FIXME: Diagnostics. Note the location of the definition as a label.
1411 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001412 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001413 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001414 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001415 return Error(EqualLoc, "redefinition of '" + Name + "'");
1416 else if (!Sym->isVariable())
1417 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001418 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001419 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1420 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001421
1422 // Don't count these checks as uses.
1423 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001424 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001425 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001426
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001427 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001428
1429 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001430 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001431
1432 return false;
1433}
1434
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001435/// ParseIdentifier:
1436/// ::= identifier
1437/// ::= string
1438bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001439 // The assembler has relaxed rules for accepting identifiers, in particular we
1440 // allow things like '.globl $foo', which would normally be separate
1441 // tokens. At this level, we have already lexed so we cannot (currently)
1442 // handle this as a context dependent token, instead we detect adjacent tokens
1443 // and return the combined identifier.
1444 if (Lexer.is(AsmToken::Dollar)) {
1445 SMLoc DollarLoc = getLexer().getLoc();
1446
1447 // Consume the dollar sign, and check for a following identifier.
1448 Lex();
1449 if (Lexer.isNot(AsmToken::Identifier))
1450 return true;
1451
1452 // We have a '$' followed by an identifier, make sure they are adjacent.
1453 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1454 return true;
1455
1456 // Construct the joined identifier and consume the token.
1457 Res = StringRef(DollarLoc.getPointer(),
1458 getTok().getIdentifier().size() + 1);
1459 Lex();
1460 return false;
1461 }
1462
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001463 if (Lexer.isNot(AsmToken::Identifier) &&
1464 Lexer.isNot(AsmToken::String))
1465 return true;
1466
Sean Callanan18b83232010-01-19 21:44:56 +00001467 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001468
Sean Callanan79ed1a82010-01-19 20:22:31 +00001469 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001470
1471 return false;
1472}
1473
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001474/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001475/// ::= .equ identifier ',' expression
1476/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001477/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001478bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001479 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001480
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001481 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001482 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001483
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001484 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001485 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001486 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001487
Nico Weber4c4c7322011-01-28 03:04:41 +00001488 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001489}
1490
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001491bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001492 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001493
1494 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001495 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001496 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1497 if (Str[i] != '\\') {
1498 Data += Str[i];
1499 continue;
1500 }
1501
1502 // Recognize escaped characters. Note that this escape semantics currently
1503 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1504 ++i;
1505 if (i == e)
1506 return TokError("unexpected backslash at end of string");
1507
1508 // Recognize octal sequences.
1509 if ((unsigned) (Str[i] - '0') <= 7) {
1510 // Consume up to three octal characters.
1511 unsigned Value = Str[i] - '0';
1512
1513 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1514 ++i;
1515 Value = Value * 8 + (Str[i] - '0');
1516
1517 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1518 ++i;
1519 Value = Value * 8 + (Str[i] - '0');
1520 }
1521 }
1522
1523 if (Value > 255)
1524 return TokError("invalid octal escape sequence (out of range)");
1525
1526 Data += (unsigned char) Value;
1527 continue;
1528 }
1529
1530 // Otherwise recognize individual escapes.
1531 switch (Str[i]) {
1532 default:
1533 // Just reject invalid escape sequences for now.
1534 return TokError("invalid escape sequence (unrecognized character)");
1535
1536 case 'b': Data += '\b'; break;
1537 case 'f': Data += '\f'; break;
1538 case 'n': Data += '\n'; break;
1539 case 'r': Data += '\r'; break;
1540 case 't': Data += '\t'; break;
1541 case '"': Data += '"'; break;
1542 case '\\': Data += '\\'; break;
1543 }
1544 }
1545
1546 return false;
1547}
1548
Daniel Dunbara0d14262009-06-24 23:30:00 +00001549/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001550/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1551bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001552 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001553 CheckForValidSection();
1554
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001556 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001557 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001558
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001559 std::string Data;
1560 if (ParseEscapedString(Data))
1561 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001562
1563 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001564 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001565 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1566
Sean Callanan79ed1a82010-01-19 20:22:31 +00001567 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001568
1569 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001570 break;
1571
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001572 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001573 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001574 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001575 }
1576 }
1577
Sean Callanan79ed1a82010-01-19 20:22:31 +00001578 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001579 return false;
1580}
1581
1582/// ParseDirectiveValue
1583/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1584bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001585 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001586 CheckForValidSection();
1587
Daniel Dunbara0d14262009-06-24 23:30:00 +00001588 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001589 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001590 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001591 return true;
1592
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001593 // Special case constant expressions to match code generator.
1594 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001595 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001596 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001597 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001598
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001599 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001600 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001601
Daniel Dunbara0d14262009-06-24 23:30:00 +00001602 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001603 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001604 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001605 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001606 }
1607 }
1608
Sean Callanan79ed1a82010-01-19 20:22:31 +00001609 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001610 return false;
1611}
1612
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001613/// ParseDirectiveRealValue
1614/// ::= (.single | .double) [ expression (, expression)* ]
1615bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1616 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1617 CheckForValidSection();
1618
1619 for (;;) {
1620 // We don't truly support arithmetic on floating point expressions, so we
1621 // have to manually parse unary prefixes.
1622 bool IsNeg = false;
1623 if (getLexer().is(AsmToken::Minus)) {
1624 Lex();
1625 IsNeg = true;
1626 } else if (getLexer().is(AsmToken::Plus))
1627 Lex();
1628
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001629 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001630 getLexer().isNot(AsmToken::Real) &&
1631 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001632 return TokError("unexpected token in directive");
1633
1634 // Convert to an APFloat.
1635 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001636 StringRef IDVal = getTok().getString();
1637 if (getLexer().is(AsmToken::Identifier)) {
1638 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1639 Value = APFloat::getInf(Semantics);
1640 else if (!IDVal.compare_lower("nan"))
1641 Value = APFloat::getNaN(Semantics, false, ~0);
1642 else
1643 return TokError("invalid floating point literal");
1644 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001645 APFloat::opInvalidOp)
1646 return TokError("invalid floating point literal");
1647 if (IsNeg)
1648 Value.changeSign();
1649
1650 // Consume the numeric token.
1651 Lex();
1652
1653 // Emit the value as an integer.
1654 APInt AsInt = Value.bitcastToAPInt();
1655 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1656 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1657
1658 if (getLexer().is(AsmToken::EndOfStatement))
1659 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001660
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001661 if (getLexer().isNot(AsmToken::Comma))
1662 return TokError("unexpected token in directive");
1663 Lex();
1664 }
1665 }
1666
1667 Lex();
1668 return false;
1669}
1670
Daniel Dunbara0d14262009-06-24 23:30:00 +00001671/// ParseDirectiveSpace
1672/// ::= .space expression [ , expression ]
1673bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001674 CheckForValidSection();
1675
Daniel Dunbara0d14262009-06-24 23:30:00 +00001676 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001677 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001678 return true;
1679
1680 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001681 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1682 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001683 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001684 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001685
Daniel Dunbar475839e2009-06-29 20:37:27 +00001686 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001687 return true;
1688
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001689 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001690 return TokError("unexpected token in '.space' directive");
1691 }
1692
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001694
1695 if (NumBytes <= 0)
1696 return TokError("invalid number of bytes in '.space' directive");
1697
1698 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001699 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001700
1701 return false;
1702}
1703
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001704/// ParseDirectiveZero
1705/// ::= .zero expression
1706bool AsmParser::ParseDirectiveZero() {
1707 CheckForValidSection();
1708
1709 int64_t NumBytes;
1710 if (ParseAbsoluteExpression(NumBytes))
1711 return true;
1712
Rafael Espindolae452b172010-10-05 19:42:57 +00001713 int64_t Val = 0;
1714 if (getLexer().is(AsmToken::Comma)) {
1715 Lex();
1716 if (ParseAbsoluteExpression(Val))
1717 return true;
1718 }
1719
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001720 if (getLexer().isNot(AsmToken::EndOfStatement))
1721 return TokError("unexpected token in '.zero' directive");
1722
1723 Lex();
1724
Rafael Espindolae452b172010-10-05 19:42:57 +00001725 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001726
1727 return false;
1728}
1729
Daniel Dunbara0d14262009-06-24 23:30:00 +00001730/// ParseDirectiveFill
1731/// ::= .fill expression , expression , expression
1732bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001733 CheckForValidSection();
1734
Daniel Dunbara0d14262009-06-24 23:30:00 +00001735 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001736 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001737 return true;
1738
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001739 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001740 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001741 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001742
Daniel Dunbara0d14262009-06-24 23:30:00 +00001743 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001744 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001745 return true;
1746
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001747 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001748 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001749 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001750
Daniel Dunbara0d14262009-06-24 23:30:00 +00001751 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001752 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001753 return true;
1754
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001755 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001756 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001757
Sean Callanan79ed1a82010-01-19 20:22:31 +00001758 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001759
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001760 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1761 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001762
1763 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001764 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001765
1766 return false;
1767}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001768
1769/// ParseDirectiveOrg
1770/// ::= .org expression [ , expression ]
1771bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001772 CheckForValidSection();
1773
Daniel Dunbar821e3332009-08-31 08:09:28 +00001774 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001775 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001776 return true;
1777
1778 // Parse optional fill expression.
1779 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001780 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1781 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001782 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001783 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001784
Daniel Dunbar475839e2009-06-29 20:37:27 +00001785 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001786 return true;
1787
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001788 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001789 return TokError("unexpected token in '.org' directive");
1790 }
1791
Sean Callanan79ed1a82010-01-19 20:22:31 +00001792 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001793
1794 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1795 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001796 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001797
1798 return false;
1799}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001800
1801/// ParseDirectiveAlign
1802/// ::= {.align, ...} expression [ , expression [ , expression ]]
1803bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001804 CheckForValidSection();
1805
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001806 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001807 int64_t Alignment;
1808 if (ParseAbsoluteExpression(Alignment))
1809 return true;
1810
1811 SMLoc MaxBytesLoc;
1812 bool HasFillExpr = false;
1813 int64_t FillExpr = 0;
1814 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001815 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1816 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001817 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001818 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001819
1820 // The fill expression can be omitted while specifying a maximum number of
1821 // alignment bytes, e.g:
1822 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001824 HasFillExpr = true;
1825 if (ParseAbsoluteExpression(FillExpr))
1826 return true;
1827 }
1828
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001829 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1830 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001831 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001832 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001833
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001834 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001835 if (ParseAbsoluteExpression(MaxBytesToFill))
1836 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001837
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001839 return TokError("unexpected token in directive");
1840 }
1841 }
1842
Sean Callanan79ed1a82010-01-19 20:22:31 +00001843 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001844
Daniel Dunbar648ac512010-05-17 21:54:30 +00001845 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001846 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001847
1848 // Compute alignment in bytes.
1849 if (IsPow2) {
1850 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001851 if (Alignment >= 32) {
1852 Error(AlignmentLoc, "invalid alignment value");
1853 Alignment = 31;
1854 }
1855
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001856 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001857 }
1858
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001859 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001860 if (MaxBytesLoc.isValid()) {
1861 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001862 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1863 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001864 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001865 }
1866
1867 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001868 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1869 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001870 MaxBytesToFill = 0;
1871 }
1872 }
1873
Daniel Dunbar648ac512010-05-17 21:54:30 +00001874 // Check whether we should use optimal code alignment for this .align
1875 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001876 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001877 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1878 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001879 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001880 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001881 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001882 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1883 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001884 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001885
1886 return false;
1887}
1888
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001889/// ParseDirectiveSymbolAttribute
1890/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001891bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001892 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001893 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001894 StringRef Name;
1895
1896 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001897 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001898
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001899 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001900
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001901 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001902
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001903 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001904 break;
1905
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001906 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001907 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001908 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001909 }
1910 }
1911
Sean Callanan79ed1a82010-01-19 20:22:31 +00001912 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001913 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001914}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001915
1916/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001917/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1918bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001919 CheckForValidSection();
1920
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001921 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001922 StringRef Name;
1923 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001924 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001925
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001926 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001927 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001928
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001929 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001930 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001931 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001932
1933 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001934 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001935 if (ParseAbsoluteExpression(Size))
1936 return true;
1937
1938 int64_t Pow2Alignment = 0;
1939 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001940 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001941 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001942 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001943 if (ParseAbsoluteExpression(Pow2Alignment))
1944 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001945
Chris Lattner258281d2010-01-19 06:22:22 +00001946 // If this target takes alignments in bytes (not log) validate and convert.
1947 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1948 if (!isPowerOf2_64(Pow2Alignment))
1949 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1950 Pow2Alignment = Log2_64(Pow2Alignment);
1951 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001952 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001953
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001954 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001955 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001956
Sean Callanan79ed1a82010-01-19 20:22:31 +00001957 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001958
Chris Lattner1fc3d752009-07-09 17:25:12 +00001959 // NOTE: a size of zero for a .comm should create a undefined symbol
1960 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001961 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001962 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1963 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001964
Eric Christopherc260a3e2010-05-14 01:38:54 +00001965 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001966 // may internally end up wanting an alignment in bytes.
1967 // FIXME: Diagnose overflow.
1968 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001969 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1970 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001971
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001972 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001973 return Error(IDLoc, "invalid symbol redefinition");
1974
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001975 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001976 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001977 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001978 getStreamer().EmitZerofill(Ctx.getMachOSection(
1979 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1980 0, SectionKind::getBSS()),
1981 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001982 return false;
1983 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001984
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001985 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001986 return false;
1987}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001988
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001989/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001990/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001991bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001992 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001993 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001994
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001995 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001996 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001997 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001998
Sean Callanan79ed1a82010-01-19 20:22:31 +00001999 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002000
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002001 if (Str.empty())
2002 Error(Loc, ".abort detected. Assembly stopping.");
2003 else
2004 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002005 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002006
2007 return false;
2008}
Kevin Enderby71148242009-07-14 21:35:03 +00002009
Kevin Enderby1f049b22009-07-14 23:21:55 +00002010/// ParseDirectiveInclude
2011/// ::= .include "filename"
2012bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002013 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002014 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002015
Sean Callanan18b83232010-01-19 21:44:56 +00002016 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002017 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002018 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002019
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002020 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002021 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002022
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002023 // Strip the quotes.
2024 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002025
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002026 // Attempt to switch the lexer to the included file before consuming the end
2027 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002028 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002029 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002030 return true;
2031 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002032
2033 return false;
2034}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002035
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002036/// ParseDirectiveIf
2037/// ::= .if expression
2038bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002039 TheCondStack.push_back(TheCondState);
2040 TheCondState.TheCond = AsmCond::IfCond;
2041 if(TheCondState.Ignore) {
2042 EatToEndOfStatement();
2043 }
2044 else {
2045 int64_t ExprValue;
2046 if (ParseAbsoluteExpression(ExprValue))
2047 return true;
2048
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002049 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002050 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002051
Sean Callanan79ed1a82010-01-19 20:22:31 +00002052 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002053
2054 TheCondState.CondMet = ExprValue;
2055 TheCondState.Ignore = !TheCondState.CondMet;
2056 }
2057
2058 return false;
2059}
2060
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002061bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2062 StringRef Name;
2063 TheCondStack.push_back(TheCondState);
2064 TheCondState.TheCond = AsmCond::IfCond;
2065
2066 if (TheCondState.Ignore) {
2067 EatToEndOfStatement();
2068 } else {
2069 if (ParseIdentifier(Name))
2070 return TokError("expected identifier after '.ifdef'");
2071
2072 Lex();
2073
2074 MCSymbol *Sym = getContext().LookupSymbol(Name);
2075
2076 if (expect_defined)
2077 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2078 else
2079 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2080 TheCondState.Ignore = !TheCondState.CondMet;
2081 }
2082
2083 return false;
2084}
2085
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002086/// ParseDirectiveElseIf
2087/// ::= .elseif expression
2088bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2089 if (TheCondState.TheCond != AsmCond::IfCond &&
2090 TheCondState.TheCond != AsmCond::ElseIfCond)
2091 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2092 " an .elseif");
2093 TheCondState.TheCond = AsmCond::ElseIfCond;
2094
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002095 bool LastIgnoreState = false;
2096 if (!TheCondStack.empty())
2097 LastIgnoreState = TheCondStack.back().Ignore;
2098 if (LastIgnoreState || TheCondState.CondMet) {
2099 TheCondState.Ignore = true;
2100 EatToEndOfStatement();
2101 }
2102 else {
2103 int64_t ExprValue;
2104 if (ParseAbsoluteExpression(ExprValue))
2105 return true;
2106
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002107 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002108 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002109
Sean Callanan79ed1a82010-01-19 20:22:31 +00002110 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002111 TheCondState.CondMet = ExprValue;
2112 TheCondState.Ignore = !TheCondState.CondMet;
2113 }
2114
2115 return false;
2116}
2117
2118/// ParseDirectiveElse
2119/// ::= .else
2120bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002121 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002122 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002123
Sean Callanan79ed1a82010-01-19 20:22:31 +00002124 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002125
2126 if (TheCondState.TheCond != AsmCond::IfCond &&
2127 TheCondState.TheCond != AsmCond::ElseIfCond)
2128 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2129 ".elseif");
2130 TheCondState.TheCond = AsmCond::ElseCond;
2131 bool LastIgnoreState = false;
2132 if (!TheCondStack.empty())
2133 LastIgnoreState = TheCondStack.back().Ignore;
2134 if (LastIgnoreState || TheCondState.CondMet)
2135 TheCondState.Ignore = true;
2136 else
2137 TheCondState.Ignore = false;
2138
2139 return false;
2140}
2141
2142/// ParseDirectiveEndIf
2143/// ::= .endif
2144bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002145 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002146 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002147
Sean Callanan79ed1a82010-01-19 20:22:31 +00002148 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002149
2150 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2151 TheCondStack.empty())
2152 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2153 ".else");
2154 if (!TheCondStack.empty()) {
2155 TheCondState = TheCondStack.back();
2156 TheCondStack.pop_back();
2157 }
2158
2159 return false;
2160}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002161
2162/// ParseDirectiveFile
2163/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002164bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002165 // FIXME: I'm not sure what this is.
2166 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002167 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002168 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002169 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002170 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002171
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002172 if (FileNumber < 1)
2173 return TokError("file number less than one");
2174 }
2175
Daniel Dunbareceec052010-07-12 17:45:27 +00002176 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002177 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002178
Chris Lattnerd32e8032010-01-25 19:02:58 +00002179 StringRef Filename = getTok().getString();
2180 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002181 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002182
Daniel Dunbareceec052010-07-12 17:45:27 +00002183 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002184 return TokError("unexpected token in '.file' directive");
2185
Chris Lattnerd32e8032010-01-25 19:02:58 +00002186 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002187 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002188 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002189 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002190 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002191 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002192
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002193 return false;
2194}
2195
2196/// ParseDirectiveLine
2197/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002198bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002199 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2200 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002201 return TokError("unexpected token in '.line' directive");
2202
Sean Callanan18b83232010-01-19 21:44:56 +00002203 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002204 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002205 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002206
2207 // FIXME: Do something with the .line.
2208 }
2209
Daniel Dunbareceec052010-07-12 17:45:27 +00002210 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002211 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002212
2213 return false;
2214}
2215
2216
2217/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002218/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002219/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2220/// The first number is a file number, must have been previously assigned with
2221/// a .file directive, the second number is the line number and optionally the
2222/// third number is a column position (zero if not specified). The remaining
2223/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002224bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002225
Daniel Dunbareceec052010-07-12 17:45:27 +00002226 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002227 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002228 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002229 if (FileNumber < 1)
2230 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002231 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002232 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002233 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002234
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002235 int64_t LineNumber = 0;
2236 if (getLexer().is(AsmToken::Integer)) {
2237 LineNumber = getTok().getIntVal();
2238 if (LineNumber < 1)
2239 return TokError("line number less than one in '.loc' directive");
2240 Lex();
2241 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002242
2243 int64_t ColumnPos = 0;
2244 if (getLexer().is(AsmToken::Integer)) {
2245 ColumnPos = getTok().getIntVal();
2246 if (ColumnPos < 0)
2247 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002248 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002249 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002250
Kevin Enderbyc0957932010-09-30 16:52:03 +00002251 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002252 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002253 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002254 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2255 for (;;) {
2256 if (getLexer().is(AsmToken::EndOfStatement))
2257 break;
2258
2259 StringRef Name;
2260 SMLoc Loc = getTok().getLoc();
2261 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002262 return TokError("unexpected token in '.loc' directive");
2263
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002264 if (Name == "basic_block")
2265 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2266 else if (Name == "prologue_end")
2267 Flags |= DWARF2_FLAG_PROLOGUE_END;
2268 else if (Name == "epilogue_begin")
2269 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2270 else if (Name == "is_stmt") {
2271 SMLoc Loc = getTok().getLoc();
2272 const MCExpr *Value;
2273 if (getParser().ParseExpression(Value))
2274 return true;
2275 // The expression must be the constant 0 or 1.
2276 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2277 int Value = MCE->getValue();
2278 if (Value == 0)
2279 Flags &= ~DWARF2_FLAG_IS_STMT;
2280 else if (Value == 1)
2281 Flags |= DWARF2_FLAG_IS_STMT;
2282 else
2283 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002284 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002285 else {
2286 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2287 }
2288 }
2289 else if (Name == "isa") {
2290 SMLoc Loc = getTok().getLoc();
2291 const MCExpr *Value;
2292 if (getParser().ParseExpression(Value))
2293 return true;
2294 // The expression must be a constant greater or equal to 0.
2295 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2296 int Value = MCE->getValue();
2297 if (Value < 0)
2298 return Error(Loc, "isa number less than zero");
2299 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002300 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002301 else {
2302 return Error(Loc, "isa number not a constant value");
2303 }
2304 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002305 else if (Name == "discriminator") {
2306 if (getParser().ParseAbsoluteExpression(Discriminator))
2307 return true;
2308 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002309 else {
2310 return Error(Loc, "unknown sub-directive in '.loc' directive");
2311 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002312
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002313 if (getLexer().is(AsmToken::EndOfStatement))
2314 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002315 }
2316 }
2317
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002318 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002319 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002320
2321 return false;
2322}
2323
Daniel Dunbar138abae2010-10-16 04:56:42 +00002324/// ParseDirectiveStabs
2325/// ::= .stabs string, number, number, number
2326bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2327 SMLoc DirectiveLoc) {
2328 return TokError("unsupported directive '" + Directive + "'");
2329}
2330
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002331/// ParseDirectiveCFISections
2332/// ::= .cfi_sections section [, section]
2333bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2334 SMLoc DirectiveLoc) {
2335 StringRef Name;
2336 bool EH = false;
2337 bool Debug = false;
2338
2339 if (getParser().ParseIdentifier(Name))
2340 return TokError("Expected an identifier");
2341
2342 if (Name == ".eh_frame")
2343 EH = true;
2344 else if (Name == ".debug_frame")
2345 Debug = true;
2346
2347 if (getLexer().is(AsmToken::Comma)) {
2348 Lex();
2349
2350 if (getParser().ParseIdentifier(Name))
2351 return TokError("Expected an identifier");
2352
2353 if (Name == ".eh_frame")
2354 EH = true;
2355 else if (Name == ".debug_frame")
2356 Debug = true;
2357 }
2358
2359 getStreamer().EmitCFISections(EH, Debug);
2360
2361 return false;
2362}
2363
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002364/// ParseDirectiveCFIStartProc
2365/// ::= .cfi_startproc
2366bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2367 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002368 getStreamer().EmitCFIStartProc();
2369 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002370}
2371
2372/// ParseDirectiveCFIEndProc
2373/// ::= .cfi_endproc
2374bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002375 getStreamer().EmitCFIEndProc();
2376 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002377}
2378
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002379/// ParseRegisterOrRegisterNumber - parse register name or number.
2380bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2381 SMLoc DirectiveLoc) {
2382 unsigned RegNo;
2383
Jim Grosbach6f888a82011-06-02 17:14:04 +00002384 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002385 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2386 DirectiveLoc))
2387 return true;
2388 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2389 } else
2390 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002391
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002392 return false;
2393}
2394
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002395/// ParseDirectiveCFIDefCfa
2396/// ::= .cfi_def_cfa register, offset
2397bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2398 SMLoc DirectiveLoc) {
2399 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002400 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002401 return true;
2402
2403 if (getLexer().isNot(AsmToken::Comma))
2404 return TokError("unexpected token in directive");
2405 Lex();
2406
2407 int64_t Offset = 0;
2408 if (getParser().ParseAbsoluteExpression(Offset))
2409 return true;
2410
Rafael Espindola066c2f42011-04-12 23:59:07 +00002411 getStreamer().EmitCFIDefCfa(Register, Offset);
2412 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002413}
2414
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002415/// ParseDirectiveCFIDefCfaOffset
2416/// ::= .cfi_def_cfa_offset offset
2417bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2418 SMLoc DirectiveLoc) {
2419 int64_t Offset = 0;
2420 if (getParser().ParseAbsoluteExpression(Offset))
2421 return true;
2422
Rafael Espindola066c2f42011-04-12 23:59:07 +00002423 getStreamer().EmitCFIDefCfaOffset(Offset);
2424 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002425}
2426
2427/// ParseDirectiveCFIAdjustCfaOffset
2428/// ::= .cfi_adjust_cfa_offset adjustment
2429bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2430 SMLoc DirectiveLoc) {
2431 int64_t Adjustment = 0;
2432 if (getParser().ParseAbsoluteExpression(Adjustment))
2433 return true;
2434
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002435 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2436 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002437}
2438
2439/// ParseDirectiveCFIDefCfaRegister
2440/// ::= .cfi_def_cfa_register register
2441bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2442 SMLoc DirectiveLoc) {
2443 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002444 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002445 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002446
Rafael Espindola066c2f42011-04-12 23:59:07 +00002447 getStreamer().EmitCFIDefCfaRegister(Register);
2448 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002449}
2450
2451/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002452/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002453bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2454 int64_t Register = 0;
2455 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002456
2457 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002458 return true;
2459
2460 if (getLexer().isNot(AsmToken::Comma))
2461 return TokError("unexpected token in directive");
2462 Lex();
2463
2464 if (getParser().ParseAbsoluteExpression(Offset))
2465 return true;
2466
Rafael Espindola066c2f42011-04-12 23:59:07 +00002467 getStreamer().EmitCFIOffset(Register, Offset);
2468 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002469}
2470
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002471/// ParseDirectiveCFIRelOffset
2472/// ::= .cfi_rel_offset register, offset
2473bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2474 SMLoc DirectiveLoc) {
2475 int64_t Register = 0;
2476
2477 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2478 return true;
2479
2480 if (getLexer().isNot(AsmToken::Comma))
2481 return TokError("unexpected token in directive");
2482 Lex();
2483
2484 int64_t Offset = 0;
2485 if (getParser().ParseAbsoluteExpression(Offset))
2486 return true;
2487
Rafael Espindola25f492e2011-04-12 16:12:03 +00002488 getStreamer().EmitCFIRelOffset(Register, Offset);
2489 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002490}
2491
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002492static bool isValidEncoding(int64_t Encoding) {
2493 if (Encoding & ~0xff)
2494 return false;
2495
2496 if (Encoding == dwarf::DW_EH_PE_omit)
2497 return true;
2498
2499 const unsigned Format = Encoding & 0xf;
2500 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2501 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2502 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2503 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2504 return false;
2505
Rafael Espindolacaf11582010-12-29 04:31:26 +00002506 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002507 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002508 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002509 return false;
2510
2511 return true;
2512}
2513
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002514/// ParseDirectiveCFIPersonalityOrLsda
2515/// ::= .cfi_personality encoding, [symbol_name]
2516/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002517bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002518 SMLoc DirectiveLoc) {
2519 int64_t Encoding = 0;
2520 if (getParser().ParseAbsoluteExpression(Encoding))
2521 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002522 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002523 return false;
2524
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002525 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002526 return TokError("unsupported encoding.");
2527
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002528 if (getLexer().isNot(AsmToken::Comma))
2529 return TokError("unexpected token in directive");
2530 Lex();
2531
2532 StringRef Name;
2533 if (getParser().ParseIdentifier(Name))
2534 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002535
2536 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2537
2538 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002539 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002540 else {
2541 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002542 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002543 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002544 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002545}
2546
Rafael Espindolafe024d02010-12-28 18:36:23 +00002547/// ParseDirectiveCFIRememberState
2548/// ::= .cfi_remember_state
2549bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2550 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002551 getStreamer().EmitCFIRememberState();
2552 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002553}
2554
2555/// ParseDirectiveCFIRestoreState
2556/// ::= .cfi_remember_state
2557bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2558 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002559 getStreamer().EmitCFIRestoreState();
2560 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002561}
2562
Rafael Espindolac5754392011-04-12 15:31:05 +00002563/// ParseDirectiveCFISameValue
2564/// ::= .cfi_same_value register
2565bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2566 SMLoc DirectiveLoc) {
2567 int64_t Register = 0;
2568
2569 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2570 return true;
2571
2572 getStreamer().EmitCFISameValue(Register);
2573
2574 return false;
2575}
2576
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002577/// ParseDirectiveMacrosOnOff
2578/// ::= .macros_on
2579/// ::= .macros_off
2580bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2581 SMLoc DirectiveLoc) {
2582 if (getLexer().isNot(AsmToken::EndOfStatement))
2583 return Error(getLexer().getLoc(),
2584 "unexpected token in '" + Directive + "' directive");
2585
2586 getParser().MacrosEnabled = Directive == ".macros_on";
2587
2588 return false;
2589}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002590
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002591/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002592/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002593bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2594 SMLoc DirectiveLoc) {
2595 StringRef Name;
2596 if (getParser().ParseIdentifier(Name))
2597 return TokError("expected identifier in directive");
2598
Rafael Espindola65366442011-06-05 02:43:45 +00002599 std::vector<StringRef> Parameters;
2600 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2601 for(;;) {
2602 StringRef Parameter;
2603 if (getParser().ParseIdentifier(Parameter))
2604 return TokError("expected identifier in directive");
2605 Parameters.push_back(Parameter);
2606
2607 if (getLexer().isNot(AsmToken::Comma))
2608 break;
2609 Lex();
2610 }
2611 }
2612
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002613 if (getLexer().isNot(AsmToken::EndOfStatement))
2614 return TokError("unexpected token in '.macro' directive");
2615
2616 // Eat the end of statement.
2617 Lex();
2618
2619 AsmToken EndToken, StartToken = getTok();
2620
2621 // Lex the macro definition.
2622 for (;;) {
2623 // Check whether we have reached the end of the file.
2624 if (getLexer().is(AsmToken::Eof))
2625 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2626
2627 // Otherwise, check whether we have reach the .endmacro.
2628 if (getLexer().is(AsmToken::Identifier) &&
2629 (getTok().getIdentifier() == ".endm" ||
2630 getTok().getIdentifier() == ".endmacro")) {
2631 EndToken = getTok();
2632 Lex();
2633 if (getLexer().isNot(AsmToken::EndOfStatement))
2634 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2635 "' directive");
2636 break;
2637 }
2638
2639 // Otherwise, scan til the end of the statement.
2640 getParser().EatToEndOfStatement();
2641 }
2642
2643 if (getParser().MacroMap.lookup(Name)) {
2644 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2645 }
2646
2647 const char *BodyStart = StartToken.getLoc().getPointer();
2648 const char *BodyEnd = EndToken.getLoc().getPointer();
2649 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002650 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002651 return false;
2652}
2653
2654/// ParseDirectiveEndMacro
2655/// ::= .endm
2656/// ::= .endmacro
2657bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2658 SMLoc DirectiveLoc) {
2659 if (getLexer().isNot(AsmToken::EndOfStatement))
2660 return TokError("unexpected token in '" + Directive + "' directive");
2661
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002662 // If we are inside a macro instantiation, terminate the current
2663 // instantiation.
2664 if (!getParser().ActiveMacros.empty()) {
2665 getParser().HandleMacroExit();
2666 return false;
2667 }
2668
2669 // Otherwise, this .endmacro is a stray entry in the file; well formed
2670 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002671 return TokError("unexpected '" + Directive + "' in file, "
2672 "no current macro definition");
2673}
2674
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002675bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002676 getParser().CheckForValidSection();
2677
2678 const MCExpr *Value;
2679
2680 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002681 return true;
2682
2683 if (getLexer().isNot(AsmToken::EndOfStatement))
2684 return TokError("unexpected token in directive");
2685
2686 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002687 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002688 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002689 getStreamer().EmitULEB128Value(Value);
2690
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002691 return false;
2692}
2693
2694
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002695/// \brief Create an MCAsmParser instance.
2696MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2697 MCContext &C, MCStreamer &Out,
2698 const MCAsmInfo &MAI) {
2699 return new AsmParser(T, SM, C, Out, MAI);
2700}