blob: 0df839b84d9bf6d020e03f606858e37a54cd1dfc [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"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000016#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000018#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000019#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000024#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000025#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000027#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000028using namespace llvm;
29
Chris Lattneraaec2052010-01-19 19:46:13 +000030
31enum { DEFAULT_ADDRSPACE = 0 };
32
Chris Lattnerebb89b42009-09-27 21:16:52 +000033AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
34 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000035 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
Chris Lattnerf0559e42010-04-08 20:30:37 +000036 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000037 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
38
Chris Lattnerebb89b42009-09-27 21:16:52 +000039 // Debugging directives.
40 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
41 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
42 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
43}
44
45
46
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000047AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000048}
49
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000050void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000051 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000052}
53
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000054bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000055 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000056 return true;
57}
58
59bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000060 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000061 return true;
62}
63
Sean Callananbf2013e2010-01-20 23:19:55 +000064void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
65 const char *Type) const {
66 SrcMgr.PrintMessage(Loc, Msg, Type);
67}
Sean Callananfd0b0282010-01-21 00:19:58 +000068
69bool AsmParser::EnterIncludeFile(const std::string &Filename) {
70 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
71 if (NewBuf == -1)
72 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000073
Sean Callananfd0b0282010-01-21 00:19:58 +000074 CurBuffer = NewBuf;
75
76 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
77
78 return false;
79}
80
81const AsmToken &AsmParser::Lex() {
82 const AsmToken *tok = &Lexer.Lex();
83
84 if (tok->is(AsmToken::Eof)) {
85 // If this is the end of an included file, pop the parent file off the
86 // include stack.
87 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
88 if (ParentIncludeLoc != SMLoc()) {
89 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
90 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
91 ParentIncludeLoc.getPointer());
92 tok = &Lexer.Lex();
93 }
94 }
95
96 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000097 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000098
Sean Callananfd0b0282010-01-21 00:19:58 +000099 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000100}
101
Chris Lattner79180e22010-04-05 23:15:42 +0000102bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000103 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000104 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000105 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000106 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000107 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
109 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000110
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000111 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000112 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000113
Chris Lattnerb717fb02009-07-02 21:53:43 +0000114 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000115
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000116 AsmCond StartingCondState = TheCondState;
117
Chris Lattnerb717fb02009-07-02 21:53:43 +0000118 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000119 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000120 if (!ParseStatement()) continue;
121
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000122 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000123 HadError = true;
124 EatToEndOfStatement();
125 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000126
127 if (TheCondState.TheCond != StartingCondState.TheCond ||
128 TheCondState.Ignore != StartingCondState.Ignore)
129 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000130
Chris Lattner79180e22010-04-05 23:15:42 +0000131 // Finalize the output stream if there are no errors and if the client wants
132 // us to.
133 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000134 Out.Finish();
135
Chris Lattnerb717fb02009-07-02 21:53:43 +0000136 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000137}
138
Chris Lattner2cf5f142009-06-22 01:29:09 +0000139/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
140void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000141 while (Lexer.isNot(AsmToken::EndOfStatement) &&
142 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000143 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000144
145 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000146 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000147 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000148}
149
Chris Lattnerc4193832009-06-22 05:51:26 +0000150
Chris Lattner74ec1a32009-06-22 06:32:03 +0000151/// ParseParenExpr - Parse a paren expression and return it.
152/// NOTE: This assumes the leading '(' has already been consumed.
153///
154/// parenexpr ::= expr)
155///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000156bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000157 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000158 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000159 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000160 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000161 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000162 return false;
163}
Chris Lattnerc4193832009-06-22 05:51:26 +0000164
Daniel Dunbar959fd882009-08-26 22:13:22 +0000165MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000166 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000167 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000168}
169
Chris Lattner74ec1a32009-06-22 06:32:03 +0000170/// ParsePrimaryExpr - Parse a primary expression and return it.
171/// primaryexpr ::= (parenexpr
172/// primaryexpr ::= symbol
173/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000174/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000175/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000176bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000177 switch (Lexer.getKind()) {
178 default:
179 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000180 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000181 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000182 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000183 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000184 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000185 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000186 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000187 case AsmToken::Identifier: {
188 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000189 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
190 MCSymbol *Sym = CreateSymbol(Split.first);
191
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000192 // Mark the symbol as used in an expression.
193 Sym->setUsedInExpr(true);
194
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000195 // Lookup the symbol variant if used.
196 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
197 if (Split.first.size() != getTok().getIdentifier().size())
198 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
199
Chris Lattnerb4307b32010-01-15 19:28:38 +0000200 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000201 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000202
203 // If this is an absolute variable reference, substitute it now to preserve
204 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000205 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000206 if (Variant)
207 return Error(EndLoc, "unexpected modified on variable reference");
208
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000209 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000210 return false;
211 }
212
213 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000214 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000215 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000216 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000218 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000219 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000220 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000221 return false;
Chris Lattnerd3050352010-04-14 04:40:28 +0000222 case AsmToken::Dot: {
223 // This is a '.' reference, which references the current PC. Emit a
224 // temporary label to the streamer and refer to it.
225 MCSymbol *Sym = Ctx.CreateTempSymbol();
226 Out.EmitLabel(Sym);
227 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
228 EndLoc = Lexer.getLoc();
229 Lex(); // Eat identifier.
230 return false;
231 }
232
Daniel Dunbar3f872332009-07-28 16:08:33 +0000233 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000234 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000235 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000236 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000237 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000238 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000239 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000240 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000241 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000242 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000243 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000244 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000245 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000246 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000247 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000248 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000249 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000250 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000251 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000252 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000254 }
255}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000256
Chris Lattnerb4307b32010-01-15 19:28:38 +0000257bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000258 SMLoc EndLoc;
259 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000260}
261
Chris Lattner74ec1a32009-06-22 06:32:03 +0000262/// ParseExpression - Parse an expression and return it.
263///
264/// expr ::= expr +,- expr -> lowest.
265/// expr ::= expr |,^,&,! expr -> middle.
266/// expr ::= expr *,/,%,<<,>> expr -> highest.
267/// expr ::= primaryexpr
268///
Chris Lattner54482b42010-01-15 19:39:23 +0000269bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000270 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000272 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
273 return true;
274
275 // Try to constant fold it up front, if possible.
276 int64_t Value;
277 if (Res->EvaluateAsAbsolute(Value))
278 Res = MCConstantExpr::Create(Value, getContext());
279
280 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000281}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000282
Chris Lattnerb4307b32010-01-15 19:28:38 +0000283bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000284 Res = 0;
285 return ParseParenExpr(Res, EndLoc) ||
286 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000287}
288
Daniel Dunbar475839e2009-06-29 20:37:27 +0000289bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000290 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000291
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000292 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 if (ParseExpression(Expr))
294 return true;
295
Daniel Dunbare00b0112009-10-16 01:57:52 +0000296 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000297 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298
299 return false;
300}
301
Daniel Dunbar3f872332009-07-28 16:08:33 +0000302static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000303 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000304 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000305 default:
306 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307
308 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000309 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000310 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000311 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000312 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000313 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000314 return 1;
315
316 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000317 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000318 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000319 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000321 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000322 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000324 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::ExclaimEqual:
327 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000328 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000331 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000332 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000333 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000334 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
342
343 // Intermediate Precedence: |, &, ^
344 //
345 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000348 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000353 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000354 return 3;
355
356 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000361 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000362 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000372 }
373}
374
375
376/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
377/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000378bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
379 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000380 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000383
384 // If the next token is lower precedence than we are allowed to eat, return
385 // successfully with what we ate already.
386 if (TokPrec < Precedence)
387 return false;
388
Sean Callanan79ed1a82010-01-19 20:22:31 +0000389 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000390
391 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000392 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000393 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000394
395 // If BinOp binds less tightly with RHS than the operator after RHS, let
396 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000397 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000398 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000399 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000400 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401 }
402
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000404 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000405 }
406}
407
Chris Lattnerc4193832009-06-22 05:51:26 +0000408
409
410
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000411/// ParseStatement:
412/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000413/// ::= Label* Directive ...Operands... EndOfStatement
414/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000415bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000416 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000417 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000418 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000419 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000420
421 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000422 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000423 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000424 StringRef IDVal;
Chris Lattner7834fac2010-04-17 18:14:27 +0000425 if (ParseIdentifier(IDVal)) {
426 if (!TheCondState.Ignore)
427 return TokError("unexpected token at start of statement");
428 IDVal = "";
429 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000430
Chris Lattner7834fac2010-04-17 18:14:27 +0000431 // Handle conditional assembly here before checking for skipping. We
432 // have to do this so that .endif isn't skipped in a ".if 0" block for
433 // example.
434 if (IDVal == ".if")
435 return ParseDirectiveIf(IDLoc);
436 if (IDVal == ".elseif")
437 return ParseDirectiveElseIf(IDLoc);
438 if (IDVal == ".else")
439 return ParseDirectiveElse(IDLoc);
440 if (IDVal == ".endif")
441 return ParseDirectiveEndIf(IDLoc);
442
443 // If we are in a ".if 0" block, ignore this statement.
444 if (TheCondState.Ignore) {
445 EatToEndOfStatement();
446 return false;
447 }
448
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000449 // FIXME: Recurse on local labels?
450
451 // See what kind of statement we have.
452 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000454 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000455 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000456
457 // Diagnose attempt to use a variable as a label.
458 //
459 // FIXME: Diagnostics. Note the location of the definition as a label.
460 // FIXME: This doesn't diagnose assignment to a symbol which has been
461 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000462 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000463 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000464 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000465
Daniel Dunbar959fd882009-08-26 22:13:22 +0000466 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000467 Out.EmitLabel(Sym);
468
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000469 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000470 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000471
Daniel Dunbar3f872332009-07-28 16:08:33 +0000472 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000473 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000474 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000475
Daniel Dunbare2ace502009-08-31 08:09:09 +0000476 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000477
478 default: // Normal instruction or directive.
479 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000480 }
481
482 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000483 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000484 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000485 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000486 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000488 // FIXME: This changes behavior based on the -static flag to the
489 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000490 return ParseDirectiveSectionSwitch("__TEXT", "__text",
491 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000495 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000496 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000497 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
498 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000500 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000501 MCSectionMachO::S_4BYTE_LITERALS,
502 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000504 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000505 MCSectionMachO::S_8BYTE_LITERALS,
506 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000508 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000509 MCSectionMachO::S_16BYTE_LITERALS,
510 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000512 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000514 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000516 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000518 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
519
520 // FIXME: The assembler manual claims that this has the self modify code
521 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000522 if (IDVal == ".symbol_stub")
523 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
524 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000525 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
526 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000527 0, 16);
528 // FIXME: PowerPC only?
529 if (IDVal == ".picsymbol_stub")
530 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
531 MCSectionMachO::S_SYMBOL_STUBS |
532 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
533 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
538
539 // FIXME: The section names of these two are misspelled in the assembler
540 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
543 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
544 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
547 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
548 4);
549
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000553 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000554 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
555 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000557 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
559 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000562
563
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__class",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
590 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
591 MCSectionMachO::S_LITERAL_POINTERS,
592 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
595 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
596 MCSectionMachO::S_LITERAL_POINTERS,
597 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000602 return ParseDirectiveSectionSwitch("__OBJC", "__category",
603 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000605 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
606 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
612 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000614 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
615 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000617 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
618 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000620 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
621 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000623 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
624 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000625
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000626 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000628 return ParseDirectiveSet();
629
Daniel Dunbara0d14262009-06-24 23:30:00 +0000630 // Data directives
631
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000633 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000635 return ParseDirectiveAscii(true);
636
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000638 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000639 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000640 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000642 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000644 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000645
646 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000648 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000650 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000652 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000654 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000656 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000658 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000660 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000662 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
663
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000665 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000666
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000668 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000670 return ParseDirectiveSpace();
671
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000672 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000673
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000675 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000677 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000679 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000681 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000683 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000685 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000687 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000689 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000691 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000693 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000695 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000697 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000698
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000700 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000702 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000704 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000706 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000708 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000709 if (IDVal == ".tbss")
710 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000711
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000713 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000715 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000717 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000719 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000721 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000722
Chris Lattnerebb89b42009-09-27 21:16:52 +0000723 // Look up the handler in the handler table,
724 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
725 if (Handler)
726 return (this->*Handler)(IDVal, IDLoc);
727
Kevin Enderby9c656452009-09-10 20:51:44 +0000728 // Target hook for parsing target specific directives.
729 if (!getTargetParser().ParseDirective(ID))
730 return false;
731
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000732 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000733 EatToEndOfStatement();
734 return false;
735 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000736
Chris Lattner98986712010-01-14 22:21:20 +0000737 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000738 bool HadError = getTargetParser().ParseInstruction(IDVal, IDLoc,
739 ParsedOperands);
740 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
741 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000742
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000743 // If parsing succeeded, match the instruction.
744 if (!HadError) {
745 MCInst Inst;
746 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
747 // Emit the instruction on success.
748 Out.EmitInstruction(Inst);
749 } else {
750 // Otherwise emit a diagnostic about the match failure and set the error
751 // flag.
752 //
753 // FIXME: We should give nicer diagnostics about the exact failure.
754 Error(IDLoc, "unrecognized instruction");
755 HadError = true;
756 }
757 }
Chris Lattner98986712010-01-14 22:21:20 +0000758
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000759 // If there was no error, consume the end-of-statement token. Otherwise this
760 // will be done by our caller.
761 if (!HadError)
762 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000763
764 // Free any parsed operands.
765 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
766 delete ParsedOperands[i];
767
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000768 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000769}
Chris Lattner9a023f72009-06-24 04:43:34 +0000770
Daniel Dunbare2ace502009-08-31 08:09:09 +0000771bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000772 // FIXME: Use better location, we should use proper tokens.
773 SMLoc EqualLoc = Lexer.getLoc();
774
Daniel Dunbar821e3332009-08-31 08:09:28 +0000775 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000776 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000777 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000778 return true;
779
Daniel Dunbar3f872332009-07-28 16:08:33 +0000780 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000781 return TokError("unexpected token in assignment");
782
783 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000784 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000785
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000786 // Validate that the LHS is allowed to be a variable (either it has not been
787 // used as a symbol, or it is an absolute symbol).
788 MCSymbol *Sym = getContext().LookupSymbol(Name);
789 if (Sym) {
790 // Diagnose assignment to a label.
791 //
792 // FIXME: Diagnostics. Note the location of the definition as a label.
793 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000794 if (Sym->isUndefined() && !Sym->isUsedInExpr())
795 ; // Allow redefinitions of undefined symbols only used in directives.
796 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000797 return Error(EqualLoc, "redefinition of '" + Name + "'");
798 else if (!Sym->isVariable())
799 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000800 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000801 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
802 Name + "'");
803 } else
804 Sym = CreateSymbol(Name);
805
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000806 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000807
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000808 Sym->setUsedInExpr(true);
809
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000810 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000811 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000812
813 return false;
814}
815
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000816/// ParseIdentifier:
817/// ::= identifier
818/// ::= string
819bool AsmParser::ParseIdentifier(StringRef &Res) {
820 if (Lexer.isNot(AsmToken::Identifier) &&
821 Lexer.isNot(AsmToken::String))
822 return true;
823
Sean Callanan18b83232010-01-19 21:44:56 +0000824 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000825
Sean Callanan79ed1a82010-01-19 20:22:31 +0000826 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000827
828 return false;
829}
830
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000831/// ParseDirectiveSet:
832/// ::= .set identifier ',' expression
833bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000834 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000835
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000836 if (ParseIdentifier(Name))
837 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000838
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000839 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000840 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000841 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000842
Daniel Dunbare2ace502009-08-31 08:09:09 +0000843 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000844}
845
Chris Lattner9a023f72009-06-24 04:43:34 +0000846/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000847/// ::= .section identifier (',' identifier)*
848/// FIXME: This should actually parse out the segment, section, attributes and
849/// sizeof_stub fields.
850bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000851 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000852
Daniel Dunbarace63122009-08-11 03:42:33 +0000853 StringRef SectionName;
854 if (ParseIdentifier(SectionName))
855 return Error(Loc, "expected identifier after '.section' directive");
856
857 // Verify there is a following comma.
858 if (!Lexer.is(AsmToken::Comma))
859 return TokError("unexpected token in '.section' directive");
860
Chris Lattnerff4bc462009-08-10 01:39:42 +0000861 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000862 SectionSpec += ",";
863
864 // Add all the tokens until the end of the line, ParseSectionSpecifier will
865 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000866 StringRef EOL = Lexer.LexUntilEndOfStatement();
867 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000868
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000870 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000871 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000872 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000873
Chris Lattnerff4bc462009-08-10 01:39:42 +0000874
875 StringRef Segment, Section;
876 unsigned TAA, StubSize;
877 std::string ErrorStr =
878 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
879 TAA, StubSize);
880
881 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000882 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000883
Chris Lattner56594f92009-07-31 17:47:16 +0000884 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000885 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000886 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
887 isText ? SectionKind::getText()
888 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000889 return false;
890}
891
Chris Lattnere15c2d72009-08-10 18:05:55 +0000892/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000893bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
894 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000895 unsigned TAA, unsigned Align,
896 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000897 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000898 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000899 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000900
Chris Lattner56594f92009-07-31 17:47:16 +0000901 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000902 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000903 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
904 isText ? SectionKind::getText()
905 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000906
907 // Set the implicit alignment, if any.
908 //
909 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
910 // alignment on the section (e.g., if one manually inserts bytes into the
911 // section, then just issueing the section switch directive will not realign
912 // the section. However, this is arguably more reasonable behavior, and there
913 // is no good reason for someone to intentionally emit incorrectly sized
914 // values into the implicitly aligned sections.
915 if (Align)
916 Out.EmitValueToAlignment(Align, 0, 1, 0);
917
Chris Lattner529fb542009-06-24 05:13:15 +0000918 return false;
919}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000920
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000921bool AsmParser::ParseEscapedString(std::string &Data) {
922 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
923
924 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000925 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000926 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
927 if (Str[i] != '\\') {
928 Data += Str[i];
929 continue;
930 }
931
932 // Recognize escaped characters. Note that this escape semantics currently
933 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
934 ++i;
935 if (i == e)
936 return TokError("unexpected backslash at end of string");
937
938 // Recognize octal sequences.
939 if ((unsigned) (Str[i] - '0') <= 7) {
940 // Consume up to three octal characters.
941 unsigned Value = Str[i] - '0';
942
943 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
944 ++i;
945 Value = Value * 8 + (Str[i] - '0');
946
947 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
948 ++i;
949 Value = Value * 8 + (Str[i] - '0');
950 }
951 }
952
953 if (Value > 255)
954 return TokError("invalid octal escape sequence (out of range)");
955
956 Data += (unsigned char) Value;
957 continue;
958 }
959
960 // Otherwise recognize individual escapes.
961 switch (Str[i]) {
962 default:
963 // Just reject invalid escape sequences for now.
964 return TokError("invalid escape sequence (unrecognized character)");
965
966 case 'b': Data += '\b'; break;
967 case 'f': Data += '\f'; break;
968 case 'n': Data += '\n'; break;
969 case 'r': Data += '\r'; break;
970 case 't': Data += '\t'; break;
971 case '"': Data += '"'; break;
972 case '\\': Data += '\\'; break;
973 }
974 }
975
976 return false;
977}
978
Daniel Dunbara0d14262009-06-24 23:30:00 +0000979/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000980/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000981bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000982 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000983 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000984 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000985 return TokError("expected string in '.ascii' or '.asciz' directive");
986
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000987 std::string Data;
988 if (ParseEscapedString(Data))
989 return true;
990
Chris Lattneraaec2052010-01-19 19:46:13 +0000991 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000992 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +0000993 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000994
Sean Callanan79ed1a82010-01-19 20:22:31 +0000995 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996
Daniel Dunbar3f872332009-07-28 16:08:33 +0000997 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998 break;
999
Daniel Dunbar3f872332009-07-28 16:08:33 +00001000 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001001 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001002 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001003 }
1004 }
1005
Sean Callanan79ed1a82010-01-19 20:22:31 +00001006 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 return false;
1008}
1009
1010/// ParseDirectiveValue
1011/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1012bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001013 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001014 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001015 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001016 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001017 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018 return true;
1019
Chris Lattneraaec2052010-01-19 19:46:13 +00001020 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021
Daniel Dunbar3f872332009-07-28 16:08:33 +00001022 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023 break;
1024
1025 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001026 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001027 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001028 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 }
1030 }
1031
Sean Callanan79ed1a82010-01-19 20:22:31 +00001032 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033 return false;
1034}
1035
1036/// ParseDirectiveSpace
1037/// ::= .space expression [ , expression ]
1038bool AsmParser::ParseDirectiveSpace() {
1039 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001040 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001041 return true;
1042
1043 int64_t FillExpr = 0;
1044 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001045 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1046 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001047 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001048 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049
Daniel Dunbar475839e2009-06-29 20:37:27 +00001050 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 return true;
1052
1053 HasFillExpr = true;
1054
Daniel Dunbar3f872332009-07-28 16:08:33 +00001055 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001056 return TokError("unexpected token in '.space' directive");
1057 }
1058
Sean Callanan79ed1a82010-01-19 20:22:31 +00001059 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060
1061 if (NumBytes <= 0)
1062 return TokError("invalid number of bytes in '.space' directive");
1063
1064 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001065 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001066
1067 return false;
1068}
1069
1070/// ParseDirectiveFill
1071/// ::= .fill expression , expression , expression
1072bool AsmParser::ParseDirectiveFill() {
1073 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001074 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001075 return true;
1076
Daniel Dunbar3f872332009-07-28 16:08:33 +00001077 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001079 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001080
1081 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001082 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083 return true;
1084
Daniel Dunbar3f872332009-07-28 16:08:33 +00001085 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001087 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088
1089 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001090 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001091 return true;
1092
Daniel Dunbar3f872332009-07-28 16:08:33 +00001093 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001094 return TokError("unexpected token in '.fill' directive");
1095
Sean Callanan79ed1a82010-01-19 20:22:31 +00001096 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001097
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001098 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1099 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001100
1101 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001102 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1103 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001104
1105 return false;
1106}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001107
1108/// ParseDirectiveOrg
1109/// ::= .org expression [ , expression ]
1110bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001111 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001112 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001113 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001114 return true;
1115
1116 // Parse optional fill expression.
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 Dunbarc238b582009-06-25 22:44:51 +00001120 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001121 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001122
Daniel Dunbar475839e2009-06-29 20:37:27 +00001123 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001124 return true;
1125
Daniel Dunbar3f872332009-07-28 16:08:33 +00001126 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001127 return TokError("unexpected token in '.org' directive");
1128 }
1129
Sean Callanan79ed1a82010-01-19 20:22:31 +00001130 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001131
1132 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1133 // has to be relative to the current section.
1134 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001135
1136 return false;
1137}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001138
1139/// ParseDirectiveAlign
1140/// ::= {.align, ...} expression [ , expression [ , expression ]]
1141bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001142 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001143 int64_t Alignment;
1144 if (ParseAbsoluteExpression(Alignment))
1145 return true;
1146
1147 SMLoc MaxBytesLoc;
1148 bool HasFillExpr = false;
1149 int64_t FillExpr = 0;
1150 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001151 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1152 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001153 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001154 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001155
1156 // The fill expression can be omitted while specifying a maximum number of
1157 // alignment bytes, e.g:
1158 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001159 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001160 HasFillExpr = true;
1161 if (ParseAbsoluteExpression(FillExpr))
1162 return true;
1163 }
1164
Daniel Dunbar3f872332009-07-28 16:08:33 +00001165 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1166 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001167 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001168 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001169
1170 MaxBytesLoc = Lexer.getLoc();
1171 if (ParseAbsoluteExpression(MaxBytesToFill))
1172 return true;
1173
Daniel Dunbar3f872332009-07-28 16:08:33 +00001174 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001175 return TokError("unexpected token in directive");
1176 }
1177 }
1178
Sean Callanan79ed1a82010-01-19 20:22:31 +00001179 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001180
1181 if (!HasFillExpr) {
1182 // FIXME: Sometimes fill with nop.
1183 FillExpr = 0;
1184 }
1185
1186 // Compute alignment in bytes.
1187 if (IsPow2) {
1188 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001189 if (Alignment >= 32) {
1190 Error(AlignmentLoc, "invalid alignment value");
1191 Alignment = 31;
1192 }
1193
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001194 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001195 }
1196
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001197 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001198 if (MaxBytesLoc.isValid()) {
1199 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001200 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1201 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001202 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001203 }
1204
1205 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001206 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1207 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001208 MaxBytesToFill = 0;
1209 }
1210 }
1211
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001212 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1213 // the TextAlignFillValue.
1214 if(Out.getCurrentSection()->getKind().isText() &&
1215 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1216 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1217 else
1218 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1219 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001220
1221 return false;
1222}
1223
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001224/// ParseDirectiveSymbolAttribute
1225/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001226bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001227 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001228 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001229 StringRef Name;
1230
1231 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001232 return TokError("expected identifier in directive");
1233
Daniel Dunbar959fd882009-08-26 22:13:22 +00001234 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001235
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001236 Out.EmitSymbolAttribute(Sym, Attr);
1237
Daniel Dunbar3f872332009-07-28 16:08:33 +00001238 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001239 break;
1240
Daniel Dunbar3f872332009-07-28 16:08:33 +00001241 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001242 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001243 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001244 }
1245 }
1246
Sean Callanan79ed1a82010-01-19 20:22:31 +00001247 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001248 return false;
1249}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001250
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001251/// ParseDirectiveDarwinSymbolDesc
1252/// ::= .desc identifier , expression
1253bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001254 StringRef Name;
1255 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001256 return TokError("expected identifier in directive");
1257
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001258 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001259 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001260
Daniel Dunbar3f872332009-07-28 16:08:33 +00001261 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001262 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001263 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001264
1265 SMLoc DescLoc = Lexer.getLoc();
1266 int64_t DescValue;
1267 if (ParseAbsoluteExpression(DescValue))
1268 return true;
1269
Daniel Dunbar3f872332009-07-28 16:08:33 +00001270 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001271 return TokError("unexpected token in '.desc' directive");
1272
Sean Callanan79ed1a82010-01-19 20:22:31 +00001273 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001274
1275 // Set the n_desc field of this Symbol to this DescValue
1276 Out.EmitSymbolDesc(Sym, DescValue);
1277
1278 return false;
1279}
1280
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001281/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001282/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1283bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001284 SMLoc IDLoc = Lexer.getLoc();
1285 StringRef Name;
1286 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001287 return TokError("expected identifier in directive");
1288
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001289 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001290 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001291
Daniel Dunbar3f872332009-07-28 16:08:33 +00001292 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001293 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001294 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001295
1296 int64_t Size;
1297 SMLoc SizeLoc = Lexer.getLoc();
1298 if (ParseAbsoluteExpression(Size))
1299 return true;
1300
1301 int64_t Pow2Alignment = 0;
1302 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001303 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001304 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001305 Pow2AlignmentLoc = Lexer.getLoc();
1306 if (ParseAbsoluteExpression(Pow2Alignment))
1307 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001308
1309 // If this target takes alignments in bytes (not log) validate and convert.
1310 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1311 if (!isPowerOf2_64(Pow2Alignment))
1312 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1313 Pow2Alignment = Log2_64(Pow2Alignment);
1314 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001315 }
1316
Daniel Dunbar3f872332009-07-28 16:08:33 +00001317 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001318 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001319
Sean Callanan79ed1a82010-01-19 20:22:31 +00001320 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001321
Chris Lattner1fc3d752009-07-09 17:25:12 +00001322 // NOTE: a size of zero for a .comm should create a undefined symbol
1323 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001324 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001325 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1326 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001327
Eric Christopherc260a3e2010-05-14 01:38:54 +00001328 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001329 // may internally end up wanting an alignment in bytes.
1330 // FIXME: Diagnose overflow.
1331 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001332 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1333 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001334
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001335 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001336 return Error(IDLoc, "invalid symbol redefinition");
1337
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001338 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001339 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001340 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001341 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1342 MCSectionMachO::S_ZEROFILL, 0,
1343 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001344 Sym, Size, 1 << Pow2Alignment);
1345 return false;
1346 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001347
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001348 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001349 return false;
1350}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001351
1352/// ParseDirectiveDarwinZerofill
1353/// ::= .zerofill segname , sectname [, identifier , size_expression [
1354/// , align_expression ]]
1355bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001356 StringRef Segment;
1357 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001358 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001359
Daniel Dunbar3f872332009-07-28 16:08:33 +00001360 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001361 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001362 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001363
1364 StringRef Section;
1365 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001366 return TokError("expected section name after comma in '.zerofill' "
1367 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001368
Chris Lattner9be3fee2009-07-10 22:20:30 +00001369 // If this is the end of the line all that was wanted was to create the
1370 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001371 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001372 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001373 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1374 MCSectionMachO::S_ZEROFILL, 0,
1375 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001376 return false;
1377 }
1378
Daniel Dunbar3f872332009-07-28 16:08:33 +00001379 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001380 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001381 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001382
Chris Lattner7bb7c552010-05-13 00:10:34 +00001383 SMLoc IDLoc = Lexer.getLoc();
1384 StringRef IDStr;
1385 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001386 return TokError("expected identifier in directive");
1387
1388 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001389 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001390
Daniel Dunbar3f872332009-07-28 16:08:33 +00001391 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001392 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001393 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001394
1395 int64_t Size;
1396 SMLoc SizeLoc = Lexer.getLoc();
1397 if (ParseAbsoluteExpression(Size))
1398 return true;
1399
1400 int64_t Pow2Alignment = 0;
1401 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001402 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001403 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001404 Pow2AlignmentLoc = Lexer.getLoc();
1405 if (ParseAbsoluteExpression(Pow2Alignment))
1406 return true;
1407 }
1408
Daniel Dunbar3f872332009-07-28 16:08:33 +00001409 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001410 return TokError("unexpected token in '.zerofill' directive");
1411
Sean Callanan79ed1a82010-01-19 20:22:31 +00001412 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001413
1414 if (Size < 0)
1415 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1416 "than zero");
1417
Eric Christopherc260a3e2010-05-14 01:38:54 +00001418 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001419 // may internally end up wanting an alignment in bytes.
1420 // FIXME: Diagnose overflow.
1421 if (Pow2Alignment < 0)
1422 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1423 "can't be less than zero");
1424
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001425 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001426 return Error(IDLoc, "invalid symbol redefinition");
1427
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001428 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001429 //
1430 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001431 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1432 MCSectionMachO::S_ZEROFILL, 0,
1433 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001434 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001435
1436 return false;
1437}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001438
Eric Christopher482eba02010-05-14 01:50:28 +00001439/// ParseDirectiveDarwinTBSS
1440/// ::= .tbss identifier, size, align
1441bool AsmParser::ParseDirectiveDarwinTBSS() {
1442 SMLoc IDLoc = Lexer.getLoc();
1443 StringRef Name;
1444 if (ParseIdentifier(Name))
1445 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001446
Eric Christopher482eba02010-05-14 01:50:28 +00001447 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001448 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001449
1450 if (Lexer.isNot(AsmToken::Comma))
1451 return TokError("unexpected token in directive");
1452 Lex();
1453
1454 int64_t Size;
1455 SMLoc SizeLoc = Lexer.getLoc();
1456 if (ParseAbsoluteExpression(Size))
1457 return true;
1458
1459 int64_t Pow2Alignment = 0;
1460 SMLoc Pow2AlignmentLoc;
1461 if (Lexer.is(AsmToken::Comma)) {
1462 Lex();
1463 Pow2AlignmentLoc = Lexer.getLoc();
1464 if (ParseAbsoluteExpression(Pow2Alignment))
1465 return true;
1466 }
1467
1468 if (Lexer.isNot(AsmToken::EndOfStatement))
1469 return TokError("unexpected token in '.tbss' directive");
1470
1471 Lex();
1472
1473 if (Size < 0)
1474 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1475 "zero");
1476
1477 // FIXME: Diagnose overflow.
1478 if (Pow2Alignment < 0)
1479 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1480 "than zero");
1481
1482 if (!Sym->isUndefined())
1483 return Error(IDLoc, "invalid symbol redefinition");
1484
1485 Out.EmitTBSSSymbol(Sym, Size, Pow2Alignment ? 1 << Pow2Alignment : 0);
1486
1487 return false;
1488}
1489
Kevin Enderbya5c78322009-07-13 21:03:15 +00001490/// ParseDirectiveDarwinSubsectionsViaSymbols
1491/// ::= .subsections_via_symbols
1492bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001493 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001494 return TokError("unexpected token in '.subsections_via_symbols' directive");
1495
Sean Callanan79ed1a82010-01-19 20:22:31 +00001496 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001497
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001498 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001499
1500 return false;
1501}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001502
1503/// ParseDirectiveAbort
1504/// ::= .abort [ "abort_string" ]
1505bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001506 // FIXME: Use loc from directive.
1507 SMLoc Loc = Lexer.getLoc();
1508
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001509 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001510 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1511 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001512 return TokError("expected string in '.abort' directive");
1513
Sean Callanan18b83232010-01-19 21:44:56 +00001514 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001515
Sean Callanan79ed1a82010-01-19 20:22:31 +00001516 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001517 }
1518
Daniel Dunbar3f872332009-07-28 16:08:33 +00001519 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001520 return TokError("unexpected token in '.abort' directive");
1521
Sean Callanan79ed1a82010-01-19 20:22:31 +00001522 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001523
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001524 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001525 if (Str.empty())
1526 Error(Loc, ".abort detected. Assembly stopping.");
1527 else
1528 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001529
1530 return false;
1531}
Kevin Enderby71148242009-07-14 21:35:03 +00001532
1533/// ParseDirectiveLsym
1534/// ::= .lsym identifier , expression
1535bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001536 StringRef Name;
1537 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001538 return TokError("expected identifier in directive");
1539
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001540 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001541 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001542
Daniel Dunbar3f872332009-07-28 16:08:33 +00001543 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001544 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001545 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001546
Daniel Dunbar821e3332009-08-31 08:09:28 +00001547 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001548 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001549 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001550 return true;
1551
Daniel Dunbar3f872332009-07-28 16:08:33 +00001552 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001553 return TokError("unexpected token in '.lsym' directive");
1554
Sean Callanan79ed1a82010-01-19 20:22:31 +00001555 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001556
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001557 // We don't currently support this directive.
1558 //
1559 // FIXME: Diagnostic location!
1560 (void) Sym;
1561 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001562}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001563
1564/// ParseDirectiveInclude
1565/// ::= .include "filename"
1566bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001567 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001568 return TokError("expected string in '.include' directive");
1569
Sean Callanan18b83232010-01-19 21:44:56 +00001570 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001571 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001572 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001573
Daniel Dunbar3f872332009-07-28 16:08:33 +00001574 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001575 return TokError("unexpected token in '.include' directive");
1576
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001577 // Strip the quotes.
1578 Filename = Filename.substr(1, Filename.size()-2);
1579
1580 // Attempt to switch the lexer to the included file before consuming the end
1581 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001582 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001583 PrintMessage(IncludeLoc,
1584 "Could not find include file '" + Filename + "'",
1585 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001586 return true;
1587 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001588
1589 return false;
1590}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001591
1592/// ParseDirectiveDarwinDumpOrLoad
1593/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001594bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001595 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001596 return TokError("expected string in '.dump' or '.load' directive");
1597
Sean Callanan79ed1a82010-01-19 20:22:31 +00001598 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001599
Daniel Dunbar3f872332009-07-28 16:08:33 +00001600 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001601 return TokError("unexpected token in '.dump' or '.load' directive");
1602
Sean Callanan79ed1a82010-01-19 20:22:31 +00001603 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001604
Kevin Enderby5026ae42009-07-20 20:25:37 +00001605 // FIXME: If/when .dump and .load are implemented they will be done in the
1606 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001607 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001608 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001609 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001610 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001611
1612 return false;
1613}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001614
1615/// ParseDirectiveIf
1616/// ::= .if expression
1617bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001618 TheCondStack.push_back(TheCondState);
1619 TheCondState.TheCond = AsmCond::IfCond;
1620 if(TheCondState.Ignore) {
1621 EatToEndOfStatement();
1622 }
1623 else {
1624 int64_t ExprValue;
1625 if (ParseAbsoluteExpression(ExprValue))
1626 return true;
1627
1628 if (Lexer.isNot(AsmToken::EndOfStatement))
1629 return TokError("unexpected token in '.if' directive");
1630
Sean Callanan79ed1a82010-01-19 20:22:31 +00001631 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001632
1633 TheCondState.CondMet = ExprValue;
1634 TheCondState.Ignore = !TheCondState.CondMet;
1635 }
1636
1637 return false;
1638}
1639
1640/// ParseDirectiveElseIf
1641/// ::= .elseif expression
1642bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1643 if (TheCondState.TheCond != AsmCond::IfCond &&
1644 TheCondState.TheCond != AsmCond::ElseIfCond)
1645 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1646 " an .elseif");
1647 TheCondState.TheCond = AsmCond::ElseIfCond;
1648
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001649 bool LastIgnoreState = false;
1650 if (!TheCondStack.empty())
1651 LastIgnoreState = TheCondStack.back().Ignore;
1652 if (LastIgnoreState || TheCondState.CondMet) {
1653 TheCondState.Ignore = true;
1654 EatToEndOfStatement();
1655 }
1656 else {
1657 int64_t ExprValue;
1658 if (ParseAbsoluteExpression(ExprValue))
1659 return true;
1660
1661 if (Lexer.isNot(AsmToken::EndOfStatement))
1662 return TokError("unexpected token in '.elseif' directive");
1663
Sean Callanan79ed1a82010-01-19 20:22:31 +00001664 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001665 TheCondState.CondMet = ExprValue;
1666 TheCondState.Ignore = !TheCondState.CondMet;
1667 }
1668
1669 return false;
1670}
1671
1672/// ParseDirectiveElse
1673/// ::= .else
1674bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001675 if (Lexer.isNot(AsmToken::EndOfStatement))
1676 return TokError("unexpected token in '.else' directive");
1677
Sean Callanan79ed1a82010-01-19 20:22:31 +00001678 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001679
1680 if (TheCondState.TheCond != AsmCond::IfCond &&
1681 TheCondState.TheCond != AsmCond::ElseIfCond)
1682 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1683 ".elseif");
1684 TheCondState.TheCond = AsmCond::ElseCond;
1685 bool LastIgnoreState = false;
1686 if (!TheCondStack.empty())
1687 LastIgnoreState = TheCondStack.back().Ignore;
1688 if (LastIgnoreState || TheCondState.CondMet)
1689 TheCondState.Ignore = true;
1690 else
1691 TheCondState.Ignore = false;
1692
1693 return false;
1694}
1695
1696/// ParseDirectiveEndIf
1697/// ::= .endif
1698bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001699 if (Lexer.isNot(AsmToken::EndOfStatement))
1700 return TokError("unexpected token in '.endif' directive");
1701
Sean Callanan79ed1a82010-01-19 20:22:31 +00001702 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001703
1704 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1705 TheCondStack.empty())
1706 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1707 ".else");
1708 if (!TheCondStack.empty()) {
1709 TheCondState = TheCondStack.back();
1710 TheCondStack.pop_back();
1711 }
1712
1713 return false;
1714}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001715
1716/// ParseDirectiveFile
1717/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001718bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001719 // FIXME: I'm not sure what this is.
1720 int64_t FileNumber = -1;
1721 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001722 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001723 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001724
1725 if (FileNumber < 1)
1726 return TokError("file number less than one");
1727 }
1728
1729 if (Lexer.isNot(AsmToken::String))
1730 return TokError("unexpected token in '.file' directive");
1731
Chris Lattnerd32e8032010-01-25 19:02:58 +00001732 StringRef Filename = getTok().getString();
1733 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001734 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001735
1736 if (Lexer.isNot(AsmToken::EndOfStatement))
1737 return TokError("unexpected token in '.file' directive");
1738
Chris Lattnerd32e8032010-01-25 19:02:58 +00001739 if (FileNumber == -1)
1740 Out.EmitFileDirective(Filename);
1741 else
1742 Out.EmitDwarfFileDirective(FileNumber, Filename);
1743
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001744 return false;
1745}
1746
1747/// ParseDirectiveLine
1748/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001749bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001750 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1751 if (Lexer.isNot(AsmToken::Integer))
1752 return TokError("unexpected token in '.line' directive");
1753
Sean Callanan18b83232010-01-19 21:44:56 +00001754 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001755 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001756 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001757
1758 // FIXME: Do something with the .line.
1759 }
1760
1761 if (Lexer.isNot(AsmToken::EndOfStatement))
1762 return TokError("unexpected token in '.file' directive");
1763
1764 return false;
1765}
1766
1767
1768/// ParseDirectiveLoc
1769/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001770bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001771 if (Lexer.isNot(AsmToken::Integer))
1772 return TokError("unexpected token in '.loc' directive");
1773
1774 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001775 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001776 (void) FileNumber;
1777 // FIXME: Validate file.
1778
Sean Callanan79ed1a82010-01-19 20:22:31 +00001779 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001780 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1781 if (Lexer.isNot(AsmToken::Integer))
1782 return TokError("unexpected token in '.loc' directive");
1783
Sean Callanan18b83232010-01-19 21:44:56 +00001784 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001785 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001786 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001787
1788 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1789 if (Lexer.isNot(AsmToken::Integer))
1790 return TokError("unexpected token in '.loc' directive");
1791
Sean Callanan18b83232010-01-19 21:44:56 +00001792 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001793 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001794 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001795
1796 // FIXME: Do something with the .loc.
1797 }
1798 }
1799
1800 if (Lexer.isNot(AsmToken::EndOfStatement))
1801 return TokError("unexpected token in '.file' directive");
1802
1803 return false;
1804}
1805