blob: 8af4ac62e221c321cdffe472900cfbfa0b1185db [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"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000033#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000035#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000036#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000037using namespace llvm;
38
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000039namespace {
40
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000041/// \brief Helper class for tracking macro definitions.
42struct Macro {
43 StringRef Name;
44 StringRef Body;
45
46public:
47 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
48};
49
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000050/// \brief Helper class for storing information about an active macro
51/// instantiation.
52struct MacroInstantiation {
53 /// The macro being instantiated.
54 const Macro *TheMacro;
55
56 /// The macro instantiation with substitutions.
57 MemoryBuffer *Instantiation;
58
59 /// The location of the instantiation.
60 SMLoc InstantiationLoc;
61
62 /// The location where parsing should resume upon instantiation completion.
63 SMLoc ExitLoc;
64
65public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000066 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
67 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000068};
69
Daniel Dunbaraef87e32010-07-18 18:31:38 +000070/// \brief The concrete assembly parser instance.
71class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000072 friend class GenericAsmParser;
73
Daniel Dunbaraef87e32010-07-18 18:31:38 +000074 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
75 void operator=(const AsmParser &); // DO NOT IMPLEMENT
76private:
77 AsmLexer Lexer;
78 MCContext &Ctx;
79 MCStreamer &Out;
80 SourceMgr &SrcMgr;
81 MCAsmParserExtension *GenericParser;
82 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000083
84 // FIXME: This is not the best place to store this. To handle a (for example)
85 // .cfi_rel_offset before a .cfi_def_cfa_offset we need to know the initial
86 // frame state.
Rafael Espindola53abbe52011-04-11 20:29:16 +000087 int64_t LastOffset;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000088
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 /// This is the current buffer index we're lexing from as managed by the
90 /// SourceMgr object.
91 int CurBuffer;
92
93 AsmCond TheCondState;
94 std::vector<AsmCond> TheCondStack;
95
96 /// DirectiveMap - This is a table handlers for directives. Each handler is
97 /// invoked after the directive identifier is read and is responsible for
98 /// parsing and validating the rest of the directive. The handler is passed
99 /// in the directive name and the location of the directive keyword.
100 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000101
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000102 /// MacroMap - Map of currently defined macros.
103 StringMap<Macro*> MacroMap;
104
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000105 /// ActiveMacros - Stack of active macro instantiations.
106 std::vector<MacroInstantiation*> ActiveMacros;
107
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000108 /// Boolean tracking whether macro substitution is enabled.
109 unsigned MacrosEnabled : 1;
110
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000111 /// Flag tracking whether any errors have been encountered.
112 unsigned HadError : 1;
113
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000114public:
115 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
116 const MCAsmInfo &MAI);
117 ~AsmParser();
118
119 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
120
121 void AddDirectiveHandler(MCAsmParserExtension *Object,
122 StringRef Directive,
123 DirectiveHandler Handler) {
124 DirectiveMap[Directive] = std::make_pair(Object, Handler);
125 }
126
127public:
128 /// @name MCAsmParser Interface
129 /// {
130
131 virtual SourceMgr &getSourceManager() { return SrcMgr; }
132 virtual MCAsmLexer &getLexer() { return Lexer; }
133 virtual MCContext &getContext() { return Ctx; }
134 virtual MCStreamer &getStreamer() { return Out; }
135
136 virtual void Warning(SMLoc L, const Twine &Meg);
137 virtual bool Error(SMLoc L, const Twine &Msg);
138
139 const AsmToken &Lex();
140
141 bool ParseExpression(const MCExpr *&Res);
142 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
143 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
144 virtual bool ParseAbsoluteExpression(int64_t &Res);
145
146 /// }
147
Rafael Espindola53abbe52011-04-11 20:29:16 +0000148 int64_t adjustLastOffset(int64_t Adjustment) {
149 LastOffset += Adjustment;
150 return LastOffset;
151 }
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000152 int64_t getLastOffset() {
153 return LastOffset;
154 }
Rafael Espindola53abbe52011-04-11 20:29:16 +0000155 void setLastOffset(int64_t Offset) {
156 LastOffset = Offset;
157 }
158
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000159private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000160 void CheckForValidSection();
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 bool ParseStatement();
163
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000164 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
165 void HandleMacroExit();
166
167 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000168 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
169 SrcMgr.PrintMessage(Loc, Msg, Type);
170 }
171
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000172 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
173 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000174
175 /// \brief Reset the current lexer position to that given by \arg Loc. The
176 /// current token is not set; clients should ensure Lex() is called
177 /// subsequently.
178 void JumpToLoc(SMLoc Loc);
179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000181
182 /// \brief Parse up to the end of statement and a return the contents from the
183 /// current token until the end of the statement; the current token on exit
184 /// will be either the EndOfStatement or EOF.
185 StringRef ParseStringToEndOfStatement();
186
Nico Weber4c4c7322011-01-28 03:04:41 +0000187 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188
189 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
190 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
191 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000192 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000193
194 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
195 /// and set \arg Res to the identifier contents.
196 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000197
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000199
200 // ".ascii", ".asciiz", ".string"
201 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000203 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204 bool ParseDirectiveFill(); // ".fill"
205 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000206 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000207 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 bool ParseDirectiveOrg(); // ".org"
209 // ".align{,32}", ".p2align{,w,l}"
210 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
211
212 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
213 /// accepts a single symbol (which should be a label or an external).
214 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000215
216 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
217
218 bool ParseDirectiveAbort(); // ".abort"
219 bool ParseDirectiveInclude(); // ".include"
220
221 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000222 // ".ifdef" or ".ifndef", depending on expect_defined
223 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
225 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
226 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
227
228 /// ParseEscapedString - Parse the current token as a string which may include
229 /// escaped characters and return the string contents.
230 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000231
232 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
233 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234};
235
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000236/// \brief Generic implementations of directive handling, etc. which is shared
237/// (or the default, at least) for all assembler parser.
238class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000239 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
240 void AddDirectiveHandler(StringRef Directive) {
241 getParser().AddDirectiveHandler(this, Directive,
242 HandleDirective<GenericAsmParser, Handler>);
243 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000244public:
245 GenericAsmParser() {}
246
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000247 AsmParser &getParser() {
248 return (AsmParser&) this->MCAsmParserExtension::getParser();
249 }
250
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000251 virtual void Initialize(MCAsmParser &Parser) {
252 // Call the base implementation.
253 this->MCAsmParserExtension::Initialize(Parser);
254
255 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
257 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
258 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000260
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000261 // CFI directives.
262 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
263 ".cfi_startproc");
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
265 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
267 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
269 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
271 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
273 ".cfi_def_cfa_register");
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
275 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
277 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000278 AddDirectiveHandler<
279 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
280 AddDirectiveHandler<
281 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000282 AddDirectiveHandler<
283 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
284 AddDirectiveHandler<
285 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000286
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000287 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000288 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
289 ".macros_on");
290 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
291 ".macros_off");
292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
294 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000295
296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
297 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000298 }
299
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000300 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
301
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000302 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
303 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
304 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000305 bool ParseDirectiveStabs(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);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000317
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000318 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000319 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
320 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000321
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000322 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000323};
324
325}
326
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000327namespace llvm {
328
329extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000330extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000331extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000332
333}
334
Chris Lattneraaec2052010-01-19 19:46:13 +0000335enum { DEFAULT_ADDRSPACE = 0 };
336
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000337AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
338 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000339 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Rafael Espindola53abbe52011-04-11 20:29:16 +0000340 GenericParser(new GenericAsmParser), PlatformParser(0), LastOffset(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000341 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000342 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000343
344 // Initialize the generic parser.
345 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000346
347 // Initialize the platform / file format parser.
348 //
349 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
350 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000351 if (_MAI.hasMicrosoftFastStdCallMangling()) {
352 PlatformParser = createCOFFAsmParser();
353 PlatformParser->Initialize(*this);
354 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000355 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000356 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000357 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000358 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000359 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000360 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000361}
362
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000363AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000364 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
365
366 // Destroy any macros.
367 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
368 ie = MacroMap.end(); it != ie; ++it)
369 delete it->getValue();
370
Daniel Dunbare4749702010-07-12 18:12:02 +0000371 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000372 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000373}
374
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000375void AsmParser::PrintMacroInstantiations() {
376 // Print the active macro instantiation stack.
377 for (std::vector<MacroInstantiation*>::const_reverse_iterator
378 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
379 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
380 "note");
381}
382
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000383void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000384 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000385 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000386}
387
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000388bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000389 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000390 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000391 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000392 return true;
393}
394
Sean Callananfd0b0282010-01-21 00:19:58 +0000395bool AsmParser::EnterIncludeFile(const std::string &Filename) {
396 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
397 if (NewBuf == -1)
398 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000399
Sean Callananfd0b0282010-01-21 00:19:58 +0000400 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000401
Sean Callananfd0b0282010-01-21 00:19:58 +0000402 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000403
Sean Callananfd0b0282010-01-21 00:19:58 +0000404 return false;
405}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000406
407void AsmParser::JumpToLoc(SMLoc Loc) {
408 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
409 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
410}
411
Sean Callananfd0b0282010-01-21 00:19:58 +0000412const AsmToken &AsmParser::Lex() {
413 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000414
Sean Callananfd0b0282010-01-21 00:19:58 +0000415 if (tok->is(AsmToken::Eof)) {
416 // If this is the end of an included file, pop the parent file off the
417 // include stack.
418 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
419 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000420 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000421 tok = &Lexer.Lex();
422 }
423 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000424
Sean Callananfd0b0282010-01-21 00:19:58 +0000425 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000426 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000427
Sean Callananfd0b0282010-01-21 00:19:58 +0000428 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000429}
430
Chris Lattner79180e22010-04-05 23:15:42 +0000431bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000432 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000433 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000434 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000435
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000436 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000437 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000438
439 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000440 AsmCond StartingCondState = TheCondState;
441
Chris Lattnerb717fb02009-07-02 21:53:43 +0000442 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000443 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000444 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000445
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000446 // We had an error, validate that one was emitted and recover by skipping to
447 // the next line.
448 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000449 EatToEndOfStatement();
450 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000451
452 if (TheCondState.TheCond != StartingCondState.TheCond ||
453 TheCondState.Ignore != StartingCondState.Ignore)
454 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000455
456 // Check to see there are no empty DwarfFile slots.
457 const std::vector<MCDwarfFile *> &MCDwarfFiles =
458 getContext().getMCDwarfFiles();
459 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000460 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000461 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000462 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000463
Chris Lattner79180e22010-04-05 23:15:42 +0000464 // Finalize the output stream if there are no errors and if the client wants
465 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000466 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000467 Out.Finish();
468
Chris Lattnerb717fb02009-07-02 21:53:43 +0000469 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000470}
471
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000472void AsmParser::CheckForValidSection() {
473 if (!getStreamer().getCurrentSection()) {
474 TokError("expected section directive before assembly directive");
475 Out.SwitchSection(Ctx.getMachOSection(
476 "__TEXT", "__text",
477 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
478 0, SectionKind::getText()));
479 }
480}
481
Chris Lattner2cf5f142009-06-22 01:29:09 +0000482/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
483void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000484 while (Lexer.isNot(AsmToken::EndOfStatement) &&
485 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000486 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000487
Chris Lattner2cf5f142009-06-22 01:29:09 +0000488 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000489 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000490 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000491}
492
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000493StringRef AsmParser::ParseStringToEndOfStatement() {
494 const char *Start = getTok().getLoc().getPointer();
495
496 while (Lexer.isNot(AsmToken::EndOfStatement) &&
497 Lexer.isNot(AsmToken::Eof))
498 Lex();
499
500 const char *End = getTok().getLoc().getPointer();
501 return StringRef(Start, End - Start);
502}
Chris Lattnerc4193832009-06-22 05:51:26 +0000503
Chris Lattner74ec1a32009-06-22 06:32:03 +0000504/// ParseParenExpr - Parse a paren expression and return it.
505/// NOTE: This assumes the leading '(' has already been consumed.
506///
507/// parenexpr ::= expr)
508///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000509bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000510 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000511 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000512 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000513 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000514 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000515 return false;
516}
Chris Lattnerc4193832009-06-22 05:51:26 +0000517
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000518/// ParseBracketExpr - Parse a bracket expression and return it.
519/// NOTE: This assumes the leading '[' has already been consumed.
520///
521/// bracketexpr ::= expr]
522///
523bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
524 if (ParseExpression(Res)) return true;
525 if (Lexer.isNot(AsmToken::RBrac))
526 return TokError("expected ']' in brackets expression");
527 EndLoc = Lexer.getLoc();
528 Lex();
529 return false;
530}
531
Chris Lattner74ec1a32009-06-22 06:32:03 +0000532/// ParsePrimaryExpr - Parse a primary expression and return it.
533/// primaryexpr ::= (parenexpr
534/// primaryexpr ::= symbol
535/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000536/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000537/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000538bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000539 switch (Lexer.getKind()) {
540 default:
541 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000542 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000543 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000544 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000545 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000546 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000547 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000548 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000549 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000550 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000551 EndLoc = Lexer.getLoc();
552
553 StringRef Identifier;
554 if (ParseIdentifier(Identifier))
555 return false;
556
Daniel Dunbarfffff912009-10-16 01:34:54 +0000557 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000558 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000559 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000560
561 // Lookup the symbol variant if used.
562 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000563 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000564 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000565 if (Variant == MCSymbolRefExpr::VK_Invalid) {
566 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000567 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000568 }
569 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000570
Daniel Dunbarfffff912009-10-16 01:34:54 +0000571 // If this is an absolute variable reference, substitute it now to preserve
572 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000573 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000574 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000575 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000576
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000577 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000578 return false;
579 }
580
581 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000582 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000583 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000584 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000585 case AsmToken::Integer: {
586 SMLoc Loc = getTok().getLoc();
587 int64_t IntVal = getTok().getIntVal();
588 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000589 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000590 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000591 // Look for 'b' or 'f' following an Integer as a directional label
592 if (Lexer.getKind() == AsmToken::Identifier) {
593 StringRef IDVal = getTok().getString();
594 if (IDVal == "f" || IDVal == "b"){
595 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
596 IDVal == "f" ? 1 : 0);
597 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
598 getContext());
599 if(IDVal == "b" && Sym->isUndefined())
600 return Error(Loc, "invalid reference to undefined symbol");
601 EndLoc = Lexer.getLoc();
602 Lex(); // Eat identifier.
603 }
604 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000605 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000606 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000607 case AsmToken::Real: {
608 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000609 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000610 Res = MCConstantExpr::Create(IntVal, getContext());
611 Lex(); // Eat token.
612 return false;
613 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000614 case AsmToken::Dot: {
615 // This is a '.' reference, which references the current PC. Emit a
616 // temporary label to the streamer and refer to it.
617 MCSymbol *Sym = Ctx.CreateTempSymbol();
618 Out.EmitLabel(Sym);
619 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
620 EndLoc = Lexer.getLoc();
621 Lex(); // Eat identifier.
622 return false;
623 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000624 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000625 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000626 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000627 case AsmToken::LBrac:
628 if (!PlatformParser->HasBracketExpressions())
629 return TokError("brackets expression not supported on this target");
630 Lex(); // Eat the '['.
631 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000632 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000633 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000634 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000635 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000636 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000637 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000638 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000639 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000640 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000641 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000642 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000643 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000644 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000645 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000646 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000647 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000648 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000650 }
651}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000652
Chris Lattnerb4307b32010-01-15 19:28:38 +0000653bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000654 SMLoc EndLoc;
655 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000656}
657
Daniel Dunbarcceba832010-09-17 02:47:07 +0000658const MCExpr *
659AsmParser::ApplyModifierToExpr(const MCExpr *E,
660 MCSymbolRefExpr::VariantKind Variant) {
661 // Recurse over the given expression, rebuilding it to apply the given variant
662 // if there is exactly one symbol.
663 switch (E->getKind()) {
664 case MCExpr::Target:
665 case MCExpr::Constant:
666 return 0;
667
668 case MCExpr::SymbolRef: {
669 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
670
671 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
672 TokError("invalid variant on expression '" +
673 getTok().getIdentifier() + "' (already modified)");
674 return E;
675 }
676
677 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
678 }
679
680 case MCExpr::Unary: {
681 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
682 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
683 if (!Sub)
684 return 0;
685 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
686 }
687
688 case MCExpr::Binary: {
689 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
690 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
691 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
692
693 if (!LHS && !RHS)
694 return 0;
695
696 if (!LHS) LHS = BE->getLHS();
697 if (!RHS) RHS = BE->getRHS();
698
699 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
700 }
701 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000702
703 assert(0 && "Invalid expression kind!");
704 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000705}
706
Chris Lattner74ec1a32009-06-22 06:32:03 +0000707/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000708///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000709/// expr ::= expr +,- expr -> lowest.
710/// expr ::= expr |,^,&,! expr -> middle.
711/// expr ::= expr *,/,%,<<,>> expr -> highest.
712/// expr ::= primaryexpr
713///
Chris Lattner54482b42010-01-15 19:39:23 +0000714bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000715 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000716 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000717 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
718 return true;
719
Daniel Dunbarcceba832010-09-17 02:47:07 +0000720 // As a special case, we support 'a op b @ modifier' by rewriting the
721 // expression to include the modifier. This is inefficient, but in general we
722 // expect users to use 'a@modifier op b'.
723 if (Lexer.getKind() == AsmToken::At) {
724 Lex();
725
726 if (Lexer.isNot(AsmToken::Identifier))
727 return TokError("unexpected symbol modifier following '@'");
728
729 MCSymbolRefExpr::VariantKind Variant =
730 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
731 if (Variant == MCSymbolRefExpr::VK_Invalid)
732 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
733
734 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
735 if (!ModifiedRes) {
736 return TokError("invalid modifier '" + getTok().getIdentifier() +
737 "' (no symbols present)");
738 return true;
739 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000740
Daniel Dunbarcceba832010-09-17 02:47:07 +0000741 Res = ModifiedRes;
742 Lex();
743 }
744
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000745 // Try to constant fold it up front, if possible.
746 int64_t Value;
747 if (Res->EvaluateAsAbsolute(Value))
748 Res = MCConstantExpr::Create(Value, getContext());
749
750 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000751}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000752
Chris Lattnerb4307b32010-01-15 19:28:38 +0000753bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000754 Res = 0;
755 return ParseParenExpr(Res, EndLoc) ||
756 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000757}
758
Daniel Dunbar475839e2009-06-29 20:37:27 +0000759bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000760 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000761
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000762 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000763 if (ParseExpression(Expr))
764 return true;
765
Daniel Dunbare00b0112009-10-16 01:57:52 +0000766 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000767 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000768
769 return false;
770}
771
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000772static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000773 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000774 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000775 default:
776 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000777
Daniel Dunbarcceba832010-09-17 02:47:07 +0000778 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000779 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000780 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000781 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000782 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000783 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000784 return 1;
785
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000786
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000787 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000788 //
789 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000791 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000792 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000793 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000794 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000795 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000796 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000797 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000798 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000799
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000800 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000801 case AsmToken::EqualEqual:
802 Kind = MCBinaryExpr::EQ;
803 return 3;
804 case AsmToken::ExclaimEqual:
805 case AsmToken::LessGreater:
806 Kind = MCBinaryExpr::NE;
807 return 3;
808 case AsmToken::Less:
809 Kind = MCBinaryExpr::LT;
810 return 3;
811 case AsmToken::LessEqual:
812 Kind = MCBinaryExpr::LTE;
813 return 3;
814 case AsmToken::Greater:
815 Kind = MCBinaryExpr::GT;
816 return 3;
817 case AsmToken::GreaterEqual:
818 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000819 return 3;
820
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000821 // High Intermediate Precedence: +, -
822 case AsmToken::Plus:
823 Kind = MCBinaryExpr::Add;
824 return 4;
825 case AsmToken::Minus:
826 Kind = MCBinaryExpr::Sub;
827 return 4;
828
Daniel Dunbar475839e2009-06-29 20:37:27 +0000829 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000830 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000831 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000832 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000833 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000834 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000835 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000836 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000837 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000838 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000839 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000840 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000841 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000843 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000844 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000845 }
846}
847
848
849/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
850/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000851bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
852 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000853 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000854 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000855 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000856
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000857 // If the next token is lower precedence than we are allowed to eat, return
858 // successfully with what we ate already.
859 if (TokPrec < Precedence)
860 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000861
Sean Callanan79ed1a82010-01-19 20:22:31 +0000862 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000863
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000864 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000865 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000866 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000867
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000868 // If BinOp binds less tightly with RHS than the operator after RHS, let
869 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000870 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000871 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000872 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000873 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000874 }
875
Daniel Dunbar475839e2009-06-29 20:37:27 +0000876 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000877 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000878 }
879}
880
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000881
882
883
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000884/// ParseStatement:
885/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000886/// ::= Label* Directive ...Operands... EndOfStatement
887/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000888bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000889 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000890 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000891 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000892 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000893 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000894
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000895 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000896 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000897 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000898 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000899 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000900 // A full line comment is a '#' as the first token.
901 if (Lexer.is(AsmToken::Hash)) {
902 EatToEndOfStatement();
903 return false;
904 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000905
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000906 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000907 if (Lexer.is(AsmToken::Integer)) {
908 LocalLabelVal = getTok().getIntVal();
909 if (LocalLabelVal < 0) {
910 if (!TheCondState.Ignore)
911 return TokError("unexpected token at start of statement");
912 IDVal = "";
913 }
914 else {
915 IDVal = getTok().getString();
916 Lex(); // Consume the integer token to be used as an identifier token.
917 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000918 if (!TheCondState.Ignore)
919 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000920 }
921 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000922
923 } else if (Lexer.is(AsmToken::Dot)) {
924 // Treat '.' as a valid identifier in this context.
925 Lex();
926 IDVal = ".";
927
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000928 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000929 if (!TheCondState.Ignore)
930 return TokError("unexpected token at start of statement");
931 IDVal = "";
932 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000933
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000934
Chris Lattner7834fac2010-04-17 18:14:27 +0000935 // Handle conditional assembly here before checking for skipping. We
936 // have to do this so that .endif isn't skipped in a ".if 0" block for
937 // example.
938 if (IDVal == ".if")
939 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000940 if (IDVal == ".ifdef")
941 return ParseDirectiveIfdef(IDLoc, true);
942 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
943 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000944 if (IDVal == ".elseif")
945 return ParseDirectiveElseIf(IDLoc);
946 if (IDVal == ".else")
947 return ParseDirectiveElse(IDLoc);
948 if (IDVal == ".endif")
949 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000950
Chris Lattner7834fac2010-04-17 18:14:27 +0000951 // If we are in a ".if 0" block, ignore this statement.
952 if (TheCondState.Ignore) {
953 EatToEndOfStatement();
954 return false;
955 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000956
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000957 // FIXME: Recurse on local labels?
958
959 // See what kind of statement we have.
960 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000961 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000962 CheckForValidSection();
963
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000964 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000965 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000966
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000967 // Diagnose attempt to use '.' as a label.
968 if (IDVal == ".")
969 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
970
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000971 // Diagnose attempt to use a variable as a label.
972 //
973 // FIXME: Diagnostics. Note the location of the definition as a label.
974 // FIXME: This doesn't diagnose assignment to a symbol which has been
975 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000976 MCSymbol *Sym;
977 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000978 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000979 else
980 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000981 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000982 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000983
Daniel Dunbar959fd882009-08-26 22:13:22 +0000984 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000985 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000986
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000987 // Consume any end of statement token, if present, to avoid spurious
988 // AddBlankLine calls().
989 if (Lexer.is(AsmToken::EndOfStatement)) {
990 Lex();
991 if (Lexer.is(AsmToken::Eof))
992 return false;
993 }
994
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000995 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000996 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000997
Daniel Dunbar3f872332009-07-28 16:08:33 +0000998 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000999 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001000 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001001
Nico Weber4c4c7322011-01-28 03:04:41 +00001002 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001003
1004 default: // Normal instruction or directive.
1005 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001006 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001007
1008 // If macros are enabled, check to see if this is a macro instantiation.
1009 if (MacrosEnabled)
1010 if (const Macro *M = MacroMap.lookup(IDVal))
1011 return HandleMacroEntry(IDVal, IDLoc, M);
1012
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001014 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001015 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001016 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001017 return ParseDirectiveSet(IDVal, true);
1018 if (IDVal == ".equiv")
1019 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001020
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 // Data directives
1022
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001023 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001024 return ParseDirectiveAscii(IDVal, false);
1025 if (IDVal == ".asciz" || IDVal == ".string")
1026 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001027
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001028 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001030 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001031 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001032 if (IDVal == ".value")
1033 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001034 if (IDVal == ".2byte")
1035 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001036 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001038 if (IDVal == ".int")
1039 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001040 if (IDVal == ".4byte")
1041 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001042 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001044 if (IDVal == ".8byte")
1045 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001046 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001047 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1048 if (IDVal == ".double")
1049 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001050
Eli Friedman5d68ec22010-07-19 04:17:25 +00001051 if (IDVal == ".align") {
1052 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1053 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1054 }
1055 if (IDVal == ".align32") {
1056 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1057 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1058 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001059 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001060 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001061 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001062 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001063 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001064 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001065 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001066 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001067 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001068 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001069 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001070 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1071
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001072 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001073 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001074
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001075 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001076 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001077 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001079 if (IDVal == ".zero")
1080 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001082 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001083
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001084 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001085 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001086 // ELF only? Should it be here?
1087 if (IDVal == ".local")
1088 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001089 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001090 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001091 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001092 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001093 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001094 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001095 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001096 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001098 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001099 if (IDVal == ".symbol_resolver")
1100 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001101 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001102 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001103 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001104 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001105 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001106 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001107 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001108 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001110 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001112 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001113 if (IDVal == ".weak_def_can_be_hidden")
1114 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001115
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001116 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001117 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001118 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001119 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001120
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001121 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001122 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001123 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001124 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001125
Roman Divackybb6d14f2011-01-31 21:19:43 +00001126 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001127 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001128
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001129 // Look up the handler in the handler table.
1130 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1131 DirectiveMap.lookup(IDVal);
1132 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001133 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001134
Kevin Enderby9c656452009-09-10 20:51:44 +00001135 // Target hook for parsing target specific directives.
1136 if (!getTargetParser().ParseDirective(ID))
1137 return false;
1138
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001139 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001140 EatToEndOfStatement();
1141 return false;
1142 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001143
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001144 CheckForValidSection();
1145
Chris Lattnera7f13542010-05-19 23:34:33 +00001146 // Canonicalize the opcode to lower case.
1147 SmallString<128> Opcode;
1148 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1149 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001150
Chris Lattner98986712010-01-14 22:21:20 +00001151 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001152 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001153 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001154
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001155 // Dump the parsed representation, if requested.
1156 if (getShowParsedOperands()) {
1157 SmallString<256> Str;
1158 raw_svector_ostream OS(Str);
1159 OS << "parsed instruction: [";
1160 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1161 if (i != 0)
1162 OS << ", ";
1163 ParsedOperands[i]->dump(OS);
1164 }
1165 OS << "]";
1166
1167 PrintMessage(IDLoc, OS.str(), "note");
1168 }
1169
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001170 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001171 if (!HadError)
1172 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1173 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001174
Chris Lattner98986712010-01-14 22:21:20 +00001175 // Free any parsed operands.
1176 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1177 delete ParsedOperands[i];
1178
Chris Lattnercbf8a982010-09-11 16:18:25 +00001179 // Don't skip the rest of the line, the instruction parser is responsible for
1180 // that.
1181 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001182}
Chris Lattner9a023f72009-06-24 04:43:34 +00001183
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001184MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1185 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001186 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1187{
1188 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1189 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001190 SmallString<256> Buf;
1191 raw_svector_ostream OS(Buf);
1192
1193 StringRef Body = M->Body;
1194 while (!Body.empty()) {
1195 // Scan for the next substitution.
1196 std::size_t End = Body.size(), Pos = 0;
1197 for (; Pos != End; ++Pos) {
1198 // Check for a substitution or escape.
1199 if (Body[Pos] != '$' || Pos + 1 == End)
1200 continue;
1201
1202 char Next = Body[Pos + 1];
1203 if (Next == '$' || Next == 'n' || isdigit(Next))
1204 break;
1205 }
1206
1207 // Add the prefix.
1208 OS << Body.slice(0, Pos);
1209
1210 // Check if we reached the end.
1211 if (Pos == End)
1212 break;
1213
1214 switch (Body[Pos+1]) {
1215 // $$ => $
1216 case '$':
1217 OS << '$';
1218 break;
1219
1220 // $n => number of arguments
1221 case 'n':
1222 OS << A.size();
1223 break;
1224
1225 // $[0-9] => argument
1226 default: {
1227 // Missing arguments are ignored.
1228 unsigned Index = Body[Pos+1] - '0';
1229 if (Index >= A.size())
1230 break;
1231
1232 // Otherwise substitute with the token values, with spaces eliminated.
1233 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1234 ie = A[Index].end(); it != ie; ++it)
1235 OS << it->getString();
1236 break;
1237 }
1238 }
1239
1240 // Update the scan point.
1241 Body = Body.substr(Pos + 2);
1242 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001243
1244 // We include the .endmacro in the buffer as our queue to exit the macro
1245 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001246 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001247
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001248 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001249}
1250
1251bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1252 const Macro *M) {
1253 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1254 // this, although we should protect against infinite loops.
1255 if (ActiveMacros.size() == 20)
1256 return TokError("macros cannot be nested more than 20 levels deep");
1257
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001258 // Parse the macro instantiation arguments.
1259 std::vector<std::vector<AsmToken> > MacroArguments;
1260 MacroArguments.push_back(std::vector<AsmToken>());
1261 unsigned ParenLevel = 0;
1262 for (;;) {
1263 if (Lexer.is(AsmToken::Eof))
1264 return TokError("unexpected token in macro instantiation");
1265 if (Lexer.is(AsmToken::EndOfStatement))
1266 break;
1267
1268 // If we aren't inside parentheses and this is a comma, start a new token
1269 // list.
1270 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1271 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001272 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001273 // Adjust the current parentheses level.
1274 if (Lexer.is(AsmToken::LParen))
1275 ++ParenLevel;
1276 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1277 --ParenLevel;
1278
1279 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001280 MacroArguments.back().push_back(getTok());
1281 }
1282 Lex();
1283 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001284
1285 // Create the macro instantiation object and add to the current macro
1286 // instantiation stack.
1287 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001288 getTok().getLoc(),
1289 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001290 ActiveMacros.push_back(MI);
1291
1292 // Jump to the macro instantiation and prime the lexer.
1293 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1294 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1295 Lex();
1296
1297 return false;
1298}
1299
1300void AsmParser::HandleMacroExit() {
1301 // Jump to the EndOfStatement we should return to, and consume it.
1302 JumpToLoc(ActiveMacros.back()->ExitLoc);
1303 Lex();
1304
1305 // Pop the instantiation entry.
1306 delete ActiveMacros.back();
1307 ActiveMacros.pop_back();
1308}
1309
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001310static void MarkUsed(const MCExpr *Value) {
1311 switch (Value->getKind()) {
1312 case MCExpr::Binary:
1313 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1314 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1315 break;
1316 case MCExpr::Target:
1317 case MCExpr::Constant:
1318 break;
1319 case MCExpr::SymbolRef: {
1320 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1321 break;
1322 }
1323 case MCExpr::Unary:
1324 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1325 break;
1326 }
1327}
1328
Nico Weber4c4c7322011-01-28 03:04:41 +00001329bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001330 // FIXME: Use better location, we should use proper tokens.
1331 SMLoc EqualLoc = Lexer.getLoc();
1332
Daniel Dunbar821e3332009-08-31 08:09:28 +00001333 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001334 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001335 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001336
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001337 MarkUsed(Value);
1338
Daniel Dunbar3f872332009-07-28 16:08:33 +00001339 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001340 return TokError("unexpected token in assignment");
1341
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001342 // Error on assignment to '.'.
1343 if (Name == ".") {
1344 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1345 "(use '.space' or '.org').)"));
1346 }
1347
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001348 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001349 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001350
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001351 // Validate that the LHS is allowed to be a variable (either it has not been
1352 // used as a symbol, or it is an absolute symbol).
1353 MCSymbol *Sym = getContext().LookupSymbol(Name);
1354 if (Sym) {
1355 // Diagnose assignment to a label.
1356 //
1357 // FIXME: Diagnostics. Note the location of the definition as a label.
1358 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001359 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001360 ; // Allow redefinitions of undefined symbols only used in directives.
Nico Weber4c4c7322011-01-28 03:04:41 +00001361 else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001362 return Error(EqualLoc, "redefinition of '" + Name + "'");
1363 else if (!Sym->isVariable())
1364 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001365 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001366 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1367 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001368
1369 // Don't count these checks as uses.
1370 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001371 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001372 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001373
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001374 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001375
1376 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001377 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001378
1379 return false;
1380}
1381
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001382/// ParseIdentifier:
1383/// ::= identifier
1384/// ::= string
1385bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001386 // The assembler has relaxed rules for accepting identifiers, in particular we
1387 // allow things like '.globl $foo', which would normally be separate
1388 // tokens. At this level, we have already lexed so we cannot (currently)
1389 // handle this as a context dependent token, instead we detect adjacent tokens
1390 // and return the combined identifier.
1391 if (Lexer.is(AsmToken::Dollar)) {
1392 SMLoc DollarLoc = getLexer().getLoc();
1393
1394 // Consume the dollar sign, and check for a following identifier.
1395 Lex();
1396 if (Lexer.isNot(AsmToken::Identifier))
1397 return true;
1398
1399 // We have a '$' followed by an identifier, make sure they are adjacent.
1400 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1401 return true;
1402
1403 // Construct the joined identifier and consume the token.
1404 Res = StringRef(DollarLoc.getPointer(),
1405 getTok().getIdentifier().size() + 1);
1406 Lex();
1407 return false;
1408 }
1409
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001410 if (Lexer.isNot(AsmToken::Identifier) &&
1411 Lexer.isNot(AsmToken::String))
1412 return true;
1413
Sean Callanan18b83232010-01-19 21:44:56 +00001414 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001415
Sean Callanan79ed1a82010-01-19 20:22:31 +00001416 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001417
1418 return false;
1419}
1420
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001421/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001422/// ::= .equ identifier ',' expression
1423/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001424/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001425bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001426 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001427
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001428 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001429 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001430
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001431 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001432 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001433 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001434
Nico Weber4c4c7322011-01-28 03:04:41 +00001435 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001436}
1437
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001438bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001439 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001440
1441 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001442 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001443 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1444 if (Str[i] != '\\') {
1445 Data += Str[i];
1446 continue;
1447 }
1448
1449 // Recognize escaped characters. Note that this escape semantics currently
1450 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1451 ++i;
1452 if (i == e)
1453 return TokError("unexpected backslash at end of string");
1454
1455 // Recognize octal sequences.
1456 if ((unsigned) (Str[i] - '0') <= 7) {
1457 // Consume up to three octal characters.
1458 unsigned Value = Str[i] - '0';
1459
1460 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1461 ++i;
1462 Value = Value * 8 + (Str[i] - '0');
1463
1464 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1465 ++i;
1466 Value = Value * 8 + (Str[i] - '0');
1467 }
1468 }
1469
1470 if (Value > 255)
1471 return TokError("invalid octal escape sequence (out of range)");
1472
1473 Data += (unsigned char) Value;
1474 continue;
1475 }
1476
1477 // Otherwise recognize individual escapes.
1478 switch (Str[i]) {
1479 default:
1480 // Just reject invalid escape sequences for now.
1481 return TokError("invalid escape sequence (unrecognized character)");
1482
1483 case 'b': Data += '\b'; break;
1484 case 'f': Data += '\f'; break;
1485 case 'n': Data += '\n'; break;
1486 case 'r': Data += '\r'; break;
1487 case 't': Data += '\t'; break;
1488 case '"': Data += '"'; break;
1489 case '\\': Data += '\\'; break;
1490 }
1491 }
1492
1493 return false;
1494}
1495
Daniel Dunbara0d14262009-06-24 23:30:00 +00001496/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001497/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1498bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001499 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001500 CheckForValidSection();
1501
Daniel Dunbara0d14262009-06-24 23:30:00 +00001502 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001503 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001504 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001505
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001506 std::string Data;
1507 if (ParseEscapedString(Data))
1508 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001509
1510 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001511 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001512 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1513
Sean Callanan79ed1a82010-01-19 20:22:31 +00001514 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001515
1516 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001517 break;
1518
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001519 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001520 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001521 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001522 }
1523 }
1524
Sean Callanan79ed1a82010-01-19 20:22:31 +00001525 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001526 return false;
1527}
1528
1529/// ParseDirectiveValue
1530/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1531bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001533 CheckForValidSection();
1534
Daniel Dunbara0d14262009-06-24 23:30:00 +00001535 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001536 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001537 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001538 return true;
1539
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001540 // Special case constant expressions to match code generator.
1541 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001542 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001543 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001544 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001545
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001546 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001547 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001548
Daniel Dunbara0d14262009-06-24 23:30:00 +00001549 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001550 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001551 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001552 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001553 }
1554 }
1555
Sean Callanan79ed1a82010-01-19 20:22:31 +00001556 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001557 return false;
1558}
1559
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001560/// ParseDirectiveRealValue
1561/// ::= (.single | .double) [ expression (, expression)* ]
1562bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1563 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1564 CheckForValidSection();
1565
1566 for (;;) {
1567 // We don't truly support arithmetic on floating point expressions, so we
1568 // have to manually parse unary prefixes.
1569 bool IsNeg = false;
1570 if (getLexer().is(AsmToken::Minus)) {
1571 Lex();
1572 IsNeg = true;
1573 } else if (getLexer().is(AsmToken::Plus))
1574 Lex();
1575
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001576 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001577 getLexer().isNot(AsmToken::Real) &&
1578 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001579 return TokError("unexpected token in directive");
1580
1581 // Convert to an APFloat.
1582 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001583 StringRef IDVal = getTok().getString();
1584 if (getLexer().is(AsmToken::Identifier)) {
1585 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1586 Value = APFloat::getInf(Semantics);
1587 else if (!IDVal.compare_lower("nan"))
1588 Value = APFloat::getNaN(Semantics, false, ~0);
1589 else
1590 return TokError("invalid floating point literal");
1591 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001592 APFloat::opInvalidOp)
1593 return TokError("invalid floating point literal");
1594 if (IsNeg)
1595 Value.changeSign();
1596
1597 // Consume the numeric token.
1598 Lex();
1599
1600 // Emit the value as an integer.
1601 APInt AsInt = Value.bitcastToAPInt();
1602 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1603 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1604
1605 if (getLexer().is(AsmToken::EndOfStatement))
1606 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001607
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001608 if (getLexer().isNot(AsmToken::Comma))
1609 return TokError("unexpected token in directive");
1610 Lex();
1611 }
1612 }
1613
1614 Lex();
1615 return false;
1616}
1617
Daniel Dunbara0d14262009-06-24 23:30:00 +00001618/// ParseDirectiveSpace
1619/// ::= .space expression [ , expression ]
1620bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001621 CheckForValidSection();
1622
Daniel Dunbara0d14262009-06-24 23:30:00 +00001623 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001624 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001625 return true;
1626
1627 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001628 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1629 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001630 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001631 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001632
Daniel Dunbar475839e2009-06-29 20:37:27 +00001633 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001634 return true;
1635
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001637 return TokError("unexpected token in '.space' directive");
1638 }
1639
Sean Callanan79ed1a82010-01-19 20:22:31 +00001640 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001641
1642 if (NumBytes <= 0)
1643 return TokError("invalid number of bytes in '.space' directive");
1644
1645 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001646 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001647
1648 return false;
1649}
1650
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001651/// ParseDirectiveZero
1652/// ::= .zero expression
1653bool AsmParser::ParseDirectiveZero() {
1654 CheckForValidSection();
1655
1656 int64_t NumBytes;
1657 if (ParseAbsoluteExpression(NumBytes))
1658 return true;
1659
Rafael Espindolae452b172010-10-05 19:42:57 +00001660 int64_t Val = 0;
1661 if (getLexer().is(AsmToken::Comma)) {
1662 Lex();
1663 if (ParseAbsoluteExpression(Val))
1664 return true;
1665 }
1666
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001667 if (getLexer().isNot(AsmToken::EndOfStatement))
1668 return TokError("unexpected token in '.zero' directive");
1669
1670 Lex();
1671
Rafael Espindolae452b172010-10-05 19:42:57 +00001672 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001673
1674 return false;
1675}
1676
Daniel Dunbara0d14262009-06-24 23:30:00 +00001677/// ParseDirectiveFill
1678/// ::= .fill expression , expression , expression
1679bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001680 CheckForValidSection();
1681
Daniel Dunbara0d14262009-06-24 23:30:00 +00001682 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001683 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001684 return true;
1685
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001686 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001687 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001688 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001689
Daniel Dunbara0d14262009-06-24 23:30:00 +00001690 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001691 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001692 return true;
1693
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001695 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001696 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001697
Daniel Dunbara0d14262009-06-24 23:30:00 +00001698 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001699 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001700 return true;
1701
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001702 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001703 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001704
Sean Callanan79ed1a82010-01-19 20:22:31 +00001705 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001706
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001707 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1708 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001709
1710 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001711 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001712
1713 return false;
1714}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001715
1716/// ParseDirectiveOrg
1717/// ::= .org expression [ , expression ]
1718bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001719 CheckForValidSection();
1720
Daniel Dunbar821e3332009-08-31 08:09:28 +00001721 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001722 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001723 return true;
1724
1725 // Parse optional fill expression.
1726 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001727 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1728 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001729 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001730 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001731
Daniel Dunbar475839e2009-06-29 20:37:27 +00001732 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001733 return true;
1734
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001735 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001736 return TokError("unexpected token in '.org' directive");
1737 }
1738
Sean Callanan79ed1a82010-01-19 20:22:31 +00001739 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001740
1741 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1742 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001743 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001744
1745 return false;
1746}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001747
1748/// ParseDirectiveAlign
1749/// ::= {.align, ...} expression [ , expression [ , expression ]]
1750bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001751 CheckForValidSection();
1752
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001753 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001754 int64_t Alignment;
1755 if (ParseAbsoluteExpression(Alignment))
1756 return true;
1757
1758 SMLoc MaxBytesLoc;
1759 bool HasFillExpr = false;
1760 int64_t FillExpr = 0;
1761 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001762 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1763 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001764 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001765 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001766
1767 // The fill expression can be omitted while specifying a maximum number of
1768 // alignment bytes, e.g:
1769 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001770 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001771 HasFillExpr = true;
1772 if (ParseAbsoluteExpression(FillExpr))
1773 return true;
1774 }
1775
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001776 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1777 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001778 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001779 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001780
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001782 if (ParseAbsoluteExpression(MaxBytesToFill))
1783 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001784
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001785 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001786 return TokError("unexpected token in directive");
1787 }
1788 }
1789
Sean Callanan79ed1a82010-01-19 20:22:31 +00001790 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001791
Daniel Dunbar648ac512010-05-17 21:54:30 +00001792 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001793 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001794
1795 // Compute alignment in bytes.
1796 if (IsPow2) {
1797 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001798 if (Alignment >= 32) {
1799 Error(AlignmentLoc, "invalid alignment value");
1800 Alignment = 31;
1801 }
1802
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001803 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001804 }
1805
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001806 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001807 if (MaxBytesLoc.isValid()) {
1808 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001809 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1810 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001811 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001812 }
1813
1814 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001815 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1816 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001817 MaxBytesToFill = 0;
1818 }
1819 }
1820
Daniel Dunbar648ac512010-05-17 21:54:30 +00001821 // Check whether we should use optimal code alignment for this .align
1822 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001823 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001824 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1825 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001826 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001827 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001828 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001829 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1830 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001831 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001832
1833 return false;
1834}
1835
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001836/// ParseDirectiveSymbolAttribute
1837/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001838bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001839 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001840 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001841 StringRef Name;
1842
1843 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001844 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001845
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001846 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001847
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001848 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001849
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001850 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001851 break;
1852
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001853 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001854 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001855 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001856 }
1857 }
1858
Sean Callanan79ed1a82010-01-19 20:22:31 +00001859 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001860 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001861}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001862
1863/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001864/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1865bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001866 CheckForValidSection();
1867
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001868 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001869 StringRef Name;
1870 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001871 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001872
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001873 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001874 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001875
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001876 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001877 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001878 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001879
1880 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001881 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001882 if (ParseAbsoluteExpression(Size))
1883 return true;
1884
1885 int64_t Pow2Alignment = 0;
1886 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001887 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001888 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001889 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001890 if (ParseAbsoluteExpression(Pow2Alignment))
1891 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001892
Chris Lattner258281d2010-01-19 06:22:22 +00001893 // If this target takes alignments in bytes (not log) validate and convert.
1894 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1895 if (!isPowerOf2_64(Pow2Alignment))
1896 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1897 Pow2Alignment = Log2_64(Pow2Alignment);
1898 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001899 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001900
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001901 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001902 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001903
Sean Callanan79ed1a82010-01-19 20:22:31 +00001904 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001905
Chris Lattner1fc3d752009-07-09 17:25:12 +00001906 // NOTE: a size of zero for a .comm should create a undefined symbol
1907 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001908 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001909 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1910 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001911
Eric Christopherc260a3e2010-05-14 01:38:54 +00001912 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001913 // may internally end up wanting an alignment in bytes.
1914 // FIXME: Diagnose overflow.
1915 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001916 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1917 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001918
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001919 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001920 return Error(IDLoc, "invalid symbol redefinition");
1921
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001922 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001923 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001924 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001925 getStreamer().EmitZerofill(Ctx.getMachOSection(
1926 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1927 0, SectionKind::getBSS()),
1928 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001929 return false;
1930 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001931
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001933 return false;
1934}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001935
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001936/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001937/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001938bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001939 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001940 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001941
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001942 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001943 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001944 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001945
Sean Callanan79ed1a82010-01-19 20:22:31 +00001946 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001947
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001948 if (Str.empty())
1949 Error(Loc, ".abort detected. Assembly stopping.");
1950 else
1951 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001952 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001953
1954 return false;
1955}
Kevin Enderby71148242009-07-14 21:35:03 +00001956
Kevin Enderby1f049b22009-07-14 23:21:55 +00001957/// ParseDirectiveInclude
1958/// ::= .include "filename"
1959bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001960 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001961 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001962
Sean Callanan18b83232010-01-19 21:44:56 +00001963 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001964 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001965 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001966
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001967 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001968 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001969
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001970 // Strip the quotes.
1971 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001972
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001973 // Attempt to switch the lexer to the included file before consuming the end
1974 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001975 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001976 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001977 return true;
1978 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001979
1980 return false;
1981}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001982
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001983/// ParseDirectiveIf
1984/// ::= .if expression
1985bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001986 TheCondStack.push_back(TheCondState);
1987 TheCondState.TheCond = AsmCond::IfCond;
1988 if(TheCondState.Ignore) {
1989 EatToEndOfStatement();
1990 }
1991 else {
1992 int64_t ExprValue;
1993 if (ParseAbsoluteExpression(ExprValue))
1994 return true;
1995
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001996 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001997 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001998
Sean Callanan79ed1a82010-01-19 20:22:31 +00001999 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002000
2001 TheCondState.CondMet = ExprValue;
2002 TheCondState.Ignore = !TheCondState.CondMet;
2003 }
2004
2005 return false;
2006}
2007
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002008bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2009 StringRef Name;
2010 TheCondStack.push_back(TheCondState);
2011 TheCondState.TheCond = AsmCond::IfCond;
2012
2013 if (TheCondState.Ignore) {
2014 EatToEndOfStatement();
2015 } else {
2016 if (ParseIdentifier(Name))
2017 return TokError("expected identifier after '.ifdef'");
2018
2019 Lex();
2020
2021 MCSymbol *Sym = getContext().LookupSymbol(Name);
2022
2023 if (expect_defined)
2024 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2025 else
2026 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2027 TheCondState.Ignore = !TheCondState.CondMet;
2028 }
2029
2030 return false;
2031}
2032
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002033/// ParseDirectiveElseIf
2034/// ::= .elseif expression
2035bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2036 if (TheCondState.TheCond != AsmCond::IfCond &&
2037 TheCondState.TheCond != AsmCond::ElseIfCond)
2038 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2039 " an .elseif");
2040 TheCondState.TheCond = AsmCond::ElseIfCond;
2041
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002042 bool LastIgnoreState = false;
2043 if (!TheCondStack.empty())
2044 LastIgnoreState = TheCondStack.back().Ignore;
2045 if (LastIgnoreState || TheCondState.CondMet) {
2046 TheCondState.Ignore = true;
2047 EatToEndOfStatement();
2048 }
2049 else {
2050 int64_t ExprValue;
2051 if (ParseAbsoluteExpression(ExprValue))
2052 return true;
2053
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002054 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002055 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002056
Sean Callanan79ed1a82010-01-19 20:22:31 +00002057 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002058 TheCondState.CondMet = ExprValue;
2059 TheCondState.Ignore = !TheCondState.CondMet;
2060 }
2061
2062 return false;
2063}
2064
2065/// ParseDirectiveElse
2066/// ::= .else
2067bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002068 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002069 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002070
Sean Callanan79ed1a82010-01-19 20:22:31 +00002071 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002072
2073 if (TheCondState.TheCond != AsmCond::IfCond &&
2074 TheCondState.TheCond != AsmCond::ElseIfCond)
2075 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2076 ".elseif");
2077 TheCondState.TheCond = AsmCond::ElseCond;
2078 bool LastIgnoreState = false;
2079 if (!TheCondStack.empty())
2080 LastIgnoreState = TheCondStack.back().Ignore;
2081 if (LastIgnoreState || TheCondState.CondMet)
2082 TheCondState.Ignore = true;
2083 else
2084 TheCondState.Ignore = false;
2085
2086 return false;
2087}
2088
2089/// ParseDirectiveEndIf
2090/// ::= .endif
2091bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002092 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002093 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002094
Sean Callanan79ed1a82010-01-19 20:22:31 +00002095 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002096
2097 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2098 TheCondStack.empty())
2099 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2100 ".else");
2101 if (!TheCondStack.empty()) {
2102 TheCondState = TheCondStack.back();
2103 TheCondStack.pop_back();
2104 }
2105
2106 return false;
2107}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002108
2109/// ParseDirectiveFile
2110/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002111bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002112 // FIXME: I'm not sure what this is.
2113 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002114 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002115 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002116 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002117 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002118
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002119 if (FileNumber < 1)
2120 return TokError("file number less than one");
2121 }
2122
Daniel Dunbareceec052010-07-12 17:45:27 +00002123 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002124 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002125
Chris Lattnerd32e8032010-01-25 19:02:58 +00002126 StringRef Filename = getTok().getString();
2127 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002128 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002129
Daniel Dunbareceec052010-07-12 17:45:27 +00002130 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002131 return TokError("unexpected token in '.file' directive");
2132
Chris Lattnerd32e8032010-01-25 19:02:58 +00002133 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002134 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002135 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002136 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002137 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002138 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002139
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002140 return false;
2141}
2142
2143/// ParseDirectiveLine
2144/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002145bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002146 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2147 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002148 return TokError("unexpected token in '.line' directive");
2149
Sean Callanan18b83232010-01-19 21:44:56 +00002150 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002151 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002152 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002153
2154 // FIXME: Do something with the .line.
2155 }
2156
Daniel Dunbareceec052010-07-12 17:45:27 +00002157 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002158 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002159
2160 return false;
2161}
2162
2163
2164/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002165/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002166/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2167/// The first number is a file number, must have been previously assigned with
2168/// a .file directive, the second number is the line number and optionally the
2169/// third number is a column position (zero if not specified). The remaining
2170/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002171bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002172
Daniel Dunbareceec052010-07-12 17:45:27 +00002173 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002174 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002175 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002176 if (FileNumber < 1)
2177 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002178 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002179 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002180 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002181
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002182 int64_t LineNumber = 0;
2183 if (getLexer().is(AsmToken::Integer)) {
2184 LineNumber = getTok().getIntVal();
2185 if (LineNumber < 1)
2186 return TokError("line number less than one in '.loc' directive");
2187 Lex();
2188 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002189
2190 int64_t ColumnPos = 0;
2191 if (getLexer().is(AsmToken::Integer)) {
2192 ColumnPos = getTok().getIntVal();
2193 if (ColumnPos < 0)
2194 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002195 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002196 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002197
Kevin Enderbyc0957932010-09-30 16:52:03 +00002198 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002199 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002200 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002201 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2202 for (;;) {
2203 if (getLexer().is(AsmToken::EndOfStatement))
2204 break;
2205
2206 StringRef Name;
2207 SMLoc Loc = getTok().getLoc();
2208 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002209 return TokError("unexpected token in '.loc' directive");
2210
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002211 if (Name == "basic_block")
2212 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2213 else if (Name == "prologue_end")
2214 Flags |= DWARF2_FLAG_PROLOGUE_END;
2215 else if (Name == "epilogue_begin")
2216 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2217 else if (Name == "is_stmt") {
2218 SMLoc Loc = getTok().getLoc();
2219 const MCExpr *Value;
2220 if (getParser().ParseExpression(Value))
2221 return true;
2222 // The expression must be the constant 0 or 1.
2223 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2224 int Value = MCE->getValue();
2225 if (Value == 0)
2226 Flags &= ~DWARF2_FLAG_IS_STMT;
2227 else if (Value == 1)
2228 Flags |= DWARF2_FLAG_IS_STMT;
2229 else
2230 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002231 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002232 else {
2233 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2234 }
2235 }
2236 else if (Name == "isa") {
2237 SMLoc Loc = getTok().getLoc();
2238 const MCExpr *Value;
2239 if (getParser().ParseExpression(Value))
2240 return true;
2241 // The expression must be a constant greater or equal to 0.
2242 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2243 int Value = MCE->getValue();
2244 if (Value < 0)
2245 return Error(Loc, "isa number less than zero");
2246 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002247 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002248 else {
2249 return Error(Loc, "isa number not a constant value");
2250 }
2251 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002252 else if (Name == "discriminator") {
2253 if (getParser().ParseAbsoluteExpression(Discriminator))
2254 return true;
2255 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002256 else {
2257 return Error(Loc, "unknown sub-directive in '.loc' directive");
2258 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002259
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002260 if (getLexer().is(AsmToken::EndOfStatement))
2261 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002262 }
2263 }
2264
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002265 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2266 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002267
2268 return false;
2269}
2270
Daniel Dunbar138abae2010-10-16 04:56:42 +00002271/// ParseDirectiveStabs
2272/// ::= .stabs string, number, number, number
2273bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2274 SMLoc DirectiveLoc) {
2275 return TokError("unsupported directive '" + Directive + "'");
2276}
2277
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002278/// ParseDirectiveCFIStartProc
2279/// ::= .cfi_startproc
2280bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2281 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002282 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002283}
2284
2285/// ParseDirectiveCFIEndProc
2286/// ::= .cfi_endproc
2287bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002288 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002289}
2290
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002291/// ParseRegisterOrRegisterNumber - parse register name or number.
2292bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2293 SMLoc DirectiveLoc) {
2294 unsigned RegNo;
2295
2296 if (getLexer().is(AsmToken::Percent)) {
2297 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2298 DirectiveLoc))
2299 return true;
2300 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2301 } else
2302 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002303
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002304 return false;
2305}
2306
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002307/// ParseDirectiveCFIDefCfa
2308/// ::= .cfi_def_cfa register, offset
2309bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2310 SMLoc DirectiveLoc) {
2311 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002312 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002313 return true;
2314
2315 if (getLexer().isNot(AsmToken::Comma))
2316 return TokError("unexpected token in directive");
2317 Lex();
2318
2319 int64_t Offset = 0;
2320 if (getParser().ParseAbsoluteExpression(Offset))
2321 return true;
2322
2323 return getStreamer().EmitCFIDefCfa(Register, Offset);
2324}
2325
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002326/// ParseDirectiveCFIDefCfaOffset
2327/// ::= .cfi_def_cfa_offset offset
2328bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2329 SMLoc DirectiveLoc) {
2330 int64_t Offset = 0;
2331 if (getParser().ParseAbsoluteExpression(Offset))
2332 return true;
2333
Rafael Espindola53abbe52011-04-11 20:29:16 +00002334 getParser().setLastOffset(Offset);
2335
2336 return getStreamer().EmitCFIDefCfaOffset(Offset);
2337}
2338
2339/// ParseDirectiveCFIAdjustCfaOffset
2340/// ::= .cfi_adjust_cfa_offset adjustment
2341bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2342 SMLoc DirectiveLoc) {
2343 int64_t Adjustment = 0;
2344 if (getParser().ParseAbsoluteExpression(Adjustment))
2345 return true;
2346
2347 int64_t Offset = getParser().adjustLastOffset(Adjustment);
2348
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002349 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002350}
2351
2352/// ParseDirectiveCFIDefCfaRegister
2353/// ::= .cfi_def_cfa_register register
2354bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2355 SMLoc DirectiveLoc) {
2356 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002357 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002358 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002359
2360 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002361}
2362
2363/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002364/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002365bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2366 int64_t Register = 0;
2367 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002368
2369 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002370 return true;
2371
2372 if (getLexer().isNot(AsmToken::Comma))
2373 return TokError("unexpected token in directive");
2374 Lex();
2375
2376 if (getParser().ParseAbsoluteExpression(Offset))
2377 return true;
2378
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002379 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002380}
2381
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002382/// ParseDirectiveCFIRelOffset
2383/// ::= .cfi_rel_offset register, offset
2384bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2385 SMLoc DirectiveLoc) {
2386 int64_t Register = 0;
2387
2388 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2389 return true;
2390
2391 if (getLexer().isNot(AsmToken::Comma))
2392 return TokError("unexpected token in directive");
2393 Lex();
2394
2395 int64_t Offset = 0;
2396 if (getParser().ParseAbsoluteExpression(Offset))
2397 return true;
2398
2399 Offset -= getParser().getLastOffset();
2400
2401 return getStreamer().EmitCFIOffset(Register, Offset);
2402}
2403
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002404static bool isValidEncoding(int64_t Encoding) {
2405 if (Encoding & ~0xff)
2406 return false;
2407
2408 if (Encoding == dwarf::DW_EH_PE_omit)
2409 return true;
2410
2411 const unsigned Format = Encoding & 0xf;
2412 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2413 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2414 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2415 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2416 return false;
2417
Rafael Espindolacaf11582010-12-29 04:31:26 +00002418 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002419 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002420 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002421 return false;
2422
2423 return true;
2424}
2425
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002426/// ParseDirectiveCFIPersonalityOrLsda
2427/// ::= .cfi_personality encoding, [symbol_name]
2428/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002429bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002430 SMLoc DirectiveLoc) {
2431 int64_t Encoding = 0;
2432 if (getParser().ParseAbsoluteExpression(Encoding))
2433 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002434 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002435 return false;
2436
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002437 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002438 return TokError("unsupported encoding.");
2439
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002440 if (getLexer().isNot(AsmToken::Comma))
2441 return TokError("unexpected token in directive");
2442 Lex();
2443
2444 StringRef Name;
2445 if (getParser().ParseIdentifier(Name))
2446 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002447
2448 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2449
2450 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002451 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002452 else {
2453 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002454 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002455 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002456}
2457
Rafael Espindolafe024d02010-12-28 18:36:23 +00002458/// ParseDirectiveCFIRememberState
2459/// ::= .cfi_remember_state
2460bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2461 SMLoc DirectiveLoc) {
2462 return getStreamer().EmitCFIRememberState();
2463}
2464
2465/// ParseDirectiveCFIRestoreState
2466/// ::= .cfi_remember_state
2467bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2468 SMLoc DirectiveLoc) {
2469 return getStreamer().EmitCFIRestoreState();
2470}
2471
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002472/// ParseDirectiveMacrosOnOff
2473/// ::= .macros_on
2474/// ::= .macros_off
2475bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2476 SMLoc DirectiveLoc) {
2477 if (getLexer().isNot(AsmToken::EndOfStatement))
2478 return Error(getLexer().getLoc(),
2479 "unexpected token in '" + Directive + "' directive");
2480
2481 getParser().MacrosEnabled = Directive == ".macros_on";
2482
2483 return false;
2484}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002485
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002486/// ParseDirectiveMacro
2487/// ::= .macro name
2488bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2489 SMLoc DirectiveLoc) {
2490 StringRef Name;
2491 if (getParser().ParseIdentifier(Name))
2492 return TokError("expected identifier in directive");
2493
2494 if (getLexer().isNot(AsmToken::EndOfStatement))
2495 return TokError("unexpected token in '.macro' directive");
2496
2497 // Eat the end of statement.
2498 Lex();
2499
2500 AsmToken EndToken, StartToken = getTok();
2501
2502 // Lex the macro definition.
2503 for (;;) {
2504 // Check whether we have reached the end of the file.
2505 if (getLexer().is(AsmToken::Eof))
2506 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2507
2508 // Otherwise, check whether we have reach the .endmacro.
2509 if (getLexer().is(AsmToken::Identifier) &&
2510 (getTok().getIdentifier() == ".endm" ||
2511 getTok().getIdentifier() == ".endmacro")) {
2512 EndToken = getTok();
2513 Lex();
2514 if (getLexer().isNot(AsmToken::EndOfStatement))
2515 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2516 "' directive");
2517 break;
2518 }
2519
2520 // Otherwise, scan til the end of the statement.
2521 getParser().EatToEndOfStatement();
2522 }
2523
2524 if (getParser().MacroMap.lookup(Name)) {
2525 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2526 }
2527
2528 const char *BodyStart = StartToken.getLoc().getPointer();
2529 const char *BodyEnd = EndToken.getLoc().getPointer();
2530 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2531 getParser().MacroMap[Name] = new Macro(Name, Body);
2532 return false;
2533}
2534
2535/// ParseDirectiveEndMacro
2536/// ::= .endm
2537/// ::= .endmacro
2538bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2539 SMLoc DirectiveLoc) {
2540 if (getLexer().isNot(AsmToken::EndOfStatement))
2541 return TokError("unexpected token in '" + Directive + "' directive");
2542
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002543 // If we are inside a macro instantiation, terminate the current
2544 // instantiation.
2545 if (!getParser().ActiveMacros.empty()) {
2546 getParser().HandleMacroExit();
2547 return false;
2548 }
2549
2550 // Otherwise, this .endmacro is a stray entry in the file; well formed
2551 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002552 return TokError("unexpected '" + Directive + "' in file, "
2553 "no current macro definition");
2554}
2555
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002556bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002557 getParser().CheckForValidSection();
2558
2559 const MCExpr *Value;
2560
2561 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002562 return true;
2563
2564 if (getLexer().isNot(AsmToken::EndOfStatement))
2565 return TokError("unexpected token in directive");
2566
2567 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002568 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002569 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002570 getStreamer().EmitULEB128Value(Value);
2571
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002572 return false;
2573}
2574
2575
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002576/// \brief Create an MCAsmParser instance.
2577MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2578 MCContext &C, MCStreamer &Out,
2579 const MCAsmInfo &MAI) {
2580 return new AsmParser(T, SM, C, Out, MAI);
2581}