blob: 18977de8adb9e035126a64f7fbb1f5dea1d6a9a4 [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
192 // Lookup the symbol variant if used.
193 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
194 if (Split.first.size() != getTok().getIdentifier().size())
195 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
196
Chris Lattnerb4307b32010-01-15 19:28:38 +0000197 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000198 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000199
200 // If this is an absolute variable reference, substitute it now to preserve
201 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000202 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000203 if (Variant)
204 return Error(EndLoc, "unexpected modified on variable reference");
205
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000206 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000207 return false;
208 }
209
210 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000211 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000212 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000213 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000214 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000215 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000216 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000217 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000218 return false;
Chris Lattnerd3050352010-04-14 04:40:28 +0000219 case AsmToken::Dot: {
220 // This is a '.' reference, which references the current PC. Emit a
221 // temporary label to the streamer and refer to it.
222 MCSymbol *Sym = Ctx.CreateTempSymbol();
223 Out.EmitLabel(Sym);
224 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
225 EndLoc = Lexer.getLoc();
226 Lex(); // Eat identifier.
227 return false;
228 }
229
Daniel Dunbar3f872332009-07-28 16:08:33 +0000230 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000231 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000232 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000233 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000234 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000235 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000236 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000237 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000238 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000239 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000240 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000241 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000242 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000243 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000244 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000245 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000246 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000247 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000248 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000249 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000250 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000251 }
252}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000253
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000255 SMLoc EndLoc;
256 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000257}
258
Chris Lattner74ec1a32009-06-22 06:32:03 +0000259/// ParseExpression - Parse an expression and return it.
260///
261/// expr ::= expr +,- expr -> lowest.
262/// expr ::= expr |,^,&,! expr -> middle.
263/// expr ::= expr *,/,%,<<,>> expr -> highest.
264/// expr ::= primaryexpr
265///
Chris Lattner54482b42010-01-15 19:39:23 +0000266bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000267 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000269 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
270 return true;
271
272 // Try to constant fold it up front, if possible.
273 int64_t Value;
274 if (Res->EvaluateAsAbsolute(Value))
275 Res = MCConstantExpr::Create(Value, getContext());
276
277 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000278}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000279
Chris Lattnerb4307b32010-01-15 19:28:38 +0000280bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000281 Res = 0;
282 return ParseParenExpr(Res, EndLoc) ||
283 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000284}
285
Daniel Dunbar475839e2009-06-29 20:37:27 +0000286bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000287 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000288
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000289 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000290 if (ParseExpression(Expr))
291 return true;
292
Daniel Dunbare00b0112009-10-16 01:57:52 +0000293 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000294 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000295
296 return false;
297}
298
Daniel Dunbar3f872332009-07-28 16:08:33 +0000299static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000300 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000301 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000302 default:
303 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304
305 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000306 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000307 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000308 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000309 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000310 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000311 return 1;
312
313 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000314 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000315 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000316 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000317 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000318 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000319 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000321 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000322 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::ExclaimEqual:
324 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000325 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000326 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000327 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000328 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000331 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000332 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000333 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000334 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
339
340 // Intermediate Precedence: |, &, ^
341 //
342 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000344 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000345 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 3;
352
353 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000354 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000355 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000356 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000361 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000362 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000369 }
370}
371
372
373/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
374/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000375bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
376 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000377 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000378 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000379 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000380
381 // If the next token is lower precedence than we are allowed to eat, return
382 // successfully with what we ate already.
383 if (TokPrec < Precedence)
384 return false;
385
Sean Callanan79ed1a82010-01-19 20:22:31 +0000386 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000387
388 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000389 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000390 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000391
392 // If BinOp binds less tightly with RHS than the operator after RHS, let
393 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000394 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000395 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000396 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000397 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 }
399
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000401 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000402 }
403}
404
Chris Lattnerc4193832009-06-22 05:51:26 +0000405
406
407
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000408/// ParseStatement:
409/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000410/// ::= Label* Directive ...Operands... EndOfStatement
411/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000412bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000413 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000414 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000415 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000416 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000417
418 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000419 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000420 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000421 StringRef IDVal;
Chris Lattner7834fac2010-04-17 18:14:27 +0000422 if (ParseIdentifier(IDVal)) {
423 if (!TheCondState.Ignore)
424 return TokError("unexpected token at start of statement");
425 IDVal = "";
426 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000427
Chris Lattner7834fac2010-04-17 18:14:27 +0000428 // Handle conditional assembly here before checking for skipping. We
429 // have to do this so that .endif isn't skipped in a ".if 0" block for
430 // example.
431 if (IDVal == ".if")
432 return ParseDirectiveIf(IDLoc);
433 if (IDVal == ".elseif")
434 return ParseDirectiveElseIf(IDLoc);
435 if (IDVal == ".else")
436 return ParseDirectiveElse(IDLoc);
437 if (IDVal == ".endif")
438 return ParseDirectiveEndIf(IDLoc);
439
440 // If we are in a ".if 0" block, ignore this statement.
441 if (TheCondState.Ignore) {
442 EatToEndOfStatement();
443 return false;
444 }
445
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000446 // FIXME: Recurse on local labels?
447
448 // See what kind of statement we have.
449 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000451 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000452 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000453
454 // Diagnose attempt to use a variable as a label.
455 //
456 // FIXME: Diagnostics. Note the location of the definition as a label.
457 // FIXME: This doesn't diagnose assignment to a symbol which has been
458 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000459 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000460 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000461 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000462
Daniel Dunbar959fd882009-08-26 22:13:22 +0000463 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000464 Out.EmitLabel(Sym);
465
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000466 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000467 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000468
Daniel Dunbar3f872332009-07-28 16:08:33 +0000469 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000470 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000471 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000472
Daniel Dunbare2ace502009-08-31 08:09:09 +0000473 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000474
475 default: // Normal instruction or directive.
476 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000477 }
478
479 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000480 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000481 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000483 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000485 // FIXME: This changes behavior based on the -static flag to the
486 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000487 return ParseDirectiveSectionSwitch("__TEXT", "__text",
488 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000489 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000490 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000491 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000492 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000494 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
495 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000496 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000497 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000498 MCSectionMachO::S_4BYTE_LITERALS,
499 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000500 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000501 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000502 MCSectionMachO::S_8BYTE_LITERALS,
503 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000504 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000505 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000506 MCSectionMachO::S_16BYTE_LITERALS,
507 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000508 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000509 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000510 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000511 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000513 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000514 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000515 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
516
517 // FIXME: The assembler manual claims that this has the self modify code
518 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000519 if (IDVal == ".symbol_stub")
520 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
521 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000522 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
523 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000524 0, 16);
525 // FIXME: PowerPC only?
526 if (IDVal == ".picsymbol_stub")
527 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
528 MCSectionMachO::S_SYMBOL_STUBS |
529 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
530 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000532 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
535
536 // FIXME: The section names of these two are misspelled in the assembler
537 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000539 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
540 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
541 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
544 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
545 4);
546
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000549 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000550 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
552 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000553 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000554 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
556 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000559
560
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000562 return ParseDirectiveSectionSwitch("__OBJC", "__class",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
588 MCSectionMachO::S_LITERAL_POINTERS,
589 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
592 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
593 MCSectionMachO::S_LITERAL_POINTERS,
594 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000596 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
597 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__category",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000602 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
603 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000605 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
606 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000611 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
612 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_meth_var_types")
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_names")
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_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000620 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
621 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000622
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000623 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000625 return ParseDirectiveSet();
626
Daniel Dunbara0d14262009-06-24 23:30:00 +0000627 // Data directives
628
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000630 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000632 return ParseDirectiveAscii(true);
633
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000635 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000636 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000637 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000639 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000641 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000642
643 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000645 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000647 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000649 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000651 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000653 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000655 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000657 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000659 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
660
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000662 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000663
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000665 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000667 return ParseDirectiveSpace();
668
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000669 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000670
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000672 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000674 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000676 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000678 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000680 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000682 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000684 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000686 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000688 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000690 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000692 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000694 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000695
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000697 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000699 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000701 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000703 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000705 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000706 if (IDVal == ".tbss")
707 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000708
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000710 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000712 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000714 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000716 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000718 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000719
Chris Lattnerebb89b42009-09-27 21:16:52 +0000720 // Look up the handler in the handler table,
721 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
722 if (Handler)
723 return (this->*Handler)(IDVal, IDLoc);
724
Kevin Enderby9c656452009-09-10 20:51:44 +0000725 // Target hook for parsing target specific directives.
726 if (!getTargetParser().ParseDirective(ID))
727 return false;
728
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000729 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000730 EatToEndOfStatement();
731 return false;
732 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000733
Chris Lattner98986712010-01-14 22:21:20 +0000734 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000735 bool HadError = getTargetParser().ParseInstruction(IDVal, IDLoc,
736 ParsedOperands);
737 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
738 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000739
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000740 // If parsing succeeded, match the instruction.
741 if (!HadError) {
742 MCInst Inst;
743 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
744 // Emit the instruction on success.
745 Out.EmitInstruction(Inst);
746 } else {
747 // Otherwise emit a diagnostic about the match failure and set the error
748 // flag.
749 //
750 // FIXME: We should give nicer diagnostics about the exact failure.
751 Error(IDLoc, "unrecognized instruction");
752 HadError = true;
753 }
754 }
Chris Lattner98986712010-01-14 22:21:20 +0000755
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000756 // If there was no error, consume the end-of-statement token. Otherwise this
757 // will be done by our caller.
758 if (!HadError)
759 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000760
761 // Free any parsed operands.
762 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
763 delete ParsedOperands[i];
764
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000765 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000766}
Chris Lattner9a023f72009-06-24 04:43:34 +0000767
Daniel Dunbare2ace502009-08-31 08:09:09 +0000768bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000769 // FIXME: Use better location, we should use proper tokens.
770 SMLoc EqualLoc = Lexer.getLoc();
771
Daniel Dunbar821e3332009-08-31 08:09:28 +0000772 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000773 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000774 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000775 return true;
776
Daniel Dunbar3f872332009-07-28 16:08:33 +0000777 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000778 return TokError("unexpected token in assignment");
779
780 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000781 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000782
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000783 // Validate that the LHS is allowed to be a variable (either it has not been
784 // used as a symbol, or it is an absolute symbol).
785 MCSymbol *Sym = getContext().LookupSymbol(Name);
786 if (Sym) {
787 // Diagnose assignment to a label.
788 //
789 // FIXME: Diagnostics. Note the location of the definition as a label.
790 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
791 if (!Sym->isUndefined() && !Sym->isAbsolute())
792 return Error(EqualLoc, "redefinition of '" + Name + "'");
793 else if (!Sym->isVariable())
794 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000795 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000796 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
797 Name + "'");
798 } else
799 Sym = CreateSymbol(Name);
800
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000801 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000802
803 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000804 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000805
806 return false;
807}
808
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000809/// ParseIdentifier:
810/// ::= identifier
811/// ::= string
812bool AsmParser::ParseIdentifier(StringRef &Res) {
813 if (Lexer.isNot(AsmToken::Identifier) &&
814 Lexer.isNot(AsmToken::String))
815 return true;
816
Sean Callanan18b83232010-01-19 21:44:56 +0000817 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000818
Sean Callanan79ed1a82010-01-19 20:22:31 +0000819 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000820
821 return false;
822}
823
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000824/// ParseDirectiveSet:
825/// ::= .set identifier ',' expression
826bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000827 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000828
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000829 if (ParseIdentifier(Name))
830 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000831
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000832 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000833 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000834 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000835
Daniel Dunbare2ace502009-08-31 08:09:09 +0000836 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000837}
838
Chris Lattner9a023f72009-06-24 04:43:34 +0000839/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000840/// ::= .section identifier (',' identifier)*
841/// FIXME: This should actually parse out the segment, section, attributes and
842/// sizeof_stub fields.
843bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000844 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000845
Daniel Dunbarace63122009-08-11 03:42:33 +0000846 StringRef SectionName;
847 if (ParseIdentifier(SectionName))
848 return Error(Loc, "expected identifier after '.section' directive");
849
850 // Verify there is a following comma.
851 if (!Lexer.is(AsmToken::Comma))
852 return TokError("unexpected token in '.section' directive");
853
Chris Lattnerff4bc462009-08-10 01:39:42 +0000854 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000855 SectionSpec += ",";
856
857 // Add all the tokens until the end of the line, ParseSectionSpecifier will
858 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000859 StringRef EOL = Lexer.LexUntilEndOfStatement();
860 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000861
Sean Callanan79ed1a82010-01-19 20:22:31 +0000862 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000863 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000864 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000865 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000866
Chris Lattnerff4bc462009-08-10 01:39:42 +0000867
868 StringRef Segment, Section;
869 unsigned TAA, StubSize;
870 std::string ErrorStr =
871 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
872 TAA, StubSize);
873
874 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000875 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000876
Chris Lattner56594f92009-07-31 17:47:16 +0000877 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000878 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000879 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
880 isText ? SectionKind::getText()
881 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000882 return false;
883}
884
Chris Lattnere15c2d72009-08-10 18:05:55 +0000885/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000886bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
887 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000888 unsigned TAA, unsigned Align,
889 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000890 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000891 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000892 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000893
Chris Lattner56594f92009-07-31 17:47:16 +0000894 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000895 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000896 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
897 isText ? SectionKind::getText()
898 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000899
900 // Set the implicit alignment, if any.
901 //
902 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
903 // alignment on the section (e.g., if one manually inserts bytes into the
904 // section, then just issueing the section switch directive will not realign
905 // the section. However, this is arguably more reasonable behavior, and there
906 // is no good reason for someone to intentionally emit incorrectly sized
907 // values into the implicitly aligned sections.
908 if (Align)
909 Out.EmitValueToAlignment(Align, 0, 1, 0);
910
Chris Lattner529fb542009-06-24 05:13:15 +0000911 return false;
912}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000913
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000914bool AsmParser::ParseEscapedString(std::string &Data) {
915 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
916
917 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000918 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000919 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
920 if (Str[i] != '\\') {
921 Data += Str[i];
922 continue;
923 }
924
925 // Recognize escaped characters. Note that this escape semantics currently
926 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
927 ++i;
928 if (i == e)
929 return TokError("unexpected backslash at end of string");
930
931 // Recognize octal sequences.
932 if ((unsigned) (Str[i] - '0') <= 7) {
933 // Consume up to three octal characters.
934 unsigned Value = Str[i] - '0';
935
936 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
937 ++i;
938 Value = Value * 8 + (Str[i] - '0');
939
940 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
941 ++i;
942 Value = Value * 8 + (Str[i] - '0');
943 }
944 }
945
946 if (Value > 255)
947 return TokError("invalid octal escape sequence (out of range)");
948
949 Data += (unsigned char) Value;
950 continue;
951 }
952
953 // Otherwise recognize individual escapes.
954 switch (Str[i]) {
955 default:
956 // Just reject invalid escape sequences for now.
957 return TokError("invalid escape sequence (unrecognized character)");
958
959 case 'b': Data += '\b'; break;
960 case 'f': Data += '\f'; break;
961 case 'n': Data += '\n'; break;
962 case 'r': Data += '\r'; break;
963 case 't': Data += '\t'; break;
964 case '"': Data += '"'; break;
965 case '\\': Data += '\\'; break;
966 }
967 }
968
969 return false;
970}
971
Daniel Dunbara0d14262009-06-24 23:30:00 +0000972/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000973/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000975 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000977 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000978 return TokError("expected string in '.ascii' or '.asciz' directive");
979
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000980 std::string Data;
981 if (ParseEscapedString(Data))
982 return true;
983
Chris Lattneraaec2052010-01-19 19:46:13 +0000984 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000985 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +0000986 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000987
Sean Callanan79ed1a82010-01-19 20:22:31 +0000988 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000989
Daniel Dunbar3f872332009-07-28 16:08:33 +0000990 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000991 break;
992
Daniel Dunbar3f872332009-07-28 16:08:33 +0000993 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000994 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000995 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996 }
997 }
998
Sean Callanan79ed1a82010-01-19 20:22:31 +0000999 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001000 return false;
1001}
1002
1003/// ParseDirectiveValue
1004/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1005bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001006 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001008 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001009 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001010 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001011 return true;
1012
Chris Lattneraaec2052010-01-19 19:46:13 +00001013 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001014
Daniel Dunbar3f872332009-07-28 16:08:33 +00001015 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001016 break;
1017
1018 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001019 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001021 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022 }
1023 }
1024
Sean Callanan79ed1a82010-01-19 20:22:31 +00001025 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 return false;
1027}
1028
1029/// ParseDirectiveSpace
1030/// ::= .space expression [ , expression ]
1031bool AsmParser::ParseDirectiveSpace() {
1032 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001033 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001034 return true;
1035
1036 int64_t FillExpr = 0;
1037 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001038 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1039 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001040 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001041 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001042
Daniel Dunbar475839e2009-06-29 20:37:27 +00001043 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001044 return true;
1045
1046 HasFillExpr = true;
1047
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 return TokError("unexpected token in '.space' directive");
1050 }
1051
Sean Callanan79ed1a82010-01-19 20:22:31 +00001052 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001053
1054 if (NumBytes <= 0)
1055 return TokError("invalid number of bytes in '.space' directive");
1056
1057 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001058 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059
1060 return false;
1061}
1062
1063/// ParseDirectiveFill
1064/// ::= .fill expression , expression , expression
1065bool AsmParser::ParseDirectiveFill() {
1066 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001067 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return true;
1069
Daniel Dunbar3f872332009-07-28 16:08:33 +00001070 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001072 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073
1074 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001075 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076 return true;
1077
Daniel Dunbar3f872332009-07-28 16:08:33 +00001078 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001080 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081
1082 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001083 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return true;
1085
Daniel Dunbar3f872332009-07-28 16:08:33 +00001086 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087 return TokError("unexpected token in '.fill' directive");
1088
Sean Callanan79ed1a82010-01-19 20:22:31 +00001089 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001090
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001091 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1092 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093
1094 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001095 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1096 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001097
1098 return false;
1099}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001100
1101/// ParseDirectiveOrg
1102/// ::= .org expression [ , expression ]
1103bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001104 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001105 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001106 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001107 return true;
1108
1109 // Parse optional fill expression.
1110 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001111 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1112 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001113 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001114 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001115
Daniel Dunbar475839e2009-06-29 20:37:27 +00001116 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001117 return true;
1118
Daniel Dunbar3f872332009-07-28 16:08:33 +00001119 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001120 return TokError("unexpected token in '.org' directive");
1121 }
1122
Sean Callanan79ed1a82010-01-19 20:22:31 +00001123 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001124
1125 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1126 // has to be relative to the current section.
1127 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001128
1129 return false;
1130}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131
1132/// ParseDirectiveAlign
1133/// ::= {.align, ...} expression [ , expression [ , expression ]]
1134bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001135 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001136 int64_t Alignment;
1137 if (ParseAbsoluteExpression(Alignment))
1138 return true;
1139
1140 SMLoc MaxBytesLoc;
1141 bool HasFillExpr = false;
1142 int64_t FillExpr = 0;
1143 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001144 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1145 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001146 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001147 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001148
1149 // The fill expression can be omitted while specifying a maximum number of
1150 // alignment bytes, e.g:
1151 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001152 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001153 HasFillExpr = true;
1154 if (ParseAbsoluteExpression(FillExpr))
1155 return true;
1156 }
1157
Daniel Dunbar3f872332009-07-28 16:08:33 +00001158 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1159 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001160 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001161 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001162
1163 MaxBytesLoc = Lexer.getLoc();
1164 if (ParseAbsoluteExpression(MaxBytesToFill))
1165 return true;
1166
Daniel Dunbar3f872332009-07-28 16:08:33 +00001167 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001168 return TokError("unexpected token in directive");
1169 }
1170 }
1171
Sean Callanan79ed1a82010-01-19 20:22:31 +00001172 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001173
1174 if (!HasFillExpr) {
1175 // FIXME: Sometimes fill with nop.
1176 FillExpr = 0;
1177 }
1178
1179 // Compute alignment in bytes.
1180 if (IsPow2) {
1181 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001182 if (Alignment >= 32) {
1183 Error(AlignmentLoc, "invalid alignment value");
1184 Alignment = 31;
1185 }
1186
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001187 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001188 }
1189
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001190 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001191 if (MaxBytesLoc.isValid()) {
1192 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001193 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1194 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001195 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001196 }
1197
1198 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001199 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1200 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001201 MaxBytesToFill = 0;
1202 }
1203 }
1204
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001205 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1206 // the TextAlignFillValue.
1207 if(Out.getCurrentSection()->getKind().isText() &&
1208 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1209 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1210 else
1211 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1212 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001213
1214 return false;
1215}
1216
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001217/// ParseDirectiveSymbolAttribute
1218/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001219bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001221 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001222 StringRef Name;
1223
1224 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001225 return TokError("expected identifier in directive");
1226
Daniel Dunbar959fd882009-08-26 22:13:22 +00001227 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001228
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001229 Out.EmitSymbolAttribute(Sym, Attr);
1230
Daniel Dunbar3f872332009-07-28 16:08:33 +00001231 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001232 break;
1233
Daniel Dunbar3f872332009-07-28 16:08:33 +00001234 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001235 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001236 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001237 }
1238 }
1239
Sean Callanan79ed1a82010-01-19 20:22:31 +00001240 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001241 return false;
1242}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001243
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001244/// ParseDirectiveDarwinSymbolDesc
1245/// ::= .desc identifier , expression
1246bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001247 StringRef Name;
1248 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001249 return TokError("expected identifier in directive");
1250
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001251 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001252 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001253
Daniel Dunbar3f872332009-07-28 16:08:33 +00001254 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001255 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001256 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001257
1258 SMLoc DescLoc = Lexer.getLoc();
1259 int64_t DescValue;
1260 if (ParseAbsoluteExpression(DescValue))
1261 return true;
1262
Daniel Dunbar3f872332009-07-28 16:08:33 +00001263 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001264 return TokError("unexpected token in '.desc' directive");
1265
Sean Callanan79ed1a82010-01-19 20:22:31 +00001266 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001267
1268 // Set the n_desc field of this Symbol to this DescValue
1269 Out.EmitSymbolDesc(Sym, DescValue);
1270
1271 return false;
1272}
1273
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001274/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001275/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1276bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001277 SMLoc IDLoc = Lexer.getLoc();
1278 StringRef Name;
1279 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001280 return TokError("expected identifier in directive");
1281
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001282 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001283 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001284
Daniel Dunbar3f872332009-07-28 16:08:33 +00001285 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001286 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001287 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001288
1289 int64_t Size;
1290 SMLoc SizeLoc = Lexer.getLoc();
1291 if (ParseAbsoluteExpression(Size))
1292 return true;
1293
1294 int64_t Pow2Alignment = 0;
1295 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001296 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001297 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001298 Pow2AlignmentLoc = Lexer.getLoc();
1299 if (ParseAbsoluteExpression(Pow2Alignment))
1300 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001301
1302 // If this target takes alignments in bytes (not log) validate and convert.
1303 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1304 if (!isPowerOf2_64(Pow2Alignment))
1305 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1306 Pow2Alignment = Log2_64(Pow2Alignment);
1307 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001308 }
1309
Daniel Dunbar3f872332009-07-28 16:08:33 +00001310 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001311 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001312
Sean Callanan79ed1a82010-01-19 20:22:31 +00001313 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001314
Chris Lattner1fc3d752009-07-09 17:25:12 +00001315 // NOTE: a size of zero for a .comm should create a undefined symbol
1316 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001317 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001318 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1319 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001320
Eric Christopherc260a3e2010-05-14 01:38:54 +00001321 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001322 // may internally end up wanting an alignment in bytes.
1323 // FIXME: Diagnose overflow.
1324 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001325 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1326 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001327
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001328 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001329 return Error(IDLoc, "invalid symbol redefinition");
1330
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001331 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001332 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001333 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001334 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1335 MCSectionMachO::S_ZEROFILL, 0,
1336 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001337 Sym, Size, 1 << Pow2Alignment);
1338 return false;
1339 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001340
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001341 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001342 return false;
1343}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001344
1345/// ParseDirectiveDarwinZerofill
1346/// ::= .zerofill segname , sectname [, identifier , size_expression [
1347/// , align_expression ]]
1348bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001349 StringRef Segment;
1350 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001351 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001352
Daniel Dunbar3f872332009-07-28 16:08:33 +00001353 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001354 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001355 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001356
1357 StringRef Section;
1358 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001359 return TokError("expected section name after comma in '.zerofill' "
1360 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001361
Chris Lattner9be3fee2009-07-10 22:20:30 +00001362 // If this is the end of the line all that was wanted was to create the
1363 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001364 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001365 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001366 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1367 MCSectionMachO::S_ZEROFILL, 0,
1368 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001369 return false;
1370 }
1371
Daniel Dunbar3f872332009-07-28 16:08:33 +00001372 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001373 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001374 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001375
Chris Lattner7bb7c552010-05-13 00:10:34 +00001376 SMLoc IDLoc = Lexer.getLoc();
1377 StringRef IDStr;
1378 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001379 return TokError("expected identifier in directive");
1380
1381 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001382 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001383
Daniel Dunbar3f872332009-07-28 16:08:33 +00001384 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001385 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001386 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001387
1388 int64_t Size;
1389 SMLoc SizeLoc = Lexer.getLoc();
1390 if (ParseAbsoluteExpression(Size))
1391 return true;
1392
1393 int64_t Pow2Alignment = 0;
1394 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001395 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001396 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001397 Pow2AlignmentLoc = Lexer.getLoc();
1398 if (ParseAbsoluteExpression(Pow2Alignment))
1399 return true;
1400 }
1401
Daniel Dunbar3f872332009-07-28 16:08:33 +00001402 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001403 return TokError("unexpected token in '.zerofill' directive");
1404
Sean Callanan79ed1a82010-01-19 20:22:31 +00001405 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001406
1407 if (Size < 0)
1408 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1409 "than zero");
1410
Eric Christopherc260a3e2010-05-14 01:38:54 +00001411 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001412 // may internally end up wanting an alignment in bytes.
1413 // FIXME: Diagnose overflow.
1414 if (Pow2Alignment < 0)
1415 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1416 "can't be less than zero");
1417
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001418 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001419 return Error(IDLoc, "invalid symbol redefinition");
1420
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001421 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001422 //
1423 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001424 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1425 MCSectionMachO::S_ZEROFILL, 0,
1426 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001427 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001428
1429 return false;
1430}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001431
Eric Christopher482eba02010-05-14 01:50:28 +00001432/// ParseDirectiveDarwinTBSS
1433/// ::= .tbss identifier, size, align
1434bool AsmParser::ParseDirectiveDarwinTBSS() {
1435 SMLoc IDLoc = Lexer.getLoc();
1436 StringRef Name;
1437 if (ParseIdentifier(Name))
1438 return TokError("expected identifier in directive");
1439
1440 // Demangle the name output. The trailing characters are guaranteed to be
1441 // $tlv$init so just strip that off.
1442 StringRef DemName = Name.substr(0, Name.size() - strlen("$tlv$init"));
1443
1444 // Handle the identifier as the key symbol.
1445 MCSymbol *Sym = CreateSymbol(DemName);
1446
1447 if (Lexer.isNot(AsmToken::Comma))
1448 return TokError("unexpected token in directive");
1449 Lex();
1450
1451 int64_t Size;
1452 SMLoc SizeLoc = Lexer.getLoc();
1453 if (ParseAbsoluteExpression(Size))
1454 return true;
1455
1456 int64_t Pow2Alignment = 0;
1457 SMLoc Pow2AlignmentLoc;
1458 if (Lexer.is(AsmToken::Comma)) {
1459 Lex();
1460 Pow2AlignmentLoc = Lexer.getLoc();
1461 if (ParseAbsoluteExpression(Pow2Alignment))
1462 return true;
1463 }
1464
1465 if (Lexer.isNot(AsmToken::EndOfStatement))
1466 return TokError("unexpected token in '.tbss' directive");
1467
1468 Lex();
1469
1470 if (Size < 0)
1471 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1472 "zero");
1473
1474 // FIXME: Diagnose overflow.
1475 if (Pow2Alignment < 0)
1476 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1477 "than zero");
1478
1479 if (!Sym->isUndefined())
1480 return Error(IDLoc, "invalid symbol redefinition");
1481
1482 Out.EmitTBSSSymbol(Sym, Size, Pow2Alignment ? 1 << Pow2Alignment : 0);
1483
1484 return false;
1485}
1486
Kevin Enderbya5c78322009-07-13 21:03:15 +00001487/// ParseDirectiveDarwinSubsectionsViaSymbols
1488/// ::= .subsections_via_symbols
1489bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001490 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001491 return TokError("unexpected token in '.subsections_via_symbols' directive");
1492
Sean Callanan79ed1a82010-01-19 20:22:31 +00001493 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001494
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001495 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001496
1497 return false;
1498}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001499
1500/// ParseDirectiveAbort
1501/// ::= .abort [ "abort_string" ]
1502bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001503 // FIXME: Use loc from directive.
1504 SMLoc Loc = Lexer.getLoc();
1505
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001506 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001507 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1508 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001509 return TokError("expected string in '.abort' directive");
1510
Sean Callanan18b83232010-01-19 21:44:56 +00001511 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001512
Sean Callanan79ed1a82010-01-19 20:22:31 +00001513 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001514 }
1515
Daniel Dunbar3f872332009-07-28 16:08:33 +00001516 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001517 return TokError("unexpected token in '.abort' directive");
1518
Sean Callanan79ed1a82010-01-19 20:22:31 +00001519 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001520
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001521 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001522 if (Str.empty())
1523 Error(Loc, ".abort detected. Assembly stopping.");
1524 else
1525 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001526
1527 return false;
1528}
Kevin Enderby71148242009-07-14 21:35:03 +00001529
1530/// ParseDirectiveLsym
1531/// ::= .lsym identifier , expression
1532bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001533 StringRef Name;
1534 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001535 return TokError("expected identifier in directive");
1536
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001537 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001538 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001539
Daniel Dunbar3f872332009-07-28 16:08:33 +00001540 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001541 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001542 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001543
Daniel Dunbar821e3332009-08-31 08:09:28 +00001544 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001545 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001546 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001547 return true;
1548
Daniel Dunbar3f872332009-07-28 16:08:33 +00001549 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001550 return TokError("unexpected token in '.lsym' directive");
1551
Sean Callanan79ed1a82010-01-19 20:22:31 +00001552 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001553
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001554 // We don't currently support this directive.
1555 //
1556 // FIXME: Diagnostic location!
1557 (void) Sym;
1558 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001559}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001560
1561/// ParseDirectiveInclude
1562/// ::= .include "filename"
1563bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001564 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001565 return TokError("expected string in '.include' directive");
1566
Sean Callanan18b83232010-01-19 21:44:56 +00001567 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001568 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001569 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001570
Daniel Dunbar3f872332009-07-28 16:08:33 +00001571 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001572 return TokError("unexpected token in '.include' directive");
1573
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001574 // Strip the quotes.
1575 Filename = Filename.substr(1, Filename.size()-2);
1576
1577 // Attempt to switch the lexer to the included file before consuming the end
1578 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001579 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001580 PrintMessage(IncludeLoc,
1581 "Could not find include file '" + Filename + "'",
1582 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001583 return true;
1584 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001585
1586 return false;
1587}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001588
1589/// ParseDirectiveDarwinDumpOrLoad
1590/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001591bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001592 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001593 return TokError("expected string in '.dump' or '.load' directive");
1594
Sean Callanan79ed1a82010-01-19 20:22:31 +00001595 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001596
Daniel Dunbar3f872332009-07-28 16:08:33 +00001597 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001598 return TokError("unexpected token in '.dump' or '.load' directive");
1599
Sean Callanan79ed1a82010-01-19 20:22:31 +00001600 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001601
Kevin Enderby5026ae42009-07-20 20:25:37 +00001602 // FIXME: If/when .dump and .load are implemented they will be done in the
1603 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001604 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001605 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001606 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001607 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001608
1609 return false;
1610}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001611
1612/// ParseDirectiveIf
1613/// ::= .if expression
1614bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001615 TheCondStack.push_back(TheCondState);
1616 TheCondState.TheCond = AsmCond::IfCond;
1617 if(TheCondState.Ignore) {
1618 EatToEndOfStatement();
1619 }
1620 else {
1621 int64_t ExprValue;
1622 if (ParseAbsoluteExpression(ExprValue))
1623 return true;
1624
1625 if (Lexer.isNot(AsmToken::EndOfStatement))
1626 return TokError("unexpected token in '.if' directive");
1627
Sean Callanan79ed1a82010-01-19 20:22:31 +00001628 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001629
1630 TheCondState.CondMet = ExprValue;
1631 TheCondState.Ignore = !TheCondState.CondMet;
1632 }
1633
1634 return false;
1635}
1636
1637/// ParseDirectiveElseIf
1638/// ::= .elseif expression
1639bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1640 if (TheCondState.TheCond != AsmCond::IfCond &&
1641 TheCondState.TheCond != AsmCond::ElseIfCond)
1642 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1643 " an .elseif");
1644 TheCondState.TheCond = AsmCond::ElseIfCond;
1645
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001646 bool LastIgnoreState = false;
1647 if (!TheCondStack.empty())
1648 LastIgnoreState = TheCondStack.back().Ignore;
1649 if (LastIgnoreState || TheCondState.CondMet) {
1650 TheCondState.Ignore = true;
1651 EatToEndOfStatement();
1652 }
1653 else {
1654 int64_t ExprValue;
1655 if (ParseAbsoluteExpression(ExprValue))
1656 return true;
1657
1658 if (Lexer.isNot(AsmToken::EndOfStatement))
1659 return TokError("unexpected token in '.elseif' directive");
1660
Sean Callanan79ed1a82010-01-19 20:22:31 +00001661 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001662 TheCondState.CondMet = ExprValue;
1663 TheCondState.Ignore = !TheCondState.CondMet;
1664 }
1665
1666 return false;
1667}
1668
1669/// ParseDirectiveElse
1670/// ::= .else
1671bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001672 if (Lexer.isNot(AsmToken::EndOfStatement))
1673 return TokError("unexpected token in '.else' directive");
1674
Sean Callanan79ed1a82010-01-19 20:22:31 +00001675 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001676
1677 if (TheCondState.TheCond != AsmCond::IfCond &&
1678 TheCondState.TheCond != AsmCond::ElseIfCond)
1679 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1680 ".elseif");
1681 TheCondState.TheCond = AsmCond::ElseCond;
1682 bool LastIgnoreState = false;
1683 if (!TheCondStack.empty())
1684 LastIgnoreState = TheCondStack.back().Ignore;
1685 if (LastIgnoreState || TheCondState.CondMet)
1686 TheCondState.Ignore = true;
1687 else
1688 TheCondState.Ignore = false;
1689
1690 return false;
1691}
1692
1693/// ParseDirectiveEndIf
1694/// ::= .endif
1695bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001696 if (Lexer.isNot(AsmToken::EndOfStatement))
1697 return TokError("unexpected token in '.endif' directive");
1698
Sean Callanan79ed1a82010-01-19 20:22:31 +00001699 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001700
1701 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1702 TheCondStack.empty())
1703 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1704 ".else");
1705 if (!TheCondStack.empty()) {
1706 TheCondState = TheCondStack.back();
1707 TheCondStack.pop_back();
1708 }
1709
1710 return false;
1711}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001712
1713/// ParseDirectiveFile
1714/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001715bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001716 // FIXME: I'm not sure what this is.
1717 int64_t FileNumber = -1;
1718 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001719 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001720 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001721
1722 if (FileNumber < 1)
1723 return TokError("file number less than one");
1724 }
1725
1726 if (Lexer.isNot(AsmToken::String))
1727 return TokError("unexpected token in '.file' directive");
1728
Chris Lattnerd32e8032010-01-25 19:02:58 +00001729 StringRef Filename = getTok().getString();
1730 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001731 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001732
1733 if (Lexer.isNot(AsmToken::EndOfStatement))
1734 return TokError("unexpected token in '.file' directive");
1735
Chris Lattnerd32e8032010-01-25 19:02:58 +00001736 if (FileNumber == -1)
1737 Out.EmitFileDirective(Filename);
1738 else
1739 Out.EmitDwarfFileDirective(FileNumber, Filename);
1740
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001741 return false;
1742}
1743
1744/// ParseDirectiveLine
1745/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001746bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001747 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1748 if (Lexer.isNot(AsmToken::Integer))
1749 return TokError("unexpected token in '.line' directive");
1750
Sean Callanan18b83232010-01-19 21:44:56 +00001751 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001752 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001753 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001754
1755 // FIXME: Do something with the .line.
1756 }
1757
1758 if (Lexer.isNot(AsmToken::EndOfStatement))
1759 return TokError("unexpected token in '.file' directive");
1760
1761 return false;
1762}
1763
1764
1765/// ParseDirectiveLoc
1766/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001767bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001768 if (Lexer.isNot(AsmToken::Integer))
1769 return TokError("unexpected token in '.loc' directive");
1770
1771 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001772 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001773 (void) FileNumber;
1774 // FIXME: Validate file.
1775
Sean Callanan79ed1a82010-01-19 20:22:31 +00001776 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001777 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1778 if (Lexer.isNot(AsmToken::Integer))
1779 return TokError("unexpected token in '.loc' directive");
1780
Sean Callanan18b83232010-01-19 21:44:56 +00001781 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001782 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001783 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001784
1785 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1786 if (Lexer.isNot(AsmToken::Integer))
1787 return TokError("unexpected token in '.loc' directive");
1788
Sean Callanan18b83232010-01-19 21:44:56 +00001789 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001790 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001791 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001792
1793 // FIXME: Do something with the .loc.
1794 }
1795 }
1796
1797 if (Lexer.isNot(AsmToken::EndOfStatement))
1798 return TokError("unexpected token in '.file' directive");
1799
1800 return false;
1801}
1802