blob: f51f43adc1226461f6557a1ec2bd3f5b5d07ddc2 [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
594 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000596 return ParseDirectiveValue(1);
Daniel Dunbar1e840b22009-08-11 04:44:00 +0000597 if (IDVal == ".short" || IDVal == ".word")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000598 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000600 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000602 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000603
604 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000606 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000608 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000610 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000612 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000614 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000616 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000618 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000620 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
621
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000623 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000624
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000626 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000628 return ParseDirectiveSpace();
629
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000630 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000631
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000633 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000635 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000637 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000639 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000641 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000643 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000645 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000647 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000649 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000651 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000653 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000655 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
656
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000658 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000660 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000662 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000664 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000666 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000667
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000669 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000671 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000673 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000675 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000677 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000678
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000679 // Debugging directives
680
681 if (IDVal == ".file")
682 return ParseDirectiveFile(IDLoc);
683 if (IDVal == ".line")
684 return ParseDirectiveLine(IDLoc);
685 if (IDVal == ".loc")
686 return ParseDirectiveLoc(IDLoc);
687
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000688 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000689 EatToEndOfStatement();
690 return false;
691 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000692
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000693 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000694 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000695 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000696
Daniel Dunbar3f872332009-07-28 16:08:33 +0000697 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000698 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000699
700 // Eat the end of statement marker.
701 Lexer.Lex();
702
703 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000704 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000705
706 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000707 return false;
708}
Chris Lattner9a023f72009-06-24 04:43:34 +0000709
Daniel Dunbare2ace502009-08-31 08:09:09 +0000710bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000711 // FIXME: Use better location, we should use proper tokens.
712 SMLoc EqualLoc = Lexer.getLoc();
713
Daniel Dunbar821e3332009-08-31 08:09:28 +0000714 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000715 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000716 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000717 return true;
718
Daniel Dunbar3f872332009-07-28 16:08:33 +0000719 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000720 return TokError("unexpected token in assignment");
721
722 // Eat the end of statement marker.
723 Lexer.Lex();
724
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000725 // Diagnose assignment to a label.
726 //
727 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000728 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000729 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar959fd882009-08-26 22:13:22 +0000730 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000731 if (!Sym->isUndefined() && !Sym->isAbsolute())
732 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000733
734 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000735 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000736
737 return false;
738}
739
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000740/// ParseIdentifier:
741/// ::= identifier
742/// ::= string
743bool AsmParser::ParseIdentifier(StringRef &Res) {
744 if (Lexer.isNot(AsmToken::Identifier) &&
745 Lexer.isNot(AsmToken::String))
746 return true;
747
748 Res = Lexer.getTok().getIdentifier();
749
750 Lexer.Lex(); // Consume the identifier token.
751
752 return false;
753}
754
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000755/// ParseDirectiveSet:
756/// ::= .set identifier ',' expression
757bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000758 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000759
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000760 if (ParseIdentifier(Name))
761 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000762
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000763 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000764 return TokError("unexpected token in '.set'");
765 Lexer.Lex();
766
Daniel Dunbare2ace502009-08-31 08:09:09 +0000767 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000768}
769
Chris Lattner9a023f72009-06-24 04:43:34 +0000770/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000771/// ::= .section identifier (',' identifier)*
772/// FIXME: This should actually parse out the segment, section, attributes and
773/// sizeof_stub fields.
774bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000775 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000776
Daniel Dunbarace63122009-08-11 03:42:33 +0000777 StringRef SectionName;
778 if (ParseIdentifier(SectionName))
779 return Error(Loc, "expected identifier after '.section' directive");
780
781 // Verify there is a following comma.
782 if (!Lexer.is(AsmToken::Comma))
783 return TokError("unexpected token in '.section' directive");
784
Chris Lattnerff4bc462009-08-10 01:39:42 +0000785 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000786 SectionSpec += ",";
787
788 // Add all the tokens until the end of the line, ParseSectionSpecifier will
789 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000790 StringRef EOL = Lexer.LexUntilEndOfStatement();
791 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000792
Chris Lattnerff4bc462009-08-10 01:39:42 +0000793 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000794 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000795 return TokError("unexpected token in '.section' directive");
796 Lexer.Lex();
797
Chris Lattnerff4bc462009-08-10 01:39:42 +0000798
799 StringRef Segment, Section;
800 unsigned TAA, StubSize;
801 std::string ErrorStr =
802 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
803 TAA, StubSize);
804
805 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000806 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000807
Chris Lattner56594f92009-07-31 17:47:16 +0000808 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000809 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
810 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000811 return false;
812}
813
Chris Lattnere15c2d72009-08-10 18:05:55 +0000814/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000815bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
816 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000817 unsigned TAA, unsigned Align,
818 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000819 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000820 return TokError("unexpected token in section switching directive");
821 Lexer.Lex();
822
Chris Lattner56594f92009-07-31 17:47:16 +0000823 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000824 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
825 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000826
827 // Set the implicit alignment, if any.
828 //
829 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
830 // alignment on the section (e.g., if one manually inserts bytes into the
831 // section, then just issueing the section switch directive will not realign
832 // the section. However, this is arguably more reasonable behavior, and there
833 // is no good reason for someone to intentionally emit incorrectly sized
834 // values into the implicitly aligned sections.
835 if (Align)
836 Out.EmitValueToAlignment(Align, 0, 1, 0);
837
Chris Lattner529fb542009-06-24 05:13:15 +0000838 return false;
839}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000840
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000841bool AsmParser::ParseEscapedString(std::string &Data) {
842 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
843
844 Data = "";
845 StringRef Str = Lexer.getTok().getStringContents();
846 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
847 if (Str[i] != '\\') {
848 Data += Str[i];
849 continue;
850 }
851
852 // Recognize escaped characters. Note that this escape semantics currently
853 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
854 ++i;
855 if (i == e)
856 return TokError("unexpected backslash at end of string");
857
858 // Recognize octal sequences.
859 if ((unsigned) (Str[i] - '0') <= 7) {
860 // Consume up to three octal characters.
861 unsigned Value = Str[i] - '0';
862
863 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
864 ++i;
865 Value = Value * 8 + (Str[i] - '0');
866
867 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
868 ++i;
869 Value = Value * 8 + (Str[i] - '0');
870 }
871 }
872
873 if (Value > 255)
874 return TokError("invalid octal escape sequence (out of range)");
875
876 Data += (unsigned char) Value;
877 continue;
878 }
879
880 // Otherwise recognize individual escapes.
881 switch (Str[i]) {
882 default:
883 // Just reject invalid escape sequences for now.
884 return TokError("invalid escape sequence (unrecognized character)");
885
886 case 'b': Data += '\b'; break;
887 case 'f': Data += '\f'; break;
888 case 'n': Data += '\n'; break;
889 case 'r': Data += '\r'; break;
890 case 't': Data += '\t'; break;
891 case '"': Data += '"'; break;
892 case '\\': Data += '\\'; break;
893 }
894 }
895
896 return false;
897}
898
Daniel Dunbara0d14262009-06-24 23:30:00 +0000899/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000900/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000901bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000902 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000903 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000904 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000905 return TokError("expected string in '.ascii' or '.asciz' directive");
906
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000907 std::string Data;
908 if (ParseEscapedString(Data))
909 return true;
910
911 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000912 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000913 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000914
915 Lexer.Lex();
916
Daniel Dunbar3f872332009-07-28 16:08:33 +0000917 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000918 break;
919
Daniel Dunbar3f872332009-07-28 16:08:33 +0000920 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000921 return TokError("unexpected token in '.ascii' or '.asciz' directive");
922 Lexer.Lex();
923 }
924 }
925
926 Lexer.Lex();
927 return false;
928}
929
930/// ParseDirectiveValue
931/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
932bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000933 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000934 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000935 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000936 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000937 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000938 return true;
939
Daniel Dunbar883f9202009-08-31 08:08:50 +0000940 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000941
Daniel Dunbar3f872332009-07-28 16:08:33 +0000942 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000943 break;
944
945 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000946 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000947 return TokError("unexpected token in directive");
948 Lexer.Lex();
949 }
950 }
951
952 Lexer.Lex();
953 return false;
954}
955
956/// ParseDirectiveSpace
957/// ::= .space expression [ , expression ]
958bool AsmParser::ParseDirectiveSpace() {
959 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000960 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000961 return true;
962
963 int64_t FillExpr = 0;
964 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000965 if (Lexer.isNot(AsmToken::EndOfStatement)) {
966 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000967 return TokError("unexpected token in '.space' directive");
968 Lexer.Lex();
969
Daniel Dunbar475839e2009-06-29 20:37:27 +0000970 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000971 return true;
972
973 HasFillExpr = true;
974
Daniel Dunbar3f872332009-07-28 16:08:33 +0000975 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976 return TokError("unexpected token in '.space' directive");
977 }
978
979 Lexer.Lex();
980
981 if (NumBytes <= 0)
982 return TokError("invalid number of bytes in '.space' directive");
983
984 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
985 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +0000986 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000987
988 return false;
989}
990
991/// ParseDirectiveFill
992/// ::= .fill expression , expression , expression
993bool AsmParser::ParseDirectiveFill() {
994 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000995 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996 return true;
997
Daniel Dunbar3f872332009-07-28 16:08:33 +0000998 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000999 return TokError("unexpected token in '.fill' directive");
1000 Lexer.Lex();
1001
1002 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001003 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001004 return true;
1005
Daniel Dunbar3f872332009-07-28 16:08:33 +00001006 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 return TokError("unexpected token in '.fill' directive");
1008 Lexer.Lex();
1009
1010 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001011 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001012 return true;
1013
Daniel Dunbar3f872332009-07-28 16:08:33 +00001014 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015 return TokError("unexpected token in '.fill' directive");
1016
1017 Lexer.Lex();
1018
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001019 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1020 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021
1022 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001023 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024
1025 return false;
1026}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001027
1028/// ParseDirectiveOrg
1029/// ::= .org expression [ , expression ]
1030bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001031 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001032 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001033 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001034 return true;
1035
1036 // Parse optional fill expression.
1037 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001038 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1039 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001040 return TokError("unexpected token in '.org' directive");
1041 Lexer.Lex();
1042
Daniel Dunbar475839e2009-06-29 20:37:27 +00001043 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001044 return true;
1045
Daniel Dunbar3f872332009-07-28 16:08:33 +00001046 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001047 return TokError("unexpected token in '.org' directive");
1048 }
1049
1050 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001051
1052 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1053 // has to be relative to the current section.
1054 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001055
1056 return false;
1057}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001058
1059/// ParseDirectiveAlign
1060/// ::= {.align, ...} expression [ , expression [ , expression ]]
1061bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001062 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001063 int64_t Alignment;
1064 if (ParseAbsoluteExpression(Alignment))
1065 return true;
1066
1067 SMLoc MaxBytesLoc;
1068 bool HasFillExpr = false;
1069 int64_t FillExpr = 0;
1070 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001071 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1072 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001073 return TokError("unexpected token in directive");
1074 Lexer.Lex();
1075
1076 // The fill expression can be omitted while specifying a maximum number of
1077 // alignment bytes, e.g:
1078 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001080 HasFillExpr = true;
1081 if (ParseAbsoluteExpression(FillExpr))
1082 return true;
1083 }
1084
Daniel Dunbar3f872332009-07-28 16:08:33 +00001085 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1086 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001087 return TokError("unexpected token in directive");
1088 Lexer.Lex();
1089
1090 MaxBytesLoc = Lexer.getLoc();
1091 if (ParseAbsoluteExpression(MaxBytesToFill))
1092 return true;
1093
Daniel Dunbar3f872332009-07-28 16:08:33 +00001094 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001095 return TokError("unexpected token in directive");
1096 }
1097 }
1098
1099 Lexer.Lex();
1100
1101 if (!HasFillExpr) {
1102 // FIXME: Sometimes fill with nop.
1103 FillExpr = 0;
1104 }
1105
1106 // Compute alignment in bytes.
1107 if (IsPow2) {
1108 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001109 if (Alignment >= 32) {
1110 Error(AlignmentLoc, "invalid alignment value");
1111 Alignment = 31;
1112 }
1113
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001114 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001115 }
1116
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001117 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001118 if (MaxBytesLoc.isValid()) {
1119 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001120 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1121 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001122 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001123 }
1124
1125 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001126 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1127 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001128 MaxBytesToFill = 0;
1129 }
1130 }
1131
1132 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1133 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1134
1135 return false;
1136}
1137
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001138/// ParseDirectiveSymbolAttribute
1139/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1140bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001141 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001142 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001143 StringRef Name;
1144
1145 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001146 return TokError("expected identifier in directive");
1147
Daniel Dunbar959fd882009-08-26 22:13:22 +00001148 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001149
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001150 Out.EmitSymbolAttribute(Sym, Attr);
1151
Daniel Dunbar3f872332009-07-28 16:08:33 +00001152 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001153 break;
1154
Daniel Dunbar3f872332009-07-28 16:08:33 +00001155 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001156 return TokError("unexpected token in directive");
1157 Lexer.Lex();
1158 }
1159 }
1160
1161 Lexer.Lex();
1162 return false;
1163}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001164
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001165/// ParseDirectiveDarwinSymbolDesc
1166/// ::= .desc identifier , expression
1167bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001168 StringRef Name;
1169 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001170 return TokError("expected identifier in directive");
1171
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001172 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001173 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001174
Daniel Dunbar3f872332009-07-28 16:08:33 +00001175 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001176 return TokError("unexpected token in '.desc' directive");
1177 Lexer.Lex();
1178
1179 SMLoc DescLoc = Lexer.getLoc();
1180 int64_t DescValue;
1181 if (ParseAbsoluteExpression(DescValue))
1182 return true;
1183
Daniel Dunbar3f872332009-07-28 16:08:33 +00001184 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001185 return TokError("unexpected token in '.desc' directive");
1186
1187 Lexer.Lex();
1188
1189 // Set the n_desc field of this Symbol to this DescValue
1190 Out.EmitSymbolDesc(Sym, DescValue);
1191
1192 return false;
1193}
1194
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001195/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001196/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1197bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001198 SMLoc IDLoc = Lexer.getLoc();
1199 StringRef Name;
1200 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001201 return TokError("expected identifier in directive");
1202
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001203 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001204 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001205
Daniel Dunbar3f872332009-07-28 16:08:33 +00001206 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001207 return TokError("unexpected token in directive");
1208 Lexer.Lex();
1209
1210 int64_t Size;
1211 SMLoc SizeLoc = Lexer.getLoc();
1212 if (ParseAbsoluteExpression(Size))
1213 return true;
1214
1215 int64_t Pow2Alignment = 0;
1216 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001217 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001218 Lexer.Lex();
1219 Pow2AlignmentLoc = Lexer.getLoc();
1220 if (ParseAbsoluteExpression(Pow2Alignment))
1221 return true;
1222 }
1223
Daniel Dunbar3f872332009-07-28 16:08:33 +00001224 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001225 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001226
1227 Lexer.Lex();
1228
Chris Lattner1fc3d752009-07-09 17:25:12 +00001229 // NOTE: a size of zero for a .comm should create a undefined symbol
1230 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001231 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001232 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1233 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001234
1235 // NOTE: The alignment in the directive is a power of 2 value, the assember
1236 // may internally end up wanting an alignment in bytes.
1237 // FIXME: Diagnose overflow.
1238 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001239 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1240 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001241
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001242 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001243 return Error(IDLoc, "invalid symbol redefinition");
1244
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001245 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001246 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001247 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001248 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1249 MCSectionMachO::S_ZEROFILL, 0,
1250 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001251 Sym, Size, 1 << Pow2Alignment);
1252 return false;
1253 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001254
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001255 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001256 return false;
1257}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001258
1259/// ParseDirectiveDarwinZerofill
1260/// ::= .zerofill segname , sectname [, identifier , size_expression [
1261/// , align_expression ]]
1262bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001263 // FIXME: Handle quoted names here.
1264
Daniel Dunbar3f872332009-07-28 16:08:33 +00001265 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001266 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001267 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001268 Lexer.Lex();
1269
Daniel Dunbar3f872332009-07-28 16:08:33 +00001270 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001271 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001272 Lexer.Lex();
1273
Daniel Dunbar3f872332009-07-28 16:08:33 +00001274 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001275 return TokError("expected section name after comma in '.zerofill' "
1276 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001277 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001278 Lexer.Lex();
1279
Chris Lattner9be3fee2009-07-10 22:20:30 +00001280 // If this is the end of the line all that was wanted was to create the
1281 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001282 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001283 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001284 Out.EmitZerofill(getMachOSection(Segment, Section,
1285 MCSectionMachO::S_ZEROFILL, 0,
1286 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001287 return false;
1288 }
1289
Daniel Dunbar3f872332009-07-28 16:08:33 +00001290 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001291 return TokError("unexpected token in directive");
1292 Lexer.Lex();
1293
Daniel Dunbar3f872332009-07-28 16:08:33 +00001294 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001295 return TokError("expected identifier in directive");
1296
1297 // handle the identifier as the key symbol.
1298 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001299 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001300 Lexer.Lex();
1301
Daniel Dunbar3f872332009-07-28 16:08:33 +00001302 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001303 return TokError("unexpected token in directive");
1304 Lexer.Lex();
1305
1306 int64_t Size;
1307 SMLoc SizeLoc = Lexer.getLoc();
1308 if (ParseAbsoluteExpression(Size))
1309 return true;
1310
1311 int64_t Pow2Alignment = 0;
1312 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001313 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001314 Lexer.Lex();
1315 Pow2AlignmentLoc = Lexer.getLoc();
1316 if (ParseAbsoluteExpression(Pow2Alignment))
1317 return true;
1318 }
1319
Daniel Dunbar3f872332009-07-28 16:08:33 +00001320 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001321 return TokError("unexpected token in '.zerofill' directive");
1322
1323 Lexer.Lex();
1324
1325 if (Size < 0)
1326 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1327 "than zero");
1328
1329 // NOTE: The alignment in the directive is a power of 2 value, the assember
1330 // may internally end up wanting an alignment in bytes.
1331 // FIXME: Diagnose overflow.
1332 if (Pow2Alignment < 0)
1333 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1334 "can't be less than zero");
1335
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001336 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001337 return Error(IDLoc, "invalid symbol redefinition");
1338
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001339 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001340 //
1341 // FIXME: Arch specific.
1342 Out.EmitZerofill(getMachOSection(Segment, Section,
1343 MCSectionMachO::S_ZEROFILL, 0,
1344 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001345 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001346
1347 return false;
1348}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001349
1350/// ParseDirectiveDarwinSubsectionsViaSymbols
1351/// ::= .subsections_via_symbols
1352bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001353 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001354 return TokError("unexpected token in '.subsections_via_symbols' directive");
1355
1356 Lexer.Lex();
1357
Kevin Enderbyf96db462009-07-16 17:56:39 +00001358 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001359
1360 return false;
1361}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001362
1363/// ParseDirectiveAbort
1364/// ::= .abort [ "abort_string" ]
1365bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001366 // FIXME: Use loc from directive.
1367 SMLoc Loc = Lexer.getLoc();
1368
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001369 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001370 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1371 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001372 return TokError("expected string in '.abort' directive");
1373
Daniel Dunbar419aded2009-07-28 16:38:40 +00001374 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001375
1376 Lexer.Lex();
1377 }
1378
Daniel Dunbar3f872332009-07-28 16:08:33 +00001379 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001380 return TokError("unexpected token in '.abort' directive");
1381
1382 Lexer.Lex();
1383
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001384 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001385 if (Str.empty())
1386 Error(Loc, ".abort detected. Assembly stopping.");
1387 else
1388 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001389
1390 return false;
1391}
Kevin Enderby71148242009-07-14 21:35:03 +00001392
1393/// ParseDirectiveLsym
1394/// ::= .lsym identifier , expression
1395bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001396 StringRef Name;
1397 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001398 return TokError("expected identifier in directive");
1399
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001400 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001401 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001402
Daniel Dunbar3f872332009-07-28 16:08:33 +00001403 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001404 return TokError("unexpected token in '.lsym' directive");
1405 Lexer.Lex();
1406
Daniel Dunbar821e3332009-08-31 08:09:28 +00001407 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001408 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001409 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001410 return true;
1411
Daniel Dunbar3f872332009-07-28 16:08:33 +00001412 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001413 return TokError("unexpected token in '.lsym' directive");
1414
1415 Lexer.Lex();
1416
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001417 // We don't currently support this directive.
1418 //
1419 // FIXME: Diagnostic location!
1420 (void) Sym;
1421 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001422}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001423
1424/// ParseDirectiveInclude
1425/// ::= .include "filename"
1426bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001427 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001428 return TokError("expected string in '.include' directive");
1429
Daniel Dunbar419aded2009-07-28 16:38:40 +00001430 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001431 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001432 Lexer.Lex();
1433
Daniel Dunbar3f872332009-07-28 16:08:33 +00001434 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001435 return TokError("unexpected token in '.include' directive");
1436
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001437 // Strip the quotes.
1438 Filename = Filename.substr(1, Filename.size()-2);
1439
1440 // Attempt to switch the lexer to the included file before consuming the end
1441 // of statement to avoid losing it when we switch.
1442 if (Lexer.EnterIncludeFile(Filename)) {
1443 Lexer.PrintMessage(IncludeLoc,
1444 "Could not find include file '" + Filename + "'",
1445 "error");
1446 return true;
1447 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001448
1449 return false;
1450}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001451
1452/// ParseDirectiveDarwinDumpOrLoad
1453/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001454bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001455 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001456 return TokError("expected string in '.dump' or '.load' directive");
1457
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001458 Lexer.Lex();
1459
Daniel Dunbar3f872332009-07-28 16:08:33 +00001460 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001461 return TokError("unexpected token in '.dump' or '.load' directive");
1462
1463 Lexer.Lex();
1464
Kevin Enderby5026ae42009-07-20 20:25:37 +00001465 // FIXME: If/when .dump and .load are implemented they will be done in the
1466 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001467 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001468 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001469 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001470 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001471
1472 return false;
1473}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001474
1475/// ParseDirectiveIf
1476/// ::= .if expression
1477bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1478 // Consume the identifier that was the .if directive
1479 Lexer.Lex();
1480
1481 TheCondStack.push_back(TheCondState);
1482 TheCondState.TheCond = AsmCond::IfCond;
1483 if(TheCondState.Ignore) {
1484 EatToEndOfStatement();
1485 }
1486 else {
1487 int64_t ExprValue;
1488 if (ParseAbsoluteExpression(ExprValue))
1489 return true;
1490
1491 if (Lexer.isNot(AsmToken::EndOfStatement))
1492 return TokError("unexpected token in '.if' directive");
1493
1494 Lexer.Lex();
1495
1496 TheCondState.CondMet = ExprValue;
1497 TheCondState.Ignore = !TheCondState.CondMet;
1498 }
1499
1500 return false;
1501}
1502
1503/// ParseDirectiveElseIf
1504/// ::= .elseif expression
1505bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1506 if (TheCondState.TheCond != AsmCond::IfCond &&
1507 TheCondState.TheCond != AsmCond::ElseIfCond)
1508 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1509 " an .elseif");
1510 TheCondState.TheCond = AsmCond::ElseIfCond;
1511
1512 // Consume the identifier that was the .elseif directive
1513 Lexer.Lex();
1514
1515 bool LastIgnoreState = false;
1516 if (!TheCondStack.empty())
1517 LastIgnoreState = TheCondStack.back().Ignore;
1518 if (LastIgnoreState || TheCondState.CondMet) {
1519 TheCondState.Ignore = true;
1520 EatToEndOfStatement();
1521 }
1522 else {
1523 int64_t ExprValue;
1524 if (ParseAbsoluteExpression(ExprValue))
1525 return true;
1526
1527 if (Lexer.isNot(AsmToken::EndOfStatement))
1528 return TokError("unexpected token in '.elseif' directive");
1529
1530 Lexer.Lex();
1531 TheCondState.CondMet = ExprValue;
1532 TheCondState.Ignore = !TheCondState.CondMet;
1533 }
1534
1535 return false;
1536}
1537
1538/// ParseDirectiveElse
1539/// ::= .else
1540bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1541 // Consume the identifier that was the .else directive
1542 Lexer.Lex();
1543
1544 if (Lexer.isNot(AsmToken::EndOfStatement))
1545 return TokError("unexpected token in '.else' directive");
1546
1547 Lexer.Lex();
1548
1549 if (TheCondState.TheCond != AsmCond::IfCond &&
1550 TheCondState.TheCond != AsmCond::ElseIfCond)
1551 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1552 ".elseif");
1553 TheCondState.TheCond = AsmCond::ElseCond;
1554 bool LastIgnoreState = false;
1555 if (!TheCondStack.empty())
1556 LastIgnoreState = TheCondStack.back().Ignore;
1557 if (LastIgnoreState || TheCondState.CondMet)
1558 TheCondState.Ignore = true;
1559 else
1560 TheCondState.Ignore = false;
1561
1562 return false;
1563}
1564
1565/// ParseDirectiveEndIf
1566/// ::= .endif
1567bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1568 // Consume the identifier that was the .endif directive
1569 Lexer.Lex();
1570
1571 if (Lexer.isNot(AsmToken::EndOfStatement))
1572 return TokError("unexpected token in '.endif' directive");
1573
1574 Lexer.Lex();
1575
1576 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1577 TheCondStack.empty())
1578 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1579 ".else");
1580 if (!TheCondStack.empty()) {
1581 TheCondState = TheCondStack.back();
1582 TheCondStack.pop_back();
1583 }
1584
1585 return false;
1586}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001587
1588/// ParseDirectiveFile
1589/// ::= .file [number] string
1590bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1591 // FIXME: I'm not sure what this is.
1592 int64_t FileNumber = -1;
1593 if (Lexer.is(AsmToken::Integer)) {
1594 FileNumber = Lexer.getTok().getIntVal();
1595 Lexer.Lex();
1596
1597 if (FileNumber < 1)
1598 return TokError("file number less than one");
1599 }
1600
1601 if (Lexer.isNot(AsmToken::String))
1602 return TokError("unexpected token in '.file' directive");
1603
1604 StringRef FileName = Lexer.getTok().getString();
1605 Lexer.Lex();
1606
1607 if (Lexer.isNot(AsmToken::EndOfStatement))
1608 return TokError("unexpected token in '.file' directive");
1609
1610 // FIXME: Do something with the .file.
1611
1612 return false;
1613}
1614
1615/// ParseDirectiveLine
1616/// ::= .line [number]
1617bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1618 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1619 if (Lexer.isNot(AsmToken::Integer))
1620 return TokError("unexpected token in '.line' directive");
1621
1622 int64_t LineNumber = Lexer.getTok().getIntVal();
1623 (void) LineNumber;
1624 Lexer.Lex();
1625
1626 // FIXME: Do something with the .line.
1627 }
1628
1629 if (Lexer.isNot(AsmToken::EndOfStatement))
1630 return TokError("unexpected token in '.file' directive");
1631
1632 return false;
1633}
1634
1635
1636/// ParseDirectiveLoc
1637/// ::= .loc number [number [number]]
1638bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1639 if (Lexer.isNot(AsmToken::Integer))
1640 return TokError("unexpected token in '.loc' directive");
1641
1642 // FIXME: What are these fields?
1643 int64_t FileNumber = Lexer.getTok().getIntVal();
1644 (void) FileNumber;
1645 // FIXME: Validate file.
1646
1647 Lexer.Lex();
1648 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1649 if (Lexer.isNot(AsmToken::Integer))
1650 return TokError("unexpected token in '.loc' directive");
1651
1652 int64_t Param2 = Lexer.getTok().getIntVal();
1653 (void) Param2;
1654 Lexer.Lex();
1655
1656 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1657 if (Lexer.isNot(AsmToken::Integer))
1658 return TokError("unexpected token in '.loc' directive");
1659
1660 int64_t Param3 = Lexer.getTok().getIntVal();
1661 (void) Param3;
1662 Lexer.Lex();
1663
1664 // FIXME: Do something with the .loc.
1665 }
1666 }
1667
1668 if (Lexer.isNot(AsmToken::EndOfStatement))
1669 return TokError("unexpected token in '.file' directive");
1670
1671 return false;
1672}
1673