blob: aae27f5d7649a614af8f92d87c549459bb372938 [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
Chris Lattnerebb89b42009-09-27 21:16:52 +000035AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
36 const MCAsmInfo &_MAI)
37 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
38 SectionUniquingMap(0) {
39 // Debugging directives.
40 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
41 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
42 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
43}
44
45
46
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000047AsmParser::~AsmParser() {
48 // If we have the MachO uniquing map, free it.
49 delete (MachOUniqueMapTy*)SectionUniquingMap;
50}
51
52const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
53 const StringRef &Section,
54 unsigned TypeAndAttributes,
55 unsigned Reserved2,
56 SectionKind Kind) const {
57 // We unique sections by their segment/section pair. The returned section
58 // may not have the same flags as the requested section, if so this should be
59 // diagnosed by the client as an error.
60
61 // Create the map if it doesn't already exist.
62 if (SectionUniquingMap == 0)
63 SectionUniquingMap = new MachOUniqueMapTy();
64 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
65
66 // Form the name to look up.
67 SmallString<64> Name;
68 Name += Segment;
69 Name.push_back(',');
70 Name += Section;
71
72 // Do the lookup, if we have a hit, return it.
73 const MCSectionMachO *&Entry = Map[Name.str()];
74
75 // FIXME: This should validate the type and attributes.
76 if (Entry) return Entry;
77
78 // Otherwise, return a new section.
79 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
80 Reserved2, Kind, Ctx);
81}
82
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000083void AsmParser::Warning(SMLoc L, const Twine &Msg) {
84 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000085}
86
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000087bool AsmParser::Error(SMLoc L, const Twine &Msg) {
88 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000089 return true;
90}
91
92bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000093 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000094 return true;
95}
96
Chris Lattner27aa7d22009-06-21 20:16:42 +000097bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000098 // Create the initial section.
99 //
100 // FIXME: Support -n.
101 // FIXME: Target hook & command line option for initial section.
102 Out.SwitchSection(getMachOSection("__TEXT", "__text",
103 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
104 0, SectionKind()));
105
106
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000107 // Prime the lexer.
108 Lexer.Lex();
109
Chris Lattnerb717fb02009-07-02 21:53:43 +0000110 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000111
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000112 AsmCond StartingCondState = TheCondState;
113
Chris Lattnerb717fb02009-07-02 21:53:43 +0000114 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000115 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000116 // Handle conditional assembly here before calling ParseStatement()
117 if (Lexer.getKind() == AsmToken::Identifier) {
118 // If we have an identifier, handle it as the key symbol.
119 AsmToken ID = Lexer.getTok();
120 SMLoc IDLoc = ID.getLoc();
121 StringRef IDVal = ID.getString();
122
123 if (IDVal == ".if" ||
124 IDVal == ".elseif" ||
125 IDVal == ".else" ||
126 IDVal == ".endif") {
127 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
128 continue;
129 HadError = true;
130 EatToEndOfStatement();
131 continue;
132 }
133 }
134 if (TheCondState.Ignore) {
135 EatToEndOfStatement();
136 continue;
137 }
138
Chris Lattnerb717fb02009-07-02 21:53:43 +0000139 if (!ParseStatement()) continue;
140
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000141 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000142 HadError = true;
143 EatToEndOfStatement();
144 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000145
146 if (TheCondState.TheCond != StartingCondState.TheCond ||
147 TheCondState.Ignore != StartingCondState.Ignore)
148 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000149
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000150 if (!HadError)
151 Out.Finish();
152
Chris Lattnerb717fb02009-07-02 21:53:43 +0000153 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000154}
155
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000156/// ParseConditionalAssemblyDirectives - parse the conditional assembly
157/// directives
158bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
159 SMLoc DirectiveLoc) {
160 if (Directive == ".if")
161 return ParseDirectiveIf(DirectiveLoc);
162 if (Directive == ".elseif")
163 return ParseDirectiveElseIf(DirectiveLoc);
164 if (Directive == ".else")
165 return ParseDirectiveElse(DirectiveLoc);
166 if (Directive == ".endif")
167 return ParseDirectiveEndIf(DirectiveLoc);
168 return true;
169}
170
Chris Lattner2cf5f142009-06-22 01:29:09 +0000171/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
172void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000173 while (Lexer.isNot(AsmToken::EndOfStatement) &&
174 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000175 Lexer.Lex();
176
177 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000178 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000179 Lexer.Lex();
180}
181
Chris Lattnerc4193832009-06-22 05:51:26 +0000182
Chris Lattner74ec1a32009-06-22 06:32:03 +0000183/// ParseParenExpr - Parse a paren expression and return it.
184/// NOTE: This assumes the leading '(' has already been consumed.
185///
186/// parenexpr ::= expr)
187///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000188bool AsmParser::ParseParenExpr(const MCExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000189 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000190 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000191 return TokError("expected ')' in parentheses expression");
192 Lexer.Lex();
193 return false;
194}
Chris Lattnerc4193832009-06-22 05:51:26 +0000195
Daniel Dunbar959fd882009-08-26 22:13:22 +0000196MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
197 if (MCSymbol *S = Ctx.LookupSymbol(Name))
198 return S;
199
200 // If the label starts with L it is an assembler temporary label.
201 if (Name.startswith("L"))
202 return Ctx.CreateTemporarySymbol(Name);
203
204 return Ctx.CreateSymbol(Name);
205}
206
Chris Lattner74ec1a32009-06-22 06:32:03 +0000207/// ParsePrimaryExpr - Parse a primary expression and return it.
208/// primaryexpr ::= (parenexpr
209/// primaryexpr ::= symbol
210/// primaryexpr ::= number
211/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000212bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000213 switch (Lexer.getKind()) {
214 default:
215 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000216 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000217 Lexer.Lex(); // Eat the operator.
218 if (ParsePrimaryExpr(Res))
219 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000220 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000221 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000222 case AsmToken::String:
Chris Lattner3b13d362009-09-16 04:12:47 +0000223 case AsmToken::Identifier:
Chris Lattnerc4193832009-06-22 05:51:26 +0000224 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000225 // handle things like LFOO+4.
Chris Lattner3b13d362009-09-16 04:12:47 +0000226 Res = MCSymbolRefExpr::Create(Lexer.getTok().getIdentifier(), getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000227 Lexer.Lex(); // Eat identifier.
228 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000229 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000230 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000231 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000232 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000233 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000234 Lexer.Lex(); // Eat the '('.
235 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000236 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000237 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000238 if (ParsePrimaryExpr(Res))
239 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000240 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000241 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000242 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000243 Lexer.Lex(); // Eat the operator.
244 if (ParsePrimaryExpr(Res))
245 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000246 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000247 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000248 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000249 Lexer.Lex(); // Eat the operator.
250 if (ParsePrimaryExpr(Res))
251 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000252 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000254 }
255}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000256
257/// ParseExpression - Parse an expression and return it.
258///
259/// expr ::= expr +,- expr -> lowest.
260/// expr ::= expr |,^,&,! expr -> middle.
261/// expr ::= expr *,/,%,<<,>> expr -> highest.
262/// expr ::= primaryexpr
263///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000264bool AsmParser::ParseExpression(const MCExpr *&Res) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000266 return ParsePrimaryExpr(Res) ||
267 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000268}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000269
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000270bool AsmParser::ParseParenExpression(const MCExpr *&Res) {
271 if (ParseParenExpr(Res))
272 return true;
273
274 return false;
275}
276
Daniel Dunbar475839e2009-06-29 20:37:27 +0000277bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000278 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000279
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000280 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000281 if (ParseExpression(Expr))
282 return true;
283
284 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000285 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000286
287 return false;
288}
289
Daniel Dunbar3f872332009-07-28 16:08:33 +0000290static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000291 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000292 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000293 default:
294 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000295
296 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000297 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000298 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000299 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000300 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000301 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000302 return 1;
303
304 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000305 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000306 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000307 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000308 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000309 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000311 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000312 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000313 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000314 case AsmToken::ExclaimEqual:
315 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000316 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000317 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000318 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000319 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000320 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000321 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000322 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000323 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000324 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000325 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000326 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000327 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000328 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 return 2;
330
331 // Intermediate Precedence: |, &, ^
332 //
333 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000334 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000335 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000336 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000337 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000338 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000339 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000340 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000341 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342 return 3;
343
344 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000349 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000351 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000352 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000353 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000354 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000355 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000356 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000360 }
361}
362
363
364/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
365/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000366bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000367 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000368 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000369 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000370
371 // If the next token is lower precedence than we are allowed to eat, return
372 // successfully with what we ate already.
373 if (TokPrec < Precedence)
374 return false;
375
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000376 Lexer.Lex();
377
378 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000379 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000380 if (ParsePrimaryExpr(RHS)) return true;
381
382 // If BinOp binds less tightly with RHS than the operator after RHS, let
383 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000384 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000386 if (TokPrec < NextTokPrec) {
387 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
388 }
389
Daniel Dunbar475839e2009-06-29 20:37:27 +0000390 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000391 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000392 }
393}
394
Chris Lattnerc4193832009-06-22 05:51:26 +0000395
396
397
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000398/// ParseStatement:
399/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000400/// ::= Label* Directive ...Operands... EndOfStatement
401/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000402bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000403 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000404 Lexer.Lex();
405 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000406 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000407
408 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000409 AsmToken ID = Lexer.getTok();
410 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000411 StringRef IDVal;
412 if (ParseIdentifier(IDVal))
413 return TokError("unexpected token at start of statement");
414
415 // FIXME: Recurse on local labels?
416
417 // See what kind of statement we have.
418 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000420 // identifier ':' -> Label.
421 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000422
423 // Diagnose attempt to use a variable as a label.
424 //
425 // FIXME: Diagnostics. Note the location of the definition as a label.
426 // FIXME: This doesn't diagnose assignment to a symbol which has been
427 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000428 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000429 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000430 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000431
Daniel Dunbar959fd882009-08-26 22:13:22 +0000432 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000433 Out.EmitLabel(Sym);
434
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000435 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000436 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000437
Daniel Dunbar3f872332009-07-28 16:08:33 +0000438 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000439 // identifier '=' ... -> assignment statement
440 Lexer.Lex();
441
Daniel Dunbare2ace502009-08-31 08:09:09 +0000442 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000443
444 default: // Normal instruction or directive.
445 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000446 }
447
448 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000449 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000450 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000451 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000452 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000453 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000454 // FIXME: This changes behavior based on the -static flag to the
455 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000456 return ParseDirectiveSectionSwitch("__TEXT", "__text",
457 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000458 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000459 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000460 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000461 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000462 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000463 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
464 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000465 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000466 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000467 MCSectionMachO::S_4BYTE_LITERALS,
468 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000470 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000471 MCSectionMachO::S_8BYTE_LITERALS,
472 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000473 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000474 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000475 MCSectionMachO::S_16BYTE_LITERALS,
476 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000477 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000478 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000479 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000480 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000481 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000482 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000483 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000484 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
485
486 // FIXME: The assembler manual claims that this has the self modify code
487 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000488 if (IDVal == ".symbol_stub")
489 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
490 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000491 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
492 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 0, 16);
494 // FIXME: PowerPC only?
495 if (IDVal == ".picsymbol_stub")
496 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
497 MCSectionMachO::S_SYMBOL_STUBS |
498 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
499 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000500 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000501 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000502 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000503 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
504
505 // FIXME: The section names of these two are misspelled in the assembler
506 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000508 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
509 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
510 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000512 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
513 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
514 4);
515
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000516 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000517 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000519 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000520 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
521 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000523 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000524 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
525 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000527 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000528
529
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000531 return ParseDirectiveSectionSwitch("__OBJC", "__class",
532 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
535 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
538 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000540 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
541 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
544 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
547 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
550 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
553 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
556 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
557 MCSectionMachO::S_LITERAL_POINTERS,
558 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000559 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000560 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
561 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
562 MCSectionMachO::S_LITERAL_POINTERS,
563 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__OBJC", "__category",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000580 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
581 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000583 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
584 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000586 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
587 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
590 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000591
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000592 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000594 return ParseDirectiveSet();
595
Daniel Dunbara0d14262009-06-24 23:30:00 +0000596 // Data directives
597
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000599 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000601 return ParseDirectiveAscii(true);
602
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000604 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000605 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000606 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000608 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000610 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000611
612 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000614 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000616 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000618 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000620 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000622 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000624 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000626 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000628 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
629
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000631 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000632
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000634 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000636 return ParseDirectiveSpace();
637
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000638 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000639
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000641 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000643 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000645 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000647 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000649 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000651 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000653 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000655 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000657 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000659 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000661 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000663 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
664
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000666 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000668 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000670 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000672 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000674 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000675
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000677 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000679 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000681 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000683 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000685 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000686
Chris Lattnerebb89b42009-09-27 21:16:52 +0000687 // Look up the handler in the handler table,
688 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
689 if (Handler)
690 return (this->*Handler)(IDVal, IDLoc);
691
Kevin Enderby9c656452009-09-10 20:51:44 +0000692 // Target hook for parsing target specific directives.
693 if (!getTargetParser().ParseDirective(ID))
694 return false;
695
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000696 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000697 EatToEndOfStatement();
698 return false;
699 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000700
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000701 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000702 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000703 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000704
Daniel Dunbar3f872332009-07-28 16:08:33 +0000705 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000706 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000707
708 // Eat the end of statement marker.
709 Lexer.Lex();
710
711 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000712 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000713
714 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000715 return false;
716}
Chris Lattner9a023f72009-06-24 04:43:34 +0000717
Daniel Dunbare2ace502009-08-31 08:09:09 +0000718bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000719 // FIXME: Use better location, we should use proper tokens.
720 SMLoc EqualLoc = Lexer.getLoc();
721
Daniel Dunbar821e3332009-08-31 08:09:28 +0000722 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000723 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000724 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000725 return true;
726
Daniel Dunbar3f872332009-07-28 16:08:33 +0000727 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000728 return TokError("unexpected token in assignment");
729
730 // Eat the end of statement marker.
731 Lexer.Lex();
732
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000733 // Diagnose assignment to a label.
734 //
735 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000736 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000737 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar959fd882009-08-26 22:13:22 +0000738 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000739 if (!Sym->isUndefined() && !Sym->isAbsolute())
740 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000741
742 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000743 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000744
745 return false;
746}
747
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000748/// ParseIdentifier:
749/// ::= identifier
750/// ::= string
751bool AsmParser::ParseIdentifier(StringRef &Res) {
752 if (Lexer.isNot(AsmToken::Identifier) &&
753 Lexer.isNot(AsmToken::String))
754 return true;
755
756 Res = Lexer.getTok().getIdentifier();
757
758 Lexer.Lex(); // Consume the identifier token.
759
760 return false;
761}
762
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000763/// ParseDirectiveSet:
764/// ::= .set identifier ',' expression
765bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000766 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000767
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000768 if (ParseIdentifier(Name))
769 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000770
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000771 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000772 return TokError("unexpected token in '.set'");
773 Lexer.Lex();
774
Daniel Dunbare2ace502009-08-31 08:09:09 +0000775 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000776}
777
Chris Lattner9a023f72009-06-24 04:43:34 +0000778/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000779/// ::= .section identifier (',' identifier)*
780/// FIXME: This should actually parse out the segment, section, attributes and
781/// sizeof_stub fields.
782bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000783 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000784
Daniel Dunbarace63122009-08-11 03:42:33 +0000785 StringRef SectionName;
786 if (ParseIdentifier(SectionName))
787 return Error(Loc, "expected identifier after '.section' directive");
788
789 // Verify there is a following comma.
790 if (!Lexer.is(AsmToken::Comma))
791 return TokError("unexpected token in '.section' directive");
792
Chris Lattnerff4bc462009-08-10 01:39:42 +0000793 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000794 SectionSpec += ",";
795
796 // Add all the tokens until the end of the line, ParseSectionSpecifier will
797 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000798 StringRef EOL = Lexer.LexUntilEndOfStatement();
799 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000800
Chris Lattnerff4bc462009-08-10 01:39:42 +0000801 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000802 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000803 return TokError("unexpected token in '.section' directive");
804 Lexer.Lex();
805
Chris Lattnerff4bc462009-08-10 01:39:42 +0000806
807 StringRef Segment, Section;
808 unsigned TAA, StubSize;
809 std::string ErrorStr =
810 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
811 TAA, StubSize);
812
813 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000814 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000815
Chris Lattner56594f92009-07-31 17:47:16 +0000816 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000817 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
818 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000819 return false;
820}
821
Chris Lattnere15c2d72009-08-10 18:05:55 +0000822/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000823bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
824 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000825 unsigned TAA, unsigned Align,
826 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000827 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000828 return TokError("unexpected token in section switching directive");
829 Lexer.Lex();
830
Chris Lattner56594f92009-07-31 17:47:16 +0000831 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000832 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
833 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000834
835 // Set the implicit alignment, if any.
836 //
837 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
838 // alignment on the section (e.g., if one manually inserts bytes into the
839 // section, then just issueing the section switch directive will not realign
840 // the section. However, this is arguably more reasonable behavior, and there
841 // is no good reason for someone to intentionally emit incorrectly sized
842 // values into the implicitly aligned sections.
843 if (Align)
844 Out.EmitValueToAlignment(Align, 0, 1, 0);
845
Chris Lattner529fb542009-06-24 05:13:15 +0000846 return false;
847}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000848
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000849bool AsmParser::ParseEscapedString(std::string &Data) {
850 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
851
852 Data = "";
853 StringRef Str = Lexer.getTok().getStringContents();
854 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
855 if (Str[i] != '\\') {
856 Data += Str[i];
857 continue;
858 }
859
860 // Recognize escaped characters. Note that this escape semantics currently
861 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
862 ++i;
863 if (i == e)
864 return TokError("unexpected backslash at end of string");
865
866 // Recognize octal sequences.
867 if ((unsigned) (Str[i] - '0') <= 7) {
868 // Consume up to three octal characters.
869 unsigned Value = Str[i] - '0';
870
871 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
872 ++i;
873 Value = Value * 8 + (Str[i] - '0');
874
875 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
876 ++i;
877 Value = Value * 8 + (Str[i] - '0');
878 }
879 }
880
881 if (Value > 255)
882 return TokError("invalid octal escape sequence (out of range)");
883
884 Data += (unsigned char) Value;
885 continue;
886 }
887
888 // Otherwise recognize individual escapes.
889 switch (Str[i]) {
890 default:
891 // Just reject invalid escape sequences for now.
892 return TokError("invalid escape sequence (unrecognized character)");
893
894 case 'b': Data += '\b'; break;
895 case 'f': Data += '\f'; break;
896 case 'n': Data += '\n'; break;
897 case 'r': Data += '\r'; break;
898 case 't': Data += '\t'; break;
899 case '"': Data += '"'; break;
900 case '\\': Data += '\\'; break;
901 }
902 }
903
904 return false;
905}
906
Daniel Dunbara0d14262009-06-24 23:30:00 +0000907/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000908/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000909bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000910 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000911 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000912 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000913 return TokError("expected string in '.ascii' or '.asciz' directive");
914
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000915 std::string Data;
916 if (ParseEscapedString(Data))
917 return true;
918
919 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000920 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000921 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000922
923 Lexer.Lex();
924
Daniel Dunbar3f872332009-07-28 16:08:33 +0000925 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000926 break;
927
Daniel Dunbar3f872332009-07-28 16:08:33 +0000928 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929 return TokError("unexpected token in '.ascii' or '.asciz' directive");
930 Lexer.Lex();
931 }
932 }
933
934 Lexer.Lex();
935 return false;
936}
937
938/// ParseDirectiveValue
939/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
940bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000941 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000942 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000943 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000944 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000945 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000946 return true;
947
Daniel Dunbar883f9202009-08-31 08:08:50 +0000948 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000949
Daniel Dunbar3f872332009-07-28 16:08:33 +0000950 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000951 break;
952
953 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000954 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000955 return TokError("unexpected token in directive");
956 Lexer.Lex();
957 }
958 }
959
960 Lexer.Lex();
961 return false;
962}
963
964/// ParseDirectiveSpace
965/// ::= .space expression [ , expression ]
966bool AsmParser::ParseDirectiveSpace() {
967 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000968 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969 return true;
970
971 int64_t FillExpr = 0;
972 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000973 if (Lexer.isNot(AsmToken::EndOfStatement)) {
974 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000975 return TokError("unexpected token in '.space' directive");
976 Lexer.Lex();
977
Daniel Dunbar475839e2009-06-29 20:37:27 +0000978 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000979 return true;
980
981 HasFillExpr = true;
982
Daniel Dunbar3f872332009-07-28 16:08:33 +0000983 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000984 return TokError("unexpected token in '.space' directive");
985 }
986
987 Lexer.Lex();
988
989 if (NumBytes <= 0)
990 return TokError("invalid number of bytes in '.space' directive");
991
992 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
993 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +0000994 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000995
996 return false;
997}
998
999/// ParseDirectiveFill
1000/// ::= .fill expression , expression , expression
1001bool AsmParser::ParseDirectiveFill() {
1002 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001003 if (ParseAbsoluteExpression(NumValues))
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 FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001011 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001012 return true;
1013
Daniel Dunbar3f872332009-07-28 16:08:33 +00001014 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015 return TokError("unexpected token in '.fill' directive");
1016 Lexer.Lex();
1017
1018 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001019 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 return true;
1021
Daniel Dunbar3f872332009-07-28 16:08:33 +00001022 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023 return TokError("unexpected token in '.fill' directive");
1024
1025 Lexer.Lex();
1026
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001027 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1028 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029
1030 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001031 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032
1033 return false;
1034}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001035
1036/// ParseDirectiveOrg
1037/// ::= .org expression [ , expression ]
1038bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001039 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001040 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001041 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001042 return true;
1043
1044 // Parse optional fill expression.
1045 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001046 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1047 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001048 return TokError("unexpected token in '.org' directive");
1049 Lexer.Lex();
1050
Daniel Dunbar475839e2009-06-29 20:37:27 +00001051 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001052 return true;
1053
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001055 return TokError("unexpected token in '.org' directive");
1056 }
1057
1058 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001059
1060 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1061 // has to be relative to the current section.
1062 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001063
1064 return false;
1065}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001066
1067/// ParseDirectiveAlign
1068/// ::= {.align, ...} expression [ , expression [ , expression ]]
1069bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001070 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001071 int64_t Alignment;
1072 if (ParseAbsoluteExpression(Alignment))
1073 return true;
1074
1075 SMLoc MaxBytesLoc;
1076 bool HasFillExpr = false;
1077 int64_t FillExpr = 0;
1078 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1080 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001081 return TokError("unexpected token in directive");
1082 Lexer.Lex();
1083
1084 // The fill expression can be omitted while specifying a maximum number of
1085 // alignment bytes, e.g:
1086 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001087 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001088 HasFillExpr = true;
1089 if (ParseAbsoluteExpression(FillExpr))
1090 return true;
1091 }
1092
Daniel Dunbar3f872332009-07-28 16:08:33 +00001093 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1094 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001095 return TokError("unexpected token in directive");
1096 Lexer.Lex();
1097
1098 MaxBytesLoc = Lexer.getLoc();
1099 if (ParseAbsoluteExpression(MaxBytesToFill))
1100 return true;
1101
Daniel Dunbar3f872332009-07-28 16:08:33 +00001102 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001103 return TokError("unexpected token in directive");
1104 }
1105 }
1106
1107 Lexer.Lex();
1108
1109 if (!HasFillExpr) {
1110 // FIXME: Sometimes fill with nop.
1111 FillExpr = 0;
1112 }
1113
1114 // Compute alignment in bytes.
1115 if (IsPow2) {
1116 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001117 if (Alignment >= 32) {
1118 Error(AlignmentLoc, "invalid alignment value");
1119 Alignment = 31;
1120 }
1121
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001122 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001123 }
1124
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001125 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001126 if (MaxBytesLoc.isValid()) {
1127 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001128 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1129 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001130 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131 }
1132
1133 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001134 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1135 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001136 MaxBytesToFill = 0;
1137 }
1138 }
1139
1140 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1141 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1142
1143 return false;
1144}
1145
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001146/// ParseDirectiveSymbolAttribute
1147/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1148bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001149 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001150 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001151 StringRef Name;
1152
1153 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001154 return TokError("expected identifier in directive");
1155
Daniel Dunbar959fd882009-08-26 22:13:22 +00001156 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001157
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001158 Out.EmitSymbolAttribute(Sym, Attr);
1159
Daniel Dunbar3f872332009-07-28 16:08:33 +00001160 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001161 break;
1162
Daniel Dunbar3f872332009-07-28 16:08:33 +00001163 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001164 return TokError("unexpected token in directive");
1165 Lexer.Lex();
1166 }
1167 }
1168
1169 Lexer.Lex();
1170 return false;
1171}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001172
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001173/// ParseDirectiveDarwinSymbolDesc
1174/// ::= .desc identifier , expression
1175bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001176 StringRef Name;
1177 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001178 return TokError("expected identifier in directive");
1179
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001180 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001181 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001182
Daniel Dunbar3f872332009-07-28 16:08:33 +00001183 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001184 return TokError("unexpected token in '.desc' directive");
1185 Lexer.Lex();
1186
1187 SMLoc DescLoc = Lexer.getLoc();
1188 int64_t DescValue;
1189 if (ParseAbsoluteExpression(DescValue))
1190 return true;
1191
Daniel Dunbar3f872332009-07-28 16:08:33 +00001192 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001193 return TokError("unexpected token in '.desc' directive");
1194
1195 Lexer.Lex();
1196
1197 // Set the n_desc field of this Symbol to this DescValue
1198 Out.EmitSymbolDesc(Sym, DescValue);
1199
1200 return false;
1201}
1202
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001203/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001204/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1205bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001206 SMLoc IDLoc = Lexer.getLoc();
1207 StringRef Name;
1208 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001209 return TokError("expected identifier in directive");
1210
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001211 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001212 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001213
Daniel Dunbar3f872332009-07-28 16:08:33 +00001214 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001215 return TokError("unexpected token in directive");
1216 Lexer.Lex();
1217
1218 int64_t Size;
1219 SMLoc SizeLoc = Lexer.getLoc();
1220 if (ParseAbsoluteExpression(Size))
1221 return true;
1222
1223 int64_t Pow2Alignment = 0;
1224 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001225 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001226 Lexer.Lex();
1227 Pow2AlignmentLoc = Lexer.getLoc();
1228 if (ParseAbsoluteExpression(Pow2Alignment))
1229 return true;
1230 }
1231
Daniel Dunbar3f872332009-07-28 16:08:33 +00001232 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001233 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001234
1235 Lexer.Lex();
1236
Chris Lattner1fc3d752009-07-09 17:25:12 +00001237 // NOTE: a size of zero for a .comm should create a undefined symbol
1238 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001239 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001240 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1241 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001242
1243 // NOTE: The alignment in the directive is a power of 2 value, the assember
1244 // may internally end up wanting an alignment in bytes.
1245 // FIXME: Diagnose overflow.
1246 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001247 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1248 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001249
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001250 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001251 return Error(IDLoc, "invalid symbol redefinition");
1252
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001253 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001254 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001255 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001256 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1257 MCSectionMachO::S_ZEROFILL, 0,
1258 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001259 Sym, Size, 1 << Pow2Alignment);
1260 return false;
1261 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001262
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001263 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001264 return false;
1265}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001266
1267/// ParseDirectiveDarwinZerofill
1268/// ::= .zerofill segname , sectname [, identifier , size_expression [
1269/// , align_expression ]]
1270bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001271 // FIXME: Handle quoted names here.
1272
Daniel Dunbar3f872332009-07-28 16:08:33 +00001273 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001274 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001275 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001276 Lexer.Lex();
1277
Daniel Dunbar3f872332009-07-28 16:08:33 +00001278 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001279 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001280 Lexer.Lex();
1281
Daniel Dunbar3f872332009-07-28 16:08:33 +00001282 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001283 return TokError("expected section name after comma in '.zerofill' "
1284 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001285 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001286 Lexer.Lex();
1287
Chris Lattner9be3fee2009-07-10 22:20:30 +00001288 // If this is the end of the line all that was wanted was to create the
1289 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001290 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001291 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001292 Out.EmitZerofill(getMachOSection(Segment, Section,
1293 MCSectionMachO::S_ZEROFILL, 0,
1294 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001295 return false;
1296 }
1297
Daniel Dunbar3f872332009-07-28 16:08:33 +00001298 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001299 return TokError("unexpected token in directive");
1300 Lexer.Lex();
1301
Daniel Dunbar3f872332009-07-28 16:08:33 +00001302 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001303 return TokError("expected identifier in directive");
1304
1305 // handle the identifier as the key symbol.
1306 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001307 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001308 Lexer.Lex();
1309
Daniel Dunbar3f872332009-07-28 16:08:33 +00001310 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001311 return TokError("unexpected token in directive");
1312 Lexer.Lex();
1313
1314 int64_t Size;
1315 SMLoc SizeLoc = Lexer.getLoc();
1316 if (ParseAbsoluteExpression(Size))
1317 return true;
1318
1319 int64_t Pow2Alignment = 0;
1320 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001321 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001322 Lexer.Lex();
1323 Pow2AlignmentLoc = Lexer.getLoc();
1324 if (ParseAbsoluteExpression(Pow2Alignment))
1325 return true;
1326 }
1327
Daniel Dunbar3f872332009-07-28 16:08:33 +00001328 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001329 return TokError("unexpected token in '.zerofill' directive");
1330
1331 Lexer.Lex();
1332
1333 if (Size < 0)
1334 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1335 "than zero");
1336
1337 // NOTE: The alignment in the directive is a power of 2 value, the assember
1338 // may internally end up wanting an alignment in bytes.
1339 // FIXME: Diagnose overflow.
1340 if (Pow2Alignment < 0)
1341 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1342 "can't be less than zero");
1343
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001344 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001345 return Error(IDLoc, "invalid symbol redefinition");
1346
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001347 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001348 //
1349 // FIXME: Arch specific.
1350 Out.EmitZerofill(getMachOSection(Segment, Section,
1351 MCSectionMachO::S_ZEROFILL, 0,
1352 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001353 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001354
1355 return false;
1356}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001357
1358/// ParseDirectiveDarwinSubsectionsViaSymbols
1359/// ::= .subsections_via_symbols
1360bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001361 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001362 return TokError("unexpected token in '.subsections_via_symbols' directive");
1363
1364 Lexer.Lex();
1365
Kevin Enderbyf96db462009-07-16 17:56:39 +00001366 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001367
1368 return false;
1369}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001370
1371/// ParseDirectiveAbort
1372/// ::= .abort [ "abort_string" ]
1373bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001374 // FIXME: Use loc from directive.
1375 SMLoc Loc = Lexer.getLoc();
1376
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001377 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001378 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1379 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001380 return TokError("expected string in '.abort' directive");
1381
Daniel Dunbar419aded2009-07-28 16:38:40 +00001382 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001383
1384 Lexer.Lex();
1385 }
1386
Daniel Dunbar3f872332009-07-28 16:08:33 +00001387 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001388 return TokError("unexpected token in '.abort' directive");
1389
1390 Lexer.Lex();
1391
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001392 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001393 if (Str.empty())
1394 Error(Loc, ".abort detected. Assembly stopping.");
1395 else
1396 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001397
1398 return false;
1399}
Kevin Enderby71148242009-07-14 21:35:03 +00001400
1401/// ParseDirectiveLsym
1402/// ::= .lsym identifier , expression
1403bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001404 StringRef Name;
1405 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001406 return TokError("expected identifier in directive");
1407
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001408 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001409 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001410
Daniel Dunbar3f872332009-07-28 16:08:33 +00001411 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001412 return TokError("unexpected token in '.lsym' directive");
1413 Lexer.Lex();
1414
Daniel Dunbar821e3332009-08-31 08:09:28 +00001415 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001416 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001417 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001418 return true;
1419
Daniel Dunbar3f872332009-07-28 16:08:33 +00001420 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001421 return TokError("unexpected token in '.lsym' directive");
1422
1423 Lexer.Lex();
1424
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001425 // We don't currently support this directive.
1426 //
1427 // FIXME: Diagnostic location!
1428 (void) Sym;
1429 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001430}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001431
1432/// ParseDirectiveInclude
1433/// ::= .include "filename"
1434bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001435 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001436 return TokError("expected string in '.include' directive");
1437
Daniel Dunbar419aded2009-07-28 16:38:40 +00001438 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001439 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001440 Lexer.Lex();
1441
Daniel Dunbar3f872332009-07-28 16:08:33 +00001442 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001443 return TokError("unexpected token in '.include' directive");
1444
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001445 // Strip the quotes.
1446 Filename = Filename.substr(1, Filename.size()-2);
1447
1448 // Attempt to switch the lexer to the included file before consuming the end
1449 // of statement to avoid losing it when we switch.
1450 if (Lexer.EnterIncludeFile(Filename)) {
1451 Lexer.PrintMessage(IncludeLoc,
1452 "Could not find include file '" + Filename + "'",
1453 "error");
1454 return true;
1455 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001456
1457 return false;
1458}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001459
1460/// ParseDirectiveDarwinDumpOrLoad
1461/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001462bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001463 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001464 return TokError("expected string in '.dump' or '.load' directive");
1465
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001466 Lexer.Lex();
1467
Daniel Dunbar3f872332009-07-28 16:08:33 +00001468 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001469 return TokError("unexpected token in '.dump' or '.load' directive");
1470
1471 Lexer.Lex();
1472
Kevin Enderby5026ae42009-07-20 20:25:37 +00001473 // FIXME: If/when .dump and .load are implemented they will be done in the
1474 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001475 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001476 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001477 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001478 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001479
1480 return false;
1481}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001482
1483/// ParseDirectiveIf
1484/// ::= .if expression
1485bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1486 // Consume the identifier that was the .if directive
1487 Lexer.Lex();
1488
1489 TheCondStack.push_back(TheCondState);
1490 TheCondState.TheCond = AsmCond::IfCond;
1491 if(TheCondState.Ignore) {
1492 EatToEndOfStatement();
1493 }
1494 else {
1495 int64_t ExprValue;
1496 if (ParseAbsoluteExpression(ExprValue))
1497 return true;
1498
1499 if (Lexer.isNot(AsmToken::EndOfStatement))
1500 return TokError("unexpected token in '.if' directive");
1501
1502 Lexer.Lex();
1503
1504 TheCondState.CondMet = ExprValue;
1505 TheCondState.Ignore = !TheCondState.CondMet;
1506 }
1507
1508 return false;
1509}
1510
1511/// ParseDirectiveElseIf
1512/// ::= .elseif expression
1513bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1514 if (TheCondState.TheCond != AsmCond::IfCond &&
1515 TheCondState.TheCond != AsmCond::ElseIfCond)
1516 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1517 " an .elseif");
1518 TheCondState.TheCond = AsmCond::ElseIfCond;
1519
1520 // Consume the identifier that was the .elseif directive
1521 Lexer.Lex();
1522
1523 bool LastIgnoreState = false;
1524 if (!TheCondStack.empty())
1525 LastIgnoreState = TheCondStack.back().Ignore;
1526 if (LastIgnoreState || TheCondState.CondMet) {
1527 TheCondState.Ignore = true;
1528 EatToEndOfStatement();
1529 }
1530 else {
1531 int64_t ExprValue;
1532 if (ParseAbsoluteExpression(ExprValue))
1533 return true;
1534
1535 if (Lexer.isNot(AsmToken::EndOfStatement))
1536 return TokError("unexpected token in '.elseif' directive");
1537
1538 Lexer.Lex();
1539 TheCondState.CondMet = ExprValue;
1540 TheCondState.Ignore = !TheCondState.CondMet;
1541 }
1542
1543 return false;
1544}
1545
1546/// ParseDirectiveElse
1547/// ::= .else
1548bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1549 // Consume the identifier that was the .else directive
1550 Lexer.Lex();
1551
1552 if (Lexer.isNot(AsmToken::EndOfStatement))
1553 return TokError("unexpected token in '.else' directive");
1554
1555 Lexer.Lex();
1556
1557 if (TheCondState.TheCond != AsmCond::IfCond &&
1558 TheCondState.TheCond != AsmCond::ElseIfCond)
1559 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1560 ".elseif");
1561 TheCondState.TheCond = AsmCond::ElseCond;
1562 bool LastIgnoreState = false;
1563 if (!TheCondStack.empty())
1564 LastIgnoreState = TheCondStack.back().Ignore;
1565 if (LastIgnoreState || TheCondState.CondMet)
1566 TheCondState.Ignore = true;
1567 else
1568 TheCondState.Ignore = false;
1569
1570 return false;
1571}
1572
1573/// ParseDirectiveEndIf
1574/// ::= .endif
1575bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1576 // Consume the identifier that was the .endif directive
1577 Lexer.Lex();
1578
1579 if (Lexer.isNot(AsmToken::EndOfStatement))
1580 return TokError("unexpected token in '.endif' directive");
1581
1582 Lexer.Lex();
1583
1584 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1585 TheCondStack.empty())
1586 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1587 ".else");
1588 if (!TheCondStack.empty()) {
1589 TheCondState = TheCondStack.back();
1590 TheCondStack.pop_back();
1591 }
1592
1593 return false;
1594}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001595
1596/// ParseDirectiveFile
1597/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001598bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001599 // FIXME: I'm not sure what this is.
1600 int64_t FileNumber = -1;
1601 if (Lexer.is(AsmToken::Integer)) {
1602 FileNumber = Lexer.getTok().getIntVal();
1603 Lexer.Lex();
1604
1605 if (FileNumber < 1)
1606 return TokError("file number less than one");
1607 }
1608
1609 if (Lexer.isNot(AsmToken::String))
1610 return TokError("unexpected token in '.file' directive");
1611
1612 StringRef FileName = Lexer.getTok().getString();
1613 Lexer.Lex();
1614
1615 if (Lexer.isNot(AsmToken::EndOfStatement))
1616 return TokError("unexpected token in '.file' directive");
1617
1618 // FIXME: Do something with the .file.
1619
1620 return false;
1621}
1622
1623/// ParseDirectiveLine
1624/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001625bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001626 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1627 if (Lexer.isNot(AsmToken::Integer))
1628 return TokError("unexpected token in '.line' directive");
1629
1630 int64_t LineNumber = Lexer.getTok().getIntVal();
1631 (void) LineNumber;
1632 Lexer.Lex();
1633
1634 // FIXME: Do something with the .line.
1635 }
1636
1637 if (Lexer.isNot(AsmToken::EndOfStatement))
1638 return TokError("unexpected token in '.file' directive");
1639
1640 return false;
1641}
1642
1643
1644/// ParseDirectiveLoc
1645/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001646bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001647 if (Lexer.isNot(AsmToken::Integer))
1648 return TokError("unexpected token in '.loc' directive");
1649
1650 // FIXME: What are these fields?
1651 int64_t FileNumber = Lexer.getTok().getIntVal();
1652 (void) FileNumber;
1653 // FIXME: Validate file.
1654
1655 Lexer.Lex();
1656 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1657 if (Lexer.isNot(AsmToken::Integer))
1658 return TokError("unexpected token in '.loc' directive");
1659
1660 int64_t Param2 = Lexer.getTok().getIntVal();
1661 (void) Param2;
1662 Lexer.Lex();
1663
1664 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1665 if (Lexer.isNot(AsmToken::Integer))
1666 return TokError("unexpected token in '.loc' directive");
1667
1668 int64_t Param3 = Lexer.getTok().getIntVal();
1669 (void) Param3;
1670 Lexer.Lex();
1671
1672 // FIXME: Do something with the .loc.
1673 }
1674 }
1675
1676 if (Lexer.isNot(AsmToken::EndOfStatement))
1677 return TokError("unexpected token in '.file' directive");
1678
1679 return false;
1680}
1681