blob: 36075227b7101f53c023013f825a5b9b2c507f61 [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 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000217 case AsmToken::Integer: {
218 SMLoc Loc = getTok().getLoc();
219 int64_t IntVal = getTok().getIntVal();
220 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000221 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000222 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000223 // Look for 'b' or 'f' following an Integer as a directional label
224 if (Lexer.getKind() == AsmToken::Identifier) {
225 StringRef IDVal = getTok().getString();
226 if (IDVal == "f" || IDVal == "b"){
227 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
228 IDVal == "f" ? 1 : 0);
229 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
230 getContext());
231 if(IDVal == "b" && Sym->isUndefined())
232 return Error(Loc, "invalid reference to undefined symbol");
233 EndLoc = Lexer.getLoc();
234 Lex(); // Eat identifier.
235 }
236 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000237 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000238 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000239 case AsmToken::Dot: {
240 // This is a '.' reference, which references the current PC. Emit a
241 // temporary label to the streamer and refer to it.
242 MCSymbol *Sym = Ctx.CreateTempSymbol();
243 Out.EmitLabel(Sym);
244 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
245 EndLoc = Lexer.getLoc();
246 Lex(); // Eat identifier.
247 return false;
248 }
249
Daniel Dunbar3f872332009-07-28 16:08:33 +0000250 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000251 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000252 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000253 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000254 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000255 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000257 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000259 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000260 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000261 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000263 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000264 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000265 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000266 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000267 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000269 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000271 }
272}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000273
Chris Lattnerb4307b32010-01-15 19:28:38 +0000274bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000275 SMLoc EndLoc;
276 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000277}
278
Chris Lattner74ec1a32009-06-22 06:32:03 +0000279/// ParseExpression - Parse an expression and return it.
280///
281/// expr ::= expr +,- expr -> lowest.
282/// expr ::= expr |,^,&,! expr -> middle.
283/// expr ::= expr *,/,%,<<,>> expr -> highest.
284/// expr ::= primaryexpr
285///
Chris Lattner54482b42010-01-15 19:39:23 +0000286bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000287 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000288 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000289 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
290 return true;
291
292 // Try to constant fold it up front, if possible.
293 int64_t Value;
294 if (Res->EvaluateAsAbsolute(Value))
295 Res = MCConstantExpr::Create(Value, getContext());
296
297 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000298}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000299
Chris Lattnerb4307b32010-01-15 19:28:38 +0000300bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000301 Res = 0;
302 return ParseParenExpr(Res, EndLoc) ||
303 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000304}
305
Daniel Dunbar475839e2009-06-29 20:37:27 +0000306bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000307 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000308
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000309 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310 if (ParseExpression(Expr))
311 return true;
312
Daniel Dunbare00b0112009-10-16 01:57:52 +0000313 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000314 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315
316 return false;
317}
318
Daniel Dunbar3f872332009-07-28 16:08:33 +0000319static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000320 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000321 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000322 default:
323 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000324
325 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000327 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000328 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 return 1;
332
333 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000334 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000335 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000336 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000337 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000338 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000339 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000340 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000341 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::ExclaimEqual:
344 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000345 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000346 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000347 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000348 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000349 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000350 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000351 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000353 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000354 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000355 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000356 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000357 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358 return 2;
359
360 // Intermediate Precedence: |, &, ^
361 //
362 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000365 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 3;
372
373 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000374 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000375 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000377 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000378 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000379 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000383 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000384 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000386 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000387 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000389 }
390}
391
392
393/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
394/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000395bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
396 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000397 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000398 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000399 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000400
401 // If the next token is lower precedence than we are allowed to eat, return
402 // successfully with what we ate already.
403 if (TokPrec < Precedence)
404 return false;
405
Sean Callanan79ed1a82010-01-19 20:22:31 +0000406 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000407
408 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000409 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000410 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000411
412 // If BinOp binds less tightly with RHS than the operator after RHS, let
413 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000414 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000415 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000416 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000417 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000418 }
419
Daniel Dunbar475839e2009-06-29 20:37:27 +0000420 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000421 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000422 }
423}
424
Chris Lattnerc4193832009-06-22 05:51:26 +0000425
426
427
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000428/// ParseStatement:
429/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000430/// ::= Label* Directive ...Operands... EndOfStatement
431/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000432bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000433 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000434 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000435 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000436 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000437
438 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000439 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000440 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000441 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000442 int64_t LocalLabelVal = -1;
443 // GUESS allow an integer followed by a ':' as a directional local label
444 if (Lexer.is(AsmToken::Integer)) {
445 LocalLabelVal = getTok().getIntVal();
446 if (LocalLabelVal < 0) {
447 if (!TheCondState.Ignore)
448 return TokError("unexpected token at start of statement");
449 IDVal = "";
450 }
451 else {
452 IDVal = getTok().getString();
453 Lex(); // Consume the integer token to be used as an identifier token.
454 if (Lexer.getKind() != AsmToken::Colon) {
455 if (!TheCondState.Ignore)
456 return TokError("unexpected token at start of statement");
457 }
458 }
459 }
460 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000461 if (!TheCondState.Ignore)
462 return TokError("unexpected token at start of statement");
463 IDVal = "";
464 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000465
Chris Lattner7834fac2010-04-17 18:14:27 +0000466 // Handle conditional assembly here before checking for skipping. We
467 // have to do this so that .endif isn't skipped in a ".if 0" block for
468 // example.
469 if (IDVal == ".if")
470 return ParseDirectiveIf(IDLoc);
471 if (IDVal == ".elseif")
472 return ParseDirectiveElseIf(IDLoc);
473 if (IDVal == ".else")
474 return ParseDirectiveElse(IDLoc);
475 if (IDVal == ".endif")
476 return ParseDirectiveEndIf(IDLoc);
477
478 // If we are in a ".if 0" block, ignore this statement.
479 if (TheCondState.Ignore) {
480 EatToEndOfStatement();
481 return false;
482 }
483
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000484 // FIXME: Recurse on local labels?
485
486 // See what kind of statement we have.
487 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000488 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000489 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000490 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000491
492 // Diagnose attempt to use a variable as a label.
493 //
494 // FIXME: Diagnostics. Note the location of the definition as a label.
495 // FIXME: This doesn't diagnose assignment to a symbol which has been
496 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000497 MCSymbol *Sym;
498 if (LocalLabelVal == -1)
499 Sym = CreateSymbol(IDVal);
500 else
501 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000502 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000503 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000504
Daniel Dunbar959fd882009-08-26 22:13:22 +0000505 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000506 Out.EmitLabel(Sym);
507
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000508 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000509 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000510
Daniel Dunbar3f872332009-07-28 16:08:33 +0000511 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000512 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000513 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000514
Daniel Dunbare2ace502009-08-31 08:09:09 +0000515 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000516
517 default: // Normal instruction or directive.
518 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000519 }
520
521 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000522 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000523 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000525 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000527 // FIXME: This changes behavior based on the -static flag to the
528 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000529 return ParseDirectiveSectionSwitch("__TEXT", "__text",
530 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000532 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000536 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
537 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000539 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000540 MCSectionMachO::S_4BYTE_LITERALS,
541 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000543 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 MCSectionMachO::S_8BYTE_LITERALS,
545 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000547 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 MCSectionMachO::S_16BYTE_LITERALS,
549 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000553 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
558
559 // FIXME: The assembler manual claims that this has the self modify code
560 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000561 if (IDVal == ".symbol_stub")
562 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
563 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000564 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
565 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000566 0, 16);
567 // FIXME: PowerPC only?
568 if (IDVal == ".picsymbol_stub")
569 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
570 MCSectionMachO::S_SYMBOL_STUBS |
571 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
572 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
577
578 // FIXME: The section names of these two are misspelled in the assembler
579 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000580 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000581 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
582 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
583 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
586 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
587 4);
588
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000590 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000592 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000593 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
594 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000596 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000597 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
598 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000600 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000601
602
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 return ParseDirectiveSectionSwitch("__OBJC", "__class",
605 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
608 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000610 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
611 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
614 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000616 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
617 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000619 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
620 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000622 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
623 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000625 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
626 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000628 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
629 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
630 MCSectionMachO::S_LITERAL_POINTERS,
631 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000633 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
634 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
635 MCSectionMachO::S_LITERAL_POINTERS,
636 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000638 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
639 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 return ParseDirectiveSectionSwitch("__OBJC", "__category",
642 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000644 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
645 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000647 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
648 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000650 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
651 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000653 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
654 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000656 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
657 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000659 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
660 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000662 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
663 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000664
Eric Christopherc6177a42010-05-17 22:53:55 +0000665 if (IDVal == ".tdata")
666 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
667 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
668 if (IDVal == ".tlv")
669 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
670 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
671 if (IDVal == ".thread_init_func")
672 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
673 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
674
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000675 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000677 return ParseDirectiveSet();
678
Daniel Dunbara0d14262009-06-24 23:30:00 +0000679 // Data directives
680
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000682 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000684 return ParseDirectiveAscii(true);
685
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000687 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000688 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000689 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000691 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000693 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000694
695 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000697 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000699 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000701 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000703 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000705 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000707 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000709 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000711 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
712
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000714 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000715
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000717 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000719 return ParseDirectiveSpace();
720
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000721 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000722
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000724 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000726 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000728 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000730 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000732 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000734 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000736 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000738 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000740 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000742 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000744 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000746 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000747
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000749 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000750 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000751 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000752 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000753 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000754 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000755 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000756 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000757 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000758 if (IDVal == ".tbss")
759 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000760
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000761 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000762 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000763 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000764 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000765 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000766 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000767 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000768 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000770 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000771
Chris Lattnerebb89b42009-09-27 21:16:52 +0000772 // Look up the handler in the handler table,
773 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
774 if (Handler)
775 return (this->*Handler)(IDVal, IDLoc);
776
Kevin Enderby9c656452009-09-10 20:51:44 +0000777 // Target hook for parsing target specific directives.
778 if (!getTargetParser().ParseDirective(ID))
779 return false;
780
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000781 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000782 EatToEndOfStatement();
783 return false;
784 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000785
Chris Lattnera7f13542010-05-19 23:34:33 +0000786 // Canonicalize the opcode to lower case.
787 SmallString<128> Opcode;
788 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
789 Opcode.push_back(tolower(IDVal[i]));
790
Chris Lattner98986712010-01-14 22:21:20 +0000791 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000792 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000793 ParsedOperands);
794 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
795 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000796
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000797 // If parsing succeeded, match the instruction.
798 if (!HadError) {
799 MCInst Inst;
800 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
801 // Emit the instruction on success.
802 Out.EmitInstruction(Inst);
803 } else {
804 // Otherwise emit a diagnostic about the match failure and set the error
805 // flag.
806 //
807 // FIXME: We should give nicer diagnostics about the exact failure.
808 Error(IDLoc, "unrecognized instruction");
809 HadError = true;
810 }
811 }
Chris Lattner98986712010-01-14 22:21:20 +0000812
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000813 // If there was no error, consume the end-of-statement token. Otherwise this
814 // will be done by our caller.
815 if (!HadError)
816 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000817
818 // Free any parsed operands.
819 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
820 delete ParsedOperands[i];
821
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000822 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000823}
Chris Lattner9a023f72009-06-24 04:43:34 +0000824
Daniel Dunbare2ace502009-08-31 08:09:09 +0000825bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000826 // FIXME: Use better location, we should use proper tokens.
827 SMLoc EqualLoc = Lexer.getLoc();
828
Daniel Dunbar821e3332009-08-31 08:09:28 +0000829 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000830 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000831 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000832 return true;
833
Daniel Dunbar3f872332009-07-28 16:08:33 +0000834 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000835 return TokError("unexpected token in assignment");
836
837 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000838 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000839
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000840 // Validate that the LHS is allowed to be a variable (either it has not been
841 // used as a symbol, or it is an absolute symbol).
842 MCSymbol *Sym = getContext().LookupSymbol(Name);
843 if (Sym) {
844 // Diagnose assignment to a label.
845 //
846 // FIXME: Diagnostics. Note the location of the definition as a label.
847 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000848 if (Sym->isUndefined() && !Sym->isUsedInExpr())
849 ; // Allow redefinitions of undefined symbols only used in directives.
850 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000851 return Error(EqualLoc, "redefinition of '" + Name + "'");
852 else if (!Sym->isVariable())
853 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000854 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000855 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
856 Name + "'");
857 } else
858 Sym = CreateSymbol(Name);
859
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000860 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000861
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000862 Sym->setUsedInExpr(true);
863
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000864 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000865 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000866
867 return false;
868}
869
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000870/// ParseIdentifier:
871/// ::= identifier
872/// ::= string
873bool AsmParser::ParseIdentifier(StringRef &Res) {
874 if (Lexer.isNot(AsmToken::Identifier) &&
875 Lexer.isNot(AsmToken::String))
876 return true;
877
Sean Callanan18b83232010-01-19 21:44:56 +0000878 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000879
Sean Callanan79ed1a82010-01-19 20:22:31 +0000880 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000881
882 return false;
883}
884
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000885/// ParseDirectiveSet:
886/// ::= .set identifier ',' expression
887bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000888 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000889
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000890 if (ParseIdentifier(Name))
891 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000892
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000893 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000894 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000895 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000896
Daniel Dunbare2ace502009-08-31 08:09:09 +0000897 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000898}
899
Chris Lattner9a023f72009-06-24 04:43:34 +0000900/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000901/// ::= .section identifier (',' identifier)*
902/// FIXME: This should actually parse out the segment, section, attributes and
903/// sizeof_stub fields.
904bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000905 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000906
Daniel Dunbarace63122009-08-11 03:42:33 +0000907 StringRef SectionName;
908 if (ParseIdentifier(SectionName))
909 return Error(Loc, "expected identifier after '.section' directive");
910
911 // Verify there is a following comma.
912 if (!Lexer.is(AsmToken::Comma))
913 return TokError("unexpected token in '.section' directive");
914
Chris Lattnerff4bc462009-08-10 01:39:42 +0000915 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000916 SectionSpec += ",";
917
918 // Add all the tokens until the end of the line, ParseSectionSpecifier will
919 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000920 StringRef EOL = Lexer.LexUntilEndOfStatement();
921 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000922
Sean Callanan79ed1a82010-01-19 20:22:31 +0000923 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000924 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000925 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000926 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000927
Chris Lattnerff4bc462009-08-10 01:39:42 +0000928
929 StringRef Segment, Section;
930 unsigned TAA, StubSize;
931 std::string ErrorStr =
932 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
933 TAA, StubSize);
934
935 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000936 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000937
Chris Lattner56594f92009-07-31 17:47:16 +0000938 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000939 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000940 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
941 isText ? SectionKind::getText()
942 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000943 return false;
944}
945
Chris Lattnere15c2d72009-08-10 18:05:55 +0000946/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000947bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
948 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000949 unsigned TAA, unsigned Align,
950 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000952 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000953 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000954
Chris Lattner56594f92009-07-31 17:47:16 +0000955 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000956 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000957 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
958 isText ? SectionKind::getText()
959 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000960
961 // Set the implicit alignment, if any.
962 //
963 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
964 // alignment on the section (e.g., if one manually inserts bytes into the
965 // section, then just issueing the section switch directive will not realign
966 // the section. However, this is arguably more reasonable behavior, and there
967 // is no good reason for someone to intentionally emit incorrectly sized
968 // values into the implicitly aligned sections.
969 if (Align)
970 Out.EmitValueToAlignment(Align, 0, 1, 0);
971
Chris Lattner529fb542009-06-24 05:13:15 +0000972 return false;
973}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000975bool AsmParser::ParseEscapedString(std::string &Data) {
976 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
977
978 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000979 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000980 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
981 if (Str[i] != '\\') {
982 Data += Str[i];
983 continue;
984 }
985
986 // Recognize escaped characters. Note that this escape semantics currently
987 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
988 ++i;
989 if (i == e)
990 return TokError("unexpected backslash at end of string");
991
992 // Recognize octal sequences.
993 if ((unsigned) (Str[i] - '0') <= 7) {
994 // Consume up to three octal characters.
995 unsigned Value = Str[i] - '0';
996
997 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
998 ++i;
999 Value = Value * 8 + (Str[i] - '0');
1000
1001 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1002 ++i;
1003 Value = Value * 8 + (Str[i] - '0');
1004 }
1005 }
1006
1007 if (Value > 255)
1008 return TokError("invalid octal escape sequence (out of range)");
1009
1010 Data += (unsigned char) Value;
1011 continue;
1012 }
1013
1014 // Otherwise recognize individual escapes.
1015 switch (Str[i]) {
1016 default:
1017 // Just reject invalid escape sequences for now.
1018 return TokError("invalid escape sequence (unrecognized character)");
1019
1020 case 'b': Data += '\b'; break;
1021 case 'f': Data += '\f'; break;
1022 case 'n': Data += '\n'; break;
1023 case 'r': Data += '\r'; break;
1024 case 't': Data += '\t'; break;
1025 case '"': Data += '"'; break;
1026 case '\\': Data += '\\'; break;
1027 }
1028 }
1029
1030 return false;
1031}
1032
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001034/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001035bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001036 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001038 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001039 return TokError("expected string in '.ascii' or '.asciz' directive");
1040
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001041 std::string Data;
1042 if (ParseEscapedString(Data))
1043 return true;
1044
Chris Lattneraaec2052010-01-19 19:46:13 +00001045 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001047 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048
Sean Callanan79ed1a82010-01-19 20:22:31 +00001049 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001050
Daniel Dunbar3f872332009-07-28 16:08:33 +00001051 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052 break;
1053
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001056 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057 }
1058 }
1059
Sean Callanan79ed1a82010-01-19 20:22:31 +00001060 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001061 return false;
1062}
1063
1064/// ParseDirectiveValue
1065/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1066bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001069 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001070 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001071 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001072 return true;
1073
Chris Lattneraaec2052010-01-19 19:46:13 +00001074 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001075
Daniel Dunbar3f872332009-07-28 16:08:33 +00001076 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 break;
1078
1079 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001080 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001082 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083 }
1084 }
1085
Sean Callanan79ed1a82010-01-19 20:22:31 +00001086 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087 return false;
1088}
1089
1090/// ParseDirectiveSpace
1091/// ::= .space expression [ , expression ]
1092bool AsmParser::ParseDirectiveSpace() {
1093 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001094 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001095 return true;
1096
1097 int64_t FillExpr = 0;
1098 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001099 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1100 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001102 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001103
Daniel Dunbar475839e2009-06-29 20:37:27 +00001104 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001105 return true;
1106
1107 HasFillExpr = true;
1108
Daniel Dunbar3f872332009-07-28 16:08:33 +00001109 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001110 return TokError("unexpected token in '.space' directive");
1111 }
1112
Sean Callanan79ed1a82010-01-19 20:22:31 +00001113 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114
1115 if (NumBytes <= 0)
1116 return TokError("invalid number of bytes in '.space' directive");
1117
1118 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001119 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001120
1121 return false;
1122}
1123
1124/// ParseDirectiveFill
1125/// ::= .fill expression , expression , expression
1126bool AsmParser::ParseDirectiveFill() {
1127 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001128 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001129 return true;
1130
Daniel Dunbar3f872332009-07-28 16:08:33 +00001131 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001133 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001134
1135 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001136 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001137 return true;
1138
Daniel Dunbar3f872332009-07-28 16:08:33 +00001139 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001140 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001141 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001142
1143 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001144 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001145 return true;
1146
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148 return TokError("unexpected token in '.fill' directive");
1149
Sean Callanan79ed1a82010-01-19 20:22:31 +00001150 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001151
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001152 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1153 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154
1155 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001156 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1157 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001158
1159 return false;
1160}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001161
1162/// ParseDirectiveOrg
1163/// ::= .org expression [ , expression ]
1164bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001165 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001166 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001167 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001168 return true;
1169
1170 // Parse optional fill expression.
1171 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001172 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1173 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001174 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001175 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001176
Daniel Dunbar475839e2009-06-29 20:37:27 +00001177 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001178 return true;
1179
Daniel Dunbar3f872332009-07-28 16:08:33 +00001180 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001181 return TokError("unexpected token in '.org' directive");
1182 }
1183
Sean Callanan79ed1a82010-01-19 20:22:31 +00001184 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001185
1186 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1187 // has to be relative to the current section.
1188 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001189
1190 return false;
1191}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001192
1193/// ParseDirectiveAlign
1194/// ::= {.align, ...} expression [ , expression [ , expression ]]
1195bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001196 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001197 int64_t Alignment;
1198 if (ParseAbsoluteExpression(Alignment))
1199 return true;
1200
1201 SMLoc MaxBytesLoc;
1202 bool HasFillExpr = false;
1203 int64_t FillExpr = 0;
1204 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001205 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1206 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001208 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001209
1210 // The fill expression can be omitted while specifying a maximum number of
1211 // alignment bytes, e.g:
1212 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001213 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001214 HasFillExpr = true;
1215 if (ParseAbsoluteExpression(FillExpr))
1216 return true;
1217 }
1218
Daniel Dunbar3f872332009-07-28 16:08:33 +00001219 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1220 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001221 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001222 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001223
1224 MaxBytesLoc = Lexer.getLoc();
1225 if (ParseAbsoluteExpression(MaxBytesToFill))
1226 return true;
1227
Daniel Dunbar3f872332009-07-28 16:08:33 +00001228 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 return TokError("unexpected token in directive");
1230 }
1231 }
1232
Sean Callanan79ed1a82010-01-19 20:22:31 +00001233 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001234
Daniel Dunbar648ac512010-05-17 21:54:30 +00001235 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001237
1238 // Compute alignment in bytes.
1239 if (IsPow2) {
1240 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001241 if (Alignment >= 32) {
1242 Error(AlignmentLoc, "invalid alignment value");
1243 Alignment = 31;
1244 }
1245
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001246 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001247 }
1248
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001249 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001250 if (MaxBytesLoc.isValid()) {
1251 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001252 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1253 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001254 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001255 }
1256
1257 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001258 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1259 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001260 MaxBytesToFill = 0;
1261 }
1262 }
1263
Daniel Dunbar648ac512010-05-17 21:54:30 +00001264 // Check whether we should use optimal code alignment for this .align
1265 // directive.
1266 //
1267 // FIXME: This should be using a target hook.
1268 bool UseCodeAlign = false;
1269 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1270 Out.getCurrentSection()))
1271 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1272 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1273 ValueSize == 1 && UseCodeAlign) {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001274 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001275 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001276 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1277 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001278 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001279
1280 return false;
1281}
1282
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001283/// ParseDirectiveSymbolAttribute
1284/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001285bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001286 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001287 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001288 StringRef Name;
1289
1290 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001291 return TokError("expected identifier in directive");
1292
Daniel Dunbar959fd882009-08-26 22:13:22 +00001293 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001294
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001295 Out.EmitSymbolAttribute(Sym, Attr);
1296
Daniel Dunbar3f872332009-07-28 16:08:33 +00001297 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001298 break;
1299
Daniel Dunbar3f872332009-07-28 16:08:33 +00001300 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001301 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001302 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001303 }
1304 }
1305
Sean Callanan79ed1a82010-01-19 20:22:31 +00001306 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001307 return false;
1308}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001309
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001310/// ParseDirectiveDarwinSymbolDesc
1311/// ::= .desc identifier , expression
1312bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001313 StringRef Name;
1314 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001315 return TokError("expected identifier in directive");
1316
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001317 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001318 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001319
Daniel Dunbar3f872332009-07-28 16:08:33 +00001320 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001321 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001322 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001323
1324 SMLoc DescLoc = Lexer.getLoc();
1325 int64_t DescValue;
1326 if (ParseAbsoluteExpression(DescValue))
1327 return true;
1328
Daniel Dunbar3f872332009-07-28 16:08:33 +00001329 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001330 return TokError("unexpected token in '.desc' directive");
1331
Sean Callanan79ed1a82010-01-19 20:22:31 +00001332 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001333
1334 // Set the n_desc field of this Symbol to this DescValue
1335 Out.EmitSymbolDesc(Sym, DescValue);
1336
1337 return false;
1338}
1339
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001340/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001341/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1342bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001343 SMLoc IDLoc = Lexer.getLoc();
1344 StringRef Name;
1345 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001346 return TokError("expected identifier in directive");
1347
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001348 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001349 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001350
Daniel Dunbar3f872332009-07-28 16:08:33 +00001351 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001352 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001353 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001354
1355 int64_t Size;
1356 SMLoc SizeLoc = Lexer.getLoc();
1357 if (ParseAbsoluteExpression(Size))
1358 return true;
1359
1360 int64_t Pow2Alignment = 0;
1361 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001362 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001363 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001364 Pow2AlignmentLoc = Lexer.getLoc();
1365 if (ParseAbsoluteExpression(Pow2Alignment))
1366 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001367
1368 // If this target takes alignments in bytes (not log) validate and convert.
1369 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1370 if (!isPowerOf2_64(Pow2Alignment))
1371 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1372 Pow2Alignment = Log2_64(Pow2Alignment);
1373 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001374 }
1375
Daniel Dunbar3f872332009-07-28 16:08:33 +00001376 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001377 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001378
Sean Callanan79ed1a82010-01-19 20:22:31 +00001379 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001380
Chris Lattner1fc3d752009-07-09 17:25:12 +00001381 // NOTE: a size of zero for a .comm should create a undefined symbol
1382 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001383 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001384 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1385 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001386
Eric Christopherc260a3e2010-05-14 01:38:54 +00001387 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001388 // may internally end up wanting an alignment in bytes.
1389 // FIXME: Diagnose overflow.
1390 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001391 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1392 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001393
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001394 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001395 return Error(IDLoc, "invalid symbol redefinition");
1396
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001397 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001398 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001399 if (IsLocal) {
Chris Lattnerf0559e42010-04-08 20:30:37 +00001400 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1401 MCSectionMachO::S_ZEROFILL, 0,
1402 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001403 Sym, Size, 1 << Pow2Alignment);
1404 return false;
1405 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001406
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001407 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001408 return false;
1409}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001410
1411/// ParseDirectiveDarwinZerofill
1412/// ::= .zerofill segname , sectname [, identifier , size_expression [
1413/// , align_expression ]]
1414bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001415 StringRef Segment;
1416 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001417 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001418
Daniel Dunbar3f872332009-07-28 16:08:33 +00001419 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001420 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001421 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001422
1423 StringRef Section;
1424 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001425 return TokError("expected section name after comma in '.zerofill' "
1426 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001427
Chris Lattner9be3fee2009-07-10 22:20:30 +00001428 // If this is the end of the line all that was wanted was to create the
1429 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001430 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001431 // Create the zerofill section but no symbol
Chris Lattnerf0559e42010-04-08 20:30:37 +00001432 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1433 MCSectionMachO::S_ZEROFILL, 0,
1434 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001435 return false;
1436 }
1437
Daniel Dunbar3f872332009-07-28 16:08:33 +00001438 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001439 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001440 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001441
Chris Lattner7bb7c552010-05-13 00:10:34 +00001442 SMLoc IDLoc = Lexer.getLoc();
1443 StringRef IDStr;
1444 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001445 return TokError("expected identifier in directive");
1446
1447 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001448 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001449
Daniel Dunbar3f872332009-07-28 16:08:33 +00001450 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001451 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001452 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001453
1454 int64_t Size;
1455 SMLoc SizeLoc = Lexer.getLoc();
1456 if (ParseAbsoluteExpression(Size))
1457 return true;
1458
1459 int64_t Pow2Alignment = 0;
1460 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001461 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001462 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001463 Pow2AlignmentLoc = Lexer.getLoc();
1464 if (ParseAbsoluteExpression(Pow2Alignment))
1465 return true;
1466 }
1467
Daniel Dunbar3f872332009-07-28 16:08:33 +00001468 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001469 return TokError("unexpected token in '.zerofill' directive");
1470
Sean Callanan79ed1a82010-01-19 20:22:31 +00001471 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001472
1473 if (Size < 0)
1474 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1475 "than zero");
1476
Eric Christopherc260a3e2010-05-14 01:38:54 +00001477 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001478 // may internally end up wanting an alignment in bytes.
1479 // FIXME: Diagnose overflow.
1480 if (Pow2Alignment < 0)
1481 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1482 "can't be less than zero");
1483
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001484 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001485 return Error(IDLoc, "invalid symbol redefinition");
1486
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001487 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001488 //
1489 // FIXME: Arch specific.
Chris Lattnerf0559e42010-04-08 20:30:37 +00001490 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1491 MCSectionMachO::S_ZEROFILL, 0,
1492 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001493 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001494
1495 return false;
1496}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001497
Eric Christopher482eba02010-05-14 01:50:28 +00001498/// ParseDirectiveDarwinTBSS
1499/// ::= .tbss identifier, size, align
1500bool AsmParser::ParseDirectiveDarwinTBSS() {
1501 SMLoc IDLoc = Lexer.getLoc();
1502 StringRef Name;
1503 if (ParseIdentifier(Name))
1504 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001505
Eric Christopher482eba02010-05-14 01:50:28 +00001506 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001507 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001508
1509 if (Lexer.isNot(AsmToken::Comma))
1510 return TokError("unexpected token in directive");
1511 Lex();
1512
1513 int64_t Size;
1514 SMLoc SizeLoc = Lexer.getLoc();
1515 if (ParseAbsoluteExpression(Size))
1516 return true;
1517
1518 int64_t Pow2Alignment = 0;
1519 SMLoc Pow2AlignmentLoc;
1520 if (Lexer.is(AsmToken::Comma)) {
1521 Lex();
1522 Pow2AlignmentLoc = Lexer.getLoc();
1523 if (ParseAbsoluteExpression(Pow2Alignment))
1524 return true;
1525 }
1526
1527 if (Lexer.isNot(AsmToken::EndOfStatement))
1528 return TokError("unexpected token in '.tbss' directive");
1529
1530 Lex();
1531
1532 if (Size < 0)
1533 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1534 "zero");
1535
1536 // FIXME: Diagnose overflow.
1537 if (Pow2Alignment < 0)
1538 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1539 "than zero");
1540
1541 if (!Sym->isUndefined())
1542 return Error(IDLoc, "invalid symbol redefinition");
1543
Eric Christopher4d01cbe2010-05-18 21:16:04 +00001544 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1545 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1546 0, SectionKind::getThreadBSS()),
1547 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001548
1549 return false;
1550}
1551
Kevin Enderbya5c78322009-07-13 21:03:15 +00001552/// ParseDirectiveDarwinSubsectionsViaSymbols
1553/// ::= .subsections_via_symbols
1554bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001555 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001556 return TokError("unexpected token in '.subsections_via_symbols' directive");
1557
Sean Callanan79ed1a82010-01-19 20:22:31 +00001558 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001559
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001560 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001561
1562 return false;
1563}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001564
1565/// ParseDirectiveAbort
1566/// ::= .abort [ "abort_string" ]
1567bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001568 // FIXME: Use loc from directive.
1569 SMLoc Loc = Lexer.getLoc();
1570
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001571 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001572 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1573 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001574 return TokError("expected string in '.abort' directive");
1575
Sean Callanan18b83232010-01-19 21:44:56 +00001576 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001577
Sean Callanan79ed1a82010-01-19 20:22:31 +00001578 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001579 }
1580
Daniel Dunbar3f872332009-07-28 16:08:33 +00001581 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001582 return TokError("unexpected token in '.abort' directive");
1583
Sean Callanan79ed1a82010-01-19 20:22:31 +00001584 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001585
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001586 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001587 if (Str.empty())
1588 Error(Loc, ".abort detected. Assembly stopping.");
1589 else
1590 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001591
1592 return false;
1593}
Kevin Enderby71148242009-07-14 21:35:03 +00001594
1595/// ParseDirectiveLsym
1596/// ::= .lsym identifier , expression
1597bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001598 StringRef Name;
1599 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001600 return TokError("expected identifier in directive");
1601
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001602 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001603 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001604
Daniel Dunbar3f872332009-07-28 16:08:33 +00001605 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001606 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001607 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001608
Daniel Dunbar821e3332009-08-31 08:09:28 +00001609 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001610 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001611 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001612 return true;
1613
Daniel Dunbar3f872332009-07-28 16:08:33 +00001614 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001615 return TokError("unexpected token in '.lsym' directive");
1616
Sean Callanan79ed1a82010-01-19 20:22:31 +00001617 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001618
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001619 // We don't currently support this directive.
1620 //
1621 // FIXME: Diagnostic location!
1622 (void) Sym;
1623 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001624}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001625
1626/// ParseDirectiveInclude
1627/// ::= .include "filename"
1628bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001629 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001630 return TokError("expected string in '.include' directive");
1631
Sean Callanan18b83232010-01-19 21:44:56 +00001632 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001633 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001634 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001635
Daniel Dunbar3f872332009-07-28 16:08:33 +00001636 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001637 return TokError("unexpected token in '.include' directive");
1638
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001639 // Strip the quotes.
1640 Filename = Filename.substr(1, Filename.size()-2);
1641
1642 // Attempt to switch the lexer to the included file before consuming the end
1643 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001644 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001645 PrintMessage(IncludeLoc,
1646 "Could not find include file '" + Filename + "'",
1647 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001648 return true;
1649 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001650
1651 return false;
1652}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001653
1654/// ParseDirectiveDarwinDumpOrLoad
1655/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001656bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001657 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001658 return TokError("expected string in '.dump' or '.load' directive");
1659
Sean Callanan79ed1a82010-01-19 20:22:31 +00001660 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001661
Daniel Dunbar3f872332009-07-28 16:08:33 +00001662 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001663 return TokError("unexpected token in '.dump' or '.load' directive");
1664
Sean Callanan79ed1a82010-01-19 20:22:31 +00001665 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001666
Kevin Enderby5026ae42009-07-20 20:25:37 +00001667 // FIXME: If/when .dump and .load are implemented they will be done in the
1668 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001669 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001670 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001671 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001672 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001673
1674 return false;
1675}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001676
1677/// ParseDirectiveIf
1678/// ::= .if expression
1679bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001680 TheCondStack.push_back(TheCondState);
1681 TheCondState.TheCond = AsmCond::IfCond;
1682 if(TheCondState.Ignore) {
1683 EatToEndOfStatement();
1684 }
1685 else {
1686 int64_t ExprValue;
1687 if (ParseAbsoluteExpression(ExprValue))
1688 return true;
1689
1690 if (Lexer.isNot(AsmToken::EndOfStatement))
1691 return TokError("unexpected token in '.if' directive");
1692
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001694
1695 TheCondState.CondMet = ExprValue;
1696 TheCondState.Ignore = !TheCondState.CondMet;
1697 }
1698
1699 return false;
1700}
1701
1702/// ParseDirectiveElseIf
1703/// ::= .elseif expression
1704bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1705 if (TheCondState.TheCond != AsmCond::IfCond &&
1706 TheCondState.TheCond != AsmCond::ElseIfCond)
1707 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1708 " an .elseif");
1709 TheCondState.TheCond = AsmCond::ElseIfCond;
1710
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001711 bool LastIgnoreState = false;
1712 if (!TheCondStack.empty())
1713 LastIgnoreState = TheCondStack.back().Ignore;
1714 if (LastIgnoreState || TheCondState.CondMet) {
1715 TheCondState.Ignore = true;
1716 EatToEndOfStatement();
1717 }
1718 else {
1719 int64_t ExprValue;
1720 if (ParseAbsoluteExpression(ExprValue))
1721 return true;
1722
1723 if (Lexer.isNot(AsmToken::EndOfStatement))
1724 return TokError("unexpected token in '.elseif' directive");
1725
Sean Callanan79ed1a82010-01-19 20:22:31 +00001726 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001727 TheCondState.CondMet = ExprValue;
1728 TheCondState.Ignore = !TheCondState.CondMet;
1729 }
1730
1731 return false;
1732}
1733
1734/// ParseDirectiveElse
1735/// ::= .else
1736bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001737 if (Lexer.isNot(AsmToken::EndOfStatement))
1738 return TokError("unexpected token in '.else' directive");
1739
Sean Callanan79ed1a82010-01-19 20:22:31 +00001740 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001741
1742 if (TheCondState.TheCond != AsmCond::IfCond &&
1743 TheCondState.TheCond != AsmCond::ElseIfCond)
1744 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1745 ".elseif");
1746 TheCondState.TheCond = AsmCond::ElseCond;
1747 bool LastIgnoreState = false;
1748 if (!TheCondStack.empty())
1749 LastIgnoreState = TheCondStack.back().Ignore;
1750 if (LastIgnoreState || TheCondState.CondMet)
1751 TheCondState.Ignore = true;
1752 else
1753 TheCondState.Ignore = false;
1754
1755 return false;
1756}
1757
1758/// ParseDirectiveEndIf
1759/// ::= .endif
1760bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001761 if (Lexer.isNot(AsmToken::EndOfStatement))
1762 return TokError("unexpected token in '.endif' directive");
1763
Sean Callanan79ed1a82010-01-19 20:22:31 +00001764 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001765
1766 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1767 TheCondStack.empty())
1768 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1769 ".else");
1770 if (!TheCondStack.empty()) {
1771 TheCondState = TheCondStack.back();
1772 TheCondStack.pop_back();
1773 }
1774
1775 return false;
1776}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001777
1778/// ParseDirectiveFile
1779/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001780bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001781 // FIXME: I'm not sure what this is.
1782 int64_t FileNumber = -1;
1783 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001784 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001786
1787 if (FileNumber < 1)
1788 return TokError("file number less than one");
1789 }
1790
1791 if (Lexer.isNot(AsmToken::String))
1792 return TokError("unexpected token in '.file' directive");
1793
Chris Lattnerd32e8032010-01-25 19:02:58 +00001794 StringRef Filename = getTok().getString();
1795 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001796 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001797
1798 if (Lexer.isNot(AsmToken::EndOfStatement))
1799 return TokError("unexpected token in '.file' directive");
1800
Chris Lattnerd32e8032010-01-25 19:02:58 +00001801 if (FileNumber == -1)
1802 Out.EmitFileDirective(Filename);
1803 else
1804 Out.EmitDwarfFileDirective(FileNumber, Filename);
1805
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001806 return false;
1807}
1808
1809/// ParseDirectiveLine
1810/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001811bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001812 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1813 if (Lexer.isNot(AsmToken::Integer))
1814 return TokError("unexpected token in '.line' directive");
1815
Sean Callanan18b83232010-01-19 21:44:56 +00001816 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001817 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001818 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001819
1820 // FIXME: Do something with the .line.
1821 }
1822
1823 if (Lexer.isNot(AsmToken::EndOfStatement))
1824 return TokError("unexpected token in '.file' directive");
1825
1826 return false;
1827}
1828
1829
1830/// ParseDirectiveLoc
1831/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001832bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001833 if (Lexer.isNot(AsmToken::Integer))
1834 return TokError("unexpected token in '.loc' directive");
1835
1836 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001837 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001838 (void) FileNumber;
1839 // FIXME: Validate file.
1840
Sean Callanan79ed1a82010-01-19 20:22:31 +00001841 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001842 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1843 if (Lexer.isNot(AsmToken::Integer))
1844 return TokError("unexpected token in '.loc' directive");
1845
Sean Callanan18b83232010-01-19 21:44:56 +00001846 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001847 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001848 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001849
1850 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1851 if (Lexer.isNot(AsmToken::Integer))
1852 return TokError("unexpected token in '.loc' directive");
1853
Sean Callanan18b83232010-01-19 21:44:56 +00001854 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001855 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001856 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001857
1858 // FIXME: Do something with the .loc.
1859 }
1860 }
1861
1862 if (Lexer.isNot(AsmToken::EndOfStatement))
1863 return TokError("unexpected token in '.file' directive");
1864
1865 return false;
1866}
1867