blob: dc63e1dc5fe256f42b166c6ff44067b90f243071 [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
33enum { DEFAULT_ADDRSPACE = 0 };
34
Chris Lattnerebb89b42009-09-27 21:16:52 +000035AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
36 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000037 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
Chris Lattnerf0559e42010-04-08 20:30:37 +000038 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000039 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
40
Chris Lattnerebb89b42009-09-27 21:16:52 +000041 // Debugging directives.
42 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
43 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
44 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
45}
46
47
48
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000049AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000050}
51
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000052void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000053 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000054}
55
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000056bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000057 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000058 return true;
59}
60
61bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000062 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000063 return true;
64}
65
Sean Callananbf2013e2010-01-20 23:19:55 +000066void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
67 const char *Type) const {
68 SrcMgr.PrintMessage(Loc, Msg, Type);
69}
Sean Callananfd0b0282010-01-21 00:19:58 +000070
71bool AsmParser::EnterIncludeFile(const std::string &Filename) {
72 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
73 if (NewBuf == -1)
74 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000075
Sean Callananfd0b0282010-01-21 00:19:58 +000076 CurBuffer = NewBuf;
77
78 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
79
80 return false;
81}
82
83const AsmToken &AsmParser::Lex() {
84 const AsmToken *tok = &Lexer.Lex();
85
86 if (tok->is(AsmToken::Eof)) {
87 // If this is the end of an included file, pop the parent file off the
88 // include stack.
89 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
90 if (ParentIncludeLoc != SMLoc()) {
91 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
92 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
93 ParentIncludeLoc.getPointer());
94 tok = &Lexer.Lex();
95 }
96 }
97
98 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000099 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000100
Sean Callananfd0b0282010-01-21 00:19:58 +0000101 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000102}
103
Chris Lattner79180e22010-04-05 23:15:42 +0000104bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000105 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000106 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000107 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000108 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000109 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000110 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
111 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000112
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000113 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000114 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000115
Chris Lattnerb717fb02009-07-02 21:53:43 +0000116 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000117
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000118 AsmCond StartingCondState = TheCondState;
119
Chris Lattnerb717fb02009-07-02 21:53:43 +0000120 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000121 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000122 if (!ParseStatement()) continue;
123
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000124 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000125 HadError = true;
126 EatToEndOfStatement();
127 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000128
129 if (TheCondState.TheCond != StartingCondState.TheCond ||
130 TheCondState.Ignore != StartingCondState.Ignore)
131 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000132
Chris Lattner79180e22010-04-05 23:15:42 +0000133 // Finalize the output stream if there are no errors and if the client wants
134 // us to.
135 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000136 Out.Finish();
137
Chris Lattnerb717fb02009-07-02 21:53:43 +0000138 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000139}
140
Chris Lattner2cf5f142009-06-22 01:29:09 +0000141/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
142void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000143 while (Lexer.isNot(AsmToken::EndOfStatement) &&
144 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000145 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000146
147 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000148 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000149 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000150}
151
Chris Lattnerc4193832009-06-22 05:51:26 +0000152
Chris Lattner74ec1a32009-06-22 06:32:03 +0000153/// ParseParenExpr - Parse a paren expression and return it.
154/// NOTE: This assumes the leading '(' has already been consumed.
155///
156/// parenexpr ::= expr)
157///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000158bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000159 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000160 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000161 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000162 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000163 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000164 return false;
165}
Chris Lattnerc4193832009-06-22 05:51:26 +0000166
Daniel Dunbar959fd882009-08-26 22:13:22 +0000167MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000168 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000169 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000170}
171
Chris Lattner74ec1a32009-06-22 06:32:03 +0000172/// ParsePrimaryExpr - Parse a primary expression and return it.
173/// primaryexpr ::= (parenexpr
174/// primaryexpr ::= symbol
175/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000176/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000177/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000178bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000179 switch (Lexer.getKind()) {
180 default:
181 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000182 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000183 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000184 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000185 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000186 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000187 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000188 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000189 case AsmToken::Identifier: {
190 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000191 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
192 MCSymbol *Sym = CreateSymbol(Split.first);
193
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000194 // Mark the symbol as used in an expression.
195 Sym->setUsedInExpr(true);
196
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000197 // Lookup the symbol variant if used.
198 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
199 if (Split.first.size() != getTok().getIdentifier().size())
200 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
201
Chris Lattnerb4307b32010-01-15 19:28:38 +0000202 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000203 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000204
205 // If this is an absolute variable reference, substitute it now to preserve
206 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000207 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000208 if (Variant)
209 return Error(EndLoc, "unexpected modified on variable reference");
210
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000211 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000212 return false;
213 }
214
215 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000216 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000217 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000218 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000219 case AsmToken::Integer: {
220 SMLoc Loc = getTok().getLoc();
221 int64_t IntVal = getTok().getIntVal();
222 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000223 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000224 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000225 // Look for 'b' or 'f' following an Integer as a directional label
226 if (Lexer.getKind() == AsmToken::Identifier) {
227 StringRef IDVal = getTok().getString();
228 if (IDVal == "f" || IDVal == "b"){
229 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
230 IDVal == "f" ? 1 : 0);
231 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
232 getContext());
233 if(IDVal == "b" && Sym->isUndefined())
234 return Error(Loc, "invalid reference to undefined symbol");
235 EndLoc = Lexer.getLoc();
236 Lex(); // Eat identifier.
237 }
238 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000239 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000240 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000241 case AsmToken::Dot: {
242 // This is a '.' reference, which references the current PC. Emit a
243 // temporary label to the streamer and refer to it.
244 MCSymbol *Sym = Ctx.CreateTempSymbol();
245 Out.EmitLabel(Sym);
246 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
247 EndLoc = Lexer.getLoc();
248 Lex(); // Eat identifier.
249 return false;
250 }
251
Daniel Dunbar3f872332009-07-28 16:08:33 +0000252 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000253 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000255 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000256 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000257 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000259 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000260 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000261 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000262 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000263 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000264 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000265 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000266 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000267 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000268 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000269 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000271 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000272 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000273 }
274}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000275
Chris Lattnerb4307b32010-01-15 19:28:38 +0000276bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000277 SMLoc EndLoc;
278 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000279}
280
Chris Lattner74ec1a32009-06-22 06:32:03 +0000281/// ParseExpression - Parse an expression and return it.
282///
283/// expr ::= expr +,- expr -> lowest.
284/// expr ::= expr |,^,&,! expr -> middle.
285/// expr ::= expr *,/,%,<<,>> expr -> highest.
286/// expr ::= primaryexpr
287///
Chris Lattner54482b42010-01-15 19:39:23 +0000288bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000289 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000290 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000291 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
292 return true;
293
294 // Try to constant fold it up front, if possible.
295 int64_t Value;
296 if (Res->EvaluateAsAbsolute(Value))
297 Res = MCConstantExpr::Create(Value, getContext());
298
299 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000300}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000301
Chris Lattnerb4307b32010-01-15 19:28:38 +0000302bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000303 Res = 0;
304 return ParseParenExpr(Res, EndLoc) ||
305 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000306}
307
Daniel Dunbar475839e2009-06-29 20:37:27 +0000308bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000309 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000311 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000312 if (ParseExpression(Expr))
313 return true;
314
Daniel Dunbare00b0112009-10-16 01:57:52 +0000315 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000316 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000317
318 return false;
319}
320
Daniel Dunbar3f872332009-07-28 16:08:33 +0000321static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000322 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000323 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000324 default:
325 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000326
327 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000328 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000329 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000330 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000331 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000332 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333 return 1;
334
335 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000342 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000343 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::ExclaimEqual:
346 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000353 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000354 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000357 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000358 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000359 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360 return 2;
361
362 // Intermediate Precedence: |, &, ^
363 //
364 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000366 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000367 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000368 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000369 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000370 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000372 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000373 return 3;
374
375 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000376 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000377 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000378 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000379 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000380 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000381 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000382 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000383 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000384 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000386 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000387 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000388 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000389 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000390 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000391 }
392}
393
394
395/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
396/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000397bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
398 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000399 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000400 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000401 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000402
403 // If the next token is lower precedence than we are allowed to eat, return
404 // successfully with what we ate already.
405 if (TokPrec < Precedence)
406 return false;
407
Sean Callanan79ed1a82010-01-19 20:22:31 +0000408 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000409
410 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000411 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000412 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000413
414 // If BinOp binds less tightly with RHS than the operator after RHS, let
415 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000416 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000417 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000418 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000419 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000420 }
421
Daniel Dunbar475839e2009-06-29 20:37:27 +0000422 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000423 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000424 }
425}
426
Chris Lattnerc4193832009-06-22 05:51:26 +0000427
428
429
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000430/// ParseStatement:
431/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000432/// ::= Label* Directive ...Operands... EndOfStatement
433/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000434bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000435 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000436 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000437 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000438 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000439 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000440
441 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000442 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000443 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000444 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000445 int64_t LocalLabelVal = -1;
446 // GUESS allow an integer followed by a ':' as a directional local label
447 if (Lexer.is(AsmToken::Integer)) {
448 LocalLabelVal = getTok().getIntVal();
449 if (LocalLabelVal < 0) {
450 if (!TheCondState.Ignore)
451 return TokError("unexpected token at start of statement");
452 IDVal = "";
453 }
454 else {
455 IDVal = getTok().getString();
456 Lex(); // Consume the integer token to be used as an identifier token.
457 if (Lexer.getKind() != AsmToken::Colon) {
458 if (!TheCondState.Ignore)
459 return TokError("unexpected token at start of statement");
460 }
461 }
462 }
463 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000464 if (!TheCondState.Ignore)
465 return TokError("unexpected token at start of statement");
466 IDVal = "";
467 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000468
Chris Lattner7834fac2010-04-17 18:14:27 +0000469 // Handle conditional assembly here before checking for skipping. We
470 // have to do this so that .endif isn't skipped in a ".if 0" block for
471 // example.
472 if (IDVal == ".if")
473 return ParseDirectiveIf(IDLoc);
474 if (IDVal == ".elseif")
475 return ParseDirectiveElseIf(IDLoc);
476 if (IDVal == ".else")
477 return ParseDirectiveElse(IDLoc);
478 if (IDVal == ".endif")
479 return ParseDirectiveEndIf(IDLoc);
480
481 // If we are in a ".if 0" block, ignore this statement.
482 if (TheCondState.Ignore) {
483 EatToEndOfStatement();
484 return false;
485 }
486
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000487 // FIXME: Recurse on local labels?
488
489 // See what kind of statement we have.
490 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000491 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000492 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000493 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000494
495 // Diagnose attempt to use a variable as a label.
496 //
497 // FIXME: Diagnostics. Note the location of the definition as a label.
498 // FIXME: This doesn't diagnose assignment to a symbol which has been
499 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000500 MCSymbol *Sym;
501 if (LocalLabelVal == -1)
502 Sym = CreateSymbol(IDVal);
503 else
504 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000505 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000506 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000507
Daniel Dunbar959fd882009-08-26 22:13:22 +0000508 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000509 Out.EmitLabel(Sym);
510
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000511 // Consume any end of statement token, if present, to avoid spurious
512 // AddBlankLine calls().
513 if (Lexer.is(AsmToken::EndOfStatement)) {
514 Lex();
515 if (Lexer.is(AsmToken::Eof))
516 return false;
517 }
518
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000519 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000520 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000521
Daniel Dunbar3f872332009-07-28 16:08:33 +0000522 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000523 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000524 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000525
Daniel Dunbare2ace502009-08-31 08:09:09 +0000526 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000527
528 default: // Normal instruction or directive.
529 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000530 }
531
532 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000533 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000534 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000536 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000538 // FIXME: This changes behavior based on the -static flag to the
539 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000540 return ParseDirectiveSectionSwitch("__TEXT", "__text",
541 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000547 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
548 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000549 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000550 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 MCSectionMachO::S_4BYTE_LITERALS,
552 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000553 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000554 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 MCSectionMachO::S_8BYTE_LITERALS,
556 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000558 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 MCSectionMachO::S_16BYTE_LITERALS,
560 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000562 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000564 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000565 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000566 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
569
570 // FIXME: The assembler manual claims that this has the self modify code
571 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000572 if (IDVal == ".symbol_stub")
573 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
574 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000575 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
576 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 0, 16);
578 // FIXME: PowerPC only?
579 if (IDVal == ".picsymbol_stub")
580 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
581 MCSectionMachO::S_SYMBOL_STUBS |
582 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
583 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000586 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000587 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
588
589 // FIXME: The section names of these two are misspelled in the assembler
590 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
593 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
594 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000596 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
597 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
598 4);
599
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000602 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000603 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
605 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000607 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
609 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000612
613
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000615 return ParseDirectiveSectionSwitch("__OBJC", "__class",
616 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000618 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
619 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000621 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
622 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000624 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
625 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000627 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
628 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000630 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
631 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000633 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
634 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000636 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
637 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000639 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
640 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
641 MCSectionMachO::S_LITERAL_POINTERS,
642 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000644 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
645 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
646 MCSectionMachO::S_LITERAL_POINTERS,
647 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000649 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
650 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000652 return ParseDirectiveSectionSwitch("__OBJC", "__category",
653 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000655 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
656 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000658 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
659 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000661 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
662 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000664 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
665 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000667 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
668 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000670 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
671 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000673 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
674 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000675
Eric Christopherc6177a42010-05-17 22:53:55 +0000676 if (IDVal == ".tdata")
677 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
678 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
679 if (IDVal == ".tlv")
680 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
681 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
682 if (IDVal == ".thread_init_func")
683 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
684 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
685
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000686 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000688 return ParseDirectiveSet();
689
Daniel Dunbara0d14262009-06-24 23:30:00 +0000690 // Data directives
691
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000693 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000695 return ParseDirectiveAscii(true);
696
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000698 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000699 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000700 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000702 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000704 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000705
706 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000708 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000710 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000712 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000714 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000716 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000718 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000720 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000722 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
723
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000725 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000726
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000728 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000730 return ParseDirectiveSpace();
731
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000732 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000733
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000735 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000737 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000738 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000739 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000741 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000743 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000745 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000747 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000749 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000750 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000751 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000752 if (IDVal == ".type")
753 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000754 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000755 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000756 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000757 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000758 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000759 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000760
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000761 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000762 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000763 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000764 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000765 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000766 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000767 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000768 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000770 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000771 if (IDVal == ".tbss")
772 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000773
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000774 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000775 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000776 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000777 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000778 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000779 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000780 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000781 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000782 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000783 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbyf187ac52010-06-28 21:45:58 +0000784 if (IDVal == ".secure_log_unique")
785 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
786 if (IDVal == ".secure_log_reset")
787 return ParseDirectiveDarwinSecureLogReset(IDLoc);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000788
Chris Lattnerebb89b42009-09-27 21:16:52 +0000789 // Look up the handler in the handler table,
790 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
791 if (Handler)
792 return (this->*Handler)(IDVal, IDLoc);
793
Kevin Enderby9c656452009-09-10 20:51:44 +0000794 // Target hook for parsing target specific directives.
795 if (!getTargetParser().ParseDirective(ID))
796 return false;
797
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000798 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000799 EatToEndOfStatement();
800 return false;
801 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000802
Chris Lattnera7f13542010-05-19 23:34:33 +0000803 // Canonicalize the opcode to lower case.
804 SmallString<128> Opcode;
805 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
806 Opcode.push_back(tolower(IDVal[i]));
807
Chris Lattner98986712010-01-14 22:21:20 +0000808 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000809 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000810 ParsedOperands);
811 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
812 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000813
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000814 // If parsing succeeded, match the instruction.
815 if (!HadError) {
816 MCInst Inst;
817 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
818 // Emit the instruction on success.
819 Out.EmitInstruction(Inst);
820 } else {
821 // Otherwise emit a diagnostic about the match failure and set the error
822 // flag.
823 //
824 // FIXME: We should give nicer diagnostics about the exact failure.
825 Error(IDLoc, "unrecognized instruction");
826 HadError = true;
827 }
828 }
Chris Lattner98986712010-01-14 22:21:20 +0000829
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000830 // If there was no error, consume the end-of-statement token. Otherwise this
831 // will be done by our caller.
832 if (!HadError)
833 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000834
835 // Free any parsed operands.
836 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
837 delete ParsedOperands[i];
838
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000839 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000840}
Chris Lattner9a023f72009-06-24 04:43:34 +0000841
Daniel Dunbare2ace502009-08-31 08:09:09 +0000842bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000843 // FIXME: Use better location, we should use proper tokens.
844 SMLoc EqualLoc = Lexer.getLoc();
845
Daniel Dunbar821e3332009-08-31 08:09:28 +0000846 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000847 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000848 return true;
849
Daniel Dunbar3f872332009-07-28 16:08:33 +0000850 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000851 return TokError("unexpected token in assignment");
852
853 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000854 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000855
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000856 // Validate that the LHS is allowed to be a variable (either it has not been
857 // used as a symbol, or it is an absolute symbol).
858 MCSymbol *Sym = getContext().LookupSymbol(Name);
859 if (Sym) {
860 // Diagnose assignment to a label.
861 //
862 // FIXME: Diagnostics. Note the location of the definition as a label.
863 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000864 if (Sym->isUndefined() && !Sym->isUsedInExpr())
865 ; // Allow redefinitions of undefined symbols only used in directives.
866 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000867 return Error(EqualLoc, "redefinition of '" + Name + "'");
868 else if (!Sym->isVariable())
869 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000870 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000871 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
872 Name + "'");
873 } else
874 Sym = CreateSymbol(Name);
875
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000876 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000877
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000878 Sym->setUsedInExpr(true);
879
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000880 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000881 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000882
883 return false;
884}
885
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000886/// ParseIdentifier:
887/// ::= identifier
888/// ::= string
889bool AsmParser::ParseIdentifier(StringRef &Res) {
890 if (Lexer.isNot(AsmToken::Identifier) &&
891 Lexer.isNot(AsmToken::String))
892 return true;
893
Sean Callanan18b83232010-01-19 21:44:56 +0000894 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000895
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000897
898 return false;
899}
900
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901/// ParseDirectiveSet:
902/// ::= .set identifier ',' expression
903bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000904 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000905
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000906 if (ParseIdentifier(Name))
907 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000908
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000909 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000910 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000911 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000912
Daniel Dunbare2ace502009-08-31 08:09:09 +0000913 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000914}
915
Chris Lattner9a023f72009-06-24 04:43:34 +0000916/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000917/// ::= .section identifier (',' identifier)*
918/// FIXME: This should actually parse out the segment, section, attributes and
919/// sizeof_stub fields.
920bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000921 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000922
Daniel Dunbarace63122009-08-11 03:42:33 +0000923 StringRef SectionName;
924 if (ParseIdentifier(SectionName))
925 return Error(Loc, "expected identifier after '.section' directive");
926
927 // Verify there is a following comma.
928 if (!Lexer.is(AsmToken::Comma))
929 return TokError("unexpected token in '.section' directive");
930
Chris Lattnerff4bc462009-08-10 01:39:42 +0000931 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000932 SectionSpec += ",";
933
934 // Add all the tokens until the end of the line, ParseSectionSpecifier will
935 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000936 StringRef EOL = Lexer.LexUntilEndOfStatement();
937 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000938
Sean Callanan79ed1a82010-01-19 20:22:31 +0000939 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000941 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000942 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000943
Chris Lattnerff4bc462009-08-10 01:39:42 +0000944
945 StringRef Segment, Section;
946 unsigned TAA, StubSize;
947 std::string ErrorStr =
948 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
949 TAA, StubSize);
950
951 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000952 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000953
Chris Lattner56594f92009-07-31 17:47:16 +0000954 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000955 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000956 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
957 isText ? SectionKind::getText()
958 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000959 return false;
960}
961
Chris Lattnere15c2d72009-08-10 18:05:55 +0000962/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000963bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
964 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000965 unsigned TAA, unsigned Align,
966 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000967 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000968 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000969 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000970
Chris Lattner56594f92009-07-31 17:47:16 +0000971 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000972 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000973 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
974 isText ? SectionKind::getText()
975 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000976
977 // Set the implicit alignment, if any.
978 //
979 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
980 // alignment on the section (e.g., if one manually inserts bytes into the
981 // section, then just issueing the section switch directive will not realign
982 // the section. However, this is arguably more reasonable behavior, and there
983 // is no good reason for someone to intentionally emit incorrectly sized
984 // values into the implicitly aligned sections.
985 if (Align)
986 Out.EmitValueToAlignment(Align, 0, 1, 0);
987
Chris Lattner529fb542009-06-24 05:13:15 +0000988 return false;
989}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000990
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000991bool AsmParser::ParseEscapedString(std::string &Data) {
992 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
993
994 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000995 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000996 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
997 if (Str[i] != '\\') {
998 Data += Str[i];
999 continue;
1000 }
1001
1002 // Recognize escaped characters. Note that this escape semantics currently
1003 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1004 ++i;
1005 if (i == e)
1006 return TokError("unexpected backslash at end of string");
1007
1008 // Recognize octal sequences.
1009 if ((unsigned) (Str[i] - '0') <= 7) {
1010 // Consume up to three octal characters.
1011 unsigned Value = Str[i] - '0';
1012
1013 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1014 ++i;
1015 Value = Value * 8 + (Str[i] - '0');
1016
1017 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1018 ++i;
1019 Value = Value * 8 + (Str[i] - '0');
1020 }
1021 }
1022
1023 if (Value > 255)
1024 return TokError("invalid octal escape sequence (out of range)");
1025
1026 Data += (unsigned char) Value;
1027 continue;
1028 }
1029
1030 // Otherwise recognize individual escapes.
1031 switch (Str[i]) {
1032 default:
1033 // Just reject invalid escape sequences for now.
1034 return TokError("invalid escape sequence (unrecognized character)");
1035
1036 case 'b': Data += '\b'; break;
1037 case 'f': Data += '\f'; break;
1038 case 'n': Data += '\n'; break;
1039 case 'r': Data += '\r'; break;
1040 case 't': Data += '\t'; break;
1041 case '"': Data += '"'; break;
1042 case '\\': Data += '\\'; break;
1043 }
1044 }
1045
1046 return false;
1047}
1048
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001050/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001052 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001053 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 return TokError("expected string in '.ascii' or '.asciz' directive");
1056
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001057 std::string Data;
1058 if (ParseEscapedString(Data))
1059 return true;
1060
Chris Lattneraaec2052010-01-19 19:46:13 +00001061 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001063 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064
Sean Callanan79ed1a82010-01-19 20:22:31 +00001065 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001066
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 break;
1069
Daniel Dunbar3f872332009-07-28 16:08:33 +00001070 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001072 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 }
1074 }
1075
Sean Callanan79ed1a82010-01-19 20:22:31 +00001076 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return false;
1078}
1079
1080/// ParseDirectiveValue
1081/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1082bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001083 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001085 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001086 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001087 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088 return true;
1089
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001090 // Special case constant expressions to match code generator.
1091 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1092 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1093 else
1094 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
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
1099 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001100 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001102 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001103 }
1104 }
1105
Sean Callanan79ed1a82010-01-19 20:22:31 +00001106 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001107 return false;
1108}
1109
1110/// ParseDirectiveSpace
1111/// ::= .space expression [ , expression ]
1112bool AsmParser::ParseDirectiveSpace() {
1113 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001114 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115 return true;
1116
1117 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001118 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1119 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001120 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001121 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122
Daniel Dunbar475839e2009-06-29 20:37:27 +00001123 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124 return true;
1125
Daniel Dunbar3f872332009-07-28 16:08:33 +00001126 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127 return TokError("unexpected token in '.space' directive");
1128 }
1129
Sean Callanan79ed1a82010-01-19 20:22:31 +00001130 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131
1132 if (NumBytes <= 0)
1133 return TokError("invalid number of bytes in '.space' directive");
1134
1135 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001136 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001137
1138 return false;
1139}
1140
1141/// ParseDirectiveFill
1142/// ::= .fill expression , expression , expression
1143bool AsmParser::ParseDirectiveFill() {
1144 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001145 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146 return true;
1147
Daniel Dunbar3f872332009-07-28 16:08:33 +00001148 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001149 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001150 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001151
1152 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001153 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154 return true;
1155
Daniel Dunbar3f872332009-07-28 16:08:33 +00001156 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001157 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001158 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001159
1160 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001161 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001162 return true;
1163
Daniel Dunbar3f872332009-07-28 16:08:33 +00001164 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001165 return TokError("unexpected token in '.fill' directive");
1166
Sean Callanan79ed1a82010-01-19 20:22:31 +00001167 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001168
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001169 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1170 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001171
1172 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001173 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001174
1175 return false;
1176}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001177
1178/// ParseDirectiveOrg
1179/// ::= .org expression [ , expression ]
1180bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001181 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001182 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001183 return true;
1184
1185 // Parse optional fill expression.
1186 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1188 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001189 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001190 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001191
Daniel Dunbar475839e2009-06-29 20:37:27 +00001192 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001193 return true;
1194
Daniel Dunbar3f872332009-07-28 16:08:33 +00001195 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001196 return TokError("unexpected token in '.org' directive");
1197 }
1198
Sean Callanan79ed1a82010-01-19 20:22:31 +00001199 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001200
1201 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1202 // has to be relative to the current section.
1203 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001204
1205 return false;
1206}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207
1208/// ParseDirectiveAlign
1209/// ::= {.align, ...} expression [ , expression [ , expression ]]
1210bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001211 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001212 int64_t Alignment;
1213 if (ParseAbsoluteExpression(Alignment))
1214 return true;
1215
1216 SMLoc MaxBytesLoc;
1217 bool HasFillExpr = false;
1218 int64_t FillExpr = 0;
1219 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1221 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001222 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001223 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224
1225 // The fill expression can be omitted while specifying a maximum number of
1226 // alignment bytes, e.g:
1227 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001228 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 HasFillExpr = true;
1230 if (ParseAbsoluteExpression(FillExpr))
1231 return true;
1232 }
1233
Daniel Dunbar3f872332009-07-28 16:08:33 +00001234 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1235 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001237 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001238
1239 MaxBytesLoc = Lexer.getLoc();
1240 if (ParseAbsoluteExpression(MaxBytesToFill))
1241 return true;
1242
Daniel Dunbar3f872332009-07-28 16:08:33 +00001243 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001244 return TokError("unexpected token in directive");
1245 }
1246 }
1247
Sean Callanan79ed1a82010-01-19 20:22:31 +00001248 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001249
Daniel Dunbar648ac512010-05-17 21:54:30 +00001250 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001251 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001252
1253 // Compute alignment in bytes.
1254 if (IsPow2) {
1255 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001256 if (Alignment >= 32) {
1257 Error(AlignmentLoc, "invalid alignment value");
1258 Alignment = 31;
1259 }
1260
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001261 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001262 }
1263
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001264 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001265 if (MaxBytesLoc.isValid()) {
1266 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001267 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1268 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001269 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001270 }
1271
1272 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001273 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1274 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001275 MaxBytesToFill = 0;
1276 }
1277 }
1278
Daniel Dunbar648ac512010-05-17 21:54:30 +00001279 // Check whether we should use optimal code alignment for this .align
1280 // directive.
1281 //
1282 // FIXME: This should be using a target hook.
1283 bool UseCodeAlign = false;
1284 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1285 Out.getCurrentSection()))
1286 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1287 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1288 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001289 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001290 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001291 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1292 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001293 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001294
1295 return false;
1296}
1297
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001298/// ParseDirectiveSymbolAttribute
1299/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001300bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001301 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001302 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001303 StringRef Name;
1304
1305 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001306 return TokError("expected identifier in directive");
1307
Daniel Dunbar959fd882009-08-26 22:13:22 +00001308 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001309
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001310 Out.EmitSymbolAttribute(Sym, Attr);
1311
Daniel Dunbar3f872332009-07-28 16:08:33 +00001312 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001313 break;
1314
Daniel Dunbar3f872332009-07-28 16:08:33 +00001315 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001316 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001317 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001318 }
1319 }
1320
Sean Callanan79ed1a82010-01-19 20:22:31 +00001321 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001322 return false;
1323}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001324
Matt Fleming924c5e52010-05-21 11:36:59 +00001325/// ParseDirectiveELFType
1326/// ::= .type identifier , @attribute
1327bool AsmParser::ParseDirectiveELFType() {
1328 StringRef Name;
1329 if (ParseIdentifier(Name))
1330 return TokError("expected identifier in directive");
1331
1332 // Handle the identifier as the key symbol.
1333 MCSymbol *Sym = CreateSymbol(Name);
1334
1335 if (Lexer.isNot(AsmToken::Comma))
1336 return TokError("unexpected token in '.type' directive");
1337 Lex();
1338
1339 if (Lexer.isNot(AsmToken::At))
1340 return TokError("expected '@' before type");
1341 Lex();
1342
1343 StringRef Type;
1344 SMLoc TypeLoc;
1345
1346 TypeLoc = Lexer.getLoc();
1347 if (ParseIdentifier(Type))
1348 return TokError("expected symbol type in directive");
1349
1350 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1351 .Case("function", MCSA_ELF_TypeFunction)
1352 .Case("object", MCSA_ELF_TypeObject)
1353 .Case("tls_object", MCSA_ELF_TypeTLS)
1354 .Case("common", MCSA_ELF_TypeCommon)
1355 .Case("notype", MCSA_ELF_TypeNoType)
1356 .Default(MCSA_Invalid);
1357
1358 if (Attr == MCSA_Invalid)
1359 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1360
1361 if (Lexer.isNot(AsmToken::EndOfStatement))
1362 return TokError("unexpected token in '.type' directive");
1363
1364 Lex();
1365
1366 Out.EmitSymbolAttribute(Sym, Attr);
1367
1368 return false;
1369}
1370
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001371/// ParseDirectiveDarwinSymbolDesc
1372/// ::= .desc identifier , expression
1373bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001374 StringRef Name;
1375 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001376 return TokError("expected identifier in directive");
1377
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001378 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001379 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001380
Daniel Dunbar3f872332009-07-28 16:08:33 +00001381 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001382 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001383 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001384
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001385 int64_t DescValue;
1386 if (ParseAbsoluteExpression(DescValue))
1387 return true;
1388
Daniel Dunbar3f872332009-07-28 16:08:33 +00001389 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001390 return TokError("unexpected token in '.desc' directive");
1391
Sean Callanan79ed1a82010-01-19 20:22:31 +00001392 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001393
1394 // Set the n_desc field of this Symbol to this DescValue
1395 Out.EmitSymbolDesc(Sym, DescValue);
1396
1397 return false;
1398}
1399
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001400/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001401/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1402bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001403 SMLoc IDLoc = Lexer.getLoc();
1404 StringRef Name;
1405 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001406 return TokError("expected identifier in directive");
1407
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001408 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001409 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001410
Daniel Dunbar3f872332009-07-28 16:08:33 +00001411 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001412 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001414
1415 int64_t Size;
1416 SMLoc SizeLoc = Lexer.getLoc();
1417 if (ParseAbsoluteExpression(Size))
1418 return true;
1419
1420 int64_t Pow2Alignment = 0;
1421 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001422 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001423 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001424 Pow2AlignmentLoc = Lexer.getLoc();
1425 if (ParseAbsoluteExpression(Pow2Alignment))
1426 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001427
1428 // If this target takes alignments in bytes (not log) validate and convert.
1429 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1430 if (!isPowerOf2_64(Pow2Alignment))
1431 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1432 Pow2Alignment = Log2_64(Pow2Alignment);
1433 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001434 }
1435
Daniel Dunbar3f872332009-07-28 16:08:33 +00001436 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001437 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001438
Sean Callanan79ed1a82010-01-19 20:22:31 +00001439 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001440
Chris Lattner1fc3d752009-07-09 17:25:12 +00001441 // NOTE: a size of zero for a .comm should create a undefined symbol
1442 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001443 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001444 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1445 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001446
Eric Christopherc260a3e2010-05-14 01:38:54 +00001447 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001448 // may internally end up wanting an alignment in bytes.
1449 // FIXME: Diagnose overflow.
1450 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001451 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1452 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001453
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001454 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001455 return Error(IDLoc, "invalid symbol redefinition");
1456
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001457 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001458 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001459 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001460 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1461 MCSectionMachO::S_ZEROFILL, 0,
1462 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001463 Sym, Size, 1 << Pow2Alignment);
1464 return false;
1465 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001466
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001467 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001468 return false;
1469}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001470
1471/// ParseDirectiveDarwinZerofill
1472/// ::= .zerofill segname , sectname [, identifier , size_expression [
1473/// , align_expression ]]
1474bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001475 StringRef Segment;
1476 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001477 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001478
Daniel Dunbar3f872332009-07-28 16:08:33 +00001479 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001480 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001481 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001482
1483 StringRef Section;
1484 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001485 return TokError("expected section name after comma in '.zerofill' "
1486 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001487
Chris Lattner9be3fee2009-07-10 22:20:30 +00001488 // If this is the end of the line all that was wanted was to create the
1489 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001490 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001491 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001492 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1493 MCSectionMachO::S_ZEROFILL, 0,
1494 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001495 return false;
1496 }
1497
Daniel Dunbar3f872332009-07-28 16:08:33 +00001498 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001499 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001500 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001501
Chris Lattner7bb7c552010-05-13 00:10:34 +00001502 SMLoc IDLoc = Lexer.getLoc();
1503 StringRef IDStr;
1504 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001505 return TokError("expected identifier in directive");
1506
1507 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001508 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001509
Daniel Dunbar3f872332009-07-28 16:08:33 +00001510 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001511 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001512 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001513
1514 int64_t Size;
1515 SMLoc SizeLoc = Lexer.getLoc();
1516 if (ParseAbsoluteExpression(Size))
1517 return true;
1518
1519 int64_t Pow2Alignment = 0;
1520 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001521 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001522 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001523 Pow2AlignmentLoc = Lexer.getLoc();
1524 if (ParseAbsoluteExpression(Pow2Alignment))
1525 return true;
1526 }
1527
Daniel Dunbar3f872332009-07-28 16:08:33 +00001528 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001529 return TokError("unexpected token in '.zerofill' directive");
1530
Sean Callanan79ed1a82010-01-19 20:22:31 +00001531 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001532
1533 if (Size < 0)
1534 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1535 "than zero");
1536
Eric Christopherc260a3e2010-05-14 01:38:54 +00001537 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001538 // may internally end up wanting an alignment in bytes.
1539 // FIXME: Diagnose overflow.
1540 if (Pow2Alignment < 0)
1541 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1542 "can't be less than zero");
1543
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001544 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001545 return Error(IDLoc, "invalid symbol redefinition");
1546
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001547 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001548 //
1549 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001550 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1551 MCSectionMachO::S_ZEROFILL, 0,
1552 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001553 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001554
1555 return false;
1556}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001557
Eric Christopher482eba02010-05-14 01:50:28 +00001558/// ParseDirectiveDarwinTBSS
1559/// ::= .tbss identifier, size, align
1560bool AsmParser::ParseDirectiveDarwinTBSS() {
1561 SMLoc IDLoc = Lexer.getLoc();
1562 StringRef Name;
1563 if (ParseIdentifier(Name))
1564 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001565
Eric Christopher482eba02010-05-14 01:50:28 +00001566 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001567 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001568
1569 if (Lexer.isNot(AsmToken::Comma))
1570 return TokError("unexpected token in directive");
1571 Lex();
1572
1573 int64_t Size;
1574 SMLoc SizeLoc = Lexer.getLoc();
1575 if (ParseAbsoluteExpression(Size))
1576 return true;
1577
1578 int64_t Pow2Alignment = 0;
1579 SMLoc Pow2AlignmentLoc;
1580 if (Lexer.is(AsmToken::Comma)) {
1581 Lex();
1582 Pow2AlignmentLoc = Lexer.getLoc();
1583 if (ParseAbsoluteExpression(Pow2Alignment))
1584 return true;
1585 }
1586
1587 if (Lexer.isNot(AsmToken::EndOfStatement))
1588 return TokError("unexpected token in '.tbss' directive");
1589
1590 Lex();
1591
1592 if (Size < 0)
1593 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1594 "zero");
1595
1596 // FIXME: Diagnose overflow.
1597 if (Pow2Alignment < 0)
1598 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1599 "than zero");
1600
1601 if (!Sym->isUndefined())
1602 return Error(IDLoc, "invalid symbol redefinition");
1603
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001604 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1605 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1606 0, SectionKind::getThreadBSS()),
1607 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001608
1609 return false;
1610}
1611
Kevin Enderbya5c78322009-07-13 21:03:15 +00001612/// ParseDirectiveDarwinSubsectionsViaSymbols
1613/// ::= .subsections_via_symbols
1614bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001615 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001616 return TokError("unexpected token in '.subsections_via_symbols' directive");
1617
Sean Callanan79ed1a82010-01-19 20:22:31 +00001618 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001619
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001620 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001621
1622 return false;
1623}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001624
1625/// ParseDirectiveAbort
1626/// ::= .abort [ "abort_string" ]
1627bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001628 // FIXME: Use loc from directive.
1629 SMLoc Loc = Lexer.getLoc();
1630
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001631 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001632 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1633 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001634 return TokError("expected string in '.abort' directive");
1635
Sean Callanan18b83232010-01-19 21:44:56 +00001636 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001637
Sean Callanan79ed1a82010-01-19 20:22:31 +00001638 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001639 }
1640
Daniel Dunbar3f872332009-07-28 16:08:33 +00001641 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001642 return TokError("unexpected token in '.abort' directive");
1643
Sean Callanan79ed1a82010-01-19 20:22:31 +00001644 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001645
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001646 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001647 if (Str.empty())
1648 Error(Loc, ".abort detected. Assembly stopping.");
1649 else
1650 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001651
1652 return false;
1653}
Kevin Enderby71148242009-07-14 21:35:03 +00001654
1655/// ParseDirectiveLsym
1656/// ::= .lsym identifier , expression
1657bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001658 StringRef Name;
1659 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001660 return TokError("expected identifier in directive");
1661
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001662 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001663 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001664
Daniel Dunbar3f872332009-07-28 16:08:33 +00001665 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001666 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001667 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001668
Daniel Dunbar821e3332009-08-31 08:09:28 +00001669 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001670 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001671 return true;
1672
Daniel Dunbar3f872332009-07-28 16:08:33 +00001673 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001674 return TokError("unexpected token in '.lsym' directive");
1675
Sean Callanan79ed1a82010-01-19 20:22:31 +00001676 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001677
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001678 // We don't currently support this directive.
1679 //
1680 // FIXME: Diagnostic location!
1681 (void) Sym;
1682 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001683}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001684
1685/// ParseDirectiveInclude
1686/// ::= .include "filename"
1687bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001688 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001689 return TokError("expected string in '.include' directive");
1690
Sean Callanan18b83232010-01-19 21:44:56 +00001691 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001692 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001694
Daniel Dunbar3f872332009-07-28 16:08:33 +00001695 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001696 return TokError("unexpected token in '.include' directive");
1697
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001698 // Strip the quotes.
1699 Filename = Filename.substr(1, Filename.size()-2);
1700
1701 // Attempt to switch the lexer to the included file before consuming the end
1702 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001703 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001704 PrintMessage(IncludeLoc,
1705 "Could not find include file '" + Filename + "'",
1706 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001707 return true;
1708 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001709
1710 return false;
1711}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001712
1713/// ParseDirectiveDarwinDumpOrLoad
1714/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001715bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001716 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001717 return TokError("expected string in '.dump' or '.load' directive");
1718
Sean Callanan79ed1a82010-01-19 20:22:31 +00001719 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001720
Daniel Dunbar3f872332009-07-28 16:08:33 +00001721 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001722 return TokError("unexpected token in '.dump' or '.load' directive");
1723
Sean Callanan79ed1a82010-01-19 20:22:31 +00001724 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001725
Kevin Enderby5026ae42009-07-20 20:25:37 +00001726 // FIXME: If/when .dump and .load are implemented they will be done in the
1727 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001728 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001729 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001730 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001731 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001732
1733 return false;
1734}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001735
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001736/// ParseDirectiveDarwinSecureLogUnique
1737/// ::= .secure_log_unique "log message"
1738bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1739 std::string LogMessage;
1740
1741 if (Lexer.isNot(AsmToken::String))
1742 LogMessage = "";
1743 else{
1744 LogMessage = getTok().getString();
1745 Lex();
1746 }
1747
1748 if (Lexer.isNot(AsmToken::EndOfStatement))
1749 return TokError("unexpected token in '.secure_log_unique' directive");
1750
1751 if (getContext().getSecureLogUsed() != false)
1752 return Error(IDLoc, ".secure_log_unique specified multiple times");
1753
1754 char *SecureLogFile = getContext().getSecureLogFile();
1755 if (SecureLogFile == NULL)
1756 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1757 "environment variable unset.");
1758
1759 raw_ostream *OS = getContext().getSecureLog();
1760 if (OS == NULL) {
1761 std::string Err;
1762 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1763 if (!Err.empty()) {
1764 delete OS;
1765 return Error(IDLoc, Twine("can't open secure log file: ") +
1766 SecureLogFile + " (" + Err + ")");
1767 }
1768 getContext().setSecureLog(OS);
1769 }
1770
1771 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1772 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1773 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1774 << LogMessage + "\n";
1775
1776 getContext().setSecureLogUsed(true);
1777
1778 return false;
1779}
1780
1781/// ParseDirectiveDarwinSecureLogReset
1782/// ::= .secure_log_reset
1783bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1784 if (Lexer.isNot(AsmToken::EndOfStatement))
1785 return TokError("unexpected token in '.secure_log_reset' directive");
1786
1787 Lex();
1788
1789 getContext().setSecureLogUsed(false);
1790
1791 return false;
1792}
1793
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001794/// ParseDirectiveIf
1795/// ::= .if expression
1796bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001797 TheCondStack.push_back(TheCondState);
1798 TheCondState.TheCond = AsmCond::IfCond;
1799 if(TheCondState.Ignore) {
1800 EatToEndOfStatement();
1801 }
1802 else {
1803 int64_t ExprValue;
1804 if (ParseAbsoluteExpression(ExprValue))
1805 return true;
1806
1807 if (Lexer.isNot(AsmToken::EndOfStatement))
1808 return TokError("unexpected token in '.if' directive");
1809
Sean Callanan79ed1a82010-01-19 20:22:31 +00001810 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001811
1812 TheCondState.CondMet = ExprValue;
1813 TheCondState.Ignore = !TheCondState.CondMet;
1814 }
1815
1816 return false;
1817}
1818
1819/// ParseDirectiveElseIf
1820/// ::= .elseif expression
1821bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1822 if (TheCondState.TheCond != AsmCond::IfCond &&
1823 TheCondState.TheCond != AsmCond::ElseIfCond)
1824 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1825 " an .elseif");
1826 TheCondState.TheCond = AsmCond::ElseIfCond;
1827
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001828 bool LastIgnoreState = false;
1829 if (!TheCondStack.empty())
1830 LastIgnoreState = TheCondStack.back().Ignore;
1831 if (LastIgnoreState || TheCondState.CondMet) {
1832 TheCondState.Ignore = true;
1833 EatToEndOfStatement();
1834 }
1835 else {
1836 int64_t ExprValue;
1837 if (ParseAbsoluteExpression(ExprValue))
1838 return true;
1839
1840 if (Lexer.isNot(AsmToken::EndOfStatement))
1841 return TokError("unexpected token in '.elseif' directive");
1842
Sean Callanan79ed1a82010-01-19 20:22:31 +00001843 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001844 TheCondState.CondMet = ExprValue;
1845 TheCondState.Ignore = !TheCondState.CondMet;
1846 }
1847
1848 return false;
1849}
1850
1851/// ParseDirectiveElse
1852/// ::= .else
1853bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001854 if (Lexer.isNot(AsmToken::EndOfStatement))
1855 return TokError("unexpected token in '.else' directive");
1856
Sean Callanan79ed1a82010-01-19 20:22:31 +00001857 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001858
1859 if (TheCondState.TheCond != AsmCond::IfCond &&
1860 TheCondState.TheCond != AsmCond::ElseIfCond)
1861 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1862 ".elseif");
1863 TheCondState.TheCond = AsmCond::ElseCond;
1864 bool LastIgnoreState = false;
1865 if (!TheCondStack.empty())
1866 LastIgnoreState = TheCondStack.back().Ignore;
1867 if (LastIgnoreState || TheCondState.CondMet)
1868 TheCondState.Ignore = true;
1869 else
1870 TheCondState.Ignore = false;
1871
1872 return false;
1873}
1874
1875/// ParseDirectiveEndIf
1876/// ::= .endif
1877bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001878 if (Lexer.isNot(AsmToken::EndOfStatement))
1879 return TokError("unexpected token in '.endif' directive");
1880
Sean Callanan79ed1a82010-01-19 20:22:31 +00001881 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001882
1883 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1884 TheCondStack.empty())
1885 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1886 ".else");
1887 if (!TheCondStack.empty()) {
1888 TheCondState = TheCondStack.back();
1889 TheCondStack.pop_back();
1890 }
1891
1892 return false;
1893}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001894
1895/// ParseDirectiveFile
1896/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001897bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001898 // FIXME: I'm not sure what this is.
1899 int64_t FileNumber = -1;
1900 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001901 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001902 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001903
1904 if (FileNumber < 1)
1905 return TokError("file number less than one");
1906 }
1907
1908 if (Lexer.isNot(AsmToken::String))
1909 return TokError("unexpected token in '.file' directive");
1910
Chris Lattnerd32e8032010-01-25 19:02:58 +00001911 StringRef Filename = getTok().getString();
1912 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001913 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001914
1915 if (Lexer.isNot(AsmToken::EndOfStatement))
1916 return TokError("unexpected token in '.file' directive");
1917
Chris Lattnerd32e8032010-01-25 19:02:58 +00001918 if (FileNumber == -1)
1919 Out.EmitFileDirective(Filename);
1920 else
1921 Out.EmitDwarfFileDirective(FileNumber, Filename);
1922
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001923 return false;
1924}
1925
1926/// ParseDirectiveLine
1927/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001928bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001929 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1930 if (Lexer.isNot(AsmToken::Integer))
1931 return TokError("unexpected token in '.line' directive");
1932
Sean Callanan18b83232010-01-19 21:44:56 +00001933 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001934 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001935 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001936
1937 // FIXME: Do something with the .line.
1938 }
1939
1940 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001941 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001942
1943 return false;
1944}
1945
1946
1947/// ParseDirectiveLoc
1948/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001949bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001950 if (Lexer.isNot(AsmToken::Integer))
1951 return TokError("unexpected token in '.loc' directive");
1952
1953 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001954 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001955 (void) FileNumber;
1956 // FIXME: Validate file.
1957
Sean Callanan79ed1a82010-01-19 20:22:31 +00001958 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001959 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1960 if (Lexer.isNot(AsmToken::Integer))
1961 return TokError("unexpected token in '.loc' directive");
1962
Sean Callanan18b83232010-01-19 21:44:56 +00001963 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001964 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001965 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001966
1967 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1968 if (Lexer.isNot(AsmToken::Integer))
1969 return TokError("unexpected token in '.loc' directive");
1970
Sean Callanan18b83232010-01-19 21:44:56 +00001971 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001972 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001973 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001974
1975 // FIXME: Do something with the .loc.
1976 }
1977 }
1978
1979 if (Lexer.isNot(AsmToken::EndOfStatement))
1980 return TokError("unexpected token in '.file' directive");
1981
1982 return false;
1983}
1984