blob: ea63a2227b427ab164e57043d2f2fd2b27cfed37 [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
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Chris Lattneraaec2052010-01-19 19:46:13 +000032
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000033namespace {
34
35/// \brief Generic implementations of directive handling, etc. which is shared
36/// (or the default, at least) for all assembler parser.
37class GenericAsmParser : public MCAsmParserExtension {
38public:
39 GenericAsmParser() {}
40
41 virtual void Initialize(MCAsmParser &Parser) {
42 // Call the base implementation.
43 this->MCAsmParserExtension::Initialize(Parser);
44
45 // Debugging directives.
46 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
47 &GenericAsmParser::ParseDirectiveFile));
48 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
49 &GenericAsmParser::ParseDirectiveLine));
50 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
51 &GenericAsmParser::ParseDirectiveLoc));
52 }
53
54 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
55 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
56 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
57};
58
59}
60
Chris Lattneraaec2052010-01-19 19:46:13 +000061enum { DEFAULT_ADDRSPACE = 0 };
62
Daniel Dunbar9186fa62010-07-01 20:41:56 +000063AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
64 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000065 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
66 GenericParser(new GenericAsmParser), TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000067 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000068
69 // Initialize the generic parser.
70 GenericParser->Initialize(*this);
Chris Lattnerebb89b42009-09-27 21:16:52 +000071}
72
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000073AsmParser::~AsmParser() {
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000074 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000075}
76
Daniel Dunbar53131982010-07-12 17:27:45 +000077void AsmParser::setTargetParser(TargetAsmParser &P) {
78 assert(!TargetParser && "Target parser is already initialized!");
79 TargetParser = &P;
80 TargetParser->Initialize(*this);
81}
82
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000083void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000084 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000085}
86
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000087bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000088 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000089 return true;
90}
91
Sean Callananbf2013e2010-01-20 23:19:55 +000092void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
93 const char *Type) const {
94 SrcMgr.PrintMessage(Loc, Msg, Type);
95}
Sean Callananfd0b0282010-01-21 00:19:58 +000096
97bool AsmParser::EnterIncludeFile(const std::string &Filename) {
98 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
99 if (NewBuf == -1)
100 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000101
Sean Callananfd0b0282010-01-21 00:19:58 +0000102 CurBuffer = NewBuf;
103
104 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
105
106 return false;
107}
108
109const AsmToken &AsmParser::Lex() {
110 const AsmToken *tok = &Lexer.Lex();
111
112 if (tok->is(AsmToken::Eof)) {
113 // If this is the end of an included file, pop the parent file off the
114 // include stack.
115 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
116 if (ParentIncludeLoc != SMLoc()) {
117 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
118 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
119 ParentIncludeLoc.getPointer());
120 tok = &Lexer.Lex();
121 }
122 }
123
124 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000125 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000126
Sean Callananfd0b0282010-01-21 00:19:58 +0000127 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000128}
129
Chris Lattner79180e22010-04-05 23:15:42 +0000130bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000131 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000132 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000133 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000134 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000135 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000136 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
137 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000138
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000139 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000140 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000141
Chris Lattnerb717fb02009-07-02 21:53:43 +0000142 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000143
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000144 AsmCond StartingCondState = TheCondState;
145
Chris Lattnerb717fb02009-07-02 21:53:43 +0000146 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000147 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000148 if (!ParseStatement()) continue;
149
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000150 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000151 HadError = true;
152 EatToEndOfStatement();
153 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000154
155 if (TheCondState.TheCond != StartingCondState.TheCond ||
156 TheCondState.Ignore != StartingCondState.Ignore)
157 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000158
Chris Lattner79180e22010-04-05 23:15:42 +0000159 // Finalize the output stream if there are no errors and if the client wants
160 // us to.
161 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000162 Out.Finish();
163
Chris Lattnerb717fb02009-07-02 21:53:43 +0000164 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000165}
166
Chris Lattner2cf5f142009-06-22 01:29:09 +0000167/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
168void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000169 while (Lexer.isNot(AsmToken::EndOfStatement) &&
170 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000171 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000172
173 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000174 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000175 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000176}
177
Chris Lattnerc4193832009-06-22 05:51:26 +0000178
Chris Lattner74ec1a32009-06-22 06:32:03 +0000179/// ParseParenExpr - Parse a paren expression and return it.
180/// NOTE: This assumes the leading '(' has already been consumed.
181///
182/// parenexpr ::= expr)
183///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000184bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000185 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000186 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000187 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000188 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000189 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000190 return false;
191}
Chris Lattnerc4193832009-06-22 05:51:26 +0000192
Daniel Dunbar959fd882009-08-26 22:13:22 +0000193MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000194 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000195 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000196}
197
Chris Lattner74ec1a32009-06-22 06:32:03 +0000198/// ParsePrimaryExpr - Parse a primary expression and return it.
199/// primaryexpr ::= (parenexpr
200/// primaryexpr ::= symbol
201/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000202/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000203/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000204bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000205 switch (Lexer.getKind()) {
206 default:
207 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000208 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000209 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000210 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000211 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000212 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000213 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000214 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000215 case AsmToken::Identifier: {
216 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000217 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
218 MCSymbol *Sym = CreateSymbol(Split.first);
219
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000220 // Mark the symbol as used in an expression.
221 Sym->setUsedInExpr(true);
222
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000223 // Lookup the symbol variant if used.
224 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
225 if (Split.first.size() != getTok().getIdentifier().size())
226 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
227
Chris Lattnerb4307b32010-01-15 19:28:38 +0000228 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000229 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000230
231 // If this is an absolute variable reference, substitute it now to preserve
232 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000233 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000234 if (Variant)
235 return Error(EndLoc, "unexpected modified on variable reference");
236
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000237 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000238 return false;
239 }
240
241 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000242 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000243 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000244 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000245 case AsmToken::Integer: {
246 SMLoc Loc = getTok().getLoc();
247 int64_t IntVal = getTok().getIntVal();
248 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000249 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000250 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000251 // Look for 'b' or 'f' following an Integer as a directional label
252 if (Lexer.getKind() == AsmToken::Identifier) {
253 StringRef IDVal = getTok().getString();
254 if (IDVal == "f" || IDVal == "b"){
255 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
256 IDVal == "f" ? 1 : 0);
257 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
258 getContext());
259 if(IDVal == "b" && Sym->isUndefined())
260 return Error(Loc, "invalid reference to undefined symbol");
261 EndLoc = Lexer.getLoc();
262 Lex(); // Eat identifier.
263 }
264 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000265 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000266 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000267 case AsmToken::Dot: {
268 // This is a '.' reference, which references the current PC. Emit a
269 // temporary label to the streamer and refer to it.
270 MCSymbol *Sym = Ctx.CreateTempSymbol();
271 Out.EmitLabel(Sym);
272 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
273 EndLoc = Lexer.getLoc();
274 Lex(); // Eat identifier.
275 return false;
276 }
277
Daniel Dunbar3f872332009-07-28 16:08:33 +0000278 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000279 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000280 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000281 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000282 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000283 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000284 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000285 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000286 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000287 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000288 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000289 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000290 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000291 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000292 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000293 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000294 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000295 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000296 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000297 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000299 }
300}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000301
Chris Lattnerb4307b32010-01-15 19:28:38 +0000302bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000303 SMLoc EndLoc;
304 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000305}
306
Chris Lattner74ec1a32009-06-22 06:32:03 +0000307/// ParseExpression - Parse an expression and return it.
308///
309/// expr ::= expr +,- expr -> lowest.
310/// expr ::= expr |,^,&,! expr -> middle.
311/// expr ::= expr *,/,%,<<,>> expr -> highest.
312/// expr ::= primaryexpr
313///
Chris Lattner54482b42010-01-15 19:39:23 +0000314bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000315 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000316 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000317 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
318 return true;
319
320 // Try to constant fold it up front, if possible.
321 int64_t Value;
322 if (Res->EvaluateAsAbsolute(Value))
323 Res = MCConstantExpr::Create(Value, getContext());
324
325 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000326}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000327
Chris Lattnerb4307b32010-01-15 19:28:38 +0000328bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000329 Res = 0;
330 return ParseParenExpr(Res, EndLoc) ||
331 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000332}
333
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000335 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000336
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000337 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 if (ParseExpression(Expr))
339 return true;
340
Daniel Dunbare00b0112009-10-16 01:57:52 +0000341 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000342 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000343
344 return false;
345}
346
Daniel Dunbar3f872332009-07-28 16:08:33 +0000347static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000348 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000349 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000350 default:
351 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352
353 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000354 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000355 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000356 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 1;
360
361 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000363 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000364 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000366 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000368 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000369 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000370 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::ExclaimEqual:
372 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000381 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000382 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000383 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000384 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000385 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000386 return 2;
387
388 // Intermediate Precedence: |, &, ^
389 //
390 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000391 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000392 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000393 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000394 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000395 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000396 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000397 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000398 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000399 return 3;
400
401 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000402 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000403 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000404 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000405 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000407 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000408 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000409 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000410 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000412 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000413 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000414 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000416 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000417 }
418}
419
420
421/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
422/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000423bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
424 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000425 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000426 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000427 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000428
429 // If the next token is lower precedence than we are allowed to eat, return
430 // successfully with what we ate already.
431 if (TokPrec < Precedence)
432 return false;
433
Sean Callanan79ed1a82010-01-19 20:22:31 +0000434 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000435
436 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000437 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000438 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000439
440 // If BinOp binds less tightly with RHS than the operator after RHS, let
441 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000442 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000443 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000444 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000445 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000446 }
447
Daniel Dunbar475839e2009-06-29 20:37:27 +0000448 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000449 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000450 }
451}
452
Chris Lattnerc4193832009-06-22 05:51:26 +0000453
454
455
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000456/// ParseStatement:
457/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000458/// ::= Label* Directive ...Operands... EndOfStatement
459/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000460bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000461 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000462 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000463 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000464 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000465 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000466
467 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000468 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000469 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000470 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000471 int64_t LocalLabelVal = -1;
472 // GUESS allow an integer followed by a ':' as a directional local label
473 if (Lexer.is(AsmToken::Integer)) {
474 LocalLabelVal = getTok().getIntVal();
475 if (LocalLabelVal < 0) {
476 if (!TheCondState.Ignore)
477 return TokError("unexpected token at start of statement");
478 IDVal = "";
479 }
480 else {
481 IDVal = getTok().getString();
482 Lex(); // Consume the integer token to be used as an identifier token.
483 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000484 if (!TheCondState.Ignore)
485 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000486 }
487 }
488 }
489 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000490 if (!TheCondState.Ignore)
491 return TokError("unexpected token at start of statement");
492 IDVal = "";
493 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000494
Chris Lattner7834fac2010-04-17 18:14:27 +0000495 // Handle conditional assembly here before checking for skipping. We
496 // have to do this so that .endif isn't skipped in a ".if 0" block for
497 // example.
498 if (IDVal == ".if")
499 return ParseDirectiveIf(IDLoc);
500 if (IDVal == ".elseif")
501 return ParseDirectiveElseIf(IDLoc);
502 if (IDVal == ".else")
503 return ParseDirectiveElse(IDLoc);
504 if (IDVal == ".endif")
505 return ParseDirectiveEndIf(IDLoc);
506
507 // If we are in a ".if 0" block, ignore this statement.
508 if (TheCondState.Ignore) {
509 EatToEndOfStatement();
510 return false;
511 }
512
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000513 // FIXME: Recurse on local labels?
514
515 // See what kind of statement we have.
516 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000517 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000518 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000519 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000520
521 // Diagnose attempt to use a variable as a label.
522 //
523 // FIXME: Diagnostics. Note the location of the definition as a label.
524 // FIXME: This doesn't diagnose assignment to a symbol which has been
525 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000526 MCSymbol *Sym;
527 if (LocalLabelVal == -1)
528 Sym = CreateSymbol(IDVal);
529 else
530 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000531 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000532 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000533
Daniel Dunbar959fd882009-08-26 22:13:22 +0000534 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000535 Out.EmitLabel(Sym);
536
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000537 // Consume any end of statement token, if present, to avoid spurious
538 // AddBlankLine calls().
539 if (Lexer.is(AsmToken::EndOfStatement)) {
540 Lex();
541 if (Lexer.is(AsmToken::Eof))
542 return false;
543 }
544
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000545 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000546 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000547
Daniel Dunbar3f872332009-07-28 16:08:33 +0000548 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000549 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000550 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000551
Daniel Dunbare2ace502009-08-31 08:09:09 +0000552 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000553
554 default: // Normal instruction or directive.
555 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000556 }
557
558 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000559 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000560 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000562 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000564 // FIXME: This changes behavior based on the -static flag to the
565 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000566 return ParseDirectiveSectionSwitch("__TEXT", "__text",
567 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000568 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000569 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000573 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
574 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000576 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 MCSectionMachO::S_4BYTE_LITERALS,
578 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000580 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000581 MCSectionMachO::S_8BYTE_LITERALS,
582 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000584 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 MCSectionMachO::S_16BYTE_LITERALS,
586 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000587 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000588 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000590 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
595
596 // FIXME: The assembler manual claims that this has the self modify code
597 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000598 if (IDVal == ".symbol_stub")
599 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
600 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000601 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
602 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000603 0, 16);
604 // FIXME: PowerPC only?
605 if (IDVal == ".picsymbol_stub")
606 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
607 MCSectionMachO::S_SYMBOL_STUBS |
608 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
609 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
614
615 // FIXME: The section names of these two are misspelled in the assembler
616 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000618 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
619 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
620 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000622 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
623 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
624 4);
625
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000627 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000629 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000630 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
631 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000633 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000634 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
635 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000637 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000638
639
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 return ParseDirectiveSectionSwitch("__OBJC", "__class",
642 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000644 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
645 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000647 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
648 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000650 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
651 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000653 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
654 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000656 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
657 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000659 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
660 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000662 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
663 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000665 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
666 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
667 MCSectionMachO::S_LITERAL_POINTERS,
668 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000670 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
671 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
672 MCSectionMachO::S_LITERAL_POINTERS,
673 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000675 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
676 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000678 return ParseDirectiveSectionSwitch("__OBJC", "__category",
679 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000681 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
682 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000684 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
685 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000687 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
688 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000690 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
691 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000693 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
694 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000696 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
697 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000699 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
700 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000701
Eric Christopherc6177a42010-05-17 22:53:55 +0000702 if (IDVal == ".tdata")
703 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
704 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
705 if (IDVal == ".tlv")
706 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
707 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
708 if (IDVal == ".thread_init_func")
709 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
710 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
711
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000712 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000714 return ParseDirectiveSet();
715
Daniel Dunbara0d14262009-06-24 23:30:00 +0000716 // Data directives
717
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000719 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000721 return ParseDirectiveAscii(true);
722
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000724 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000725 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000726 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000728 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000730 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000731
732 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000734 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000736 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000738 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000740 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000742 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000744 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000746 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000748 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
749
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000750 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000751 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000752
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000754 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000756 return ParseDirectiveSpace();
757
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000758 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000759
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000761 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000763 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000765 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000767 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000768 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000769 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000770 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000771 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000772 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000773 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000774 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000775 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000776 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000777 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000778 if (IDVal == ".type")
779 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000780 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000781 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000782 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000783 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000784 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000785 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000786 if (IDVal == ".weak_def_can_be_hidden")
787 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000788
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000789 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000790 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000791 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000792 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000793 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000794 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000795 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000796 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000797 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000798 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000799 if (IDVal == ".tbss")
800 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000801
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000802 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000803 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000805 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000806 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000807 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000808 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000809 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000810 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000811 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbyf187ac52010-06-28 21:45:58 +0000812 if (IDVal == ".secure_log_unique")
813 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
814 if (IDVal == ".secure_log_reset")
815 return ParseDirectiveDarwinSecureLogReset(IDLoc);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000816
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000817 // Look up the handler in the handler table.
818 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
819 DirectiveMap.lookup(IDVal);
820 if (Handler.first)
821 return (Handler.first->*Handler.second)(IDVal, IDLoc);
822
Kevin Enderby9c656452009-09-10 20:51:44 +0000823 // Target hook for parsing target specific directives.
824 if (!getTargetParser().ParseDirective(ID))
825 return false;
826
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000827 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000828 EatToEndOfStatement();
829 return false;
830 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000831
Chris Lattnera7f13542010-05-19 23:34:33 +0000832 // Canonicalize the opcode to lower case.
833 SmallString<128> Opcode;
834 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
835 Opcode.push_back(tolower(IDVal[i]));
836
Chris Lattner98986712010-01-14 22:21:20 +0000837 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000838 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000839 ParsedOperands);
840 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
841 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000842
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000843 // If parsing succeeded, match the instruction.
844 if (!HadError) {
845 MCInst Inst;
846 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
847 // Emit the instruction on success.
848 Out.EmitInstruction(Inst);
849 } else {
850 // Otherwise emit a diagnostic about the match failure and set the error
851 // flag.
852 //
853 // FIXME: We should give nicer diagnostics about the exact failure.
854 Error(IDLoc, "unrecognized instruction");
855 HadError = true;
856 }
857 }
Chris Lattner98986712010-01-14 22:21:20 +0000858
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000859 // If there was no error, consume the end-of-statement token. Otherwise this
860 // will be done by our caller.
861 if (!HadError)
862 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000863
864 // Free any parsed operands.
865 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
866 delete ParsedOperands[i];
867
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000868 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000869}
Chris Lattner9a023f72009-06-24 04:43:34 +0000870
Daniel Dunbare2ace502009-08-31 08:09:09 +0000871bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000872 // FIXME: Use better location, we should use proper tokens.
873 SMLoc EqualLoc = Lexer.getLoc();
874
Daniel Dunbar821e3332009-08-31 08:09:28 +0000875 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000876 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000877 return true;
878
Daniel Dunbar3f872332009-07-28 16:08:33 +0000879 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000880 return TokError("unexpected token in assignment");
881
882 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000883 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000884
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000885 // Validate that the LHS is allowed to be a variable (either it has not been
886 // used as a symbol, or it is an absolute symbol).
887 MCSymbol *Sym = getContext().LookupSymbol(Name);
888 if (Sym) {
889 // Diagnose assignment to a label.
890 //
891 // FIXME: Diagnostics. Note the location of the definition as a label.
892 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000893 if (Sym->isUndefined() && !Sym->isUsedInExpr())
894 ; // Allow redefinitions of undefined symbols only used in directives.
895 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000896 return Error(EqualLoc, "redefinition of '" + Name + "'");
897 else if (!Sym->isVariable())
898 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000899 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000900 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
901 Name + "'");
902 } else
903 Sym = CreateSymbol(Name);
904
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000905 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000906
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000907 Sym->setUsedInExpr(true);
908
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000909 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000910 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000911
912 return false;
913}
914
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000915/// ParseIdentifier:
916/// ::= identifier
917/// ::= string
918bool AsmParser::ParseIdentifier(StringRef &Res) {
919 if (Lexer.isNot(AsmToken::Identifier) &&
920 Lexer.isNot(AsmToken::String))
921 return true;
922
Sean Callanan18b83232010-01-19 21:44:56 +0000923 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000924
Sean Callanan79ed1a82010-01-19 20:22:31 +0000925 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000926
927 return false;
928}
929
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000930/// ParseDirectiveSet:
931/// ::= .set identifier ',' expression
932bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000933 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000934
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000935 if (ParseIdentifier(Name))
936 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000937
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000938 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000939 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000940 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000941
Daniel Dunbare2ace502009-08-31 08:09:09 +0000942 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000943}
944
Chris Lattner9a023f72009-06-24 04:43:34 +0000945/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000946/// ::= .section identifier (',' identifier)*
947/// FIXME: This should actually parse out the segment, section, attributes and
948/// sizeof_stub fields.
949bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000950 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000951
Daniel Dunbarace63122009-08-11 03:42:33 +0000952 StringRef SectionName;
953 if (ParseIdentifier(SectionName))
954 return Error(Loc, "expected identifier after '.section' directive");
955
956 // Verify there is a following comma.
957 if (!Lexer.is(AsmToken::Comma))
958 return TokError("unexpected token in '.section' directive");
959
Chris Lattnerff4bc462009-08-10 01:39:42 +0000960 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000961 SectionSpec += ",";
962
963 // Add all the tokens until the end of the line, ParseSectionSpecifier will
964 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000965 StringRef EOL = Lexer.LexUntilEndOfStatement();
966 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000967
Sean Callanan79ed1a82010-01-19 20:22:31 +0000968 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000969 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000970 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000971 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000972
Chris Lattnerff4bc462009-08-10 01:39:42 +0000973
974 StringRef Segment, Section;
975 unsigned TAA, StubSize;
976 std::string ErrorStr =
977 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
978 TAA, StubSize);
979
980 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000981 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000982
Chris Lattner56594f92009-07-31 17:47:16 +0000983 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000984 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000985 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
986 isText ? SectionKind::getText()
987 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000988 return false;
989}
990
Chris Lattnere15c2d72009-08-10 18:05:55 +0000991/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000992bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
993 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000994 unsigned TAA, unsigned Align,
995 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000996 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000997 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000998 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000999
Chris Lattner56594f92009-07-31 17:47:16 +00001000 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001001 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001002 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
1003 isText ? SectionKind::getText()
1004 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001005
1006 // Set the implicit alignment, if any.
1007 //
1008 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1009 // alignment on the section (e.g., if one manually inserts bytes into the
1010 // section, then just issueing the section switch directive will not realign
1011 // the section. However, this is arguably more reasonable behavior, and there
1012 // is no good reason for someone to intentionally emit incorrectly sized
1013 // values into the implicitly aligned sections.
1014 if (Align)
1015 Out.EmitValueToAlignment(Align, 0, 1, 0);
1016
Chris Lattner529fb542009-06-24 05:13:15 +00001017 return false;
1018}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001019
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001020bool AsmParser::ParseEscapedString(std::string &Data) {
1021 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
1022
1023 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001024 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001025 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1026 if (Str[i] != '\\') {
1027 Data += Str[i];
1028 continue;
1029 }
1030
1031 // Recognize escaped characters. Note that this escape semantics currently
1032 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1033 ++i;
1034 if (i == e)
1035 return TokError("unexpected backslash at end of string");
1036
1037 // Recognize octal sequences.
1038 if ((unsigned) (Str[i] - '0') <= 7) {
1039 // Consume up to three octal characters.
1040 unsigned Value = Str[i] - '0';
1041
1042 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1043 ++i;
1044 Value = Value * 8 + (Str[i] - '0');
1045
1046 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1047 ++i;
1048 Value = Value * 8 + (Str[i] - '0');
1049 }
1050 }
1051
1052 if (Value > 255)
1053 return TokError("invalid octal escape sequence (out of range)");
1054
1055 Data += (unsigned char) Value;
1056 continue;
1057 }
1058
1059 // Otherwise recognize individual escapes.
1060 switch (Str[i]) {
1061 default:
1062 // Just reject invalid escape sequences for now.
1063 return TokError("invalid escape sequence (unrecognized character)");
1064
1065 case 'b': Data += '\b'; break;
1066 case 'f': Data += '\f'; break;
1067 case 'n': Data += '\n'; break;
1068 case 'r': Data += '\r'; break;
1069 case 't': Data += '\t'; break;
1070 case '"': Data += '"'; break;
1071 case '\\': Data += '\\'; break;
1072 }
1073 }
1074
1075 return false;
1076}
1077
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001079/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001081 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001083 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return TokError("expected string in '.ascii' or '.asciz' directive");
1085
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001086 std::string Data;
1087 if (ParseEscapedString(Data))
1088 return true;
1089
Chris Lattneraaec2052010-01-19 19:46:13 +00001090 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001091 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001092 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093
Sean Callanan79ed1a82010-01-19 20:22:31 +00001094 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001095
Daniel Dunbar3f872332009-07-28 16:08:33 +00001096 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001097 break;
1098
Daniel Dunbar3f872332009-07-28 16:08:33 +00001099 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001100 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001101 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001102 }
1103 }
1104
Sean Callanan79ed1a82010-01-19 20:22:31 +00001105 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001106 return false;
1107}
1108
1109/// ParseDirectiveValue
1110/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1111bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001112 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001114 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001115 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001116 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 return true;
1118
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001119 // Special case constant expressions to match code generator.
1120 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1121 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1122 else
1123 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124
Daniel Dunbar3f872332009-07-28 16:08:33 +00001125 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126 break;
1127
1128 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001129 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001131 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132 }
1133 }
1134
Sean Callanan79ed1a82010-01-19 20:22:31 +00001135 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001136 return false;
1137}
1138
1139/// ParseDirectiveSpace
1140/// ::= .space expression [ , expression ]
1141bool AsmParser::ParseDirectiveSpace() {
1142 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001143 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001144 return true;
1145
1146 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1148 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001149 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001150 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001151
Daniel Dunbar475839e2009-06-29 20:37:27 +00001152 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001153 return true;
1154
Daniel Dunbar3f872332009-07-28 16:08:33 +00001155 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001156 return TokError("unexpected token in '.space' directive");
1157 }
1158
Sean Callanan79ed1a82010-01-19 20:22:31 +00001159 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001160
1161 if (NumBytes <= 0)
1162 return TokError("invalid number of bytes in '.space' directive");
1163
1164 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001165 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001166
1167 return false;
1168}
1169
1170/// ParseDirectiveFill
1171/// ::= .fill expression , expression , expression
1172bool AsmParser::ParseDirectiveFill() {
1173 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001174 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001175 return true;
1176
Daniel Dunbar3f872332009-07-28 16:08:33 +00001177 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001178 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001179 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001180
1181 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001182 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001183 return true;
1184
Daniel Dunbar3f872332009-07-28 16:08:33 +00001185 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001186 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001187 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001188
1189 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001190 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001191 return true;
1192
Daniel Dunbar3f872332009-07-28 16:08:33 +00001193 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001194 return TokError("unexpected token in '.fill' directive");
1195
Sean Callanan79ed1a82010-01-19 20:22:31 +00001196 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001197
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001198 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1199 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001200
1201 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001202 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001203
1204 return false;
1205}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001206
1207/// ParseDirectiveOrg
1208/// ::= .org expression [ , expression ]
1209bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001210 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001211 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001212 return true;
1213
1214 // Parse optional fill expression.
1215 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001216 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1217 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001218 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001219 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001220
Daniel Dunbar475839e2009-06-29 20:37:27 +00001221 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001222 return true;
1223
Daniel Dunbar3f872332009-07-28 16:08:33 +00001224 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001225 return TokError("unexpected token in '.org' directive");
1226 }
1227
Sean Callanan79ed1a82010-01-19 20:22:31 +00001228 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001229
1230 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1231 // has to be relative to the current section.
1232 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001233
1234 return false;
1235}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236
1237/// ParseDirectiveAlign
1238/// ::= {.align, ...} expression [ , expression [ , expression ]]
1239bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001240 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001241 int64_t Alignment;
1242 if (ParseAbsoluteExpression(Alignment))
1243 return true;
1244
1245 SMLoc MaxBytesLoc;
1246 bool HasFillExpr = false;
1247 int64_t FillExpr = 0;
1248 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001249 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1250 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001251 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001252 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001253
1254 // The fill expression can be omitted while specifying a maximum number of
1255 // alignment bytes, e.g:
1256 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001257 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001258 HasFillExpr = true;
1259 if (ParseAbsoluteExpression(FillExpr))
1260 return true;
1261 }
1262
Daniel Dunbar3f872332009-07-28 16:08:33 +00001263 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1264 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001265 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001266 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001267
1268 MaxBytesLoc = Lexer.getLoc();
1269 if (ParseAbsoluteExpression(MaxBytesToFill))
1270 return true;
1271
Daniel Dunbar3f872332009-07-28 16:08:33 +00001272 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001273 return TokError("unexpected token in directive");
1274 }
1275 }
1276
Sean Callanan79ed1a82010-01-19 20:22:31 +00001277 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001278
Daniel Dunbar648ac512010-05-17 21:54:30 +00001279 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001280 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001281
1282 // Compute alignment in bytes.
1283 if (IsPow2) {
1284 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001285 if (Alignment >= 32) {
1286 Error(AlignmentLoc, "invalid alignment value");
1287 Alignment = 31;
1288 }
1289
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001290 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001291 }
1292
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001293 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001294 if (MaxBytesLoc.isValid()) {
1295 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001296 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1297 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001298 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001299 }
1300
1301 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001302 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1303 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001304 MaxBytesToFill = 0;
1305 }
1306 }
1307
Daniel Dunbar648ac512010-05-17 21:54:30 +00001308 // Check whether we should use optimal code alignment for this .align
1309 // directive.
1310 //
1311 // FIXME: This should be using a target hook.
1312 bool UseCodeAlign = false;
1313 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1314 Out.getCurrentSection()))
1315 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1316 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1317 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001318 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001319 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001320 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1321 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001322 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001323
1324 return false;
1325}
1326
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001327/// ParseDirectiveSymbolAttribute
1328/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001329bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001330 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001331 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001332 StringRef Name;
1333
1334 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001335 return TokError("expected identifier in directive");
1336
Daniel Dunbar959fd882009-08-26 22:13:22 +00001337 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001338
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001339 Out.EmitSymbolAttribute(Sym, Attr);
1340
Daniel Dunbar3f872332009-07-28 16:08:33 +00001341 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001342 break;
1343
Daniel Dunbar3f872332009-07-28 16:08:33 +00001344 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001345 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001346 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001347 }
1348 }
1349
Sean Callanan79ed1a82010-01-19 20:22:31 +00001350 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001351 return false;
1352}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001353
Matt Fleming924c5e52010-05-21 11:36:59 +00001354/// ParseDirectiveELFType
1355/// ::= .type identifier , @attribute
1356bool AsmParser::ParseDirectiveELFType() {
1357 StringRef Name;
1358 if (ParseIdentifier(Name))
1359 return TokError("expected identifier in directive");
1360
1361 // Handle the identifier as the key symbol.
1362 MCSymbol *Sym = CreateSymbol(Name);
1363
1364 if (Lexer.isNot(AsmToken::Comma))
1365 return TokError("unexpected token in '.type' directive");
1366 Lex();
1367
1368 if (Lexer.isNot(AsmToken::At))
1369 return TokError("expected '@' before type");
1370 Lex();
1371
1372 StringRef Type;
1373 SMLoc TypeLoc;
1374
1375 TypeLoc = Lexer.getLoc();
1376 if (ParseIdentifier(Type))
1377 return TokError("expected symbol type in directive");
1378
1379 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1380 .Case("function", MCSA_ELF_TypeFunction)
1381 .Case("object", MCSA_ELF_TypeObject)
1382 .Case("tls_object", MCSA_ELF_TypeTLS)
1383 .Case("common", MCSA_ELF_TypeCommon)
1384 .Case("notype", MCSA_ELF_TypeNoType)
1385 .Default(MCSA_Invalid);
1386
1387 if (Attr == MCSA_Invalid)
1388 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1389
1390 if (Lexer.isNot(AsmToken::EndOfStatement))
1391 return TokError("unexpected token in '.type' directive");
1392
1393 Lex();
1394
1395 Out.EmitSymbolAttribute(Sym, Attr);
1396
1397 return false;
1398}
1399
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001400/// ParseDirectiveDarwinSymbolDesc
1401/// ::= .desc identifier , expression
1402bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001403 StringRef Name;
1404 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001405 return TokError("expected identifier in directive");
1406
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001407 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001408 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001409
Daniel Dunbar3f872332009-07-28 16:08:33 +00001410 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001411 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001412 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001413
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001414 int64_t DescValue;
1415 if (ParseAbsoluteExpression(DescValue))
1416 return true;
1417
Daniel Dunbar3f872332009-07-28 16:08:33 +00001418 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001419 return TokError("unexpected token in '.desc' directive");
1420
Sean Callanan79ed1a82010-01-19 20:22:31 +00001421 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001422
1423 // Set the n_desc field of this Symbol to this DescValue
1424 Out.EmitSymbolDesc(Sym, DescValue);
1425
1426 return false;
1427}
1428
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001429/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001430/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1431bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001432 SMLoc IDLoc = Lexer.getLoc();
1433 StringRef Name;
1434 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001435 return TokError("expected identifier in directive");
1436
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001437 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001438 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001439
Daniel Dunbar3f872332009-07-28 16:08:33 +00001440 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001441 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001442 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001443
1444 int64_t Size;
1445 SMLoc SizeLoc = Lexer.getLoc();
1446 if (ParseAbsoluteExpression(Size))
1447 return true;
1448
1449 int64_t Pow2Alignment = 0;
1450 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001451 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001452 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001453 Pow2AlignmentLoc = Lexer.getLoc();
1454 if (ParseAbsoluteExpression(Pow2Alignment))
1455 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001456
1457 // If this target takes alignments in bytes (not log) validate and convert.
1458 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1459 if (!isPowerOf2_64(Pow2Alignment))
1460 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1461 Pow2Alignment = Log2_64(Pow2Alignment);
1462 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001463 }
1464
Daniel Dunbar3f872332009-07-28 16:08:33 +00001465 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001466 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001467
Sean Callanan79ed1a82010-01-19 20:22:31 +00001468 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001469
Chris Lattner1fc3d752009-07-09 17:25:12 +00001470 // NOTE: a size of zero for a .comm should create a undefined symbol
1471 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001472 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001473 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1474 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001475
Eric Christopherc260a3e2010-05-14 01:38:54 +00001476 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001477 // may internally end up wanting an alignment in bytes.
1478 // FIXME: Diagnose overflow.
1479 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001480 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1481 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001482
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001483 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001484 return Error(IDLoc, "invalid symbol redefinition");
1485
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001486 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001487 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001488 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001489 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1490 MCSectionMachO::S_ZEROFILL, 0,
1491 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001492 Sym, Size, 1 << Pow2Alignment);
1493 return false;
1494 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001495
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001496 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001497 return false;
1498}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001499
1500/// ParseDirectiveDarwinZerofill
1501/// ::= .zerofill segname , sectname [, identifier , size_expression [
1502/// , align_expression ]]
1503bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001504 StringRef Segment;
1505 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001506 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001507
Daniel Dunbar3f872332009-07-28 16:08:33 +00001508 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001509 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001510 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001511
1512 StringRef Section;
1513 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001514 return TokError("expected section name after comma in '.zerofill' "
1515 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001516
Chris Lattner9be3fee2009-07-10 22:20:30 +00001517 // If this is the end of the line all that was wanted was to create the
1518 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001519 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001520 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001521 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1522 MCSectionMachO::S_ZEROFILL, 0,
1523 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001524 return false;
1525 }
1526
Daniel Dunbar3f872332009-07-28 16:08:33 +00001527 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001528 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001529 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001530
Chris Lattner7bb7c552010-05-13 00:10:34 +00001531 SMLoc IDLoc = Lexer.getLoc();
1532 StringRef IDStr;
1533 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001534 return TokError("expected identifier in directive");
1535
1536 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001537 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001538
Daniel Dunbar3f872332009-07-28 16:08:33 +00001539 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001540 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001541 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001542
1543 int64_t Size;
1544 SMLoc SizeLoc = Lexer.getLoc();
1545 if (ParseAbsoluteExpression(Size))
1546 return true;
1547
1548 int64_t Pow2Alignment = 0;
1549 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001550 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001551 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001552 Pow2AlignmentLoc = Lexer.getLoc();
1553 if (ParseAbsoluteExpression(Pow2Alignment))
1554 return true;
1555 }
1556
Daniel Dunbar3f872332009-07-28 16:08:33 +00001557 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001558 return TokError("unexpected token in '.zerofill' directive");
1559
Sean Callanan79ed1a82010-01-19 20:22:31 +00001560 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001561
1562 if (Size < 0)
1563 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1564 "than zero");
1565
Eric Christopherc260a3e2010-05-14 01:38:54 +00001566 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001567 // may internally end up wanting an alignment in bytes.
1568 // FIXME: Diagnose overflow.
1569 if (Pow2Alignment < 0)
1570 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1571 "can't be less than zero");
1572
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001573 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001574 return Error(IDLoc, "invalid symbol redefinition");
1575
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001576 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001577 //
1578 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001579 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1580 MCSectionMachO::S_ZEROFILL, 0,
1581 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001582 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001583
1584 return false;
1585}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001586
Eric Christopher482eba02010-05-14 01:50:28 +00001587/// ParseDirectiveDarwinTBSS
1588/// ::= .tbss identifier, size, align
1589bool AsmParser::ParseDirectiveDarwinTBSS() {
1590 SMLoc IDLoc = Lexer.getLoc();
1591 StringRef Name;
1592 if (ParseIdentifier(Name))
1593 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001594
Eric Christopher482eba02010-05-14 01:50:28 +00001595 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001596 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001597
1598 if (Lexer.isNot(AsmToken::Comma))
1599 return TokError("unexpected token in directive");
1600 Lex();
1601
1602 int64_t Size;
1603 SMLoc SizeLoc = Lexer.getLoc();
1604 if (ParseAbsoluteExpression(Size))
1605 return true;
1606
1607 int64_t Pow2Alignment = 0;
1608 SMLoc Pow2AlignmentLoc;
1609 if (Lexer.is(AsmToken::Comma)) {
1610 Lex();
1611 Pow2AlignmentLoc = Lexer.getLoc();
1612 if (ParseAbsoluteExpression(Pow2Alignment))
1613 return true;
1614 }
1615
1616 if (Lexer.isNot(AsmToken::EndOfStatement))
1617 return TokError("unexpected token in '.tbss' directive");
1618
1619 Lex();
1620
1621 if (Size < 0)
1622 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1623 "zero");
1624
1625 // FIXME: Diagnose overflow.
1626 if (Pow2Alignment < 0)
1627 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1628 "than zero");
1629
1630 if (!Sym->isUndefined())
1631 return Error(IDLoc, "invalid symbol redefinition");
1632
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001633 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1634 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1635 0, SectionKind::getThreadBSS()),
1636 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001637
1638 return false;
1639}
1640
Kevin Enderbya5c78322009-07-13 21:03:15 +00001641/// ParseDirectiveDarwinSubsectionsViaSymbols
1642/// ::= .subsections_via_symbols
1643bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001644 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001645 return TokError("unexpected token in '.subsections_via_symbols' directive");
1646
Sean Callanan79ed1a82010-01-19 20:22:31 +00001647 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001648
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001649 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001650
1651 return false;
1652}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001653
1654/// ParseDirectiveAbort
1655/// ::= .abort [ "abort_string" ]
1656bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001657 // FIXME: Use loc from directive.
1658 SMLoc Loc = Lexer.getLoc();
1659
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001660 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001661 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1662 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001663 return TokError("expected string in '.abort' directive");
1664
Sean Callanan18b83232010-01-19 21:44:56 +00001665 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001666
Sean Callanan79ed1a82010-01-19 20:22:31 +00001667 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001668 }
1669
Daniel Dunbar3f872332009-07-28 16:08:33 +00001670 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001671 return TokError("unexpected token in '.abort' directive");
1672
Sean Callanan79ed1a82010-01-19 20:22:31 +00001673 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001674
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001675 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001676 if (Str.empty())
1677 Error(Loc, ".abort detected. Assembly stopping.");
1678 else
1679 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001680
1681 return false;
1682}
Kevin Enderby71148242009-07-14 21:35:03 +00001683
1684/// ParseDirectiveLsym
1685/// ::= .lsym identifier , expression
1686bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001687 StringRef Name;
1688 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001689 return TokError("expected identifier in directive");
1690
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001691 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001692 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001693
Daniel Dunbar3f872332009-07-28 16:08:33 +00001694 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001695 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001696 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001697
Daniel Dunbar821e3332009-08-31 08:09:28 +00001698 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001699 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001700 return true;
1701
Daniel Dunbar3f872332009-07-28 16:08:33 +00001702 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001703 return TokError("unexpected token in '.lsym' directive");
1704
Sean Callanan79ed1a82010-01-19 20:22:31 +00001705 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001706
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001707 // We don't currently support this directive.
1708 //
1709 // FIXME: Diagnostic location!
1710 (void) Sym;
1711 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001712}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001713
1714/// ParseDirectiveInclude
1715/// ::= .include "filename"
1716bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001717 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001718 return TokError("expected string in '.include' directive");
1719
Sean Callanan18b83232010-01-19 21:44:56 +00001720 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001721 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001722 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001723
Daniel Dunbar3f872332009-07-28 16:08:33 +00001724 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001725 return TokError("unexpected token in '.include' directive");
1726
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001727 // Strip the quotes.
1728 Filename = Filename.substr(1, Filename.size()-2);
1729
1730 // Attempt to switch the lexer to the included file before consuming the end
1731 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001732 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001733 PrintMessage(IncludeLoc,
1734 "Could not find include file '" + Filename + "'",
1735 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001736 return true;
1737 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001738
1739 return false;
1740}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001741
1742/// ParseDirectiveDarwinDumpOrLoad
1743/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001744bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001745 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001746 return TokError("expected string in '.dump' or '.load' directive");
1747
Sean Callanan79ed1a82010-01-19 20:22:31 +00001748 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001749
Daniel Dunbar3f872332009-07-28 16:08:33 +00001750 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001751 return TokError("unexpected token in '.dump' or '.load' directive");
1752
Sean Callanan79ed1a82010-01-19 20:22:31 +00001753 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001754
Kevin Enderby5026ae42009-07-20 20:25:37 +00001755 // FIXME: If/when .dump and .load are implemented they will be done in the
1756 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001757 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001758 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001759 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001760 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001761
1762 return false;
1763}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001764
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001765/// ParseDirectiveDarwinSecureLogUnique
1766/// ::= .secure_log_unique "log message"
1767bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1768 std::string LogMessage;
1769
1770 if (Lexer.isNot(AsmToken::String))
1771 LogMessage = "";
1772 else{
1773 LogMessage = getTok().getString();
1774 Lex();
1775 }
1776
1777 if (Lexer.isNot(AsmToken::EndOfStatement))
1778 return TokError("unexpected token in '.secure_log_unique' directive");
1779
1780 if (getContext().getSecureLogUsed() != false)
1781 return Error(IDLoc, ".secure_log_unique specified multiple times");
1782
1783 char *SecureLogFile = getContext().getSecureLogFile();
1784 if (SecureLogFile == NULL)
1785 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1786 "environment variable unset.");
1787
1788 raw_ostream *OS = getContext().getSecureLog();
1789 if (OS == NULL) {
1790 std::string Err;
1791 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1792 if (!Err.empty()) {
1793 delete OS;
1794 return Error(IDLoc, Twine("can't open secure log file: ") +
1795 SecureLogFile + " (" + Err + ")");
1796 }
1797 getContext().setSecureLog(OS);
1798 }
1799
1800 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1801 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1802 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1803 << LogMessage + "\n";
1804
1805 getContext().setSecureLogUsed(true);
1806
1807 return false;
1808}
1809
1810/// ParseDirectiveDarwinSecureLogReset
1811/// ::= .secure_log_reset
1812bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1813 if (Lexer.isNot(AsmToken::EndOfStatement))
1814 return TokError("unexpected token in '.secure_log_reset' directive");
1815
1816 Lex();
1817
1818 getContext().setSecureLogUsed(false);
1819
1820 return false;
1821}
1822
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001823/// ParseDirectiveIf
1824/// ::= .if expression
1825bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001826 TheCondStack.push_back(TheCondState);
1827 TheCondState.TheCond = AsmCond::IfCond;
1828 if(TheCondState.Ignore) {
1829 EatToEndOfStatement();
1830 }
1831 else {
1832 int64_t ExprValue;
1833 if (ParseAbsoluteExpression(ExprValue))
1834 return true;
1835
1836 if (Lexer.isNot(AsmToken::EndOfStatement))
1837 return TokError("unexpected token in '.if' directive");
1838
Sean Callanan79ed1a82010-01-19 20:22:31 +00001839 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001840
1841 TheCondState.CondMet = ExprValue;
1842 TheCondState.Ignore = !TheCondState.CondMet;
1843 }
1844
1845 return false;
1846}
1847
1848/// ParseDirectiveElseIf
1849/// ::= .elseif expression
1850bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1851 if (TheCondState.TheCond != AsmCond::IfCond &&
1852 TheCondState.TheCond != AsmCond::ElseIfCond)
1853 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1854 " an .elseif");
1855 TheCondState.TheCond = AsmCond::ElseIfCond;
1856
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001857 bool LastIgnoreState = false;
1858 if (!TheCondStack.empty())
1859 LastIgnoreState = TheCondStack.back().Ignore;
1860 if (LastIgnoreState || TheCondState.CondMet) {
1861 TheCondState.Ignore = true;
1862 EatToEndOfStatement();
1863 }
1864 else {
1865 int64_t ExprValue;
1866 if (ParseAbsoluteExpression(ExprValue))
1867 return true;
1868
1869 if (Lexer.isNot(AsmToken::EndOfStatement))
1870 return TokError("unexpected token in '.elseif' directive");
1871
Sean Callanan79ed1a82010-01-19 20:22:31 +00001872 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001873 TheCondState.CondMet = ExprValue;
1874 TheCondState.Ignore = !TheCondState.CondMet;
1875 }
1876
1877 return false;
1878}
1879
1880/// ParseDirectiveElse
1881/// ::= .else
1882bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001883 if (Lexer.isNot(AsmToken::EndOfStatement))
1884 return TokError("unexpected token in '.else' directive");
1885
Sean Callanan79ed1a82010-01-19 20:22:31 +00001886 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001887
1888 if (TheCondState.TheCond != AsmCond::IfCond &&
1889 TheCondState.TheCond != AsmCond::ElseIfCond)
1890 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1891 ".elseif");
1892 TheCondState.TheCond = AsmCond::ElseCond;
1893 bool LastIgnoreState = false;
1894 if (!TheCondStack.empty())
1895 LastIgnoreState = TheCondStack.back().Ignore;
1896 if (LastIgnoreState || TheCondState.CondMet)
1897 TheCondState.Ignore = true;
1898 else
1899 TheCondState.Ignore = false;
1900
1901 return false;
1902}
1903
1904/// ParseDirectiveEndIf
1905/// ::= .endif
1906bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001907 if (Lexer.isNot(AsmToken::EndOfStatement))
1908 return TokError("unexpected token in '.endif' directive");
1909
Sean Callanan79ed1a82010-01-19 20:22:31 +00001910 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001911
1912 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1913 TheCondStack.empty())
1914 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1915 ".else");
1916 if (!TheCondStack.empty()) {
1917 TheCondState = TheCondStack.back();
1918 TheCondStack.pop_back();
1919 }
1920
1921 return false;
1922}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001923
1924/// ParseDirectiveFile
1925/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001926bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001927 // FIXME: I'm not sure what this is.
1928 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001929 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001930 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001931 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001932
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001933 if (FileNumber < 1)
1934 return TokError("file number less than one");
1935 }
1936
Daniel Dunbareceec052010-07-12 17:45:27 +00001937 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001938 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001939
Chris Lattnerd32e8032010-01-25 19:02:58 +00001940 StringRef Filename = getTok().getString();
1941 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001942 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001943
Daniel Dunbareceec052010-07-12 17:45:27 +00001944 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001945 return TokError("unexpected token in '.file' directive");
1946
Chris Lattnerd32e8032010-01-25 19:02:58 +00001947 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001948 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001949 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001950 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1951
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001952 return false;
1953}
1954
1955/// ParseDirectiveLine
1956/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001957bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001958 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1959 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001960 return TokError("unexpected token in '.line' directive");
1961
Sean Callanan18b83232010-01-19 21:44:56 +00001962 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001963 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001964 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001965
1966 // FIXME: Do something with the .line.
1967 }
1968
Daniel Dunbareceec052010-07-12 17:45:27 +00001969 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001970 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001971
1972 return false;
1973}
1974
1975
1976/// ParseDirectiveLoc
1977/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001978bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001979 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001980 return TokError("unexpected token in '.loc' directive");
1981
1982 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001983 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001984 (void) FileNumber;
1985 // FIXME: Validate file.
1986
Sean Callanan79ed1a82010-01-19 20:22:31 +00001987 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001988 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1989 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001990 return TokError("unexpected token in '.loc' directive");
1991
Sean Callanan18b83232010-01-19 21:44:56 +00001992 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001993 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001994 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001995
Daniel Dunbareceec052010-07-12 17:45:27 +00001996 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1997 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001998 return TokError("unexpected token in '.loc' directive");
1999
Sean Callanan18b83232010-01-19 21:44:56 +00002000 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002001 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002002 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002003
2004 // FIXME: Do something with the .loc.
2005 }
2006 }
2007
Daniel Dunbareceec052010-07-12 17:45:27 +00002008 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002009 return TokError("unexpected token in '.file' directive");
2010
2011 return false;
2012}
2013