blob: b1e8e9ae404fa64de604157d105141057771f6ba [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 Dunbarc18274b2009-08-31 08:08:17 +0000261bool AsmParser::ParseParenExpression(const MCExpr *&Res) {
262 if (ParseParenExpr(Res))
263 return true;
264
265 return false;
266}
267
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000269 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000271 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000272 if (ParseExpression(Expr))
273 return true;
274
275 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000276 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000277
278 return false;
279}
280
Daniel Dunbar3f872332009-07-28 16:08:33 +0000281static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000282 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000283 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000284 default:
285 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000286
287 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000288 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000289 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000290 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000291 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000292 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 return 1;
294
295 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000296 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000297 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000298 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000299 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000300 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000301 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000302 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000303 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000305 case AsmToken::ExclaimEqual:
306 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000307 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000308 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000309 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000310 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000311 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000312 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000313 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000314 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000315 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000316 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000317 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000318 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000319 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000320 return 2;
321
322 // Intermediate Precedence: |, &, ^
323 //
324 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000325 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000326 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000327 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000328 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000329 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000330 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000331 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000332 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333 return 3;
334
335 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000342 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000343 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000349 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000351 }
352}
353
354
355/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
356/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000357bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000358 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000359 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000361
362 // If the next token is lower precedence than we are allowed to eat, return
363 // successfully with what we ate already.
364 if (TokPrec < Precedence)
365 return false;
366
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000367 Lexer.Lex();
368
369 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000370 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000371 if (ParsePrimaryExpr(RHS)) return true;
372
373 // If BinOp binds less tightly with RHS than the operator after RHS, let
374 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000375 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000377 if (TokPrec < NextTokPrec) {
378 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
379 }
380
Daniel Dunbar475839e2009-06-29 20:37:27 +0000381 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000382 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000383 }
384}
385
Chris Lattnerc4193832009-06-22 05:51:26 +0000386
387
388
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000389/// ParseStatement:
390/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000391/// ::= Label* Directive ...Operands... EndOfStatement
392/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000393bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000394 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000395 Lexer.Lex();
396 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000397 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000398
399 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000400 AsmToken ID = Lexer.getTok();
401 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000402 StringRef IDVal;
403 if (ParseIdentifier(IDVal))
404 return TokError("unexpected token at start of statement");
405
406 // FIXME: Recurse on local labels?
407
408 // See what kind of statement we have.
409 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000410 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000411 // identifier ':' -> Label.
412 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000413
414 // Diagnose attempt to use a variable as a label.
415 //
416 // FIXME: Diagnostics. Note the location of the definition as a label.
417 // FIXME: This doesn't diagnose assignment to a symbol which has been
418 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000419 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000420 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000421 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000422
Daniel Dunbar959fd882009-08-26 22:13:22 +0000423 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000424 Out.EmitLabel(Sym);
425
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000426 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000427 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000428
Daniel Dunbar3f872332009-07-28 16:08:33 +0000429 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000430 // identifier '=' ... -> assignment statement
431 Lexer.Lex();
432
Daniel Dunbare2ace502009-08-31 08:09:09 +0000433 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000434
435 default: // Normal instruction or directive.
436 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000437 }
438
439 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000440 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000441 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000442 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000443 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000444 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000445 // FIXME: This changes behavior based on the -static flag to the
446 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000447 return ParseDirectiveSectionSwitch("__TEXT", "__text",
448 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000449 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000450 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000451 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000452 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000453 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000454 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
455 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000456 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000457 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000458 MCSectionMachO::S_4BYTE_LITERALS,
459 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000460 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000461 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000462 MCSectionMachO::S_8BYTE_LITERALS,
463 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000464 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000465 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000466 MCSectionMachO::S_16BYTE_LITERALS,
467 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000468 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000469 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000470 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000471 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000472 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000473 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000474 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000475 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
476
477 // FIXME: The assembler manual claims that this has the self modify code
478 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000479 if (IDVal == ".symbol_stub")
480 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
481 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000482 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
483 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000484 0, 16);
485 // FIXME: PowerPC only?
486 if (IDVal == ".picsymbol_stub")
487 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
488 MCSectionMachO::S_SYMBOL_STUBS |
489 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
490 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000491 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000492 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000494 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
495
496 // FIXME: The section names of these two are misspelled in the assembler
497 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000498 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000499 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
500 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
501 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000502 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000503 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
504 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
505 4);
506
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000508 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000509 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000510 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000511 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
512 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000514 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000515 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
516 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000518 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000519
520
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000521 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000522 return ParseDirectiveSectionSwitch("__OBJC", "__class",
523 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000525 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
526 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000528 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
529 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000531 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
532 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
535 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
538 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000540 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
541 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
544 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
547 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
548 MCSectionMachO::S_LITERAL_POINTERS,
549 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
552 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
553 MCSectionMachO::S_LITERAL_POINTERS,
554 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
557 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 return ParseDirectiveSectionSwitch("__OBJC", "__category",
560 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000562 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000571 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
572 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000574 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
575 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000577 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
578 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000580 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
581 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000582
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000583 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000585 return ParseDirectiveSet();
586
Daniel Dunbara0d14262009-06-24 23:30:00 +0000587 // Data directives
588
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000590 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000592 return ParseDirectiveAscii(true);
593
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000595 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000596 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000597 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000599 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000601 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000602
603 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000605 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000607 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000608 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000609 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000611 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000613 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000615 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000617 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000619 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
620
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000622 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000623
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000625 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000627 return ParseDirectiveSpace();
628
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000629 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000630
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000632 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000634 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000636 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000638 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000640 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000642 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000644 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000646 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000648 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000650 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000652 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000654 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
655
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000657 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000659 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000661 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000663 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000665 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000666
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000668 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000670 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000672 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000674 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000676 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000677
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000678 // Debugging directives
679
680 if (IDVal == ".file")
681 return ParseDirectiveFile(IDLoc);
682 if (IDVal == ".line")
683 return ParseDirectiveLine(IDLoc);
684 if (IDVal == ".loc")
685 return ParseDirectiveLoc(IDLoc);
686
Kevin Enderby9c656452009-09-10 20:51:44 +0000687 // Target hook for parsing target specific directives.
688 if (!getTargetParser().ParseDirective(ID))
689 return false;
690
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000691 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000692 EatToEndOfStatement();
693 return false;
694 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000695
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000696 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000697 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000698 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000699
Daniel Dunbar3f872332009-07-28 16:08:33 +0000700 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000701 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000702
703 // Eat the end of statement marker.
704 Lexer.Lex();
705
706 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000707 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000708
709 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000710 return false;
711}
Chris Lattner9a023f72009-06-24 04:43:34 +0000712
Daniel Dunbare2ace502009-08-31 08:09:09 +0000713bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000714 // FIXME: Use better location, we should use proper tokens.
715 SMLoc EqualLoc = Lexer.getLoc();
716
Daniel Dunbar821e3332009-08-31 08:09:28 +0000717 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000718 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000719 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000720 return true;
721
Daniel Dunbar3f872332009-07-28 16:08:33 +0000722 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000723 return TokError("unexpected token in assignment");
724
725 // Eat the end of statement marker.
726 Lexer.Lex();
727
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000728 // Diagnose assignment to a label.
729 //
730 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000731 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000732 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar959fd882009-08-26 22:13:22 +0000733 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000734 if (!Sym->isUndefined() && !Sym->isAbsolute())
735 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000736
737 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000738 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000739
740 return false;
741}
742
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000743/// ParseIdentifier:
744/// ::= identifier
745/// ::= string
746bool AsmParser::ParseIdentifier(StringRef &Res) {
747 if (Lexer.isNot(AsmToken::Identifier) &&
748 Lexer.isNot(AsmToken::String))
749 return true;
750
751 Res = Lexer.getTok().getIdentifier();
752
753 Lexer.Lex(); // Consume the identifier token.
754
755 return false;
756}
757
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000758/// ParseDirectiveSet:
759/// ::= .set identifier ',' expression
760bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000761 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000762
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000763 if (ParseIdentifier(Name))
764 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000765
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000766 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000767 return TokError("unexpected token in '.set'");
768 Lexer.Lex();
769
Daniel Dunbare2ace502009-08-31 08:09:09 +0000770 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000771}
772
Chris Lattner9a023f72009-06-24 04:43:34 +0000773/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000774/// ::= .section identifier (',' identifier)*
775/// FIXME: This should actually parse out the segment, section, attributes and
776/// sizeof_stub fields.
777bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000778 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000779
Daniel Dunbarace63122009-08-11 03:42:33 +0000780 StringRef SectionName;
781 if (ParseIdentifier(SectionName))
782 return Error(Loc, "expected identifier after '.section' directive");
783
784 // Verify there is a following comma.
785 if (!Lexer.is(AsmToken::Comma))
786 return TokError("unexpected token in '.section' directive");
787
Chris Lattnerff4bc462009-08-10 01:39:42 +0000788 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000789 SectionSpec += ",";
790
791 // Add all the tokens until the end of the line, ParseSectionSpecifier will
792 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000793 StringRef EOL = Lexer.LexUntilEndOfStatement();
794 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000795
Chris Lattnerff4bc462009-08-10 01:39:42 +0000796 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000797 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000798 return TokError("unexpected token in '.section' directive");
799 Lexer.Lex();
800
Chris Lattnerff4bc462009-08-10 01:39:42 +0000801
802 StringRef Segment, Section;
803 unsigned TAA, StubSize;
804 std::string ErrorStr =
805 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
806 TAA, StubSize);
807
808 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000809 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000810
Chris Lattner56594f92009-07-31 17:47:16 +0000811 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000812 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
813 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000814 return false;
815}
816
Chris Lattnere15c2d72009-08-10 18:05:55 +0000817/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000818bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
819 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000820 unsigned TAA, unsigned Align,
821 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000822 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000823 return TokError("unexpected token in section switching directive");
824 Lexer.Lex();
825
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()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000829
830 // Set the implicit alignment, if any.
831 //
832 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
833 // alignment on the section (e.g., if one manually inserts bytes into the
834 // section, then just issueing the section switch directive will not realign
835 // the section. However, this is arguably more reasonable behavior, and there
836 // is no good reason for someone to intentionally emit incorrectly sized
837 // values into the implicitly aligned sections.
838 if (Align)
839 Out.EmitValueToAlignment(Align, 0, 1, 0);
840
Chris Lattner529fb542009-06-24 05:13:15 +0000841 return false;
842}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000843
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000844bool AsmParser::ParseEscapedString(std::string &Data) {
845 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
846
847 Data = "";
848 StringRef Str = Lexer.getTok().getStringContents();
849 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
850 if (Str[i] != '\\') {
851 Data += Str[i];
852 continue;
853 }
854
855 // Recognize escaped characters. Note that this escape semantics currently
856 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
857 ++i;
858 if (i == e)
859 return TokError("unexpected backslash at end of string");
860
861 // Recognize octal sequences.
862 if ((unsigned) (Str[i] - '0') <= 7) {
863 // Consume up to three octal characters.
864 unsigned Value = Str[i] - '0';
865
866 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
867 ++i;
868 Value = Value * 8 + (Str[i] - '0');
869
870 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
871 ++i;
872 Value = Value * 8 + (Str[i] - '0');
873 }
874 }
875
876 if (Value > 255)
877 return TokError("invalid octal escape sequence (out of range)");
878
879 Data += (unsigned char) Value;
880 continue;
881 }
882
883 // Otherwise recognize individual escapes.
884 switch (Str[i]) {
885 default:
886 // Just reject invalid escape sequences for now.
887 return TokError("invalid escape sequence (unrecognized character)");
888
889 case 'b': Data += '\b'; break;
890 case 'f': Data += '\f'; break;
891 case 'n': Data += '\n'; break;
892 case 'r': Data += '\r'; break;
893 case 't': Data += '\t'; break;
894 case '"': Data += '"'; break;
895 case '\\': Data += '\\'; break;
896 }
897 }
898
899 return false;
900}
901
Daniel Dunbara0d14262009-06-24 23:30:00 +0000902/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000903/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000904bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000905 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000906 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000908 return TokError("expected string in '.ascii' or '.asciz' directive");
909
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000910 std::string Data;
911 if (ParseEscapedString(Data))
912 return true;
913
914 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000915 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000916 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000917
918 Lexer.Lex();
919
Daniel Dunbar3f872332009-07-28 16:08:33 +0000920 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000921 break;
922
Daniel Dunbar3f872332009-07-28 16:08:33 +0000923 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000924 return TokError("unexpected token in '.ascii' or '.asciz' directive");
925 Lexer.Lex();
926 }
927 }
928
929 Lexer.Lex();
930 return false;
931}
932
933/// ParseDirectiveValue
934/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
935bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000936 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000937 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000938 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000939 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000940 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000941 return true;
942
Daniel Dunbar883f9202009-08-31 08:08:50 +0000943 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000944
Daniel Dunbar3f872332009-07-28 16:08:33 +0000945 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000946 break;
947
948 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000949 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000950 return TokError("unexpected token in directive");
951 Lexer.Lex();
952 }
953 }
954
955 Lexer.Lex();
956 return false;
957}
958
959/// ParseDirectiveSpace
960/// ::= .space expression [ , expression ]
961bool AsmParser::ParseDirectiveSpace() {
962 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000963 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964 return true;
965
966 int64_t FillExpr = 0;
967 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000968 if (Lexer.isNot(AsmToken::EndOfStatement)) {
969 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000970 return TokError("unexpected token in '.space' directive");
971 Lexer.Lex();
972
Daniel Dunbar475839e2009-06-29 20:37:27 +0000973 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974 return true;
975
976 HasFillExpr = true;
977
Daniel Dunbar3f872332009-07-28 16:08:33 +0000978 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000979 return TokError("unexpected token in '.space' directive");
980 }
981
982 Lexer.Lex();
983
984 if (NumBytes <= 0)
985 return TokError("invalid number of bytes in '.space' directive");
986
987 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
988 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +0000989 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000990
991 return false;
992}
993
994/// ParseDirectiveFill
995/// ::= .fill expression , expression , expression
996bool AsmParser::ParseDirectiveFill() {
997 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000998 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000999 return true;
1000
Daniel Dunbar3f872332009-07-28 16:08:33 +00001001 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002 return TokError("unexpected token in '.fill' directive");
1003 Lexer.Lex();
1004
1005 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001006 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 return true;
1008
Daniel Dunbar3f872332009-07-28 16:08:33 +00001009 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001010 return TokError("unexpected token in '.fill' directive");
1011 Lexer.Lex();
1012
1013 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001014 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015 return true;
1016
Daniel Dunbar3f872332009-07-28 16:08:33 +00001017 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018 return TokError("unexpected token in '.fill' directive");
1019
1020 Lexer.Lex();
1021
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001022 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1023 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024
1025 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001026 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001027
1028 return false;
1029}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001030
1031/// ParseDirectiveOrg
1032/// ::= .org expression [ , expression ]
1033bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001034 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001035 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001036 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001037 return true;
1038
1039 // Parse optional fill expression.
1040 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001041 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1042 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001043 return TokError("unexpected token in '.org' directive");
1044 Lexer.Lex();
1045
Daniel Dunbar475839e2009-06-29 20:37:27 +00001046 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001047 return true;
1048
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001050 return TokError("unexpected token in '.org' directive");
1051 }
1052
1053 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001054
1055 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1056 // has to be relative to the current section.
1057 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001058
1059 return false;
1060}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001061
1062/// ParseDirectiveAlign
1063/// ::= {.align, ...} expression [ , expression [ , expression ]]
1064bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001065 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001066 int64_t Alignment;
1067 if (ParseAbsoluteExpression(Alignment))
1068 return true;
1069
1070 SMLoc MaxBytesLoc;
1071 bool HasFillExpr = false;
1072 int64_t FillExpr = 0;
1073 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001074 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1075 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001076 return TokError("unexpected token in directive");
1077 Lexer.Lex();
1078
1079 // The fill expression can be omitted while specifying a maximum number of
1080 // alignment bytes, e.g:
1081 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001082 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001083 HasFillExpr = true;
1084 if (ParseAbsoluteExpression(FillExpr))
1085 return true;
1086 }
1087
Daniel Dunbar3f872332009-07-28 16:08:33 +00001088 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1089 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001090 return TokError("unexpected token in directive");
1091 Lexer.Lex();
1092
1093 MaxBytesLoc = Lexer.getLoc();
1094 if (ParseAbsoluteExpression(MaxBytesToFill))
1095 return true;
1096
Daniel Dunbar3f872332009-07-28 16:08:33 +00001097 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001098 return TokError("unexpected token in directive");
1099 }
1100 }
1101
1102 Lexer.Lex();
1103
1104 if (!HasFillExpr) {
1105 // FIXME: Sometimes fill with nop.
1106 FillExpr = 0;
1107 }
1108
1109 // Compute alignment in bytes.
1110 if (IsPow2) {
1111 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001112 if (Alignment >= 32) {
1113 Error(AlignmentLoc, "invalid alignment value");
1114 Alignment = 31;
1115 }
1116
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001117 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001118 }
1119
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001120 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001121 if (MaxBytesLoc.isValid()) {
1122 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001123 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1124 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001125 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001126 }
1127
1128 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001129 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1130 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131 MaxBytesToFill = 0;
1132 }
1133 }
1134
1135 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1136 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1137
1138 return false;
1139}
1140
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001141/// ParseDirectiveSymbolAttribute
1142/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1143bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001144 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001145 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001146 StringRef Name;
1147
1148 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001149 return TokError("expected identifier in directive");
1150
Daniel Dunbar959fd882009-08-26 22:13:22 +00001151 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001152
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001153 Out.EmitSymbolAttribute(Sym, Attr);
1154
Daniel Dunbar3f872332009-07-28 16:08:33 +00001155 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001156 break;
1157
Daniel Dunbar3f872332009-07-28 16:08:33 +00001158 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001159 return TokError("unexpected token in directive");
1160 Lexer.Lex();
1161 }
1162 }
1163
1164 Lexer.Lex();
1165 return false;
1166}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001167
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001168/// ParseDirectiveDarwinSymbolDesc
1169/// ::= .desc identifier , expression
1170bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001171 StringRef Name;
1172 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001173 return TokError("expected identifier in directive");
1174
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001175 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001176 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001177
Daniel Dunbar3f872332009-07-28 16:08:33 +00001178 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001179 return TokError("unexpected token in '.desc' directive");
1180 Lexer.Lex();
1181
1182 SMLoc DescLoc = Lexer.getLoc();
1183 int64_t DescValue;
1184 if (ParseAbsoluteExpression(DescValue))
1185 return true;
1186
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001188 return TokError("unexpected token in '.desc' directive");
1189
1190 Lexer.Lex();
1191
1192 // Set the n_desc field of this Symbol to this DescValue
1193 Out.EmitSymbolDesc(Sym, DescValue);
1194
1195 return false;
1196}
1197
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001198/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001199/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1200bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001201 SMLoc IDLoc = Lexer.getLoc();
1202 StringRef Name;
1203 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001204 return TokError("expected identifier in directive");
1205
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001206 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001207 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001208
Daniel Dunbar3f872332009-07-28 16:08:33 +00001209 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001210 return TokError("unexpected token in directive");
1211 Lexer.Lex();
1212
1213 int64_t Size;
1214 SMLoc SizeLoc = Lexer.getLoc();
1215 if (ParseAbsoluteExpression(Size))
1216 return true;
1217
1218 int64_t Pow2Alignment = 0;
1219 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001220 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001221 Lexer.Lex();
1222 Pow2AlignmentLoc = Lexer.getLoc();
1223 if (ParseAbsoluteExpression(Pow2Alignment))
1224 return true;
1225 }
1226
Daniel Dunbar3f872332009-07-28 16:08:33 +00001227 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001228 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001229
1230 Lexer.Lex();
1231
Chris Lattner1fc3d752009-07-09 17:25:12 +00001232 // NOTE: a size of zero for a .comm should create a undefined symbol
1233 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001234 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001235 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1236 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001237
1238 // NOTE: The alignment in the directive is a power of 2 value, the assember
1239 // may internally end up wanting an alignment in bytes.
1240 // FIXME: Diagnose overflow.
1241 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001242 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1243 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001244
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001245 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001246 return Error(IDLoc, "invalid symbol redefinition");
1247
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001248 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001249 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001250 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001251 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1252 MCSectionMachO::S_ZEROFILL, 0,
1253 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001254 Sym, Size, 1 << Pow2Alignment);
1255 return false;
1256 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001257
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001258 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001259 return false;
1260}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001261
1262/// ParseDirectiveDarwinZerofill
1263/// ::= .zerofill segname , sectname [, identifier , size_expression [
1264/// , align_expression ]]
1265bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001266 // FIXME: Handle quoted names here.
1267
Daniel Dunbar3f872332009-07-28 16:08:33 +00001268 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001269 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001270 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001271 Lexer.Lex();
1272
Daniel Dunbar3f872332009-07-28 16:08:33 +00001273 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001274 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001275 Lexer.Lex();
1276
Daniel Dunbar3f872332009-07-28 16:08:33 +00001277 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001278 return TokError("expected section name after comma in '.zerofill' "
1279 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001280 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001281 Lexer.Lex();
1282
Chris Lattner9be3fee2009-07-10 22:20:30 +00001283 // If this is the end of the line all that was wanted was to create the
1284 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001285 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001286 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001287 Out.EmitZerofill(getMachOSection(Segment, Section,
1288 MCSectionMachO::S_ZEROFILL, 0,
1289 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001290 return false;
1291 }
1292
Daniel Dunbar3f872332009-07-28 16:08:33 +00001293 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001294 return TokError("unexpected token in directive");
1295 Lexer.Lex();
1296
Daniel Dunbar3f872332009-07-28 16:08:33 +00001297 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001298 return TokError("expected identifier in directive");
1299
1300 // handle the identifier as the key symbol.
1301 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001302 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001303 Lexer.Lex();
1304
Daniel Dunbar3f872332009-07-28 16:08:33 +00001305 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001306 return TokError("unexpected token in directive");
1307 Lexer.Lex();
1308
1309 int64_t Size;
1310 SMLoc SizeLoc = Lexer.getLoc();
1311 if (ParseAbsoluteExpression(Size))
1312 return true;
1313
1314 int64_t Pow2Alignment = 0;
1315 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001316 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001317 Lexer.Lex();
1318 Pow2AlignmentLoc = Lexer.getLoc();
1319 if (ParseAbsoluteExpression(Pow2Alignment))
1320 return true;
1321 }
1322
Daniel Dunbar3f872332009-07-28 16:08:33 +00001323 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001324 return TokError("unexpected token in '.zerofill' directive");
1325
1326 Lexer.Lex();
1327
1328 if (Size < 0)
1329 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1330 "than zero");
1331
1332 // NOTE: The alignment in the directive is a power of 2 value, the assember
1333 // may internally end up wanting an alignment in bytes.
1334 // FIXME: Diagnose overflow.
1335 if (Pow2Alignment < 0)
1336 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1337 "can't be less than zero");
1338
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001339 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001340 return Error(IDLoc, "invalid symbol redefinition");
1341
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001342 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001343 //
1344 // FIXME: Arch specific.
1345 Out.EmitZerofill(getMachOSection(Segment, Section,
1346 MCSectionMachO::S_ZEROFILL, 0,
1347 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001348 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001349
1350 return false;
1351}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001352
1353/// ParseDirectiveDarwinSubsectionsViaSymbols
1354/// ::= .subsections_via_symbols
1355bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001356 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001357 return TokError("unexpected token in '.subsections_via_symbols' directive");
1358
1359 Lexer.Lex();
1360
Kevin Enderbyf96db462009-07-16 17:56:39 +00001361 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001362
1363 return false;
1364}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001365
1366/// ParseDirectiveAbort
1367/// ::= .abort [ "abort_string" ]
1368bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001369 // FIXME: Use loc from directive.
1370 SMLoc Loc = Lexer.getLoc();
1371
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001372 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001373 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1374 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001375 return TokError("expected string in '.abort' directive");
1376
Daniel Dunbar419aded2009-07-28 16:38:40 +00001377 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001378
1379 Lexer.Lex();
1380 }
1381
Daniel Dunbar3f872332009-07-28 16:08:33 +00001382 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001383 return TokError("unexpected token in '.abort' directive");
1384
1385 Lexer.Lex();
1386
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001387 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001388 if (Str.empty())
1389 Error(Loc, ".abort detected. Assembly stopping.");
1390 else
1391 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001392
1393 return false;
1394}
Kevin Enderby71148242009-07-14 21:35:03 +00001395
1396/// ParseDirectiveLsym
1397/// ::= .lsym identifier , expression
1398bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001399 StringRef Name;
1400 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001401 return TokError("expected identifier in directive");
1402
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001403 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001404 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001405
Daniel Dunbar3f872332009-07-28 16:08:33 +00001406 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001407 return TokError("unexpected token in '.lsym' directive");
1408 Lexer.Lex();
1409
Daniel Dunbar821e3332009-08-31 08:09:28 +00001410 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001411 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001412 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001413 return true;
1414
Daniel Dunbar3f872332009-07-28 16:08:33 +00001415 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001416 return TokError("unexpected token in '.lsym' directive");
1417
1418 Lexer.Lex();
1419
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001420 // We don't currently support this directive.
1421 //
1422 // FIXME: Diagnostic location!
1423 (void) Sym;
1424 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001425}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001426
1427/// ParseDirectiveInclude
1428/// ::= .include "filename"
1429bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001430 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001431 return TokError("expected string in '.include' directive");
1432
Daniel Dunbar419aded2009-07-28 16:38:40 +00001433 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001434 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001435 Lexer.Lex();
1436
Daniel Dunbar3f872332009-07-28 16:08:33 +00001437 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001438 return TokError("unexpected token in '.include' directive");
1439
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001440 // Strip the quotes.
1441 Filename = Filename.substr(1, Filename.size()-2);
1442
1443 // Attempt to switch the lexer to the included file before consuming the end
1444 // of statement to avoid losing it when we switch.
1445 if (Lexer.EnterIncludeFile(Filename)) {
1446 Lexer.PrintMessage(IncludeLoc,
1447 "Could not find include file '" + Filename + "'",
1448 "error");
1449 return true;
1450 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001451
1452 return false;
1453}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001454
1455/// ParseDirectiveDarwinDumpOrLoad
1456/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001457bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001458 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001459 return TokError("expected string in '.dump' or '.load' directive");
1460
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001461 Lexer.Lex();
1462
Daniel Dunbar3f872332009-07-28 16:08:33 +00001463 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001464 return TokError("unexpected token in '.dump' or '.load' directive");
1465
1466 Lexer.Lex();
1467
Kevin Enderby5026ae42009-07-20 20:25:37 +00001468 // FIXME: If/when .dump and .load are implemented they will be done in the
1469 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001470 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001471 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001472 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001473 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001474
1475 return false;
1476}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001477
1478/// ParseDirectiveIf
1479/// ::= .if expression
1480bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1481 // Consume the identifier that was the .if directive
1482 Lexer.Lex();
1483
1484 TheCondStack.push_back(TheCondState);
1485 TheCondState.TheCond = AsmCond::IfCond;
1486 if(TheCondState.Ignore) {
1487 EatToEndOfStatement();
1488 }
1489 else {
1490 int64_t ExprValue;
1491 if (ParseAbsoluteExpression(ExprValue))
1492 return true;
1493
1494 if (Lexer.isNot(AsmToken::EndOfStatement))
1495 return TokError("unexpected token in '.if' directive");
1496
1497 Lexer.Lex();
1498
1499 TheCondState.CondMet = ExprValue;
1500 TheCondState.Ignore = !TheCondState.CondMet;
1501 }
1502
1503 return false;
1504}
1505
1506/// ParseDirectiveElseIf
1507/// ::= .elseif expression
1508bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1509 if (TheCondState.TheCond != AsmCond::IfCond &&
1510 TheCondState.TheCond != AsmCond::ElseIfCond)
1511 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1512 " an .elseif");
1513 TheCondState.TheCond = AsmCond::ElseIfCond;
1514
1515 // Consume the identifier that was the .elseif directive
1516 Lexer.Lex();
1517
1518 bool LastIgnoreState = false;
1519 if (!TheCondStack.empty())
1520 LastIgnoreState = TheCondStack.back().Ignore;
1521 if (LastIgnoreState || TheCondState.CondMet) {
1522 TheCondState.Ignore = true;
1523 EatToEndOfStatement();
1524 }
1525 else {
1526 int64_t ExprValue;
1527 if (ParseAbsoluteExpression(ExprValue))
1528 return true;
1529
1530 if (Lexer.isNot(AsmToken::EndOfStatement))
1531 return TokError("unexpected token in '.elseif' directive");
1532
1533 Lexer.Lex();
1534 TheCondState.CondMet = ExprValue;
1535 TheCondState.Ignore = !TheCondState.CondMet;
1536 }
1537
1538 return false;
1539}
1540
1541/// ParseDirectiveElse
1542/// ::= .else
1543bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1544 // Consume the identifier that was the .else directive
1545 Lexer.Lex();
1546
1547 if (Lexer.isNot(AsmToken::EndOfStatement))
1548 return TokError("unexpected token in '.else' directive");
1549
1550 Lexer.Lex();
1551
1552 if (TheCondState.TheCond != AsmCond::IfCond &&
1553 TheCondState.TheCond != AsmCond::ElseIfCond)
1554 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1555 ".elseif");
1556 TheCondState.TheCond = AsmCond::ElseCond;
1557 bool LastIgnoreState = false;
1558 if (!TheCondStack.empty())
1559 LastIgnoreState = TheCondStack.back().Ignore;
1560 if (LastIgnoreState || TheCondState.CondMet)
1561 TheCondState.Ignore = true;
1562 else
1563 TheCondState.Ignore = false;
1564
1565 return false;
1566}
1567
1568/// ParseDirectiveEndIf
1569/// ::= .endif
1570bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1571 // Consume the identifier that was the .endif directive
1572 Lexer.Lex();
1573
1574 if (Lexer.isNot(AsmToken::EndOfStatement))
1575 return TokError("unexpected token in '.endif' directive");
1576
1577 Lexer.Lex();
1578
1579 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1580 TheCondStack.empty())
1581 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1582 ".else");
1583 if (!TheCondStack.empty()) {
1584 TheCondState = TheCondStack.back();
1585 TheCondStack.pop_back();
1586 }
1587
1588 return false;
1589}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001590
1591/// ParseDirectiveFile
1592/// ::= .file [number] string
1593bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1594 // FIXME: I'm not sure what this is.
1595 int64_t FileNumber = -1;
1596 if (Lexer.is(AsmToken::Integer)) {
1597 FileNumber = Lexer.getTok().getIntVal();
1598 Lexer.Lex();
1599
1600 if (FileNumber < 1)
1601 return TokError("file number less than one");
1602 }
1603
1604 if (Lexer.isNot(AsmToken::String))
1605 return TokError("unexpected token in '.file' directive");
1606
1607 StringRef FileName = Lexer.getTok().getString();
1608 Lexer.Lex();
1609
1610 if (Lexer.isNot(AsmToken::EndOfStatement))
1611 return TokError("unexpected token in '.file' directive");
1612
1613 // FIXME: Do something with the .file.
1614
1615 return false;
1616}
1617
1618/// ParseDirectiveLine
1619/// ::= .line [number]
1620bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1621 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1622 if (Lexer.isNot(AsmToken::Integer))
1623 return TokError("unexpected token in '.line' directive");
1624
1625 int64_t LineNumber = Lexer.getTok().getIntVal();
1626 (void) LineNumber;
1627 Lexer.Lex();
1628
1629 // FIXME: Do something with the .line.
1630 }
1631
1632 if (Lexer.isNot(AsmToken::EndOfStatement))
1633 return TokError("unexpected token in '.file' directive");
1634
1635 return false;
1636}
1637
1638
1639/// ParseDirectiveLoc
1640/// ::= .loc number [number [number]]
1641bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1642 if (Lexer.isNot(AsmToken::Integer))
1643 return TokError("unexpected token in '.loc' directive");
1644
1645 // FIXME: What are these fields?
1646 int64_t FileNumber = Lexer.getTok().getIntVal();
1647 (void) FileNumber;
1648 // FIXME: Validate file.
1649
1650 Lexer.Lex();
1651 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1652 if (Lexer.isNot(AsmToken::Integer))
1653 return TokError("unexpected token in '.loc' directive");
1654
1655 int64_t Param2 = Lexer.getTok().getIntVal();
1656 (void) Param2;
1657 Lexer.Lex();
1658
1659 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1660 if (Lexer.isNot(AsmToken::Integer))
1661 return TokError("unexpected token in '.loc' directive");
1662
1663 int64_t Param3 = Lexer.getTok().getIntVal();
1664 (void) Param3;
1665 Lexer.Lex();
1666
1667 // FIXME: Do something with the .loc.
1668 }
1669 }
1670
1671 if (Lexer.isNot(AsmToken::EndOfStatement))
1672 return TokError("unexpected token in '.file' directive");
1673
1674 return false;
1675}
1676