blob: dbceb6b98a2426955a49965a1e569444b8969eb7 [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"
Daniel Dunbarc18274b2009-08-31 08:08:17 +000024#include "llvm/MC/MCValue.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000025#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000027#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000028using namespace llvm;
29
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000030// Mach-O section uniquing.
31//
32// FIXME: Figure out where this should live, it should be shared by
33// TargetLoweringObjectFile.
34typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
35
36AsmParser::~AsmParser() {
37 // If we have the MachO uniquing map, free it.
38 delete (MachOUniqueMapTy*)SectionUniquingMap;
39}
40
41const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
42 const StringRef &Section,
43 unsigned TypeAndAttributes,
44 unsigned Reserved2,
45 SectionKind Kind) const {
46 // We unique sections by their segment/section pair. The returned section
47 // may not have the same flags as the requested section, if so this should be
48 // diagnosed by the client as an error.
49
50 // Create the map if it doesn't already exist.
51 if (SectionUniquingMap == 0)
52 SectionUniquingMap = new MachOUniqueMapTy();
53 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
54
55 // Form the name to look up.
56 SmallString<64> Name;
57 Name += Segment;
58 Name.push_back(',');
59 Name += Section;
60
61 // Do the lookup, if we have a hit, return it.
62 const MCSectionMachO *&Entry = Map[Name.str()];
63
64 // FIXME: This should validate the type and attributes.
65 if (Entry) return Entry;
66
67 // Otherwise, return a new section.
68 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
69 Reserved2, Kind, Ctx);
70}
71
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000072void AsmParser::Warning(SMLoc L, const Twine &Msg) {
73 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000074}
75
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000076bool AsmParser::Error(SMLoc L, const Twine &Msg) {
77 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000078 return true;
79}
80
81bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000082 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000083 return true;
84}
85
Chris Lattner27aa7d22009-06-21 20:16:42 +000086bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000087 // Create the initial section.
88 //
89 // FIXME: Support -n.
90 // FIXME: Target hook & command line option for initial section.
91 Out.SwitchSection(getMachOSection("__TEXT", "__text",
92 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
93 0, SectionKind()));
94
95
Chris Lattnerb0789ed2009-06-21 20:54:55 +000096 // Prime the lexer.
97 Lexer.Lex();
98
Chris Lattnerb717fb02009-07-02 21:53:43 +000099 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000100
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000101 AsmCond StartingCondState = TheCondState;
102
Chris Lattnerb717fb02009-07-02 21:53:43 +0000103 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000104 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000105 // Handle conditional assembly here before calling ParseStatement()
106 if (Lexer.getKind() == AsmToken::Identifier) {
107 // If we have an identifier, handle it as the key symbol.
108 AsmToken ID = Lexer.getTok();
109 SMLoc IDLoc = ID.getLoc();
110 StringRef IDVal = ID.getString();
111
112 if (IDVal == ".if" ||
113 IDVal == ".elseif" ||
114 IDVal == ".else" ||
115 IDVal == ".endif") {
116 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
117 continue;
118 HadError = true;
119 EatToEndOfStatement();
120 continue;
121 }
122 }
123 if (TheCondState.Ignore) {
124 EatToEndOfStatement();
125 continue;
126 }
127
Chris Lattnerb717fb02009-07-02 21:53:43 +0000128 if (!ParseStatement()) continue;
129
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000130 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000131 HadError = true;
132 EatToEndOfStatement();
133 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000134
135 if (TheCondState.TheCond != StartingCondState.TheCond ||
136 TheCondState.Ignore != StartingCondState.Ignore)
137 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000138
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000139 if (!HadError)
140 Out.Finish();
141
Chris Lattnerb717fb02009-07-02 21:53:43 +0000142 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000143}
144
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000145/// ParseConditionalAssemblyDirectives - parse the conditional assembly
146/// directives
147bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
148 SMLoc DirectiveLoc) {
149 if (Directive == ".if")
150 return ParseDirectiveIf(DirectiveLoc);
151 if (Directive == ".elseif")
152 return ParseDirectiveElseIf(DirectiveLoc);
153 if (Directive == ".else")
154 return ParseDirectiveElse(DirectiveLoc);
155 if (Directive == ".endif")
156 return ParseDirectiveEndIf(DirectiveLoc);
157 return true;
158}
159
Chris Lattner2cf5f142009-06-22 01:29:09 +0000160/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
161void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000162 while (Lexer.isNot(AsmToken::EndOfStatement) &&
163 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000164 Lexer.Lex();
165
166 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000167 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000168 Lexer.Lex();
169}
170
Chris Lattnerc4193832009-06-22 05:51:26 +0000171
Chris Lattner74ec1a32009-06-22 06:32:03 +0000172/// ParseParenExpr - Parse a paren expression and return it.
173/// NOTE: This assumes the leading '(' has already been consumed.
174///
175/// parenexpr ::= expr)
176///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000177bool AsmParser::ParseParenExpr(const MCExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000178 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000179 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000180 return TokError("expected ')' in parentheses expression");
181 Lexer.Lex();
182 return false;
183}
Chris Lattnerc4193832009-06-22 05:51:26 +0000184
Daniel Dunbar959fd882009-08-26 22:13:22 +0000185MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
186 if (MCSymbol *S = Ctx.LookupSymbol(Name))
187 return S;
188
189 // If the label starts with L it is an assembler temporary label.
190 if (Name.startswith("L"))
191 return Ctx.CreateTemporarySymbol(Name);
192
193 return Ctx.CreateSymbol(Name);
194}
195
Chris Lattner74ec1a32009-06-22 06:32:03 +0000196/// ParsePrimaryExpr - Parse a primary expression and return it.
197/// primaryexpr ::= (parenexpr
198/// primaryexpr ::= symbol
199/// primaryexpr ::= number
200/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000201bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000202 switch (Lexer.getKind()) {
203 default:
204 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000205 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000206 Lexer.Lex(); // Eat the operator.
207 if (ParsePrimaryExpr(Res))
208 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000209 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000210 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000211 case AsmToken::String:
Daniel Dunbar3f872332009-07-28 16:08:33 +0000212 case AsmToken::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +0000213 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000214 // handle things like LFOO+4.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000215 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000216
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000217 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000218 Lexer.Lex(); // Eat identifier.
219 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000220 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000221 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000222 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000223 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000224 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000225 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000226 Lexer.Lex(); // Eat the '('.
227 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000228 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000229 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000230 if (ParsePrimaryExpr(Res))
231 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000232 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000233 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000234 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000235 Lexer.Lex(); // Eat the operator.
236 if (ParsePrimaryExpr(Res))
237 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000238 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000239 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000240 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000241 Lexer.Lex(); // Eat the operator.
242 if (ParsePrimaryExpr(Res))
243 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000244 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000245 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000246 }
247}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000248
249/// ParseExpression - Parse an expression and return it.
250///
251/// expr ::= expr +,- expr -> lowest.
252/// expr ::= expr |,^,&,! expr -> middle.
253/// expr ::= expr *,/,%,<<,>> expr -> highest.
254/// expr ::= primaryexpr
255///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000256bool AsmParser::ParseExpression(const MCExpr *&Res) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000258 return ParsePrimaryExpr(Res) ||
259 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000260}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000261
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000262bool AsmParser::ParseParenExpression(const MCExpr *&Res) {
263 if (ParseParenExpr(Res))
264 return true;
265
266 return false;
267}
268
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000270 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000272 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000273 if (ParseExpression(Expr))
274 return true;
275
276 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000277 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000278
279 return false;
280}
281
Daniel Dunbar3f872332009-07-28 16:08:33 +0000282static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000283 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000284 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000285 default:
286 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000287
288 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000289 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000290 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000291 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000292 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000293 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000294 return 1;
295
296 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000297 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000298 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000299 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000300 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000301 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000302 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000303 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000304 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000306 case AsmToken::ExclaimEqual:
307 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000308 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000310 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000311 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000312 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000313 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000314 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000316 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000317 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000318 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000319 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000320 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000321 return 2;
322
323 // Intermediate Precedence: |, &, ^
324 //
325 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000327 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000328 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000333 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 return 3;
335
336 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000337 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000338 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000339 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000340 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000341 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000344 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000345 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000352 }
353}
354
355
356/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
357/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000358bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000359 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000360 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000361 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000362
363 // If the next token is lower precedence than we are allowed to eat, return
364 // successfully with what we ate already.
365 if (TokPrec < Precedence)
366 return false;
367
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000368 Lexer.Lex();
369
370 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000371 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000372 if (ParsePrimaryExpr(RHS)) return true;
373
374 // If BinOp binds less tightly with RHS than the operator after RHS, let
375 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000378 if (TokPrec < NextTokPrec) {
379 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
380 }
381
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000383 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000384 }
385}
386
Chris Lattnerc4193832009-06-22 05:51:26 +0000387
388
389
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000390/// ParseStatement:
391/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000392/// ::= Label* Directive ...Operands... EndOfStatement
393/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000394bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000395 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000396 Lexer.Lex();
397 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000398 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000399
400 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000401 AsmToken ID = Lexer.getTok();
402 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000403 StringRef IDVal;
404 if (ParseIdentifier(IDVal))
405 return TokError("unexpected token at start of statement");
406
407 // FIXME: Recurse on local labels?
408
409 // See what kind of statement we have.
410 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000412 // identifier ':' -> Label.
413 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000414
415 // Diagnose attempt to use a variable as a label.
416 //
417 // FIXME: Diagnostics. Note the location of the definition as a label.
418 // FIXME: This doesn't diagnose assignment to a symbol which has been
419 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000420 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000421 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000422 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000423
Daniel Dunbar959fd882009-08-26 22:13:22 +0000424 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000425 Out.EmitLabel(Sym);
426
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000427 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000428 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000429
Daniel Dunbar3f872332009-07-28 16:08:33 +0000430 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000431 // identifier '=' ... -> assignment statement
432 Lexer.Lex();
433
Daniel Dunbare2ace502009-08-31 08:09:09 +0000434 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000435
436 default: // Normal instruction or directive.
437 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000438 }
439
440 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000441 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000442 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000443 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000444 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000445 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000446 // FIXME: This changes behavior based on the -static flag to the
447 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000448 return ParseDirectiveSectionSwitch("__TEXT", "__text",
449 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000450 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000451 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000452 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000453 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000454 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000455 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
456 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000457 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000458 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000459 MCSectionMachO::S_4BYTE_LITERALS,
460 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000461 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000462 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000463 MCSectionMachO::S_8BYTE_LITERALS,
464 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000465 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000466 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000467 MCSectionMachO::S_16BYTE_LITERALS,
468 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000470 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000471 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000472 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000473 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000474 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000475 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000476 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
477
478 // FIXME: The assembler manual claims that this has the self modify code
479 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000480 if (IDVal == ".symbol_stub")
481 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
482 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000483 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
484 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000485 0, 16);
486 // FIXME: PowerPC only?
487 if (IDVal == ".picsymbol_stub")
488 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
489 MCSectionMachO::S_SYMBOL_STUBS |
490 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
491 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000495 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
496
497 // FIXME: The section names of these two are misspelled in the assembler
498 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
501 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
502 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000504 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
505 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
506 4);
507
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000508 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000509 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000510 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000511 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000512 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
513 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000514 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000515 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000516 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
517 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000519 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000520
521
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000523 return ParseDirectiveSectionSwitch("__OBJC", "__class",
524 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000526 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
527 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000529 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
530 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000532 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
533 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
536 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000538 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
539 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000541 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
542 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
545 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000547 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
548 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
549 MCSectionMachO::S_LITERAL_POINTERS,
550 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
553 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
554 MCSectionMachO::S_LITERAL_POINTERS,
555 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
558 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000559 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000560 return ParseDirectiveSectionSwitch("__OBJC", "__category",
561 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000563 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
564 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000565 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000566 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
567 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000568 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000569 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
570 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000571 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000572 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
573 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000575 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
576 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000577 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000578 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
579 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000580 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000581 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
582 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000583
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000584 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000586 return ParseDirectiveSet();
587
Daniel Dunbara0d14262009-06-24 23:30:00 +0000588 // Data directives
589
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000591 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000593 return ParseDirectiveAscii(true);
594
595 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000596 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000597 return ParseDirectiveValue(1);
Daniel Dunbar1e840b22009-08-11 04:44:00 +0000598 if (IDVal == ".short" || IDVal == ".word")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000599 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000601 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000602 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000603 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000604
605 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000607 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000608 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000609 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000611 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000613 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000615 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000617 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000619 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000621 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
622
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000624 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000625
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000627 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000629 return ParseDirectiveSpace();
630
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000631 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000632
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000634 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000636 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000638 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000640 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000642 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000644 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000646 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000648 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000650 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000652 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000654 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000656 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
657
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000659 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000661 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000663 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000665 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000667 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000668
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000670 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000672 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000674 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000676 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000678 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000679
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000680 // Debugging directives
681
682 if (IDVal == ".file")
683 return ParseDirectiveFile(IDLoc);
684 if (IDVal == ".line")
685 return ParseDirectiveLine(IDLoc);
686 if (IDVal == ".loc")
687 return ParseDirectiveLoc(IDLoc);
688
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000689 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000690 EatToEndOfStatement();
691 return false;
692 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000693
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000694 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000695 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000696 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000697
Daniel Dunbar3f872332009-07-28 16:08:33 +0000698 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000699 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000700
701 // Eat the end of statement marker.
702 Lexer.Lex();
703
704 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000705 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000706
707 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000708 return false;
709}
Chris Lattner9a023f72009-06-24 04:43:34 +0000710
Daniel Dunbare2ace502009-08-31 08:09:09 +0000711bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000712 // FIXME: Use better location, we should use proper tokens.
713 SMLoc EqualLoc = Lexer.getLoc();
714
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000715 MCValue Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000716 const MCExpr *Expr;
717 SMLoc StartLoc = Lexer.getLoc();
718 if (ParseExpression(Expr))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000719 return true;
720
Daniel Dunbar883f9202009-08-31 08:08:50 +0000721 if (!Expr->EvaluateAsRelocatable(Ctx, Value))
722 return Error(StartLoc, "expected relocatable expression");
723
Daniel Dunbar3f872332009-07-28 16:08:33 +0000724 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000725 return TokError("unexpected token in assignment");
726
727 // Eat the end of statement marker.
728 Lexer.Lex();
729
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000730 // Diagnose assignment to a label.
731 //
732 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000733 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000734 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar959fd882009-08-26 22:13:22 +0000735 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000736 if (!Sym->isUndefined() && !Sym->isAbsolute())
737 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000738
739 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000740 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000741
742 return false;
743}
744
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000745/// ParseIdentifier:
746/// ::= identifier
747/// ::= string
748bool AsmParser::ParseIdentifier(StringRef &Res) {
749 if (Lexer.isNot(AsmToken::Identifier) &&
750 Lexer.isNot(AsmToken::String))
751 return true;
752
753 Res = Lexer.getTok().getIdentifier();
754
755 Lexer.Lex(); // Consume the identifier token.
756
757 return false;
758}
759
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000760/// ParseDirectiveSet:
761/// ::= .set identifier ',' expression
762bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000763 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000764
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000765 if (ParseIdentifier(Name))
766 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000767
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000768 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000769 return TokError("unexpected token in '.set'");
770 Lexer.Lex();
771
Daniel Dunbare2ace502009-08-31 08:09:09 +0000772 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000773}
774
Chris Lattner9a023f72009-06-24 04:43:34 +0000775/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000776/// ::= .section identifier (',' identifier)*
777/// FIXME: This should actually parse out the segment, section, attributes and
778/// sizeof_stub fields.
779bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000780 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000781
Daniel Dunbarace63122009-08-11 03:42:33 +0000782 StringRef SectionName;
783 if (ParseIdentifier(SectionName))
784 return Error(Loc, "expected identifier after '.section' directive");
785
786 // Verify there is a following comma.
787 if (!Lexer.is(AsmToken::Comma))
788 return TokError("unexpected token in '.section' directive");
789
Chris Lattnerff4bc462009-08-10 01:39:42 +0000790 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000791 SectionSpec += ",";
792
793 // Add all the tokens until the end of the line, ParseSectionSpecifier will
794 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000795 StringRef EOL = Lexer.LexUntilEndOfStatement();
796 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000797
Chris Lattnerff4bc462009-08-10 01:39:42 +0000798 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000799 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000800 return TokError("unexpected token in '.section' directive");
801 Lexer.Lex();
802
Chris Lattnerff4bc462009-08-10 01:39:42 +0000803
804 StringRef Segment, Section;
805 unsigned TAA, StubSize;
806 std::string ErrorStr =
807 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
808 TAA, StubSize);
809
810 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000811 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000812
Chris Lattner56594f92009-07-31 17:47:16 +0000813 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000814 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
815 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000816 return false;
817}
818
Chris Lattnere15c2d72009-08-10 18:05:55 +0000819/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000820bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
821 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000822 unsigned TAA, unsigned Align,
823 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000824 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000825 return TokError("unexpected token in section switching directive");
826 Lexer.Lex();
827
Chris Lattner56594f92009-07-31 17:47:16 +0000828 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000829 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
830 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000831
832 // Set the implicit alignment, if any.
833 //
834 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
835 // alignment on the section (e.g., if one manually inserts bytes into the
836 // section, then just issueing the section switch directive will not realign
837 // the section. However, this is arguably more reasonable behavior, and there
838 // is no good reason for someone to intentionally emit incorrectly sized
839 // values into the implicitly aligned sections.
840 if (Align)
841 Out.EmitValueToAlignment(Align, 0, 1, 0);
842
Chris Lattner529fb542009-06-24 05:13:15 +0000843 return false;
844}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000845
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000846bool AsmParser::ParseEscapedString(std::string &Data) {
847 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
848
849 Data = "";
850 StringRef Str = Lexer.getTok().getStringContents();
851 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
852 if (Str[i] != '\\') {
853 Data += Str[i];
854 continue;
855 }
856
857 // Recognize escaped characters. Note that this escape semantics currently
858 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
859 ++i;
860 if (i == e)
861 return TokError("unexpected backslash at end of string");
862
863 // Recognize octal sequences.
864 if ((unsigned) (Str[i] - '0') <= 7) {
865 // Consume up to three octal characters.
866 unsigned Value = Str[i] - '0';
867
868 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
869 ++i;
870 Value = Value * 8 + (Str[i] - '0');
871
872 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
873 ++i;
874 Value = Value * 8 + (Str[i] - '0');
875 }
876 }
877
878 if (Value > 255)
879 return TokError("invalid octal escape sequence (out of range)");
880
881 Data += (unsigned char) Value;
882 continue;
883 }
884
885 // Otherwise recognize individual escapes.
886 switch (Str[i]) {
887 default:
888 // Just reject invalid escape sequences for now.
889 return TokError("invalid escape sequence (unrecognized character)");
890
891 case 'b': Data += '\b'; break;
892 case 'f': Data += '\f'; break;
893 case 'n': Data += '\n'; break;
894 case 'r': Data += '\r'; break;
895 case 't': Data += '\t'; break;
896 case '"': Data += '"'; break;
897 case '\\': Data += '\\'; break;
898 }
899 }
900
901 return false;
902}
903
Daniel Dunbara0d14262009-06-24 23:30:00 +0000904/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000905/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000906bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000908 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000909 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000910 return TokError("expected string in '.ascii' or '.asciz' directive");
911
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000912 std::string Data;
913 if (ParseEscapedString(Data))
914 return true;
915
916 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000917 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000918 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000919
920 Lexer.Lex();
921
Daniel Dunbar3f872332009-07-28 16:08:33 +0000922 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923 break;
924
Daniel Dunbar3f872332009-07-28 16:08:33 +0000925 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000926 return TokError("unexpected token in '.ascii' or '.asciz' directive");
927 Lexer.Lex();
928 }
929 }
930
931 Lexer.Lex();
932 return false;
933}
934
935/// ParseDirectiveValue
936/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
937bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000938 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000939 for (;;) {
Daniel Dunbar883f9202009-08-31 08:08:50 +0000940 MCValue Value;
941 const MCExpr *Expr;
942 SMLoc StartLoc = Lexer.getLoc();
943 if (ParseExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000944 return true;
945
Daniel Dunbar883f9202009-08-31 08:08:50 +0000946 if (!Expr->EvaluateAsRelocatable(Ctx, Value))
947 return Error(StartLoc, "expected relocatable expression");
948
949 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000950
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000952 break;
953
954 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000955 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000956 return TokError("unexpected token in directive");
957 Lexer.Lex();
958 }
959 }
960
961 Lexer.Lex();
962 return false;
963}
964
965/// ParseDirectiveSpace
966/// ::= .space expression [ , expression ]
967bool AsmParser::ParseDirectiveSpace() {
968 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000969 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000970 return true;
971
972 int64_t FillExpr = 0;
973 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000974 if (Lexer.isNot(AsmToken::EndOfStatement)) {
975 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976 return TokError("unexpected token in '.space' directive");
977 Lexer.Lex();
978
Daniel Dunbar475839e2009-06-29 20:37:27 +0000979 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000980 return true;
981
982 HasFillExpr = true;
983
Daniel Dunbar3f872332009-07-28 16:08:33 +0000984 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000985 return TokError("unexpected token in '.space' directive");
986 }
987
988 Lexer.Lex();
989
990 if (NumBytes <= 0)
991 return TokError("invalid number of bytes in '.space' directive");
992
993 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
994 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
995 Out.EmitValue(MCValue::get(FillExpr), 1);
996
997 return false;
998}
999
1000/// ParseDirectiveFill
1001/// ::= .fill expression , expression , expression
1002bool AsmParser::ParseDirectiveFill() {
1003 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001004 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001005 return true;
1006
Daniel Dunbar3f872332009-07-28 16:08:33 +00001007 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001008 return TokError("unexpected token in '.fill' directive");
1009 Lexer.Lex();
1010
1011 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001012 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013 return true;
1014
Daniel Dunbar3f872332009-07-28 16:08:33 +00001015 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001016 return TokError("unexpected token in '.fill' directive");
1017 Lexer.Lex();
1018
1019 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001020 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 return true;
1022
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 return TokError("unexpected token in '.fill' directive");
1025
1026 Lexer.Lex();
1027
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001028 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1029 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030
1031 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1032 Out.EmitValue(MCValue::get(FillExpr), FillSize);
1033
1034 return false;
1035}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001036
1037/// ParseDirectiveOrg
1038/// ::= .org expression [ , expression ]
1039bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001040 MCValue Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001041 const MCExpr *Expr;
1042 SMLoc StartLoc = Lexer.getLoc();
1043 if (ParseExpression(Expr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001044 return true;
1045
Daniel Dunbar883f9202009-08-31 08:08:50 +00001046 if (!Expr->EvaluateAsRelocatable(Ctx, Offset))
1047 return Error(StartLoc, "expected relocatable expression");
1048
Daniel Dunbarc238b582009-06-25 22:44:51 +00001049 // Parse optional fill expression.
1050 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001051 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1052 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001053 return TokError("unexpected token in '.org' directive");
1054 Lexer.Lex();
1055
Daniel Dunbar475839e2009-06-29 20:37:27 +00001056 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001057 return true;
1058
Daniel Dunbar3f872332009-07-28 16:08:33 +00001059 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001060 return TokError("unexpected token in '.org' directive");
1061 }
1062
1063 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001064
1065 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1066 // has to be relative to the current section.
1067 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001068
1069 return false;
1070}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001071
1072/// ParseDirectiveAlign
1073/// ::= {.align, ...} expression [ , expression [ , expression ]]
1074bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001075 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001076 int64_t Alignment;
1077 if (ParseAbsoluteExpression(Alignment))
1078 return true;
1079
1080 SMLoc MaxBytesLoc;
1081 bool HasFillExpr = false;
1082 int64_t FillExpr = 0;
1083 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001084 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1085 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001086 return TokError("unexpected token in directive");
1087 Lexer.Lex();
1088
1089 // The fill expression can be omitted while specifying a maximum number of
1090 // alignment bytes, e.g:
1091 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001092 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001093 HasFillExpr = true;
1094 if (ParseAbsoluteExpression(FillExpr))
1095 return true;
1096 }
1097
Daniel Dunbar3f872332009-07-28 16:08:33 +00001098 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1099 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001100 return TokError("unexpected token in directive");
1101 Lexer.Lex();
1102
1103 MaxBytesLoc = Lexer.getLoc();
1104 if (ParseAbsoluteExpression(MaxBytesToFill))
1105 return true;
1106
Daniel Dunbar3f872332009-07-28 16:08:33 +00001107 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001108 return TokError("unexpected token in directive");
1109 }
1110 }
1111
1112 Lexer.Lex();
1113
1114 if (!HasFillExpr) {
1115 // FIXME: Sometimes fill with nop.
1116 FillExpr = 0;
1117 }
1118
1119 // Compute alignment in bytes.
1120 if (IsPow2) {
1121 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001122 if (Alignment >= 32) {
1123 Error(AlignmentLoc, "invalid alignment value");
1124 Alignment = 31;
1125 }
1126
1127 Alignment = 1 << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001128 }
1129
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001130 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131 if (MaxBytesLoc.isValid()) {
1132 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001133 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1134 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001135 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001136 }
1137
1138 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001139 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1140 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001141 MaxBytesToFill = 0;
1142 }
1143 }
1144
1145 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1146 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1147
1148 return false;
1149}
1150
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001151/// ParseDirectiveSymbolAttribute
1152/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1153bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001154 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001155 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001156 StringRef Name;
1157
1158 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001159 return TokError("expected identifier in directive");
1160
Daniel Dunbar959fd882009-08-26 22:13:22 +00001161 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001162
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001163 Out.EmitSymbolAttribute(Sym, Attr);
1164
Daniel Dunbar3f872332009-07-28 16:08:33 +00001165 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001166 break;
1167
Daniel Dunbar3f872332009-07-28 16:08:33 +00001168 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001169 return TokError("unexpected token in directive");
1170 Lexer.Lex();
1171 }
1172 }
1173
1174 Lexer.Lex();
1175 return false;
1176}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001177
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001178/// ParseDirectiveDarwinSymbolDesc
1179/// ::= .desc identifier , expression
1180bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001181 StringRef Name;
1182 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001183 return TokError("expected identifier in directive");
1184
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001185 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001186 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001187
Daniel Dunbar3f872332009-07-28 16:08:33 +00001188 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001189 return TokError("unexpected token in '.desc' directive");
1190 Lexer.Lex();
1191
1192 SMLoc DescLoc = Lexer.getLoc();
1193 int64_t DescValue;
1194 if (ParseAbsoluteExpression(DescValue))
1195 return true;
1196
Daniel Dunbar3f872332009-07-28 16:08:33 +00001197 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001198 return TokError("unexpected token in '.desc' directive");
1199
1200 Lexer.Lex();
1201
1202 // Set the n_desc field of this Symbol to this DescValue
1203 Out.EmitSymbolDesc(Sym, DescValue);
1204
1205 return false;
1206}
1207
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001208/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001209/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1210bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001211 SMLoc IDLoc = Lexer.getLoc();
1212 StringRef Name;
1213 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001214 return TokError("expected identifier in directive");
1215
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001216 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001217 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001218
Daniel Dunbar3f872332009-07-28 16:08:33 +00001219 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001220 return TokError("unexpected token in directive");
1221 Lexer.Lex();
1222
1223 int64_t Size;
1224 SMLoc SizeLoc = Lexer.getLoc();
1225 if (ParseAbsoluteExpression(Size))
1226 return true;
1227
1228 int64_t Pow2Alignment = 0;
1229 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001230 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001231 Lexer.Lex();
1232 Pow2AlignmentLoc = Lexer.getLoc();
1233 if (ParseAbsoluteExpression(Pow2Alignment))
1234 return true;
1235 }
1236
Daniel Dunbar3f872332009-07-28 16:08:33 +00001237 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001238 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001239
1240 Lexer.Lex();
1241
Chris Lattner1fc3d752009-07-09 17:25:12 +00001242 // NOTE: a size of zero for a .comm should create a undefined symbol
1243 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001244 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001245 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1246 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001247
1248 // NOTE: The alignment in the directive is a power of 2 value, the assember
1249 // may internally end up wanting an alignment in bytes.
1250 // FIXME: Diagnose overflow.
1251 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001252 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1253 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001254
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001255 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001256 return Error(IDLoc, "invalid symbol redefinition");
1257
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001258 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001259 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001260 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001261 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1262 MCSectionMachO::S_ZEROFILL, 0,
1263 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001264 Sym, Size, 1 << Pow2Alignment);
1265 return false;
1266 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001267
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001268 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001269 return false;
1270}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001271
1272/// ParseDirectiveDarwinZerofill
1273/// ::= .zerofill segname , sectname [, identifier , size_expression [
1274/// , align_expression ]]
1275bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001276 // FIXME: Handle quoted names here.
1277
Daniel Dunbar3f872332009-07-28 16:08:33 +00001278 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001279 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001280 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001281 Lexer.Lex();
1282
Daniel Dunbar3f872332009-07-28 16:08:33 +00001283 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001284 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001285 Lexer.Lex();
1286
Daniel Dunbar3f872332009-07-28 16:08:33 +00001287 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001288 return TokError("expected section name after comma in '.zerofill' "
1289 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001290 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001291 Lexer.Lex();
1292
Chris Lattner9be3fee2009-07-10 22:20:30 +00001293 // If this is the end of the line all that was wanted was to create the
1294 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001295 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001296 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001297 Out.EmitZerofill(getMachOSection(Segment, Section,
1298 MCSectionMachO::S_ZEROFILL, 0,
1299 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001300 return false;
1301 }
1302
Daniel Dunbar3f872332009-07-28 16:08:33 +00001303 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001304 return TokError("unexpected token in directive");
1305 Lexer.Lex();
1306
Daniel Dunbar3f872332009-07-28 16:08:33 +00001307 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001308 return TokError("expected identifier in directive");
1309
1310 // handle the identifier as the key symbol.
1311 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001312 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001313 Lexer.Lex();
1314
Daniel Dunbar3f872332009-07-28 16:08:33 +00001315 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001316 return TokError("unexpected token in directive");
1317 Lexer.Lex();
1318
1319 int64_t Size;
1320 SMLoc SizeLoc = Lexer.getLoc();
1321 if (ParseAbsoluteExpression(Size))
1322 return true;
1323
1324 int64_t Pow2Alignment = 0;
1325 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001326 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001327 Lexer.Lex();
1328 Pow2AlignmentLoc = Lexer.getLoc();
1329 if (ParseAbsoluteExpression(Pow2Alignment))
1330 return true;
1331 }
1332
Daniel Dunbar3f872332009-07-28 16:08:33 +00001333 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001334 return TokError("unexpected token in '.zerofill' directive");
1335
1336 Lexer.Lex();
1337
1338 if (Size < 0)
1339 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1340 "than zero");
1341
1342 // NOTE: The alignment in the directive is a power of 2 value, the assember
1343 // may internally end up wanting an alignment in bytes.
1344 // FIXME: Diagnose overflow.
1345 if (Pow2Alignment < 0)
1346 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1347 "can't be less than zero");
1348
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001349 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001350 return Error(IDLoc, "invalid symbol redefinition");
1351
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001352 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001353 //
1354 // FIXME: Arch specific.
1355 Out.EmitZerofill(getMachOSection(Segment, Section,
1356 MCSectionMachO::S_ZEROFILL, 0,
1357 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001358 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001359
1360 return false;
1361}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001362
1363/// ParseDirectiveDarwinSubsectionsViaSymbols
1364/// ::= .subsections_via_symbols
1365bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001366 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001367 return TokError("unexpected token in '.subsections_via_symbols' directive");
1368
1369 Lexer.Lex();
1370
Kevin Enderbyf96db462009-07-16 17:56:39 +00001371 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001372
1373 return false;
1374}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001375
1376/// ParseDirectiveAbort
1377/// ::= .abort [ "abort_string" ]
1378bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001379 // FIXME: Use loc from directive.
1380 SMLoc Loc = Lexer.getLoc();
1381
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001382 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001383 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1384 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001385 return TokError("expected string in '.abort' directive");
1386
Daniel Dunbar419aded2009-07-28 16:38:40 +00001387 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001388
1389 Lexer.Lex();
1390 }
1391
Daniel Dunbar3f872332009-07-28 16:08:33 +00001392 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001393 return TokError("unexpected token in '.abort' directive");
1394
1395 Lexer.Lex();
1396
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001397 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001398 if (Str.empty())
1399 Error(Loc, ".abort detected. Assembly stopping.");
1400 else
1401 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001402
1403 return false;
1404}
Kevin Enderby71148242009-07-14 21:35:03 +00001405
1406/// ParseDirectiveLsym
1407/// ::= .lsym identifier , expression
1408bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001409 StringRef Name;
1410 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001411 return TokError("expected identifier in directive");
1412
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001413 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001414 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001415
Daniel Dunbar3f872332009-07-28 16:08:33 +00001416 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001417 return TokError("unexpected token in '.lsym' directive");
1418 Lexer.Lex();
1419
Daniel Dunbar883f9202009-08-31 08:08:50 +00001420 MCValue Value;
1421 const MCExpr *Expr;
1422 SMLoc StartLoc = Lexer.getLoc();
1423 if (ParseExpression(Expr))
Kevin Enderby71148242009-07-14 21:35:03 +00001424 return true;
1425
Daniel Dunbar883f9202009-08-31 08:08:50 +00001426 if (!Expr->EvaluateAsRelocatable(Ctx, Value))
1427 return Error(StartLoc, "expected relocatable expression");
1428
Daniel Dunbar3f872332009-07-28 16:08:33 +00001429 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001430 return TokError("unexpected token in '.lsym' directive");
1431
1432 Lexer.Lex();
1433
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001434 // We don't currently support this directive.
1435 //
1436 // FIXME: Diagnostic location!
1437 (void) Sym;
1438 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001439}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001440
1441/// ParseDirectiveInclude
1442/// ::= .include "filename"
1443bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001444 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001445 return TokError("expected string in '.include' directive");
1446
Daniel Dunbar419aded2009-07-28 16:38:40 +00001447 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001448 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001449 Lexer.Lex();
1450
Daniel Dunbar3f872332009-07-28 16:08:33 +00001451 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001452 return TokError("unexpected token in '.include' directive");
1453
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001454 // Strip the quotes.
1455 Filename = Filename.substr(1, Filename.size()-2);
1456
1457 // Attempt to switch the lexer to the included file before consuming the end
1458 // of statement to avoid losing it when we switch.
1459 if (Lexer.EnterIncludeFile(Filename)) {
1460 Lexer.PrintMessage(IncludeLoc,
1461 "Could not find include file '" + Filename + "'",
1462 "error");
1463 return true;
1464 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001465
1466 return false;
1467}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001468
1469/// ParseDirectiveDarwinDumpOrLoad
1470/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001471bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001472 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001473 return TokError("expected string in '.dump' or '.load' directive");
1474
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001475 Lexer.Lex();
1476
Daniel Dunbar3f872332009-07-28 16:08:33 +00001477 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001478 return TokError("unexpected token in '.dump' or '.load' directive");
1479
1480 Lexer.Lex();
1481
Kevin Enderby5026ae42009-07-20 20:25:37 +00001482 // FIXME: If/when .dump and .load are implemented they will be done in the
1483 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001484 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001485 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001486 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001487 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001488
1489 return false;
1490}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001491
1492/// ParseDirectiveIf
1493/// ::= .if expression
1494bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1495 // Consume the identifier that was the .if directive
1496 Lexer.Lex();
1497
1498 TheCondStack.push_back(TheCondState);
1499 TheCondState.TheCond = AsmCond::IfCond;
1500 if(TheCondState.Ignore) {
1501 EatToEndOfStatement();
1502 }
1503 else {
1504 int64_t ExprValue;
1505 if (ParseAbsoluteExpression(ExprValue))
1506 return true;
1507
1508 if (Lexer.isNot(AsmToken::EndOfStatement))
1509 return TokError("unexpected token in '.if' directive");
1510
1511 Lexer.Lex();
1512
1513 TheCondState.CondMet = ExprValue;
1514 TheCondState.Ignore = !TheCondState.CondMet;
1515 }
1516
1517 return false;
1518}
1519
1520/// ParseDirectiveElseIf
1521/// ::= .elseif expression
1522bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1523 if (TheCondState.TheCond != AsmCond::IfCond &&
1524 TheCondState.TheCond != AsmCond::ElseIfCond)
1525 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1526 " an .elseif");
1527 TheCondState.TheCond = AsmCond::ElseIfCond;
1528
1529 // Consume the identifier that was the .elseif directive
1530 Lexer.Lex();
1531
1532 bool LastIgnoreState = false;
1533 if (!TheCondStack.empty())
1534 LastIgnoreState = TheCondStack.back().Ignore;
1535 if (LastIgnoreState || TheCondState.CondMet) {
1536 TheCondState.Ignore = true;
1537 EatToEndOfStatement();
1538 }
1539 else {
1540 int64_t ExprValue;
1541 if (ParseAbsoluteExpression(ExprValue))
1542 return true;
1543
1544 if (Lexer.isNot(AsmToken::EndOfStatement))
1545 return TokError("unexpected token in '.elseif' directive");
1546
1547 Lexer.Lex();
1548 TheCondState.CondMet = ExprValue;
1549 TheCondState.Ignore = !TheCondState.CondMet;
1550 }
1551
1552 return false;
1553}
1554
1555/// ParseDirectiveElse
1556/// ::= .else
1557bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1558 // Consume the identifier that was the .else directive
1559 Lexer.Lex();
1560
1561 if (Lexer.isNot(AsmToken::EndOfStatement))
1562 return TokError("unexpected token in '.else' directive");
1563
1564 Lexer.Lex();
1565
1566 if (TheCondState.TheCond != AsmCond::IfCond &&
1567 TheCondState.TheCond != AsmCond::ElseIfCond)
1568 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1569 ".elseif");
1570 TheCondState.TheCond = AsmCond::ElseCond;
1571 bool LastIgnoreState = false;
1572 if (!TheCondStack.empty())
1573 LastIgnoreState = TheCondStack.back().Ignore;
1574 if (LastIgnoreState || TheCondState.CondMet)
1575 TheCondState.Ignore = true;
1576 else
1577 TheCondState.Ignore = false;
1578
1579 return false;
1580}
1581
1582/// ParseDirectiveEndIf
1583/// ::= .endif
1584bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1585 // Consume the identifier that was the .endif directive
1586 Lexer.Lex();
1587
1588 if (Lexer.isNot(AsmToken::EndOfStatement))
1589 return TokError("unexpected token in '.endif' directive");
1590
1591 Lexer.Lex();
1592
1593 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1594 TheCondStack.empty())
1595 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1596 ".else");
1597 if (!TheCondStack.empty()) {
1598 TheCondState = TheCondStack.back();
1599 TheCondStack.pop_back();
1600 }
1601
1602 return false;
1603}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001604
1605/// ParseDirectiveFile
1606/// ::= .file [number] string
1607bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1608 // FIXME: I'm not sure what this is.
1609 int64_t FileNumber = -1;
1610 if (Lexer.is(AsmToken::Integer)) {
1611 FileNumber = Lexer.getTok().getIntVal();
1612 Lexer.Lex();
1613
1614 if (FileNumber < 1)
1615 return TokError("file number less than one");
1616 }
1617
1618 if (Lexer.isNot(AsmToken::String))
1619 return TokError("unexpected token in '.file' directive");
1620
1621 StringRef FileName = Lexer.getTok().getString();
1622 Lexer.Lex();
1623
1624 if (Lexer.isNot(AsmToken::EndOfStatement))
1625 return TokError("unexpected token in '.file' directive");
1626
1627 // FIXME: Do something with the .file.
1628
1629 return false;
1630}
1631
1632/// ParseDirectiveLine
1633/// ::= .line [number]
1634bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1635 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1636 if (Lexer.isNot(AsmToken::Integer))
1637 return TokError("unexpected token in '.line' directive");
1638
1639 int64_t LineNumber = Lexer.getTok().getIntVal();
1640 (void) LineNumber;
1641 Lexer.Lex();
1642
1643 // FIXME: Do something with the .line.
1644 }
1645
1646 if (Lexer.isNot(AsmToken::EndOfStatement))
1647 return TokError("unexpected token in '.file' directive");
1648
1649 return false;
1650}
1651
1652
1653/// ParseDirectiveLoc
1654/// ::= .loc number [number [number]]
1655bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1656 if (Lexer.isNot(AsmToken::Integer))
1657 return TokError("unexpected token in '.loc' directive");
1658
1659 // FIXME: What are these fields?
1660 int64_t FileNumber = Lexer.getTok().getIntVal();
1661 (void) FileNumber;
1662 // FIXME: Validate file.
1663
1664 Lexer.Lex();
1665 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1666 if (Lexer.isNot(AsmToken::Integer))
1667 return TokError("unexpected token in '.loc' directive");
1668
1669 int64_t Param2 = Lexer.getTok().getIntVal();
1670 (void) Param2;
1671 Lexer.Lex();
1672
1673 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1674 if (Lexer.isNot(AsmToken::Integer))
1675 return TokError("unexpected token in '.loc' directive");
1676
1677 int64_t Param3 = Lexer.getTok().getIntVal();
1678 (void) Param3;
1679 Lexer.Lex();
1680
1681 // FIXME: Do something with the .loc.
1682 }
1683 }
1684
1685 if (Lexer.isNot(AsmToken::EndOfStatement))
1686 return TokError("unexpected token in '.file' directive");
1687
1688 return false;
1689}
1690