blob: 5d73a9fa5401c042d3adb6cea140c160cbaf92d4 [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
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.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 Dunbar9643ac52009-08-31 08:07:22 +0000176bool AsmParser::ParseParenExpr(const MCExpr *&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 Dunbar9643ac52009-08-31 08:07:22 +0000200bool AsmParser::ParsePrimaryExpr(const MCExpr *&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;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000208 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000209 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
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000216 Res = MCSymbolRefExpr::Create(Sym, getContext());
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 Dunbar6ce004d2009-08-31 08:07:44 +0000221 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
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;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000231 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000232 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;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000237 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000238 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;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000243 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000244 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 Dunbar9643ac52009-08-31 08:07:22 +0000255bool AsmParser::ParseExpression(const MCExpr *&Res) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 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) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000262 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263
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) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000275 const MCExpr *Expr;
Daniel Dunbar15d17072009-06-30 01:49:52 +0000276
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) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000288 const MCExpr *Expr;
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000289
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 Dunbar28c251b2009-08-31 08:06:59 +0000301 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000302 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000303 default:
304 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305
306 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000307 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000308 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000309 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000310 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000311 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000312 return 1;
313
314 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000315 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000316 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000317 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000318 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000319 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000320 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000321 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000322 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000323 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000324 case AsmToken::ExclaimEqual:
325 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000326 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000327 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000328 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000329 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000330 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000331 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000332 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000334 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000335 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000336 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000337 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000338 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000339 return 2;
340
341 // Intermediate Precedence: |, &, ^
342 //
343 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000344 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000345 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000346 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000347 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000348 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000349 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000350 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000351 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 return 3;
353
354 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000357 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000358 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000359 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000361 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000362 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000363 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000364 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000365 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000366 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000367 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000368 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000369 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000370 }
371}
372
373
374/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
375/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000376bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000377 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000378 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000379 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000380
381 // If the next token is lower precedence than we are allowed to eat, return
382 // successfully with what we ate already.
383 if (TokPrec < Precedence)
384 return false;
385
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000386 Lexer.Lex();
387
388 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000389 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000390 if (ParsePrimaryExpr(RHS)) return true;
391
392 // If BinOp binds less tightly with RHS than the operator after RHS, let
393 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000394 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000395 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000396 if (TokPrec < NextTokPrec) {
397 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
398 }
399
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000401 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000402 }
403}
404
Chris Lattnerc4193832009-06-22 05:51:26 +0000405
406
407
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000408/// ParseStatement:
409/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000410/// ::= Label* Directive ...Operands... EndOfStatement
411/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000412bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000413 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000414 Lexer.Lex();
415 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000416 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000417
418 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000419 AsmToken ID = Lexer.getTok();
420 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000421 StringRef IDVal;
422 if (ParseIdentifier(IDVal))
423 return TokError("unexpected token at start of statement");
424
425 // FIXME: Recurse on local labels?
426
427 // See what kind of statement we have.
428 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000429 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000430 // identifier ':' -> Label.
431 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000432
433 // Diagnose attempt to use a variable as a label.
434 //
435 // FIXME: Diagnostics. Note the location of the definition as a label.
436 // FIXME: This doesn't diagnose assignment to a symbol which has been
437 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000438 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000439 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000440 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000441
Daniel Dunbar959fd882009-08-26 22:13:22 +0000442 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000443 Out.EmitLabel(Sym);
444
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000445 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000446 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000447
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000449 // identifier '=' ... -> assignment statement
450 Lexer.Lex();
451
452 return ParseAssignment(IDVal, false);
453
454 default: // Normal instruction or directive.
455 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000456 }
457
458 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000459 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000460 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000461 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000462 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000463 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000464 // FIXME: This changes behavior based on the -static flag to the
465 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000466 return ParseDirectiveSectionSwitch("__TEXT", "__text",
467 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000468 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000469 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000471 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000473 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
474 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000475 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000476 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000477 MCSectionMachO::S_4BYTE_LITERALS,
478 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000479 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000480 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000481 MCSectionMachO::S_8BYTE_LITERALS,
482 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000483 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000484 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000485 MCSectionMachO::S_16BYTE_LITERALS,
486 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000488 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000489 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000490 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000491 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000492 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000494 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
495
496 // FIXME: The assembler manual claims that this has the self modify code
497 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000498 if (IDVal == ".symbol_stub")
499 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
500 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000501 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
502 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000503 0, 16);
504 // FIXME: PowerPC only?
505 if (IDVal == ".picsymbol_stub")
506 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
507 MCSectionMachO::S_SYMBOL_STUBS |
508 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
509 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000510 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000511 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000513 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
514
515 // FIXME: The section names of these two are misspelled in the assembler
516 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000518 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
519 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
520 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000521 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000522 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
523 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
524 4);
525
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000527 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000529 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000530 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
531 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000533 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
535 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000538
539
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000541 return ParseDirectiveSectionSwitch("__OBJC", "__class",
542 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
545 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000547 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
548 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000549 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000550 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
551 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000553 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
554 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
557 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
560 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000562 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
567 MCSectionMachO::S_LITERAL_POINTERS,
568 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
571 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
572 MCSectionMachO::S_LITERAL_POINTERS,
573 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000575 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
576 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000577 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000578 return ParseDirectiveSectionSwitch("__OBJC", "__category",
579 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000580 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000581 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
582 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000584 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
585 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000586 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000587 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
588 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000590 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
591 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000593 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
594 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000596 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
597 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
600 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000601
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000602 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000604 return ParseDirectiveSet();
605
Daniel Dunbara0d14262009-06-24 23:30:00 +0000606 // Data directives
607
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000608 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000609 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000611 return ParseDirectiveAscii(true);
612
613 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000615 return ParseDirectiveValue(1);
Daniel Dunbar1e840b22009-08-11 04:44:00 +0000616 if (IDVal == ".short" || IDVal == ".word")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000617 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000619 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000621 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000622
623 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000625 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000627 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000629 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000631 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000633 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000635 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000637 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000639 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
640
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000642 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000643
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000645 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000647 return ParseDirectiveSpace();
648
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000649 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000650
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000652 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000654 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000656 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000658 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000660 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000662 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000664 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000666 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000668 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000670 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000672 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000674 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
675
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000677 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000679 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000681 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000683 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000685 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000686
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000688 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000690 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000692 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000694 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000696 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000697
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000698 // Debugging directives
699
700 if (IDVal == ".file")
701 return ParseDirectiveFile(IDLoc);
702 if (IDVal == ".line")
703 return ParseDirectiveLine(IDLoc);
704 if (IDVal == ".loc")
705 return ParseDirectiveLoc(IDLoc);
706
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000707 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000708 EatToEndOfStatement();
709 return false;
710 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000711
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000712 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000713 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000714 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000715
Daniel Dunbar3f872332009-07-28 16:08:33 +0000716 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000717 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000718
719 // Eat the end of statement marker.
720 Lexer.Lex();
721
722 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000723 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000724
725 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000726 return false;
727}
Chris Lattner9a023f72009-06-24 04:43:34 +0000728
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000730 // FIXME: Use better location, we should use proper tokens.
731 SMLoc EqualLoc = Lexer.getLoc();
732
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000733 MCValue Value;
734 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000735 return true;
736
Daniel Dunbar3f872332009-07-28 16:08:33 +0000737 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000738 return TokError("unexpected token in assignment");
739
740 // Eat the end of statement marker.
741 Lexer.Lex();
742
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000743 // Diagnose assignment to a label.
744 //
745 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000746 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000747 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar959fd882009-08-26 22:13:22 +0000748 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000749 if (!Sym->isUndefined() && !Sym->isAbsolute())
750 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000751
752 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000753 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000754
755 return false;
756}
757
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000758/// ParseIdentifier:
759/// ::= identifier
760/// ::= string
761bool AsmParser::ParseIdentifier(StringRef &Res) {
762 if (Lexer.isNot(AsmToken::Identifier) &&
763 Lexer.isNot(AsmToken::String))
764 return true;
765
766 Res = Lexer.getTok().getIdentifier();
767
768 Lexer.Lex(); // Consume the identifier token.
769
770 return false;
771}
772
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000773/// ParseDirectiveSet:
774/// ::= .set identifier ',' expression
775bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000776 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000777
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000778 if (ParseIdentifier(Name))
779 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000780
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000781 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000782 return TokError("unexpected token in '.set'");
783 Lexer.Lex();
784
785 return ParseAssignment(Name, true);
786}
787
Chris Lattner9a023f72009-06-24 04:43:34 +0000788/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000789/// ::= .section identifier (',' identifier)*
790/// FIXME: This should actually parse out the segment, section, attributes and
791/// sizeof_stub fields.
792bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000793 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000794
Daniel Dunbarace63122009-08-11 03:42:33 +0000795 StringRef SectionName;
796 if (ParseIdentifier(SectionName))
797 return Error(Loc, "expected identifier after '.section' directive");
798
799 // Verify there is a following comma.
800 if (!Lexer.is(AsmToken::Comma))
801 return TokError("unexpected token in '.section' directive");
802
Chris Lattnerff4bc462009-08-10 01:39:42 +0000803 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000804 SectionSpec += ",";
805
806 // Add all the tokens until the end of the line, ParseSectionSpecifier will
807 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000808 StringRef EOL = Lexer.LexUntilEndOfStatement();
809 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000810
Chris Lattnerff4bc462009-08-10 01:39:42 +0000811 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000812 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000813 return TokError("unexpected token in '.section' directive");
814 Lexer.Lex();
815
Chris Lattnerff4bc462009-08-10 01:39:42 +0000816
817 StringRef Segment, Section;
818 unsigned TAA, StubSize;
819 std::string ErrorStr =
820 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
821 TAA, StubSize);
822
823 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000824 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000825
Chris Lattner56594f92009-07-31 17:47:16 +0000826 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000827 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
828 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000829 return false;
830}
831
Chris Lattnere15c2d72009-08-10 18:05:55 +0000832/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000833bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
834 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000835 unsigned TAA, unsigned Align,
836 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000837 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000838 return TokError("unexpected token in section switching directive");
839 Lexer.Lex();
840
Chris Lattner56594f92009-07-31 17:47:16 +0000841 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000842 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
843 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000844
845 // Set the implicit alignment, if any.
846 //
847 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
848 // alignment on the section (e.g., if one manually inserts bytes into the
849 // section, then just issueing the section switch directive will not realign
850 // the section. However, this is arguably more reasonable behavior, and there
851 // is no good reason for someone to intentionally emit incorrectly sized
852 // values into the implicitly aligned sections.
853 if (Align)
854 Out.EmitValueToAlignment(Align, 0, 1, 0);
855
Chris Lattner529fb542009-06-24 05:13:15 +0000856 return false;
857}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000858
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000859bool AsmParser::ParseEscapedString(std::string &Data) {
860 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
861
862 Data = "";
863 StringRef Str = Lexer.getTok().getStringContents();
864 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
865 if (Str[i] != '\\') {
866 Data += Str[i];
867 continue;
868 }
869
870 // Recognize escaped characters. Note that this escape semantics currently
871 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
872 ++i;
873 if (i == e)
874 return TokError("unexpected backslash at end of string");
875
876 // Recognize octal sequences.
877 if ((unsigned) (Str[i] - '0') <= 7) {
878 // Consume up to three octal characters.
879 unsigned Value = Str[i] - '0';
880
881 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
882 ++i;
883 Value = Value * 8 + (Str[i] - '0');
884
885 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
886 ++i;
887 Value = Value * 8 + (Str[i] - '0');
888 }
889 }
890
891 if (Value > 255)
892 return TokError("invalid octal escape sequence (out of range)");
893
894 Data += (unsigned char) Value;
895 continue;
896 }
897
898 // Otherwise recognize individual escapes.
899 switch (Str[i]) {
900 default:
901 // Just reject invalid escape sequences for now.
902 return TokError("invalid escape sequence (unrecognized character)");
903
904 case 'b': Data += '\b'; break;
905 case 'f': Data += '\f'; break;
906 case 'n': Data += '\n'; break;
907 case 'r': Data += '\r'; break;
908 case 't': Data += '\t'; break;
909 case '"': Data += '"'; break;
910 case '\\': Data += '\\'; break;
911 }
912 }
913
914 return false;
915}
916
Daniel Dunbara0d14262009-06-24 23:30:00 +0000917/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000918/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000919bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000920 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000921 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000922 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923 return TokError("expected string in '.ascii' or '.asciz' directive");
924
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000925 std::string Data;
926 if (ParseEscapedString(Data))
927 return true;
928
929 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000930 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000931 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000932
933 Lexer.Lex();
934
Daniel Dunbar3f872332009-07-28 16:08:33 +0000935 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000936 break;
937
Daniel Dunbar3f872332009-07-28 16:08:33 +0000938 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000939 return TokError("unexpected token in '.ascii' or '.asciz' directive");
940 Lexer.Lex();
941 }
942 }
943
944 Lexer.Lex();
945 return false;
946}
947
948/// ParseDirectiveValue
949/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
950bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000952 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000953 MCValue Expr;
954 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000955 return true;
956
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000957 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000958
Daniel Dunbar3f872332009-07-28 16:08:33 +0000959 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960 break;
961
962 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964 return TokError("unexpected token in directive");
965 Lexer.Lex();
966 }
967 }
968
969 Lexer.Lex();
970 return false;
971}
972
973/// ParseDirectiveSpace
974/// ::= .space expression [ , expression ]
975bool AsmParser::ParseDirectiveSpace() {
976 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000977 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000978 return true;
979
980 int64_t FillExpr = 0;
981 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000982 if (Lexer.isNot(AsmToken::EndOfStatement)) {
983 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000984 return TokError("unexpected token in '.space' directive");
985 Lexer.Lex();
986
Daniel Dunbar475839e2009-06-29 20:37:27 +0000987 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000988 return true;
989
990 HasFillExpr = true;
991
Daniel Dunbar3f872332009-07-28 16:08:33 +0000992 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000993 return TokError("unexpected token in '.space' directive");
994 }
995
996 Lexer.Lex();
997
998 if (NumBytes <= 0)
999 return TokError("invalid number of bytes in '.space' directive");
1000
1001 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1002 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
1003 Out.EmitValue(MCValue::get(FillExpr), 1);
1004
1005 return false;
1006}
1007
1008/// ParseDirectiveFill
1009/// ::= .fill expression , expression , expression
1010bool AsmParser::ParseDirectiveFill() {
1011 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001012 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013 return true;
1014
Daniel Dunbar3f872332009-07-28 16:08:33 +00001015 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001016 return TokError("unexpected token in '.fill' directive");
1017 Lexer.Lex();
1018
1019 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001020 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 return true;
1022
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 return TokError("unexpected token in '.fill' directive");
1025 Lexer.Lex();
1026
1027 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001028 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return true;
1030
Daniel Dunbar3f872332009-07-28 16:08:33 +00001031 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032 return TokError("unexpected token in '.fill' directive");
1033
1034 Lexer.Lex();
1035
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001036 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1037 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001038
1039 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1040 Out.EmitValue(MCValue::get(FillExpr), FillSize);
1041
1042 return false;
1043}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001044
1045/// ParseDirectiveOrg
1046/// ::= .org expression [ , expression ]
1047bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001048 MCValue Offset;
1049 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001050 return true;
1051
1052 // Parse optional fill expression.
1053 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1055 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001056 return TokError("unexpected token in '.org' directive");
1057 Lexer.Lex();
1058
Daniel Dunbar475839e2009-06-29 20:37:27 +00001059 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001060 return true;
1061
Daniel Dunbar3f872332009-07-28 16:08:33 +00001062 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001063 return TokError("unexpected token in '.org' directive");
1064 }
1065
1066 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001067
1068 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1069 // has to be relative to the current section.
1070 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001071
1072 return false;
1073}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001074
1075/// ParseDirectiveAlign
1076/// ::= {.align, ...} expression [ , expression [ , expression ]]
1077bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001078 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001079 int64_t Alignment;
1080 if (ParseAbsoluteExpression(Alignment))
1081 return true;
1082
1083 SMLoc MaxBytesLoc;
1084 bool HasFillExpr = false;
1085 int64_t FillExpr = 0;
1086 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001087 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1088 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001089 return TokError("unexpected token in directive");
1090 Lexer.Lex();
1091
1092 // The fill expression can be omitted while specifying a maximum number of
1093 // alignment bytes, e.g:
1094 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001095 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001096 HasFillExpr = true;
1097 if (ParseAbsoluteExpression(FillExpr))
1098 return true;
1099 }
1100
Daniel Dunbar3f872332009-07-28 16:08:33 +00001101 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1102 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001103 return TokError("unexpected token in directive");
1104 Lexer.Lex();
1105
1106 MaxBytesLoc = Lexer.getLoc();
1107 if (ParseAbsoluteExpression(MaxBytesToFill))
1108 return true;
1109
Daniel Dunbar3f872332009-07-28 16:08:33 +00001110 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001111 return TokError("unexpected token in directive");
1112 }
1113 }
1114
1115 Lexer.Lex();
1116
1117 if (!HasFillExpr) {
1118 // FIXME: Sometimes fill with nop.
1119 FillExpr = 0;
1120 }
1121
1122 // Compute alignment in bytes.
1123 if (IsPow2) {
1124 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001125 if (Alignment >= 32) {
1126 Error(AlignmentLoc, "invalid alignment value");
1127 Alignment = 31;
1128 }
1129
1130 Alignment = 1 << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131 }
1132
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001133 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001134 if (MaxBytesLoc.isValid()) {
1135 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001136 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1137 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001138 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001139 }
1140
1141 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001142 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1143 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001144 MaxBytesToFill = 0;
1145 }
1146 }
1147
1148 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1149 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1150
1151 return false;
1152}
1153
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001154/// ParseDirectiveSymbolAttribute
1155/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1156bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001157 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001158 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001159 StringRef Name;
1160
1161 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001162 return TokError("expected identifier in directive");
1163
Daniel Dunbar959fd882009-08-26 22:13:22 +00001164 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001165
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001166 Out.EmitSymbolAttribute(Sym, Attr);
1167
Daniel Dunbar3f872332009-07-28 16:08:33 +00001168 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001169 break;
1170
Daniel Dunbar3f872332009-07-28 16:08:33 +00001171 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001172 return TokError("unexpected token in directive");
1173 Lexer.Lex();
1174 }
1175 }
1176
1177 Lexer.Lex();
1178 return false;
1179}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001180
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001181/// ParseDirectiveDarwinSymbolDesc
1182/// ::= .desc identifier , expression
1183bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001184 StringRef Name;
1185 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001186 return TokError("expected identifier in directive");
1187
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001188 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001189 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001190
Daniel Dunbar3f872332009-07-28 16:08:33 +00001191 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001192 return TokError("unexpected token in '.desc' directive");
1193 Lexer.Lex();
1194
1195 SMLoc DescLoc = Lexer.getLoc();
1196 int64_t DescValue;
1197 if (ParseAbsoluteExpression(DescValue))
1198 return true;
1199
Daniel Dunbar3f872332009-07-28 16:08:33 +00001200 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001201 return TokError("unexpected token in '.desc' directive");
1202
1203 Lexer.Lex();
1204
1205 // Set the n_desc field of this Symbol to this DescValue
1206 Out.EmitSymbolDesc(Sym, DescValue);
1207
1208 return false;
1209}
1210
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001211/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001212/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1213bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001214 SMLoc IDLoc = Lexer.getLoc();
1215 StringRef Name;
1216 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001217 return TokError("expected identifier in directive");
1218
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001219 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001220 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001221
Daniel Dunbar3f872332009-07-28 16:08:33 +00001222 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001223 return TokError("unexpected token in directive");
1224 Lexer.Lex();
1225
1226 int64_t Size;
1227 SMLoc SizeLoc = Lexer.getLoc();
1228 if (ParseAbsoluteExpression(Size))
1229 return true;
1230
1231 int64_t Pow2Alignment = 0;
1232 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001233 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001234 Lexer.Lex();
1235 Pow2AlignmentLoc = Lexer.getLoc();
1236 if (ParseAbsoluteExpression(Pow2Alignment))
1237 return true;
1238 }
1239
Daniel Dunbar3f872332009-07-28 16:08:33 +00001240 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001241 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001242
1243 Lexer.Lex();
1244
Chris Lattner1fc3d752009-07-09 17:25:12 +00001245 // NOTE: a size of zero for a .comm should create a undefined symbol
1246 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001247 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001248 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1249 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001250
1251 // NOTE: The alignment in the directive is a power of 2 value, the assember
1252 // may internally end up wanting an alignment in bytes.
1253 // FIXME: Diagnose overflow.
1254 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001255 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1256 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001257
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001258 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001259 return Error(IDLoc, "invalid symbol redefinition");
1260
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001261 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001262 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001263 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001264 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1265 MCSectionMachO::S_ZEROFILL, 0,
1266 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001267 Sym, Size, 1 << Pow2Alignment);
1268 return false;
1269 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001270
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001271 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001272 return false;
1273}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001274
1275/// ParseDirectiveDarwinZerofill
1276/// ::= .zerofill segname , sectname [, identifier , size_expression [
1277/// , align_expression ]]
1278bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001279 // FIXME: Handle quoted names here.
1280
Daniel Dunbar3f872332009-07-28 16:08:33 +00001281 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001282 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001283 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001284 Lexer.Lex();
1285
Daniel Dunbar3f872332009-07-28 16:08:33 +00001286 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001287 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001288 Lexer.Lex();
1289
Daniel Dunbar3f872332009-07-28 16:08:33 +00001290 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001291 return TokError("expected section name after comma in '.zerofill' "
1292 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001293 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001294 Lexer.Lex();
1295
Chris Lattner9be3fee2009-07-10 22:20:30 +00001296 // If this is the end of the line all that was wanted was to create the
1297 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001298 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001299 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001300 Out.EmitZerofill(getMachOSection(Segment, Section,
1301 MCSectionMachO::S_ZEROFILL, 0,
1302 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001303 return false;
1304 }
1305
Daniel Dunbar3f872332009-07-28 16:08:33 +00001306 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001307 return TokError("unexpected token in directive");
1308 Lexer.Lex();
1309
Daniel Dunbar3f872332009-07-28 16:08:33 +00001310 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001311 return TokError("expected identifier in directive");
1312
1313 // handle the identifier as the key symbol.
1314 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001315 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001316 Lexer.Lex();
1317
Daniel Dunbar3f872332009-07-28 16:08:33 +00001318 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001319 return TokError("unexpected token in directive");
1320 Lexer.Lex();
1321
1322 int64_t Size;
1323 SMLoc SizeLoc = Lexer.getLoc();
1324 if (ParseAbsoluteExpression(Size))
1325 return true;
1326
1327 int64_t Pow2Alignment = 0;
1328 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001329 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001330 Lexer.Lex();
1331 Pow2AlignmentLoc = Lexer.getLoc();
1332 if (ParseAbsoluteExpression(Pow2Alignment))
1333 return true;
1334 }
1335
Daniel Dunbar3f872332009-07-28 16:08:33 +00001336 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001337 return TokError("unexpected token in '.zerofill' directive");
1338
1339 Lexer.Lex();
1340
1341 if (Size < 0)
1342 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1343 "than zero");
1344
1345 // NOTE: The alignment in the directive is a power of 2 value, the assember
1346 // may internally end up wanting an alignment in bytes.
1347 // FIXME: Diagnose overflow.
1348 if (Pow2Alignment < 0)
1349 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1350 "can't be less than zero");
1351
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001352 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001353 return Error(IDLoc, "invalid symbol redefinition");
1354
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001355 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001356 //
1357 // FIXME: Arch specific.
1358 Out.EmitZerofill(getMachOSection(Segment, Section,
1359 MCSectionMachO::S_ZEROFILL, 0,
1360 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001361 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001362
1363 return false;
1364}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001365
1366/// ParseDirectiveDarwinSubsectionsViaSymbols
1367/// ::= .subsections_via_symbols
1368bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001369 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001370 return TokError("unexpected token in '.subsections_via_symbols' directive");
1371
1372 Lexer.Lex();
1373
Kevin Enderbyf96db462009-07-16 17:56:39 +00001374 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001375
1376 return false;
1377}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001378
1379/// ParseDirectiveAbort
1380/// ::= .abort [ "abort_string" ]
1381bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001382 // FIXME: Use loc from directive.
1383 SMLoc Loc = Lexer.getLoc();
1384
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001385 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001386 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1387 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001388 return TokError("expected string in '.abort' directive");
1389
Daniel Dunbar419aded2009-07-28 16:38:40 +00001390 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001391
1392 Lexer.Lex();
1393 }
1394
Daniel Dunbar3f872332009-07-28 16:08:33 +00001395 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001396 return TokError("unexpected token in '.abort' directive");
1397
1398 Lexer.Lex();
1399
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001400 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001401 if (Str.empty())
1402 Error(Loc, ".abort detected. Assembly stopping.");
1403 else
1404 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001405
1406 return false;
1407}
Kevin Enderby71148242009-07-14 21:35:03 +00001408
1409/// ParseDirectiveLsym
1410/// ::= .lsym identifier , expression
1411bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001412 StringRef Name;
1413 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001414 return TokError("expected identifier in directive");
1415
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001416 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001417 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001418
Daniel Dunbar3f872332009-07-28 16:08:33 +00001419 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001420 return TokError("unexpected token in '.lsym' directive");
1421 Lexer.Lex();
1422
1423 MCValue Expr;
1424 if (ParseRelocatableExpression(Expr))
1425 return true;
1426
Daniel Dunbar3f872332009-07-28 16:08:33 +00001427 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001428 return TokError("unexpected token in '.lsym' directive");
1429
1430 Lexer.Lex();
1431
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001432 // We don't currently support this directive.
1433 //
1434 // FIXME: Diagnostic location!
1435 (void) Sym;
1436 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001437}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001438
1439/// ParseDirectiveInclude
1440/// ::= .include "filename"
1441bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001442 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001443 return TokError("expected string in '.include' directive");
1444
Daniel Dunbar419aded2009-07-28 16:38:40 +00001445 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001446 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001447 Lexer.Lex();
1448
Daniel Dunbar3f872332009-07-28 16:08:33 +00001449 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001450 return TokError("unexpected token in '.include' directive");
1451
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001452 // Strip the quotes.
1453 Filename = Filename.substr(1, Filename.size()-2);
1454
1455 // Attempt to switch the lexer to the included file before consuming the end
1456 // of statement to avoid losing it when we switch.
1457 if (Lexer.EnterIncludeFile(Filename)) {
1458 Lexer.PrintMessage(IncludeLoc,
1459 "Could not find include file '" + Filename + "'",
1460 "error");
1461 return true;
1462 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001463
1464 return false;
1465}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001466
1467/// ParseDirectiveDarwinDumpOrLoad
1468/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001469bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001470 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001471 return TokError("expected string in '.dump' or '.load' directive");
1472
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001473 Lexer.Lex();
1474
Daniel Dunbar3f872332009-07-28 16:08:33 +00001475 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001476 return TokError("unexpected token in '.dump' or '.load' directive");
1477
1478 Lexer.Lex();
1479
Kevin Enderby5026ae42009-07-20 20:25:37 +00001480 // FIXME: If/when .dump and .load are implemented they will be done in the
1481 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001482 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001483 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001484 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001485 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001486
1487 return false;
1488}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001489
1490/// ParseDirectiveIf
1491/// ::= .if expression
1492bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1493 // Consume the identifier that was the .if directive
1494 Lexer.Lex();
1495
1496 TheCondStack.push_back(TheCondState);
1497 TheCondState.TheCond = AsmCond::IfCond;
1498 if(TheCondState.Ignore) {
1499 EatToEndOfStatement();
1500 }
1501 else {
1502 int64_t ExprValue;
1503 if (ParseAbsoluteExpression(ExprValue))
1504 return true;
1505
1506 if (Lexer.isNot(AsmToken::EndOfStatement))
1507 return TokError("unexpected token in '.if' directive");
1508
1509 Lexer.Lex();
1510
1511 TheCondState.CondMet = ExprValue;
1512 TheCondState.Ignore = !TheCondState.CondMet;
1513 }
1514
1515 return false;
1516}
1517
1518/// ParseDirectiveElseIf
1519/// ::= .elseif expression
1520bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1521 if (TheCondState.TheCond != AsmCond::IfCond &&
1522 TheCondState.TheCond != AsmCond::ElseIfCond)
1523 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1524 " an .elseif");
1525 TheCondState.TheCond = AsmCond::ElseIfCond;
1526
1527 // Consume the identifier that was the .elseif directive
1528 Lexer.Lex();
1529
1530 bool LastIgnoreState = false;
1531 if (!TheCondStack.empty())
1532 LastIgnoreState = TheCondStack.back().Ignore;
1533 if (LastIgnoreState || TheCondState.CondMet) {
1534 TheCondState.Ignore = true;
1535 EatToEndOfStatement();
1536 }
1537 else {
1538 int64_t ExprValue;
1539 if (ParseAbsoluteExpression(ExprValue))
1540 return true;
1541
1542 if (Lexer.isNot(AsmToken::EndOfStatement))
1543 return TokError("unexpected token in '.elseif' directive");
1544
1545 Lexer.Lex();
1546 TheCondState.CondMet = ExprValue;
1547 TheCondState.Ignore = !TheCondState.CondMet;
1548 }
1549
1550 return false;
1551}
1552
1553/// ParseDirectiveElse
1554/// ::= .else
1555bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1556 // Consume the identifier that was the .else directive
1557 Lexer.Lex();
1558
1559 if (Lexer.isNot(AsmToken::EndOfStatement))
1560 return TokError("unexpected token in '.else' directive");
1561
1562 Lexer.Lex();
1563
1564 if (TheCondState.TheCond != AsmCond::IfCond &&
1565 TheCondState.TheCond != AsmCond::ElseIfCond)
1566 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1567 ".elseif");
1568 TheCondState.TheCond = AsmCond::ElseCond;
1569 bool LastIgnoreState = false;
1570 if (!TheCondStack.empty())
1571 LastIgnoreState = TheCondStack.back().Ignore;
1572 if (LastIgnoreState || TheCondState.CondMet)
1573 TheCondState.Ignore = true;
1574 else
1575 TheCondState.Ignore = false;
1576
1577 return false;
1578}
1579
1580/// ParseDirectiveEndIf
1581/// ::= .endif
1582bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1583 // Consume the identifier that was the .endif directive
1584 Lexer.Lex();
1585
1586 if (Lexer.isNot(AsmToken::EndOfStatement))
1587 return TokError("unexpected token in '.endif' directive");
1588
1589 Lexer.Lex();
1590
1591 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1592 TheCondStack.empty())
1593 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1594 ".else");
1595 if (!TheCondStack.empty()) {
1596 TheCondState = TheCondStack.back();
1597 TheCondStack.pop_back();
1598 }
1599
1600 return false;
1601}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001602
1603/// ParseDirectiveFile
1604/// ::= .file [number] string
1605bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1606 // FIXME: I'm not sure what this is.
1607 int64_t FileNumber = -1;
1608 if (Lexer.is(AsmToken::Integer)) {
1609 FileNumber = Lexer.getTok().getIntVal();
1610 Lexer.Lex();
1611
1612 if (FileNumber < 1)
1613 return TokError("file number less than one");
1614 }
1615
1616 if (Lexer.isNot(AsmToken::String))
1617 return TokError("unexpected token in '.file' directive");
1618
1619 StringRef FileName = Lexer.getTok().getString();
1620 Lexer.Lex();
1621
1622 if (Lexer.isNot(AsmToken::EndOfStatement))
1623 return TokError("unexpected token in '.file' directive");
1624
1625 // FIXME: Do something with the .file.
1626
1627 return false;
1628}
1629
1630/// ParseDirectiveLine
1631/// ::= .line [number]
1632bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1633 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1634 if (Lexer.isNot(AsmToken::Integer))
1635 return TokError("unexpected token in '.line' directive");
1636
1637 int64_t LineNumber = Lexer.getTok().getIntVal();
1638 (void) LineNumber;
1639 Lexer.Lex();
1640
1641 // FIXME: Do something with the .line.
1642 }
1643
1644 if (Lexer.isNot(AsmToken::EndOfStatement))
1645 return TokError("unexpected token in '.file' directive");
1646
1647 return false;
1648}
1649
1650
1651/// ParseDirectiveLoc
1652/// ::= .loc number [number [number]]
1653bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1654 if (Lexer.isNot(AsmToken::Integer))
1655 return TokError("unexpected token in '.loc' directive");
1656
1657 // FIXME: What are these fields?
1658 int64_t FileNumber = Lexer.getTok().getIntVal();
1659 (void) FileNumber;
1660 // FIXME: Validate file.
1661
1662 Lexer.Lex();
1663 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1664 if (Lexer.isNot(AsmToken::Integer))
1665 return TokError("unexpected token in '.loc' directive");
1666
1667 int64_t Param2 = Lexer.getTok().getIntVal();
1668 (void) Param2;
1669 Lexer.Lex();
1670
1671 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1672 if (Lexer.isNot(AsmToken::Integer))
1673 return TokError("unexpected token in '.loc' directive");
1674
1675 int64_t Param3 = Lexer.getTok().getIntVal();
1676 (void) Param3;
1677 Lexer.Lex();
1678
1679 // FIXME: Do something with the .loc.
1680 }
1681 }
1682
1683 if (Lexer.isNot(AsmToken::EndOfStatement))
1684 return TokError("unexpected token in '.file' directive");
1685
1686 return false;
1687}
1688