blob: 2a60a67e1ba4a3fde70b2f9bab2496cc6a5567ba [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000016#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000018#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000019#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000024#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000025#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000027#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000028using namespace llvm;
29
Chris Lattneraaec2052010-01-19 19:46:13 +000030
31enum { DEFAULT_ADDRSPACE = 0 };
32
Chris Lattnerebb89b42009-09-27 21:16:52 +000033AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
34 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000035 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
Chris Lattnerf0559e42010-04-08 20:30:37 +000036 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000037 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
38
Chris Lattnerebb89b42009-09-27 21:16:52 +000039 // Debugging directives.
40 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
41 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
42 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
43}
44
45
46
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000047AsmParser::~AsmParser() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000048}
49
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000050void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000051 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000052}
53
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000054bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000055 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000056 return true;
57}
58
59bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000060 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000061 return true;
62}
63
Sean Callananbf2013e2010-01-20 23:19:55 +000064void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
65 const char *Type) const {
66 SrcMgr.PrintMessage(Loc, Msg, Type);
67}
Sean Callananfd0b0282010-01-21 00:19:58 +000068
69bool AsmParser::EnterIncludeFile(const std::string &Filename) {
70 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
71 if (NewBuf == -1)
72 return true;
Sean Callanan79036e42010-01-20 22:18:24 +000073
Sean Callananfd0b0282010-01-21 00:19:58 +000074 CurBuffer = NewBuf;
75
76 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
77
78 return false;
79}
80
81const AsmToken &AsmParser::Lex() {
82 const AsmToken *tok = &Lexer.Lex();
83
84 if (tok->is(AsmToken::Eof)) {
85 // If this is the end of an included file, pop the parent file off the
86 // include stack.
87 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
88 if (ParentIncludeLoc != SMLoc()) {
89 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
90 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
91 ParentIncludeLoc.getPointer());
92 tok = &Lexer.Lex();
93 }
94 }
95
96 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +000097 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +000098
Sean Callananfd0b0282010-01-21 00:19:58 +000099 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000100}
101
Chris Lattner79180e22010-04-05 23:15:42 +0000102bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000103 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000104 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000105 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000106 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000107 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
109 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000110
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000111 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000112 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000113
Chris Lattnerb717fb02009-07-02 21:53:43 +0000114 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000115
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000116 AsmCond StartingCondState = TheCondState;
117
Chris Lattnerb717fb02009-07-02 21:53:43 +0000118 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000119 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000120 if (!ParseStatement()) continue;
121
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000122 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000123 HadError = true;
124 EatToEndOfStatement();
125 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000126
127 if (TheCondState.TheCond != StartingCondState.TheCond ||
128 TheCondState.Ignore != StartingCondState.Ignore)
129 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000130
Chris Lattner79180e22010-04-05 23:15:42 +0000131 // Finalize the output stream if there are no errors and if the client wants
132 // us to.
133 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000134 Out.Finish();
135
Chris Lattnerb717fb02009-07-02 21:53:43 +0000136 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000137}
138
Chris Lattner2cf5f142009-06-22 01:29:09 +0000139/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
140void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000141 while (Lexer.isNot(AsmToken::EndOfStatement) &&
142 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000143 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000144
145 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000146 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000147 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000148}
149
Chris Lattnerc4193832009-06-22 05:51:26 +0000150
Chris Lattner74ec1a32009-06-22 06:32:03 +0000151/// ParseParenExpr - Parse a paren expression and return it.
152/// NOTE: This assumes the leading '(' has already been consumed.
153///
154/// parenexpr ::= expr)
155///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000156bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000157 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000158 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000159 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000160 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000161 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000162 return false;
163}
Chris Lattnerc4193832009-06-22 05:51:26 +0000164
Daniel Dunbar959fd882009-08-26 22:13:22 +0000165MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000166 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000167 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000168}
169
Chris Lattner74ec1a32009-06-22 06:32:03 +0000170/// ParsePrimaryExpr - Parse a primary expression and return it.
171/// primaryexpr ::= (parenexpr
172/// primaryexpr ::= symbol
173/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000174/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000175/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000176bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000177 switch (Lexer.getKind()) {
178 default:
179 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000180 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000181 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000182 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000183 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000184 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000185 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000186 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000187 case AsmToken::Identifier: {
188 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000189 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
190 MCSymbol *Sym = CreateSymbol(Split.first);
191
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000192 // Mark the symbol as used in an expression.
193 Sym->setUsedInExpr(true);
194
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000195 // Lookup the symbol variant if used.
196 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
197 if (Split.first.size() != getTok().getIdentifier().size())
198 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
199
Chris Lattnerb4307b32010-01-15 19:28:38 +0000200 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000201 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000202
203 // If this is an absolute variable reference, substitute it now to preserve
204 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000205 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000206 if (Variant)
207 return Error(EndLoc, "unexpected modified on variable reference");
208
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000209 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000210 return false;
211 }
212
213 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000214 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000215 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000216 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000218 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000219 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000220 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000221 return false;
Chris Lattnerd3050352010-04-14 04:40:28 +0000222 case AsmToken::Dot: {
223 // This is a '.' reference, which references the current PC. Emit a
224 // temporary label to the streamer and refer to it.
225 MCSymbol *Sym = Ctx.CreateTempSymbol();
226 Out.EmitLabel(Sym);
227 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
228 EndLoc = Lexer.getLoc();
229 Lex(); // Eat identifier.
230 return false;
231 }
232
Daniel Dunbar3f872332009-07-28 16:08:33 +0000233 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000234 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000235 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000236 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000237 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000238 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000239 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000240 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000241 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000242 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000243 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000244 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000245 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000246 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000247 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000248 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000249 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000250 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000251 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000252 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000254 }
255}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000256
Chris Lattnerb4307b32010-01-15 19:28:38 +0000257bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000258 SMLoc EndLoc;
259 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000260}
261
Chris Lattner74ec1a32009-06-22 06:32:03 +0000262/// ParseExpression - Parse an expression and return it.
263///
264/// expr ::= expr +,- expr -> lowest.
265/// expr ::= expr |,^,&,! expr -> middle.
266/// expr ::= expr *,/,%,<<,>> expr -> highest.
267/// expr ::= primaryexpr
268///
Chris Lattner54482b42010-01-15 19:39:23 +0000269bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000270 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000272 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
273 return true;
274
275 // Try to constant fold it up front, if possible.
276 int64_t Value;
277 if (Res->EvaluateAsAbsolute(Value))
278 Res = MCConstantExpr::Create(Value, getContext());
279
280 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000281}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000282
Chris Lattnerb4307b32010-01-15 19:28:38 +0000283bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000284 Res = 0;
285 return ParseParenExpr(Res, EndLoc) ||
286 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000287}
288
Daniel Dunbar475839e2009-06-29 20:37:27 +0000289bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000290 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000291
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000292 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 if (ParseExpression(Expr))
294 return true;
295
Daniel Dunbare00b0112009-10-16 01:57:52 +0000296 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000297 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298
299 return false;
300}
301
Daniel Dunbar3f872332009-07-28 16:08:33 +0000302static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000303 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000304 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000305 default:
306 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307
308 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000309 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000310 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000311 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000312 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000313 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000314 return 1;
315
316 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000317 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000318 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000319 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000321 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000322 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000324 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::ExclaimEqual:
327 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000328 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000331 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000332 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000333 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000334 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
342
343 // Intermediate Precedence: |, &, ^
344 //
345 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000348 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000353 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000354 return 3;
355
356 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000361 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000362 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000372 }
373}
374
375
376/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
377/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000378bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
379 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000380 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000383
384 // If the next token is lower precedence than we are allowed to eat, return
385 // successfully with what we ate already.
386 if (TokPrec < Precedence)
387 return false;
388
Sean Callanan79ed1a82010-01-19 20:22:31 +0000389 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000390
391 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000392 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000393 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000394
395 // If BinOp binds less tightly with RHS than the operator after RHS, let
396 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000397 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000398 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000399 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000400 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401 }
402
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000404 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000405 }
406}
407
Chris Lattnerc4193832009-06-22 05:51:26 +0000408
409
410
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000411/// ParseStatement:
412/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000413/// ::= Label* Directive ...Operands... EndOfStatement
414/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000415bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000416 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000417 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000418 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000419 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000420
421 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000422 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000423 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000424 StringRef IDVal;
Chris Lattner7834fac2010-04-17 18:14:27 +0000425 if (ParseIdentifier(IDVal)) {
426 if (!TheCondState.Ignore)
427 return TokError("unexpected token at start of statement");
428 IDVal = "";
429 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000430
Chris Lattner7834fac2010-04-17 18:14:27 +0000431 // Handle conditional assembly here before checking for skipping. We
432 // have to do this so that .endif isn't skipped in a ".if 0" block for
433 // example.
434 if (IDVal == ".if")
435 return ParseDirectiveIf(IDLoc);
436 if (IDVal == ".elseif")
437 return ParseDirectiveElseIf(IDLoc);
438 if (IDVal == ".else")
439 return ParseDirectiveElse(IDLoc);
440 if (IDVal == ".endif")
441 return ParseDirectiveEndIf(IDLoc);
442
443 // If we are in a ".if 0" block, ignore this statement.
444 if (TheCondState.Ignore) {
445 EatToEndOfStatement();
446 return false;
447 }
448
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000449 // FIXME: Recurse on local labels?
450
451 // See what kind of statement we have.
452 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000454 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000455 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000456
457 // Diagnose attempt to use a variable as a label.
458 //
459 // FIXME: Diagnostics. Note the location of the definition as a label.
460 // FIXME: This doesn't diagnose assignment to a symbol which has been
461 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000462 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000463 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000464 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000465
Daniel Dunbar959fd882009-08-26 22:13:22 +0000466 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000467 Out.EmitLabel(Sym);
468
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000469 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000470 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000471
Daniel Dunbar3f872332009-07-28 16:08:33 +0000472 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000473 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000474 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000475
Daniel Dunbare2ace502009-08-31 08:09:09 +0000476 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000477
478 default: // Normal instruction or directive.
479 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000480 }
481
482 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000483 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000484 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000485 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000486 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000488 // FIXME: This changes behavior based on the -static flag to the
489 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000490 return ParseDirectiveSectionSwitch("__TEXT", "__text",
491 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000495 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000496 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000497 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
498 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000500 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000501 MCSectionMachO::S_4BYTE_LITERALS,
502 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000504 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000505 MCSectionMachO::S_8BYTE_LITERALS,
506 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000508 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000509 MCSectionMachO::S_16BYTE_LITERALS,
510 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000512 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000514 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000516 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000518 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
519
520 // FIXME: The assembler manual claims that this has the self modify code
521 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000522 if (IDVal == ".symbol_stub")
523 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
524 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000525 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
526 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000527 0, 16);
528 // FIXME: PowerPC only?
529 if (IDVal == ".picsymbol_stub")
530 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
531 MCSectionMachO::S_SYMBOL_STUBS |
532 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
533 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
538
539 // FIXME: The section names of these two are misspelled in the assembler
540 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
543 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
544 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
547 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
548 4);
549
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000553 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000554 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
555 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000557 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
559 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000562
563
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__class",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
590 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
591 MCSectionMachO::S_LITERAL_POINTERS,
592 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
595 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
596 MCSectionMachO::S_LITERAL_POINTERS,
597 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000602 return ParseDirectiveSectionSwitch("__OBJC", "__category",
603 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000605 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
606 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
612 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000614 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
615 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000617 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
618 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000620 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
621 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000623 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
624 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000625
Eric Christopherc6177a42010-05-17 22:53:55 +0000626 if (IDVal == ".tdata")
627 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
628 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
629 if (IDVal == ".tlv")
630 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
631 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
632 if (IDVal == ".thread_init_func")
633 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
634 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
635
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000636 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000638 return ParseDirectiveSet();
639
Daniel Dunbara0d14262009-06-24 23:30:00 +0000640 // Data directives
641
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000643 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000645 return ParseDirectiveAscii(true);
646
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000648 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000649 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000650 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000652 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000654 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000655
656 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000658 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000660 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000662 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000664 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000666 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000668 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000670 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000672 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
673
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000675 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000676
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000678 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000680 return ParseDirectiveSpace();
681
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000682 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000683
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000685 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000687 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000689 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000691 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000693 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000695 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000697 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000699 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000701 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000703 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000705 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000707 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000708
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000710 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000712 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000714 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000716 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000718 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000719 if (IDVal == ".tbss")
720 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000721
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000723 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000725 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000727 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000729 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000731 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000732
Chris Lattnerebb89b42009-09-27 21:16:52 +0000733 // Look up the handler in the handler table,
734 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
735 if (Handler)
736 return (this->*Handler)(IDVal, IDLoc);
737
Kevin Enderby9c656452009-09-10 20:51:44 +0000738 // Target hook for parsing target specific directives.
739 if (!getTargetParser().ParseDirective(ID))
740 return false;
741
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000742 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000743 EatToEndOfStatement();
744 return false;
745 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000746
Chris Lattner98986712010-01-14 22:21:20 +0000747 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000748 bool HadError = getTargetParser().ParseInstruction(IDVal, IDLoc,
749 ParsedOperands);
750 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
751 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000752
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000753 // If parsing succeeded, match the instruction.
754 if (!HadError) {
755 MCInst Inst;
756 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
757 // Emit the instruction on success.
758 Out.EmitInstruction(Inst);
759 } else {
760 // Otherwise emit a diagnostic about the match failure and set the error
761 // flag.
762 //
763 // FIXME: We should give nicer diagnostics about the exact failure.
764 Error(IDLoc, "unrecognized instruction");
765 HadError = true;
766 }
767 }
Chris Lattner98986712010-01-14 22:21:20 +0000768
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000769 // If there was no error, consume the end-of-statement token. Otherwise this
770 // will be done by our caller.
771 if (!HadError)
772 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000773
774 // Free any parsed operands.
775 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
776 delete ParsedOperands[i];
777
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000778 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000779}
Chris Lattner9a023f72009-06-24 04:43:34 +0000780
Daniel Dunbare2ace502009-08-31 08:09:09 +0000781bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000782 // FIXME: Use better location, we should use proper tokens.
783 SMLoc EqualLoc = Lexer.getLoc();
784
Daniel Dunbar821e3332009-08-31 08:09:28 +0000785 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000786 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000787 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000788 return true;
789
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000791 return TokError("unexpected token in assignment");
792
793 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000794 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000795
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000796 // Validate that the LHS is allowed to be a variable (either it has not been
797 // used as a symbol, or it is an absolute symbol).
798 MCSymbol *Sym = getContext().LookupSymbol(Name);
799 if (Sym) {
800 // Diagnose assignment to a label.
801 //
802 // FIXME: Diagnostics. Note the location of the definition as a label.
803 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000804 if (Sym->isUndefined() && !Sym->isUsedInExpr())
805 ; // Allow redefinitions of undefined symbols only used in directives.
806 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000807 return Error(EqualLoc, "redefinition of '" + Name + "'");
808 else if (!Sym->isVariable())
809 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000810 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000811 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
812 Name + "'");
813 } else
814 Sym = CreateSymbol(Name);
815
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000816 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000817
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000818 Sym->setUsedInExpr(true);
819
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000820 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000821 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000822
823 return false;
824}
825
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000826/// ParseIdentifier:
827/// ::= identifier
828/// ::= string
829bool AsmParser::ParseIdentifier(StringRef &Res) {
830 if (Lexer.isNot(AsmToken::Identifier) &&
831 Lexer.isNot(AsmToken::String))
832 return true;
833
Sean Callanan18b83232010-01-19 21:44:56 +0000834 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000835
Sean Callanan79ed1a82010-01-19 20:22:31 +0000836 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000837
838 return false;
839}
840
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000841/// ParseDirectiveSet:
842/// ::= .set identifier ',' expression
843bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000844 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000845
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000846 if (ParseIdentifier(Name))
847 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000848
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000849 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000850 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000851 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000852
Daniel Dunbare2ace502009-08-31 08:09:09 +0000853 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000854}
855
Chris Lattner9a023f72009-06-24 04:43:34 +0000856/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000857/// ::= .section identifier (',' identifier)*
858/// FIXME: This should actually parse out the segment, section, attributes and
859/// sizeof_stub fields.
860bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000861 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000862
Daniel Dunbarace63122009-08-11 03:42:33 +0000863 StringRef SectionName;
864 if (ParseIdentifier(SectionName))
865 return Error(Loc, "expected identifier after '.section' directive");
866
867 // Verify there is a following comma.
868 if (!Lexer.is(AsmToken::Comma))
869 return TokError("unexpected token in '.section' directive");
870
Chris Lattnerff4bc462009-08-10 01:39:42 +0000871 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000872 SectionSpec += ",";
873
874 // Add all the tokens until the end of the line, ParseSectionSpecifier will
875 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000876 StringRef EOL = Lexer.LexUntilEndOfStatement();
877 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000878
Sean Callanan79ed1a82010-01-19 20:22:31 +0000879 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000880 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000881 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000882 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000883
Chris Lattnerff4bc462009-08-10 01:39:42 +0000884
885 StringRef Segment, Section;
886 unsigned TAA, StubSize;
887 std::string ErrorStr =
888 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
889 TAA, StubSize);
890
891 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000892 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000893
Chris Lattner56594f92009-07-31 17:47:16 +0000894 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000895 bool isText = 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()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000899 return false;
900}
901
Chris Lattnere15c2d72009-08-10 18:05:55 +0000902/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000903bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
904 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000905 unsigned TAA, unsigned Align,
906 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000908 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000909 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000910
Chris Lattner56594f92009-07-31 17:47:16 +0000911 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000912 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000913 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
914 isText ? SectionKind::getText()
915 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000916
917 // Set the implicit alignment, if any.
918 //
919 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
920 // alignment on the section (e.g., if one manually inserts bytes into the
921 // section, then just issueing the section switch directive will not realign
922 // the section. However, this is arguably more reasonable behavior, and there
923 // is no good reason for someone to intentionally emit incorrectly sized
924 // values into the implicitly aligned sections.
925 if (Align)
926 Out.EmitValueToAlignment(Align, 0, 1, 0);
927
Chris Lattner529fb542009-06-24 05:13:15 +0000928 return false;
929}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000930
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000931bool AsmParser::ParseEscapedString(std::string &Data) {
932 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
933
934 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000935 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000936 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
937 if (Str[i] != '\\') {
938 Data += Str[i];
939 continue;
940 }
941
942 // Recognize escaped characters. Note that this escape semantics currently
943 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
944 ++i;
945 if (i == e)
946 return TokError("unexpected backslash at end of string");
947
948 // Recognize octal sequences.
949 if ((unsigned) (Str[i] - '0') <= 7) {
950 // Consume up to three octal characters.
951 unsigned Value = Str[i] - '0';
952
953 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
954 ++i;
955 Value = Value * 8 + (Str[i] - '0');
956
957 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
958 ++i;
959 Value = Value * 8 + (Str[i] - '0');
960 }
961 }
962
963 if (Value > 255)
964 return TokError("invalid octal escape sequence (out of range)");
965
966 Data += (unsigned char) Value;
967 continue;
968 }
969
970 // Otherwise recognize individual escapes.
971 switch (Str[i]) {
972 default:
973 // Just reject invalid escape sequences for now.
974 return TokError("invalid escape sequence (unrecognized character)");
975
976 case 'b': Data += '\b'; break;
977 case 'f': Data += '\f'; break;
978 case 'n': Data += '\n'; break;
979 case 'r': Data += '\r'; break;
980 case 't': Data += '\t'; break;
981 case '"': Data += '"'; break;
982 case '\\': Data += '\\'; break;
983 }
984 }
985
986 return false;
987}
988
Daniel Dunbara0d14262009-06-24 23:30:00 +0000989/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000990/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000991bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000992 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000993 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000994 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000995 return TokError("expected string in '.ascii' or '.asciz' directive");
996
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000997 std::string Data;
998 if (ParseEscapedString(Data))
999 return true;
1000
Chris Lattneraaec2052010-01-19 19:46:13 +00001001 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001003 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001004
Sean Callanan79ed1a82010-01-19 20:22:31 +00001005 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001006
Daniel Dunbar3f872332009-07-28 16:08:33 +00001007 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001008 break;
1009
Daniel Dunbar3f872332009-07-28 16:08:33 +00001010 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001011 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001012 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013 }
1014 }
1015
Sean Callanan79ed1a82010-01-19 20:22:31 +00001016 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001017 return false;
1018}
1019
1020/// ParseDirectiveValue
1021/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1022bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001025 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001026 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001027 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 return true;
1029
Chris Lattneraaec2052010-01-19 19:46:13 +00001030 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001031
Daniel Dunbar3f872332009-07-28 16:08:33 +00001032 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033 break;
1034
1035 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001036 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001038 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001039 }
1040 }
1041
Sean Callanan79ed1a82010-01-19 20:22:31 +00001042 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043 return false;
1044}
1045
1046/// ParseDirectiveSpace
1047/// ::= .space expression [ , expression ]
1048bool AsmParser::ParseDirectiveSpace() {
1049 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001050 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 return true;
1052
1053 int64_t FillExpr = 0;
1054 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001055 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1056 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001058 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059
Daniel Dunbar475839e2009-06-29 20:37:27 +00001060 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001061 return true;
1062
1063 HasFillExpr = true;
1064
Daniel Dunbar3f872332009-07-28 16:08:33 +00001065 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001066 return TokError("unexpected token in '.space' directive");
1067 }
1068
Sean Callanan79ed1a82010-01-19 20:22:31 +00001069 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001070
1071 if (NumBytes <= 0)
1072 return TokError("invalid number of bytes in '.space' directive");
1073
1074 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001075 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076
1077 return false;
1078}
1079
1080/// ParseDirectiveFill
1081/// ::= .fill expression , expression , expression
1082bool AsmParser::ParseDirectiveFill() {
1083 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001084 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001085 return true;
1086
Daniel Dunbar3f872332009-07-28 16:08:33 +00001087 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001089 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001090
1091 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001092 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093 return true;
1094
Daniel Dunbar3f872332009-07-28 16:08:33 +00001095 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001096 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001097 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001098
1099 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001100 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101 return true;
1102
Daniel Dunbar3f872332009-07-28 16:08:33 +00001103 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001104 return TokError("unexpected token in '.fill' directive");
1105
Sean Callanan79ed1a82010-01-19 20:22:31 +00001106 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001107
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001108 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1109 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001110
1111 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001112 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1113 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114
1115 return false;
1116}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001117
1118/// ParseDirectiveOrg
1119/// ::= .org expression [ , expression ]
1120bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001121 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001122 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001123 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001124 return true;
1125
1126 // Parse optional fill expression.
1127 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001128 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1129 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001130 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001131 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001132
Daniel Dunbar475839e2009-06-29 20:37:27 +00001133 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001134 return true;
1135
Daniel Dunbar3f872332009-07-28 16:08:33 +00001136 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001137 return TokError("unexpected token in '.org' directive");
1138 }
1139
Sean Callanan79ed1a82010-01-19 20:22:31 +00001140 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001141
1142 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1143 // has to be relative to the current section.
1144 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001145
1146 return false;
1147}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001148
1149/// ParseDirectiveAlign
1150/// ::= {.align, ...} expression [ , expression [ , expression ]]
1151bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001152 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001153 int64_t Alignment;
1154 if (ParseAbsoluteExpression(Alignment))
1155 return true;
1156
1157 SMLoc MaxBytesLoc;
1158 bool HasFillExpr = false;
1159 int64_t FillExpr = 0;
1160 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001161 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1162 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001163 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001164 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001165
1166 // The fill expression can be omitted while specifying a maximum number of
1167 // alignment bytes, e.g:
1168 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001169 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001170 HasFillExpr = true;
1171 if (ParseAbsoluteExpression(FillExpr))
1172 return true;
1173 }
1174
Daniel Dunbar3f872332009-07-28 16:08:33 +00001175 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1176 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001178 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179
1180 MaxBytesLoc = Lexer.getLoc();
1181 if (ParseAbsoluteExpression(MaxBytesToFill))
1182 return true;
1183
Daniel Dunbar3f872332009-07-28 16:08:33 +00001184 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001185 return TokError("unexpected token in directive");
1186 }
1187 }
1188
Sean Callanan79ed1a82010-01-19 20:22:31 +00001189 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001190
Daniel Dunbar648ac512010-05-17 21:54:30 +00001191 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001192 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001193
1194 // Compute alignment in bytes.
1195 if (IsPow2) {
1196 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001197 if (Alignment >= 32) {
1198 Error(AlignmentLoc, "invalid alignment value");
1199 Alignment = 31;
1200 }
1201
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001202 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001203 }
1204
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001205 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001206 if (MaxBytesLoc.isValid()) {
1207 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001208 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1209 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001210 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001211 }
1212
1213 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001214 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1215 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001216 MaxBytesToFill = 0;
1217 }
1218 }
1219
Daniel Dunbar648ac512010-05-17 21:54:30 +00001220 // Check whether we should use optimal code alignment for this .align
1221 // directive.
1222 //
1223 // FIXME: This should be using a target hook.
1224 bool UseCodeAlign = false;
1225 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1226 Out.getCurrentSection()))
1227 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1228 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1229 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001230 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001231 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001232 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1233 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001234 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001235
1236 return false;
1237}
1238
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001239/// ParseDirectiveSymbolAttribute
1240/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001241bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001242 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001243 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001244 StringRef Name;
1245
1246 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001247 return TokError("expected identifier in directive");
1248
Daniel Dunbar959fd882009-08-26 22:13:22 +00001249 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001250
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001251 Out.EmitSymbolAttribute(Sym, Attr);
1252
Daniel Dunbar3f872332009-07-28 16:08:33 +00001253 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001254 break;
1255
Daniel Dunbar3f872332009-07-28 16:08:33 +00001256 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001257 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001258 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001259 }
1260 }
1261
Sean Callanan79ed1a82010-01-19 20:22:31 +00001262 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001263 return false;
1264}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001265
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001266/// ParseDirectiveDarwinSymbolDesc
1267/// ::= .desc identifier , expression
1268bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001269 StringRef Name;
1270 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001271 return TokError("expected identifier in directive");
1272
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001273 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001274 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001275
Daniel Dunbar3f872332009-07-28 16:08:33 +00001276 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001277 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001278 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001279
1280 SMLoc DescLoc = Lexer.getLoc();
1281 int64_t DescValue;
1282 if (ParseAbsoluteExpression(DescValue))
1283 return true;
1284
Daniel Dunbar3f872332009-07-28 16:08:33 +00001285 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001286 return TokError("unexpected token in '.desc' directive");
1287
Sean Callanan79ed1a82010-01-19 20:22:31 +00001288 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001289
1290 // Set the n_desc field of this Symbol to this DescValue
1291 Out.EmitSymbolDesc(Sym, DescValue);
1292
1293 return false;
1294}
1295
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001296/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001297/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1298bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001299 SMLoc IDLoc = Lexer.getLoc();
1300 StringRef Name;
1301 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001302 return TokError("expected identifier in directive");
1303
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001304 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001305 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001306
Daniel Dunbar3f872332009-07-28 16:08:33 +00001307 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001308 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001309 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001310
1311 int64_t Size;
1312 SMLoc SizeLoc = Lexer.getLoc();
1313 if (ParseAbsoluteExpression(Size))
1314 return true;
1315
1316 int64_t Pow2Alignment = 0;
1317 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001318 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001319 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001320 Pow2AlignmentLoc = Lexer.getLoc();
1321 if (ParseAbsoluteExpression(Pow2Alignment))
1322 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001323
1324 // If this target takes alignments in bytes (not log) validate and convert.
1325 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1326 if (!isPowerOf2_64(Pow2Alignment))
1327 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1328 Pow2Alignment = Log2_64(Pow2Alignment);
1329 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001330 }
1331
Daniel Dunbar3f872332009-07-28 16:08:33 +00001332 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001333 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001334
Sean Callanan79ed1a82010-01-19 20:22:31 +00001335 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001336
Chris Lattner1fc3d752009-07-09 17:25:12 +00001337 // NOTE: a size of zero for a .comm should create a undefined symbol
1338 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001339 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001340 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1341 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001342
Eric Christopherc260a3e2010-05-14 01:38:54 +00001343 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001344 // may internally end up wanting an alignment in bytes.
1345 // FIXME: Diagnose overflow.
1346 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001347 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1348 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001349
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001350 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001351 return Error(IDLoc, "invalid symbol redefinition");
1352
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001353 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001354 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001355 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001356 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1357 MCSectionMachO::S_ZEROFILL, 0,
1358 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001359 Sym, Size, 1 << Pow2Alignment);
1360 return false;
1361 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001362
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001363 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001364 return false;
1365}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001366
1367/// ParseDirectiveDarwinZerofill
1368/// ::= .zerofill segname , sectname [, identifier , size_expression [
1369/// , align_expression ]]
1370bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001371 StringRef Segment;
1372 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001373 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001374
Daniel Dunbar3f872332009-07-28 16:08:33 +00001375 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001376 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001377 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001378
1379 StringRef Section;
1380 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001381 return TokError("expected section name after comma in '.zerofill' "
1382 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001383
Chris Lattner9be3fee2009-07-10 22:20:30 +00001384 // If this is the end of the line all that was wanted was to create the
1385 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001386 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001387 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001388 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1389 MCSectionMachO::S_ZEROFILL, 0,
1390 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001391 return false;
1392 }
1393
Daniel Dunbar3f872332009-07-28 16:08:33 +00001394 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001395 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001396 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001397
Chris Lattner7bb7c552010-05-13 00:10:34 +00001398 SMLoc IDLoc = Lexer.getLoc();
1399 StringRef IDStr;
1400 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001401 return TokError("expected identifier in directive");
1402
1403 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001404 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001405
Daniel Dunbar3f872332009-07-28 16:08:33 +00001406 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001407 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001408 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001409
1410 int64_t Size;
1411 SMLoc SizeLoc = Lexer.getLoc();
1412 if (ParseAbsoluteExpression(Size))
1413 return true;
1414
1415 int64_t Pow2Alignment = 0;
1416 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001417 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001418 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001419 Pow2AlignmentLoc = Lexer.getLoc();
1420 if (ParseAbsoluteExpression(Pow2Alignment))
1421 return true;
1422 }
1423
Daniel Dunbar3f872332009-07-28 16:08:33 +00001424 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001425 return TokError("unexpected token in '.zerofill' directive");
1426
Sean Callanan79ed1a82010-01-19 20:22:31 +00001427 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001428
1429 if (Size < 0)
1430 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1431 "than zero");
1432
Eric Christopherc260a3e2010-05-14 01:38:54 +00001433 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001434 // may internally end up wanting an alignment in bytes.
1435 // FIXME: Diagnose overflow.
1436 if (Pow2Alignment < 0)
1437 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1438 "can't be less than zero");
1439
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001440 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001441 return Error(IDLoc, "invalid symbol redefinition");
1442
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001443 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001444 //
1445 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001446 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1447 MCSectionMachO::S_ZEROFILL, 0,
1448 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001449 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001450
1451 return false;
1452}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001453
Eric Christopher482eba02010-05-14 01:50:28 +00001454/// ParseDirectiveDarwinTBSS
1455/// ::= .tbss identifier, size, align
1456bool AsmParser::ParseDirectiveDarwinTBSS() {
1457 SMLoc IDLoc = Lexer.getLoc();
1458 StringRef Name;
1459 if (ParseIdentifier(Name))
1460 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001461
Eric Christopher482eba02010-05-14 01:50:28 +00001462 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001463 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001464
1465 if (Lexer.isNot(AsmToken::Comma))
1466 return TokError("unexpected token in directive");
1467 Lex();
1468
1469 int64_t Size;
1470 SMLoc SizeLoc = Lexer.getLoc();
1471 if (ParseAbsoluteExpression(Size))
1472 return true;
1473
1474 int64_t Pow2Alignment = 0;
1475 SMLoc Pow2AlignmentLoc;
1476 if (Lexer.is(AsmToken::Comma)) {
1477 Lex();
1478 Pow2AlignmentLoc = Lexer.getLoc();
1479 if (ParseAbsoluteExpression(Pow2Alignment))
1480 return true;
1481 }
1482
1483 if (Lexer.isNot(AsmToken::EndOfStatement))
1484 return TokError("unexpected token in '.tbss' directive");
1485
1486 Lex();
1487
1488 if (Size < 0)
1489 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1490 "zero");
1491
1492 // FIXME: Diagnose overflow.
1493 if (Pow2Alignment < 0)
1494 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1495 "than zero");
1496
1497 if (!Sym->isUndefined())
1498 return Error(IDLoc, "invalid symbol redefinition");
1499
1500 Out.EmitTBSSSymbol(Sym, Size, Pow2Alignment ? 1 << Pow2Alignment : 0);
1501
1502 return false;
1503}
1504
Kevin Enderbya5c78322009-07-13 21:03:15 +00001505/// ParseDirectiveDarwinSubsectionsViaSymbols
1506/// ::= .subsections_via_symbols
1507bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001508 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001509 return TokError("unexpected token in '.subsections_via_symbols' directive");
1510
Sean Callanan79ed1a82010-01-19 20:22:31 +00001511 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001512
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001513 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001514
1515 return false;
1516}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001517
1518/// ParseDirectiveAbort
1519/// ::= .abort [ "abort_string" ]
1520bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001521 // FIXME: Use loc from directive.
1522 SMLoc Loc = Lexer.getLoc();
1523
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001524 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001525 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1526 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001527 return TokError("expected string in '.abort' directive");
1528
Sean Callanan18b83232010-01-19 21:44:56 +00001529 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001530
Sean Callanan79ed1a82010-01-19 20:22:31 +00001531 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001532 }
1533
Daniel Dunbar3f872332009-07-28 16:08:33 +00001534 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001535 return TokError("unexpected token in '.abort' directive");
1536
Sean Callanan79ed1a82010-01-19 20:22:31 +00001537 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001538
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001539 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001540 if (Str.empty())
1541 Error(Loc, ".abort detected. Assembly stopping.");
1542 else
1543 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001544
1545 return false;
1546}
Kevin Enderby71148242009-07-14 21:35:03 +00001547
1548/// ParseDirectiveLsym
1549/// ::= .lsym identifier , expression
1550bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001551 StringRef Name;
1552 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001553 return TokError("expected identifier in directive");
1554
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001555 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001556 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001557
Daniel Dunbar3f872332009-07-28 16:08:33 +00001558 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001559 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001560 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001561
Daniel Dunbar821e3332009-08-31 08:09:28 +00001562 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001563 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001564 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001565 return true;
1566
Daniel Dunbar3f872332009-07-28 16:08:33 +00001567 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001568 return TokError("unexpected token in '.lsym' directive");
1569
Sean Callanan79ed1a82010-01-19 20:22:31 +00001570 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001571
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001572 // We don't currently support this directive.
1573 //
1574 // FIXME: Diagnostic location!
1575 (void) Sym;
1576 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001577}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001578
1579/// ParseDirectiveInclude
1580/// ::= .include "filename"
1581bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001582 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001583 return TokError("expected string in '.include' directive");
1584
Sean Callanan18b83232010-01-19 21:44:56 +00001585 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001586 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001587 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001588
Daniel Dunbar3f872332009-07-28 16:08:33 +00001589 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001590 return TokError("unexpected token in '.include' directive");
1591
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001592 // Strip the quotes.
1593 Filename = Filename.substr(1, Filename.size()-2);
1594
1595 // Attempt to switch the lexer to the included file before consuming the end
1596 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001597 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001598 PrintMessage(IncludeLoc,
1599 "Could not find include file '" + Filename + "'",
1600 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001601 return true;
1602 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001603
1604 return false;
1605}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001606
1607/// ParseDirectiveDarwinDumpOrLoad
1608/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001609bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001610 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001611 return TokError("expected string in '.dump' or '.load' directive");
1612
Sean Callanan79ed1a82010-01-19 20:22:31 +00001613 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001614
Daniel Dunbar3f872332009-07-28 16:08:33 +00001615 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001616 return TokError("unexpected token in '.dump' or '.load' directive");
1617
Sean Callanan79ed1a82010-01-19 20:22:31 +00001618 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001619
Kevin Enderby5026ae42009-07-20 20:25:37 +00001620 // FIXME: If/when .dump and .load are implemented they will be done in the
1621 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001622 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001623 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001624 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001625 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001626
1627 return false;
1628}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001629
1630/// ParseDirectiveIf
1631/// ::= .if expression
1632bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001633 TheCondStack.push_back(TheCondState);
1634 TheCondState.TheCond = AsmCond::IfCond;
1635 if(TheCondState.Ignore) {
1636 EatToEndOfStatement();
1637 }
1638 else {
1639 int64_t ExprValue;
1640 if (ParseAbsoluteExpression(ExprValue))
1641 return true;
1642
1643 if (Lexer.isNot(AsmToken::EndOfStatement))
1644 return TokError("unexpected token in '.if' directive");
1645
Sean Callanan79ed1a82010-01-19 20:22:31 +00001646 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001647
1648 TheCondState.CondMet = ExprValue;
1649 TheCondState.Ignore = !TheCondState.CondMet;
1650 }
1651
1652 return false;
1653}
1654
1655/// ParseDirectiveElseIf
1656/// ::= .elseif expression
1657bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1658 if (TheCondState.TheCond != AsmCond::IfCond &&
1659 TheCondState.TheCond != AsmCond::ElseIfCond)
1660 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1661 " an .elseif");
1662 TheCondState.TheCond = AsmCond::ElseIfCond;
1663
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001664 bool LastIgnoreState = false;
1665 if (!TheCondStack.empty())
1666 LastIgnoreState = TheCondStack.back().Ignore;
1667 if (LastIgnoreState || TheCondState.CondMet) {
1668 TheCondState.Ignore = true;
1669 EatToEndOfStatement();
1670 }
1671 else {
1672 int64_t ExprValue;
1673 if (ParseAbsoluteExpression(ExprValue))
1674 return true;
1675
1676 if (Lexer.isNot(AsmToken::EndOfStatement))
1677 return TokError("unexpected token in '.elseif' directive");
1678
Sean Callanan79ed1a82010-01-19 20:22:31 +00001679 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001680 TheCondState.CondMet = ExprValue;
1681 TheCondState.Ignore = !TheCondState.CondMet;
1682 }
1683
1684 return false;
1685}
1686
1687/// ParseDirectiveElse
1688/// ::= .else
1689bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001690 if (Lexer.isNot(AsmToken::EndOfStatement))
1691 return TokError("unexpected token in '.else' directive");
1692
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001694
1695 if (TheCondState.TheCond != AsmCond::IfCond &&
1696 TheCondState.TheCond != AsmCond::ElseIfCond)
1697 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1698 ".elseif");
1699 TheCondState.TheCond = AsmCond::ElseCond;
1700 bool LastIgnoreState = false;
1701 if (!TheCondStack.empty())
1702 LastIgnoreState = TheCondStack.back().Ignore;
1703 if (LastIgnoreState || TheCondState.CondMet)
1704 TheCondState.Ignore = true;
1705 else
1706 TheCondState.Ignore = false;
1707
1708 return false;
1709}
1710
1711/// ParseDirectiveEndIf
1712/// ::= .endif
1713bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001714 if (Lexer.isNot(AsmToken::EndOfStatement))
1715 return TokError("unexpected token in '.endif' directive");
1716
Sean Callanan79ed1a82010-01-19 20:22:31 +00001717 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001718
1719 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1720 TheCondStack.empty())
1721 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1722 ".else");
1723 if (!TheCondStack.empty()) {
1724 TheCondState = TheCondStack.back();
1725 TheCondStack.pop_back();
1726 }
1727
1728 return false;
1729}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001730
1731/// ParseDirectiveFile
1732/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001733bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001734 // FIXME: I'm not sure what this is.
1735 int64_t FileNumber = -1;
1736 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001737 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001738 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001739
1740 if (FileNumber < 1)
1741 return TokError("file number less than one");
1742 }
1743
1744 if (Lexer.isNot(AsmToken::String))
1745 return TokError("unexpected token in '.file' directive");
1746
Chris Lattnerd32e8032010-01-25 19:02:58 +00001747 StringRef Filename = getTok().getString();
1748 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001749 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001750
1751 if (Lexer.isNot(AsmToken::EndOfStatement))
1752 return TokError("unexpected token in '.file' directive");
1753
Chris Lattnerd32e8032010-01-25 19:02:58 +00001754 if (FileNumber == -1)
1755 Out.EmitFileDirective(Filename);
1756 else
1757 Out.EmitDwarfFileDirective(FileNumber, Filename);
1758
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001759 return false;
1760}
1761
1762/// ParseDirectiveLine
1763/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001764bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001765 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1766 if (Lexer.isNot(AsmToken::Integer))
1767 return TokError("unexpected token in '.line' directive");
1768
Sean Callanan18b83232010-01-19 21:44:56 +00001769 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001770 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001771 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001772
1773 // FIXME: Do something with the .line.
1774 }
1775
1776 if (Lexer.isNot(AsmToken::EndOfStatement))
1777 return TokError("unexpected token in '.file' directive");
1778
1779 return false;
1780}
1781
1782
1783/// ParseDirectiveLoc
1784/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001785bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001786 if (Lexer.isNot(AsmToken::Integer))
1787 return TokError("unexpected token in '.loc' directive");
1788
1789 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001790 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001791 (void) FileNumber;
1792 // FIXME: Validate file.
1793
Sean Callanan79ed1a82010-01-19 20:22:31 +00001794 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001795 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1796 if (Lexer.isNot(AsmToken::Integer))
1797 return TokError("unexpected token in '.loc' directive");
1798
Sean Callanan18b83232010-01-19 21:44:56 +00001799 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001800 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001801 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001802
1803 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1804 if (Lexer.isNot(AsmToken::Integer))
1805 return TokError("unexpected token in '.loc' directive");
1806
Sean Callanan18b83232010-01-19 21:44:56 +00001807 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001808 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001809 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001810
1811 // FIXME: Do something with the .loc.
1812 }
1813 }
1814
1815 if (Lexer.isNot(AsmToken::EndOfStatement))
1816 return TokError("unexpected token in '.file' directive");
1817
1818 return false;
1819}
1820