blob: 74512ca9353d6cac0d701106226fe3befc66735a [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
14#include "AsmParser.h"
Daniel Dunbar475839e2009-06-29 20:37:27 +000015
16#include "AsmExpr.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000017#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000024#include "llvm/Support/SourceMgr.h"
25#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000026#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000027using namespace llvm;
28
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000029// Mach-O section uniquing.
30//
31// FIXME: Figure out where this should live, it should be shared by
32// TargetLoweringObjectFile.
33typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
34
35AsmParser::~AsmParser() {
36 // If we have the MachO uniquing map, free it.
37 delete (MachOUniqueMapTy*)SectionUniquingMap;
38}
39
40const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
41 const StringRef &Section,
42 unsigned TypeAndAttributes,
43 unsigned Reserved2,
44 SectionKind Kind) const {
45 // We unique sections by their segment/section pair. The returned section
46 // may not have the same flags as the requested section, if so this should be
47 // diagnosed by the client as an error.
48
49 // Create the map if it doesn't already exist.
50 if (SectionUniquingMap == 0)
51 SectionUniquingMap = new MachOUniqueMapTy();
52 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
53
54 // Form the name to look up.
55 SmallString<64> Name;
56 Name += Segment;
57 Name.push_back(',');
58 Name += Section;
59
60 // Do the lookup, if we have a hit, return it.
61 const MCSectionMachO *&Entry = Map[Name.str()];
62
63 // FIXME: This should validate the type and attributes.
64 if (Entry) return Entry;
65
66 // Otherwise, return a new section.
67 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
68 Reserved2, Kind, Ctx);
69}
70
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000071void AsmParser::Warning(SMLoc L, const Twine &Msg) {
72 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000073}
74
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000075bool AsmParser::Error(SMLoc L, const Twine &Msg) {
76 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000077 return true;
78}
79
80bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000081 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000082 return true;
83}
84
Chris Lattner27aa7d22009-06-21 20:16:42 +000085bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000086 // Create the initial section.
87 //
88 // FIXME: Support -n.
89 // FIXME: Target hook & command line option for initial section.
90 Out.SwitchSection(getMachOSection("__TEXT", "__text",
91 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
92 0, SectionKind()));
93
94
Chris Lattnerb0789ed2009-06-21 20:54:55 +000095 // Prime the lexer.
96 Lexer.Lex();
97
Chris Lattnerb717fb02009-07-02 21:53:43 +000098 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000099
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000100 AsmCond StartingCondState = TheCondState;
101
Chris Lattnerb717fb02009-07-02 21:53:43 +0000102 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000103 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000104 // Handle conditional assembly here before calling ParseStatement()
105 if (Lexer.getKind() == AsmToken::Identifier) {
106 // If we have an identifier, handle it as the key symbol.
107 AsmToken ID = Lexer.getTok();
108 SMLoc IDLoc = ID.getLoc();
109 StringRef IDVal = ID.getString();
110
111 if (IDVal == ".if" ||
112 IDVal == ".elseif" ||
113 IDVal == ".else" ||
114 IDVal == ".endif") {
115 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
116 continue;
117 HadError = true;
118 EatToEndOfStatement();
119 continue;
120 }
121 }
122 if (TheCondState.Ignore) {
123 EatToEndOfStatement();
124 continue;
125 }
126
Chris Lattnerb717fb02009-07-02 21:53:43 +0000127 if (!ParseStatement()) continue;
128
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000129 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000130 HadError = true;
131 EatToEndOfStatement();
132 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000133
134 if (TheCondState.TheCond != StartingCondState.TheCond ||
135 TheCondState.Ignore != StartingCondState.Ignore)
136 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000137
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000138 if (!HadError)
139 Out.Finish();
140
Chris Lattnerb717fb02009-07-02 21:53:43 +0000141 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000142}
143
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000144/// ParseConditionalAssemblyDirectives - parse the conditional assembly
145/// directives
146bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
147 SMLoc DirectiveLoc) {
148 if (Directive == ".if")
149 return ParseDirectiveIf(DirectiveLoc);
150 if (Directive == ".elseif")
151 return ParseDirectiveElseIf(DirectiveLoc);
152 if (Directive == ".else")
153 return ParseDirectiveElse(DirectiveLoc);
154 if (Directive == ".endif")
155 return ParseDirectiveEndIf(DirectiveLoc);
156 return true;
157}
158
Chris Lattner2cf5f142009-06-22 01:29:09 +0000159/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
160void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000161 while (Lexer.isNot(AsmToken::EndOfStatement) &&
162 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000163 Lexer.Lex();
164
165 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000166 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000167 Lexer.Lex();
168}
169
Chris Lattnerc4193832009-06-22 05:51:26 +0000170
Chris Lattner74ec1a32009-06-22 06:32:03 +0000171/// ParseParenExpr - Parse a paren expression and return it.
172/// NOTE: This assumes the leading '(' has already been consumed.
173///
174/// parenexpr ::= expr)
175///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000176bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000177 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000178 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000179 return TokError("expected ')' in parentheses expression");
180 Lexer.Lex();
181 return false;
182}
Chris Lattnerc4193832009-06-22 05:51:26 +0000183
Daniel Dunbar959fd882009-08-26 22:13:22 +0000184MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
185 if (MCSymbol *S = Ctx.LookupSymbol(Name))
186 return S;
187
188 // If the label starts with L it is an assembler temporary label.
189 if (Name.startswith("L"))
190 return Ctx.CreateTemporarySymbol(Name);
191
192 return Ctx.CreateSymbol(Name);
193}
194
Chris Lattner74ec1a32009-06-22 06:32:03 +0000195/// ParsePrimaryExpr - Parse a primary expression and return it.
196/// primaryexpr ::= (parenexpr
197/// primaryexpr ::= symbol
198/// primaryexpr ::= number
199/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +0000200bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000201 switch (Lexer.getKind()) {
202 default:
203 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000204 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000205 Lexer.Lex(); // Eat the operator.
206 if (ParsePrimaryExpr(Res))
207 return true;
208 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
209 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000210 case AsmToken::String:
Daniel Dunbar3f872332009-07-28 16:08:33 +0000211 case AsmToken::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +0000212 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000213 // handle things like LFOO+4.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000214 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000215
216 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000217 Lexer.Lex(); // Eat identifier.
218 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000219 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000220 case AsmToken::Integer:
Daniel Dunbar419aded2009-07-28 16:38:40 +0000221 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000222 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000223 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000224 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000225 Lexer.Lex(); // Eat the '('.
226 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000227 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000228 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000229 if (ParsePrimaryExpr(Res))
230 return true;
231 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
232 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000233 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000234 Lexer.Lex(); // Eat the operator.
235 if (ParsePrimaryExpr(Res))
236 return true;
237 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
238 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000239 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000240 Lexer.Lex(); // Eat the operator.
241 if (ParsePrimaryExpr(Res))
242 return true;
243 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
244 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000245 }
246}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000247
248/// ParseExpression - Parse an expression and return it.
249///
250/// expr ::= expr +,- expr -> lowest.
251/// expr ::= expr |,^,&,! expr -> middle.
252/// expr ::= expr *,/,%,<<,>> expr -> highest.
253/// expr ::= primaryexpr
254///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255bool AsmParser::ParseExpression(AsmExpr *&Res) {
256 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000257 return ParsePrimaryExpr(Res) ||
258 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000259}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000260
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
262 AsmExpr *Expr;
263
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000264 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 if (ParseExpression(Expr))
266 return true;
267
268 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000269 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270
271 return false;
272}
273
Daniel Dunbar15d17072009-06-30 01:49:52 +0000274bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
275 AsmExpr *Expr;
276
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000277 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000278 if (ParseExpression(Expr))
279 return true;
280
281 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000282 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000283
284 return false;
285}
286
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000287bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
288 AsmExpr *Expr;
289
290 SMLoc StartLoc = Lexer.getLoc();
291 if (ParseParenExpr(Expr))
292 return true;
293
294 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
295 return Error(StartLoc, "expected relocatable expression");
296
297 return false;
298}
299
Daniel Dunbar3f872332009-07-28 16:08:33 +0000300static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar475839e2009-06-29 20:37:27 +0000301 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000302 switch (K) {
303 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304
305 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000306 case AsmToken::AmpAmp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000308 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000309 case AsmToken::PipePipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310 Kind = AsmBinaryExpr::LOr;
311 return 1;
312
313 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000314 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000316 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000317 case AsmToken::Minus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000318 Kind = AsmBinaryExpr::Sub;
319 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 case AsmToken::EqualEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000321 Kind = AsmBinaryExpr::EQ;
322 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::ExclaimEqual:
324 case AsmToken::LessGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325 Kind = AsmBinaryExpr::NE;
326 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000327 case AsmToken::Less:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000328 Kind = AsmBinaryExpr::LT;
329 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::LessEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 Kind = AsmBinaryExpr::LTE;
332 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000333 case AsmToken::Greater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 Kind = AsmBinaryExpr::GT;
335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::GreaterEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000337 Kind = AsmBinaryExpr::GTE;
338 return 2;
339
340 // Intermediate Precedence: |, &, ^
341 //
342 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::Pipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000345 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Caret:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 Kind = AsmBinaryExpr::Xor;
348 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Amp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 Kind = AsmBinaryExpr::And;
351 return 3;
352
353 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000354 case AsmToken::Star:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000355 Kind = AsmBinaryExpr::Mul;
356 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::Slash:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358 Kind = AsmBinaryExpr::Div;
359 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 case AsmToken::Percent:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000361 Kind = AsmBinaryExpr::Mod;
362 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::LessLess:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000364 Kind = AsmBinaryExpr::Shl;
365 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::GreaterGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 Kind = AsmBinaryExpr::Shr;
368 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000369 }
370}
371
372
373/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
374/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000375bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000376 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000377 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000378 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000379
380 // If the next token is lower precedence than we are allowed to eat, return
381 // successfully with what we ate already.
382 if (TokPrec < Precedence)
383 return false;
384
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000385 Lexer.Lex();
386
387 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000389 if (ParsePrimaryExpr(RHS)) return true;
390
391 // If BinOp binds less tightly with RHS than the operator after RHS, let
392 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000393 AsmBinaryExpr::Opcode Dummy;
394 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000395 if (TokPrec < NextTokPrec) {
396 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
397 }
398
Daniel Dunbar475839e2009-06-29 20:37:27 +0000399 // Merge LHS and RHS according to operator.
400 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401 }
402}
403
Chris Lattnerc4193832009-06-22 05:51:26 +0000404
405
406
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000407/// ParseStatement:
408/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000409/// ::= Label* Directive ...Operands... EndOfStatement
410/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000411bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000412 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000413 Lexer.Lex();
414 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000415 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000416
417 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000418 AsmToken ID = Lexer.getTok();
419 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000420 StringRef IDVal;
421 if (ParseIdentifier(IDVal))
422 return TokError("unexpected token at start of statement");
423
424 // FIXME: Recurse on local labels?
425
426 // See what kind of statement we have.
427 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000429 // identifier ':' -> Label.
430 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000431
432 // Diagnose attempt to use a variable as a label.
433 //
434 // FIXME: Diagnostics. Note the location of the definition as a label.
435 // FIXME: This doesn't diagnose assignment to a symbol which has been
436 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000437 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000438 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000439 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000440
Daniel Dunbar959fd882009-08-26 22:13:22 +0000441 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000442 Out.EmitLabel(Sym);
443
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000444 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000445 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000446
Daniel Dunbar3f872332009-07-28 16:08:33 +0000447 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000448 // identifier '=' ... -> assignment statement
449 Lexer.Lex();
450
451 return ParseAssignment(IDVal, false);
452
453 default: // Normal instruction or directive.
454 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000455 }
456
457 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000458 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000459 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000460 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000461 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000462 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000463 // FIXME: This changes behavior based on the -static flag to the
464 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000465 return ParseDirectiveSectionSwitch("__TEXT", "__text",
466 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000467 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000468 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000470 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000471 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000472 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
473 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000474 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000475 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000476 MCSectionMachO::S_4BYTE_LITERALS,
477 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000479 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000480 MCSectionMachO::S_8BYTE_LITERALS,
481 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000483 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000484 MCSectionMachO::S_16BYTE_LITERALS,
485 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000486 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000487 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000488 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000489 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000491 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
494
495 // FIXME: The assembler manual claims that this has the self modify code
496 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000497 if (IDVal == ".symbol_stub")
498 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
499 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000500 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
501 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000502 0, 16);
503 // FIXME: PowerPC only?
504 if (IDVal == ".picsymbol_stub")
505 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
506 MCSectionMachO::S_SYMBOL_STUBS |
507 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
508 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000509 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000510 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000512 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
513
514 // FIXME: The section names of these two are misspelled in the assembler
515 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000516 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000517 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
518 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
519 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000521 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
522 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
523 4);
524
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000526 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000528 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000529 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
530 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000532 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000533 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
534 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000536 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000537
538
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000540 return ParseDirectiveSectionSwitch("__OBJC", "__class",
541 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
544 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
547 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
550 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
553 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
556 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
559 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
562 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000564 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
565 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
566 MCSectionMachO::S_LITERAL_POINTERS,
567 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000568 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000569 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
570 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
571 MCSectionMachO::S_LITERAL_POINTERS,
572 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__category",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000589 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
590 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000592 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
593 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000595 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
596 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000598 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
599 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000600
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000601 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000602 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000603 return ParseDirectiveSet();
604
Daniel Dunbara0d14262009-06-24 23:30:00 +0000605 // Data directives
606
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000608 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000610 return ParseDirectiveAscii(true);
611
612 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000614 return ParseDirectiveValue(1);
Daniel Dunbar1e840b22009-08-11 04:44:00 +0000615 if (IDVal == ".short" || IDVal == ".word")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000616 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000618 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000620 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000621
622 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000624 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000626 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000628 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000630 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000632 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000634 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000636 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000638 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
639
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000641 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000642
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000644 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000646 return ParseDirectiveSpace();
647
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000648 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000649
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000651 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000653 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000655 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000657 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000659 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000661 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000663 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000665 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000667 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000669 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000671 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000673 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
674
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000676 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000678 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000680 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000682 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000684 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000685
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000687 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000689 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000691 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000693 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000695 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000696
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000697 // Debugging directives
698
699 if (IDVal == ".file")
700 return ParseDirectiveFile(IDLoc);
701 if (IDVal == ".line")
702 return ParseDirectiveLine(IDLoc);
703 if (IDVal == ".loc")
704 return ParseDirectiveLoc(IDLoc);
705
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000706 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000707 EatToEndOfStatement();
708 return false;
709 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000710
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000711 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000712 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000713 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000714
Daniel Dunbar3f872332009-07-28 16:08:33 +0000715 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000716 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000717
718 // Eat the end of statement marker.
719 Lexer.Lex();
720
721 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000722 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000723
724 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000725 return false;
726}
Chris Lattner9a023f72009-06-24 04:43:34 +0000727
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000729 // FIXME: Use better location, we should use proper tokens.
730 SMLoc EqualLoc = Lexer.getLoc();
731
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000732 MCValue Value;
733 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000734 return true;
735
Daniel Dunbar3f872332009-07-28 16:08:33 +0000736 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000737 return TokError("unexpected token in assignment");
738
739 // Eat the end of statement marker.
740 Lexer.Lex();
741
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000742 // Diagnose assignment to a label.
743 //
744 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000745 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000746 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar959fd882009-08-26 22:13:22 +0000747 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000748 if (!Sym->isUndefined() && !Sym->isAbsolute())
749 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000750
751 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000752 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000753
754 return false;
755}
756
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000757/// ParseIdentifier:
758/// ::= identifier
759/// ::= string
760bool AsmParser::ParseIdentifier(StringRef &Res) {
761 if (Lexer.isNot(AsmToken::Identifier) &&
762 Lexer.isNot(AsmToken::String))
763 return true;
764
765 Res = Lexer.getTok().getIdentifier();
766
767 Lexer.Lex(); // Consume the identifier token.
768
769 return false;
770}
771
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000772/// ParseDirectiveSet:
773/// ::= .set identifier ',' expression
774bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000775 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000776
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000777 if (ParseIdentifier(Name))
778 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000779
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000780 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000781 return TokError("unexpected token in '.set'");
782 Lexer.Lex();
783
784 return ParseAssignment(Name, true);
785}
786
Chris Lattner9a023f72009-06-24 04:43:34 +0000787/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000788/// ::= .section identifier (',' identifier)*
789/// FIXME: This should actually parse out the segment, section, attributes and
790/// sizeof_stub fields.
791bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000792 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000793
Daniel Dunbarace63122009-08-11 03:42:33 +0000794 StringRef SectionName;
795 if (ParseIdentifier(SectionName))
796 return Error(Loc, "expected identifier after '.section' directive");
797
798 // Verify there is a following comma.
799 if (!Lexer.is(AsmToken::Comma))
800 return TokError("unexpected token in '.section' directive");
801
Chris Lattnerff4bc462009-08-10 01:39:42 +0000802 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000803 SectionSpec += ",";
804
805 // Add all the tokens until the end of the line, ParseSectionSpecifier will
806 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000807 StringRef EOL = Lexer.LexUntilEndOfStatement();
808 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000809
Chris Lattnerff4bc462009-08-10 01:39:42 +0000810 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000811 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000812 return TokError("unexpected token in '.section' directive");
813 Lexer.Lex();
814
Chris Lattnerff4bc462009-08-10 01:39:42 +0000815
816 StringRef Segment, Section;
817 unsigned TAA, StubSize;
818 std::string ErrorStr =
819 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
820 TAA, StubSize);
821
822 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000823 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000824
Chris Lattner56594f92009-07-31 17:47:16 +0000825 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000826 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
827 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000828 return false;
829}
830
Chris Lattnere15c2d72009-08-10 18:05:55 +0000831/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000832bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
833 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000834 unsigned TAA, unsigned Align,
835 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000836 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000837 return TokError("unexpected token in section switching directive");
838 Lexer.Lex();
839
Chris Lattner56594f92009-07-31 17:47:16 +0000840 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000841 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
842 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000843
844 // Set the implicit alignment, if any.
845 //
846 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
847 // alignment on the section (e.g., if one manually inserts bytes into the
848 // section, then just issueing the section switch directive will not realign
849 // the section. However, this is arguably more reasonable behavior, and there
850 // is no good reason for someone to intentionally emit incorrectly sized
851 // values into the implicitly aligned sections.
852 if (Align)
853 Out.EmitValueToAlignment(Align, 0, 1, 0);
854
Chris Lattner529fb542009-06-24 05:13:15 +0000855 return false;
856}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000857
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000858bool AsmParser::ParseEscapedString(std::string &Data) {
859 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
860
861 Data = "";
862 StringRef Str = Lexer.getTok().getStringContents();
863 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
864 if (Str[i] != '\\') {
865 Data += Str[i];
866 continue;
867 }
868
869 // Recognize escaped characters. Note that this escape semantics currently
870 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
871 ++i;
872 if (i == e)
873 return TokError("unexpected backslash at end of string");
874
875 // Recognize octal sequences.
876 if ((unsigned) (Str[i] - '0') <= 7) {
877 // Consume up to three octal characters.
878 unsigned Value = Str[i] - '0';
879
880 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
881 ++i;
882 Value = Value * 8 + (Str[i] - '0');
883
884 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
885 ++i;
886 Value = Value * 8 + (Str[i] - '0');
887 }
888 }
889
890 if (Value > 255)
891 return TokError("invalid octal escape sequence (out of range)");
892
893 Data += (unsigned char) Value;
894 continue;
895 }
896
897 // Otherwise recognize individual escapes.
898 switch (Str[i]) {
899 default:
900 // Just reject invalid escape sequences for now.
901 return TokError("invalid escape sequence (unrecognized character)");
902
903 case 'b': Data += '\b'; break;
904 case 'f': Data += '\f'; break;
905 case 'n': Data += '\n'; break;
906 case 'r': Data += '\r'; break;
907 case 't': Data += '\t'; break;
908 case '"': Data += '"'; break;
909 case '\\': Data += '\\'; break;
910 }
911 }
912
913 return false;
914}
915
Daniel Dunbara0d14262009-06-24 23:30:00 +0000916/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000917/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000918bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000919 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000920 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000921 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000922 return TokError("expected string in '.ascii' or '.asciz' directive");
923
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000924 std::string Data;
925 if (ParseEscapedString(Data))
926 return true;
927
928 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000930 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000931
932 Lexer.Lex();
933
Daniel Dunbar3f872332009-07-28 16:08:33 +0000934 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000935 break;
936
Daniel Dunbar3f872332009-07-28 16:08:33 +0000937 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000938 return TokError("unexpected token in '.ascii' or '.asciz' directive");
939 Lexer.Lex();
940 }
941 }
942
943 Lexer.Lex();
944 return false;
945}
946
947/// ParseDirectiveValue
948/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
949bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000950 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000951 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000952 MCValue Expr;
953 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000954 return true;
955
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000956 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000957
Daniel Dunbar3f872332009-07-28 16:08:33 +0000958 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959 break;
960
961 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000962 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963 return TokError("unexpected token in directive");
964 Lexer.Lex();
965 }
966 }
967
968 Lexer.Lex();
969 return false;
970}
971
972/// ParseDirectiveSpace
973/// ::= .space expression [ , expression ]
974bool AsmParser::ParseDirectiveSpace() {
975 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000976 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977 return true;
978
979 int64_t FillExpr = 0;
980 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000981 if (Lexer.isNot(AsmToken::EndOfStatement)) {
982 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000983 return TokError("unexpected token in '.space' directive");
984 Lexer.Lex();
985
Daniel Dunbar475839e2009-06-29 20:37:27 +0000986 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000987 return true;
988
989 HasFillExpr = true;
990
Daniel Dunbar3f872332009-07-28 16:08:33 +0000991 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000992 return TokError("unexpected token in '.space' directive");
993 }
994
995 Lexer.Lex();
996
997 if (NumBytes <= 0)
998 return TokError("invalid number of bytes in '.space' directive");
999
1000 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1001 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
1002 Out.EmitValue(MCValue::get(FillExpr), 1);
1003
1004 return false;
1005}
1006
1007/// ParseDirectiveFill
1008/// ::= .fill expression , expression , expression
1009bool AsmParser::ParseDirectiveFill() {
1010 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001011 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001012 return true;
1013
Daniel Dunbar3f872332009-07-28 16:08:33 +00001014 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015 return TokError("unexpected token in '.fill' directive");
1016 Lexer.Lex();
1017
1018 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001019 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 return true;
1021
Daniel Dunbar3f872332009-07-28 16:08:33 +00001022 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023 return TokError("unexpected token in '.fill' directive");
1024 Lexer.Lex();
1025
1026 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001027 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 return true;
1029
Daniel Dunbar3f872332009-07-28 16:08:33 +00001030 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001031 return TokError("unexpected token in '.fill' directive");
1032
1033 Lexer.Lex();
1034
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001035 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1036 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037
1038 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1039 Out.EmitValue(MCValue::get(FillExpr), FillSize);
1040
1041 return false;
1042}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001043
1044/// ParseDirectiveOrg
1045/// ::= .org expression [ , expression ]
1046bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001047 MCValue Offset;
1048 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001049 return true;
1050
1051 // Parse optional fill expression.
1052 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1054 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001055 return TokError("unexpected token in '.org' directive");
1056 Lexer.Lex();
1057
Daniel Dunbar475839e2009-06-29 20:37:27 +00001058 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001059 return true;
1060
Daniel Dunbar3f872332009-07-28 16:08:33 +00001061 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001062 return TokError("unexpected token in '.org' directive");
1063 }
1064
1065 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001066
1067 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1068 // has to be relative to the current section.
1069 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001070
1071 return false;
1072}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001073
1074/// ParseDirectiveAlign
1075/// ::= {.align, ...} expression [ , expression [ , expression ]]
1076bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001077 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001078 int64_t Alignment;
1079 if (ParseAbsoluteExpression(Alignment))
1080 return true;
1081
1082 SMLoc MaxBytesLoc;
1083 bool HasFillExpr = false;
1084 int64_t FillExpr = 0;
1085 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001086 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1087 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001088 return TokError("unexpected token in directive");
1089 Lexer.Lex();
1090
1091 // The fill expression can be omitted while specifying a maximum number of
1092 // alignment bytes, e.g:
1093 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001094 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001095 HasFillExpr = true;
1096 if (ParseAbsoluteExpression(FillExpr))
1097 return true;
1098 }
1099
Daniel Dunbar3f872332009-07-28 16:08:33 +00001100 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1101 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001102 return TokError("unexpected token in directive");
1103 Lexer.Lex();
1104
1105 MaxBytesLoc = Lexer.getLoc();
1106 if (ParseAbsoluteExpression(MaxBytesToFill))
1107 return true;
1108
Daniel Dunbar3f872332009-07-28 16:08:33 +00001109 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001110 return TokError("unexpected token in directive");
1111 }
1112 }
1113
1114 Lexer.Lex();
1115
1116 if (!HasFillExpr) {
1117 // FIXME: Sometimes fill with nop.
1118 FillExpr = 0;
1119 }
1120
1121 // Compute alignment in bytes.
1122 if (IsPow2) {
1123 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001124 if (Alignment >= 32) {
1125 Error(AlignmentLoc, "invalid alignment value");
1126 Alignment = 31;
1127 }
1128
1129 Alignment = 1 << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001130 }
1131
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001132 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001133 if (MaxBytesLoc.isValid()) {
1134 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001135 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1136 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001137 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001138 }
1139
1140 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001141 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1142 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001143 MaxBytesToFill = 0;
1144 }
1145 }
1146
1147 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1148 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1149
1150 return false;
1151}
1152
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001153/// ParseDirectiveSymbolAttribute
1154/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1155bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001156 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001157 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001158 StringRef Name;
1159
1160 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001161 return TokError("expected identifier in directive");
1162
Daniel Dunbar959fd882009-08-26 22:13:22 +00001163 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001164
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001165 Out.EmitSymbolAttribute(Sym, Attr);
1166
Daniel Dunbar3f872332009-07-28 16:08:33 +00001167 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001168 break;
1169
Daniel Dunbar3f872332009-07-28 16:08:33 +00001170 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001171 return TokError("unexpected token in directive");
1172 Lexer.Lex();
1173 }
1174 }
1175
1176 Lexer.Lex();
1177 return false;
1178}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001179
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001180/// ParseDirectiveDarwinSymbolDesc
1181/// ::= .desc identifier , expression
1182bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001183 StringRef Name;
1184 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001185 return TokError("expected identifier in directive");
1186
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001187 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001188 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001189
Daniel Dunbar3f872332009-07-28 16:08:33 +00001190 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001191 return TokError("unexpected token in '.desc' directive");
1192 Lexer.Lex();
1193
1194 SMLoc DescLoc = Lexer.getLoc();
1195 int64_t DescValue;
1196 if (ParseAbsoluteExpression(DescValue))
1197 return true;
1198
Daniel Dunbar3f872332009-07-28 16:08:33 +00001199 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001200 return TokError("unexpected token in '.desc' directive");
1201
1202 Lexer.Lex();
1203
1204 // Set the n_desc field of this Symbol to this DescValue
1205 Out.EmitSymbolDesc(Sym, DescValue);
1206
1207 return false;
1208}
1209
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001210/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001211/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1212bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001213 SMLoc IDLoc = Lexer.getLoc();
1214 StringRef Name;
1215 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001216 return TokError("expected identifier in directive");
1217
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001218 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001219 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001220
Daniel Dunbar3f872332009-07-28 16:08:33 +00001221 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001222 return TokError("unexpected token in directive");
1223 Lexer.Lex();
1224
1225 int64_t Size;
1226 SMLoc SizeLoc = Lexer.getLoc();
1227 if (ParseAbsoluteExpression(Size))
1228 return true;
1229
1230 int64_t Pow2Alignment = 0;
1231 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001232 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001233 Lexer.Lex();
1234 Pow2AlignmentLoc = Lexer.getLoc();
1235 if (ParseAbsoluteExpression(Pow2Alignment))
1236 return true;
1237 }
1238
Daniel Dunbar3f872332009-07-28 16:08:33 +00001239 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001240 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001241
1242 Lexer.Lex();
1243
Chris Lattner1fc3d752009-07-09 17:25:12 +00001244 // NOTE: a size of zero for a .comm should create a undefined symbol
1245 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001246 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001247 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1248 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001249
1250 // NOTE: The alignment in the directive is a power of 2 value, the assember
1251 // may internally end up wanting an alignment in bytes.
1252 // FIXME: Diagnose overflow.
1253 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001254 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1255 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001256
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001257 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001258 return Error(IDLoc, "invalid symbol redefinition");
1259
Chris Lattner1fc3d752009-07-09 17:25:12 +00001260 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001261 if (IsLocal)
1262 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1263 MCSectionMachO::S_ZEROFILL, 0,
1264 SectionKind()),
1265 Sym, Size, Pow2Alignment);
1266 else
1267 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001268
1269 return false;
1270}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001271
1272/// ParseDirectiveDarwinZerofill
1273/// ::= .zerofill segname , sectname [, identifier , size_expression [
1274/// , align_expression ]]
1275bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001276 // FIXME: Handle quoted names here.
1277
Daniel Dunbar3f872332009-07-28 16:08:33 +00001278 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001279 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001280 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001281 Lexer.Lex();
1282
Daniel Dunbar3f872332009-07-28 16:08:33 +00001283 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001284 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001285 Lexer.Lex();
1286
Daniel Dunbar3f872332009-07-28 16:08:33 +00001287 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001288 return TokError("expected section name after comma in '.zerofill' "
1289 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001290 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001291 Lexer.Lex();
1292
Chris Lattner9be3fee2009-07-10 22:20:30 +00001293 // If this is the end of the line all that was wanted was to create the
1294 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001295 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001296 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001297 Out.EmitZerofill(getMachOSection(Segment, Section,
1298 MCSectionMachO::S_ZEROFILL, 0,
1299 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001300 return false;
1301 }
1302
Daniel Dunbar3f872332009-07-28 16:08:33 +00001303 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001304 return TokError("unexpected token in directive");
1305 Lexer.Lex();
1306
Daniel Dunbar3f872332009-07-28 16:08:33 +00001307 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001308 return TokError("expected identifier in directive");
1309
1310 // handle the identifier as the key symbol.
1311 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001312 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001313 Lexer.Lex();
1314
Daniel Dunbar3f872332009-07-28 16:08:33 +00001315 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001316 return TokError("unexpected token in directive");
1317 Lexer.Lex();
1318
1319 int64_t Size;
1320 SMLoc SizeLoc = Lexer.getLoc();
1321 if (ParseAbsoluteExpression(Size))
1322 return true;
1323
1324 int64_t Pow2Alignment = 0;
1325 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001326 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001327 Lexer.Lex();
1328 Pow2AlignmentLoc = Lexer.getLoc();
1329 if (ParseAbsoluteExpression(Pow2Alignment))
1330 return true;
1331 }
1332
Daniel Dunbar3f872332009-07-28 16:08:33 +00001333 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001334 return TokError("unexpected token in '.zerofill' directive");
1335
1336 Lexer.Lex();
1337
1338 if (Size < 0)
1339 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1340 "than zero");
1341
1342 // NOTE: The alignment in the directive is a power of 2 value, the assember
1343 // may internally end up wanting an alignment in bytes.
1344 // FIXME: Diagnose overflow.
1345 if (Pow2Alignment < 0)
1346 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1347 "can't be less than zero");
1348
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001349 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001350 return Error(IDLoc, "invalid symbol redefinition");
1351
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001352 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001353 //
1354 // FIXME: Arch specific.
1355 Out.EmitZerofill(getMachOSection(Segment, Section,
1356 MCSectionMachO::S_ZEROFILL, 0,
1357 SectionKind()),
1358 Sym, Size, Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001359
1360 return false;
1361}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001362
1363/// ParseDirectiveDarwinSubsectionsViaSymbols
1364/// ::= .subsections_via_symbols
1365bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001366 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001367 return TokError("unexpected token in '.subsections_via_symbols' directive");
1368
1369 Lexer.Lex();
1370
Kevin Enderbyf96db462009-07-16 17:56:39 +00001371 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001372
1373 return false;
1374}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001375
1376/// ParseDirectiveAbort
1377/// ::= .abort [ "abort_string" ]
1378bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001379 // FIXME: Use loc from directive.
1380 SMLoc Loc = Lexer.getLoc();
1381
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001382 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001383 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1384 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001385 return TokError("expected string in '.abort' directive");
1386
Daniel Dunbar419aded2009-07-28 16:38:40 +00001387 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001388
1389 Lexer.Lex();
1390 }
1391
Daniel Dunbar3f872332009-07-28 16:08:33 +00001392 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001393 return TokError("unexpected token in '.abort' directive");
1394
1395 Lexer.Lex();
1396
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001397 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001398 if (Str.empty())
1399 Error(Loc, ".abort detected. Assembly stopping.");
1400 else
1401 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001402
1403 return false;
1404}
Kevin Enderby71148242009-07-14 21:35:03 +00001405
1406/// ParseDirectiveLsym
1407/// ::= .lsym identifier , expression
1408bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001409 StringRef Name;
1410 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001411 return TokError("expected identifier in directive");
1412
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001413 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001414 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001415
Daniel Dunbar3f872332009-07-28 16:08:33 +00001416 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001417 return TokError("unexpected token in '.lsym' directive");
1418 Lexer.Lex();
1419
1420 MCValue Expr;
1421 if (ParseRelocatableExpression(Expr))
1422 return true;
1423
Daniel Dunbar3f872332009-07-28 16:08:33 +00001424 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001425 return TokError("unexpected token in '.lsym' directive");
1426
1427 Lexer.Lex();
1428
1429 // Create the Sym with the value of the Expr
1430 Out.EmitLocalSymbol(Sym, Expr);
1431
1432 return false;
1433}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001434
1435/// ParseDirectiveInclude
1436/// ::= .include "filename"
1437bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001438 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001439 return TokError("expected string in '.include' directive");
1440
Daniel Dunbar419aded2009-07-28 16:38:40 +00001441 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001442 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001443 Lexer.Lex();
1444
Daniel Dunbar3f872332009-07-28 16:08:33 +00001445 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001446 return TokError("unexpected token in '.include' directive");
1447
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001448 // Strip the quotes.
1449 Filename = Filename.substr(1, Filename.size()-2);
1450
1451 // Attempt to switch the lexer to the included file before consuming the end
1452 // of statement to avoid losing it when we switch.
1453 if (Lexer.EnterIncludeFile(Filename)) {
1454 Lexer.PrintMessage(IncludeLoc,
1455 "Could not find include file '" + Filename + "'",
1456 "error");
1457 return true;
1458 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001459
1460 return false;
1461}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001462
1463/// ParseDirectiveDarwinDumpOrLoad
1464/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001465bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001466 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001467 return TokError("expected string in '.dump' or '.load' directive");
1468
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001469 Lexer.Lex();
1470
Daniel Dunbar3f872332009-07-28 16:08:33 +00001471 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001472 return TokError("unexpected token in '.dump' or '.load' directive");
1473
1474 Lexer.Lex();
1475
Kevin Enderby5026ae42009-07-20 20:25:37 +00001476 // FIXME: If/when .dump and .load are implemented they will be done in the
1477 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001478 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001479 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001480 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001481 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001482
1483 return false;
1484}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001485
1486/// ParseDirectiveIf
1487/// ::= .if expression
1488bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1489 // Consume the identifier that was the .if directive
1490 Lexer.Lex();
1491
1492 TheCondStack.push_back(TheCondState);
1493 TheCondState.TheCond = AsmCond::IfCond;
1494 if(TheCondState.Ignore) {
1495 EatToEndOfStatement();
1496 }
1497 else {
1498 int64_t ExprValue;
1499 if (ParseAbsoluteExpression(ExprValue))
1500 return true;
1501
1502 if (Lexer.isNot(AsmToken::EndOfStatement))
1503 return TokError("unexpected token in '.if' directive");
1504
1505 Lexer.Lex();
1506
1507 TheCondState.CondMet = ExprValue;
1508 TheCondState.Ignore = !TheCondState.CondMet;
1509 }
1510
1511 return false;
1512}
1513
1514/// ParseDirectiveElseIf
1515/// ::= .elseif expression
1516bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1517 if (TheCondState.TheCond != AsmCond::IfCond &&
1518 TheCondState.TheCond != AsmCond::ElseIfCond)
1519 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1520 " an .elseif");
1521 TheCondState.TheCond = AsmCond::ElseIfCond;
1522
1523 // Consume the identifier that was the .elseif directive
1524 Lexer.Lex();
1525
1526 bool LastIgnoreState = false;
1527 if (!TheCondStack.empty())
1528 LastIgnoreState = TheCondStack.back().Ignore;
1529 if (LastIgnoreState || TheCondState.CondMet) {
1530 TheCondState.Ignore = true;
1531 EatToEndOfStatement();
1532 }
1533 else {
1534 int64_t ExprValue;
1535 if (ParseAbsoluteExpression(ExprValue))
1536 return true;
1537
1538 if (Lexer.isNot(AsmToken::EndOfStatement))
1539 return TokError("unexpected token in '.elseif' directive");
1540
1541 Lexer.Lex();
1542 TheCondState.CondMet = ExprValue;
1543 TheCondState.Ignore = !TheCondState.CondMet;
1544 }
1545
1546 return false;
1547}
1548
1549/// ParseDirectiveElse
1550/// ::= .else
1551bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1552 // Consume the identifier that was the .else directive
1553 Lexer.Lex();
1554
1555 if (Lexer.isNot(AsmToken::EndOfStatement))
1556 return TokError("unexpected token in '.else' directive");
1557
1558 Lexer.Lex();
1559
1560 if (TheCondState.TheCond != AsmCond::IfCond &&
1561 TheCondState.TheCond != AsmCond::ElseIfCond)
1562 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1563 ".elseif");
1564 TheCondState.TheCond = AsmCond::ElseCond;
1565 bool LastIgnoreState = false;
1566 if (!TheCondStack.empty())
1567 LastIgnoreState = TheCondStack.back().Ignore;
1568 if (LastIgnoreState || TheCondState.CondMet)
1569 TheCondState.Ignore = true;
1570 else
1571 TheCondState.Ignore = false;
1572
1573 return false;
1574}
1575
1576/// ParseDirectiveEndIf
1577/// ::= .endif
1578bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1579 // Consume the identifier that was the .endif directive
1580 Lexer.Lex();
1581
1582 if (Lexer.isNot(AsmToken::EndOfStatement))
1583 return TokError("unexpected token in '.endif' directive");
1584
1585 Lexer.Lex();
1586
1587 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1588 TheCondStack.empty())
1589 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1590 ".else");
1591 if (!TheCondStack.empty()) {
1592 TheCondState = TheCondStack.back();
1593 TheCondStack.pop_back();
1594 }
1595
1596 return false;
1597}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001598
1599/// ParseDirectiveFile
1600/// ::= .file [number] string
1601bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1602 // FIXME: I'm not sure what this is.
1603 int64_t FileNumber = -1;
1604 if (Lexer.is(AsmToken::Integer)) {
1605 FileNumber = Lexer.getTok().getIntVal();
1606 Lexer.Lex();
1607
1608 if (FileNumber < 1)
1609 return TokError("file number less than one");
1610 }
1611
1612 if (Lexer.isNot(AsmToken::String))
1613 return TokError("unexpected token in '.file' directive");
1614
1615 StringRef FileName = Lexer.getTok().getString();
1616 Lexer.Lex();
1617
1618 if (Lexer.isNot(AsmToken::EndOfStatement))
1619 return TokError("unexpected token in '.file' directive");
1620
1621 // FIXME: Do something with the .file.
1622
1623 return false;
1624}
1625
1626/// ParseDirectiveLine
1627/// ::= .line [number]
1628bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1629 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1630 if (Lexer.isNot(AsmToken::Integer))
1631 return TokError("unexpected token in '.line' directive");
1632
1633 int64_t LineNumber = Lexer.getTok().getIntVal();
1634 (void) LineNumber;
1635 Lexer.Lex();
1636
1637 // FIXME: Do something with the .line.
1638 }
1639
1640 if (Lexer.isNot(AsmToken::EndOfStatement))
1641 return TokError("unexpected token in '.file' directive");
1642
1643 return false;
1644}
1645
1646
1647/// ParseDirectiveLoc
1648/// ::= .loc number [number [number]]
1649bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1650 if (Lexer.isNot(AsmToken::Integer))
1651 return TokError("unexpected token in '.loc' directive");
1652
1653 // FIXME: What are these fields?
1654 int64_t FileNumber = Lexer.getTok().getIntVal();
1655 (void) FileNumber;
1656 // FIXME: Validate file.
1657
1658 Lexer.Lex();
1659 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1660 if (Lexer.isNot(AsmToken::Integer))
1661 return TokError("unexpected token in '.loc' directive");
1662
1663 int64_t Param2 = Lexer.getTok().getIntVal();
1664 (void) Param2;
1665 Lexer.Lex();
1666
1667 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1668 if (Lexer.isNot(AsmToken::Integer))
1669 return TokError("unexpected token in '.loc' directive");
1670
1671 int64_t Param3 = Lexer.getTok().getIntVal();
1672 (void) Param3;
1673 Lexer.Lex();
1674
1675 // FIXME: Do something with the .loc.
1676 }
1677 }
1678
1679 if (Lexer.isNot(AsmToken::EndOfStatement))
1680 return TokError("unexpected token in '.file' directive");
1681
1682 return false;
1683}
1684