blob: 78cab38b4ffc8a324077287e9eaf9173918c2aef [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 Dunbar15d17072009-06-30 01:49:52 +0000282bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000283 const MCExpr *Expr;
Daniel Dunbar15d17072009-06-30 01:49:52 +0000284
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000285 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000286 if (ParseExpression(Expr))
287 return true;
288
289 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000290 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000291
292 return false;
293}
294
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000295bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000296 const MCExpr *Expr;
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000297
298 SMLoc StartLoc = Lexer.getLoc();
299 if (ParseParenExpr(Expr))
300 return true;
301
302 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
303 return Error(StartLoc, "expected relocatable expression");
304
305 return false;
306}
307
Daniel Dunbar3f872332009-07-28 16:08:33 +0000308static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000309 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000310 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000311 default:
312 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000313
314 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000315 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000316 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000317 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000318 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000319 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000320 return 1;
321
322 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000324 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000325 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000327 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000328 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::ExclaimEqual:
333 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000334 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000342 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000343 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 2;
348
349 // Intermediate Precedence: |, &, ^
350 //
351 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000353 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000354 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000357 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000358 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000359 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360 return 3;
361
362 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000372 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000378 }
379}
380
381
382/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
383/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000384bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000385 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000386 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000387 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000388
389 // If the next token is lower precedence than we are allowed to eat, return
390 // successfully with what we ate already.
391 if (TokPrec < Precedence)
392 return false;
393
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000394 Lexer.Lex();
395
396 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000397 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 if (ParsePrimaryExpr(RHS)) return true;
399
400 // If BinOp binds less tightly with RHS than the operator after RHS, let
401 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000402 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000404 if (TokPrec < NextTokPrec) {
405 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
406 }
407
Daniel Dunbar475839e2009-06-29 20:37:27 +0000408 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000409 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000410 }
411}
412
Chris Lattnerc4193832009-06-22 05:51:26 +0000413
414
415
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000416/// ParseStatement:
417/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000418/// ::= Label* Directive ...Operands... EndOfStatement
419/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000420bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000421 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000422 Lexer.Lex();
423 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000424 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000425
426 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000427 AsmToken ID = Lexer.getTok();
428 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000429 StringRef IDVal;
430 if (ParseIdentifier(IDVal))
431 return TokError("unexpected token at start of statement");
432
433 // FIXME: Recurse on local labels?
434
435 // See what kind of statement we have.
436 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000437 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000438 // identifier ':' -> Label.
439 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000440
441 // Diagnose attempt to use a variable as a label.
442 //
443 // FIXME: Diagnostics. Note the location of the definition as a label.
444 // FIXME: This doesn't diagnose assignment to a symbol which has been
445 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000446 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000447 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000448 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000449
Daniel Dunbar959fd882009-08-26 22:13:22 +0000450 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000451 Out.EmitLabel(Sym);
452
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000453 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000454 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000455
Daniel Dunbar3f872332009-07-28 16:08:33 +0000456 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000457 // identifier '=' ... -> assignment statement
458 Lexer.Lex();
459
460 return ParseAssignment(IDVal, false);
461
462 default: // Normal instruction or directive.
463 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000464 }
465
466 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000467 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000468 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000470 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000471 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000472 // FIXME: This changes behavior based on the -static flag to the
473 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000474 return ParseDirectiveSectionSwitch("__TEXT", "__text",
475 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000477 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000479 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000481 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
482 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000483 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000484 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000485 MCSectionMachO::S_4BYTE_LITERALS,
486 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000488 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000489 MCSectionMachO::S_8BYTE_LITERALS,
490 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000491 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000492 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 MCSectionMachO::S_16BYTE_LITERALS,
494 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000495 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000496 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000497 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000498 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000502 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
503
504 // FIXME: The assembler manual claims that this has the self modify code
505 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000506 if (IDVal == ".symbol_stub")
507 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
508 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000509 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
510 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000511 0, 16);
512 // FIXME: PowerPC only?
513 if (IDVal == ".picsymbol_stub")
514 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
515 MCSectionMachO::S_SYMBOL_STUBS |
516 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
517 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000519 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000521 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
522
523 // FIXME: The section names of these two are misspelled in the assembler
524 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000526 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
527 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
528 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000530 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
531 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
532 4);
533
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000537 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000538 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
539 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000541 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
543 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000546
547
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__OBJC", "__class",
550 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
553 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
556 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
559 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
562 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000564 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
565 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
568 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
571 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000573 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
574 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
575 MCSectionMachO::S_LITERAL_POINTERS,
576 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000577 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000578 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
579 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
580 MCSectionMachO::S_LITERAL_POINTERS,
581 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__OBJC", "__category",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
590 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
593 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
596 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000598 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
599 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000601 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
602 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000604 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
605 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
608 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000609
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000610 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000612 return ParseDirectiveSet();
613
Daniel Dunbara0d14262009-06-24 23:30:00 +0000614 // Data directives
615
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000617 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000619 return ParseDirectiveAscii(true);
620
621 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000623 return ParseDirectiveValue(1);
Daniel Dunbar1e840b22009-08-11 04:44:00 +0000624 if (IDVal == ".short" || IDVal == ".word")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000625 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000627 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000629 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000630
631 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000633 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000635 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000637 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000639 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000641 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000643 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000645 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000647 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
648
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000650 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000651
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000653 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000655 return ParseDirectiveSpace();
656
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000657 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000658
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000660 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000662 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000664 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000666 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000668 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000670 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000672 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000674 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000676 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000678 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000680 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000682 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
683
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000685 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000687 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000689 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000691 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000693 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000694
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000696 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000698 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000700 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000702 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000704 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000705
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000706 // Debugging directives
707
708 if (IDVal == ".file")
709 return ParseDirectiveFile(IDLoc);
710 if (IDVal == ".line")
711 return ParseDirectiveLine(IDLoc);
712 if (IDVal == ".loc")
713 return ParseDirectiveLoc(IDLoc);
714
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000715 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000716 EatToEndOfStatement();
717 return false;
718 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000719
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000720 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000721 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000722 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000723
Daniel Dunbar3f872332009-07-28 16:08:33 +0000724 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000725 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000726
727 // Eat the end of statement marker.
728 Lexer.Lex();
729
730 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000731 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000732
733 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000734 return false;
735}
Chris Lattner9a023f72009-06-24 04:43:34 +0000736
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000738 // FIXME: Use better location, we should use proper tokens.
739 SMLoc EqualLoc = Lexer.getLoc();
740
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000741 MCValue Value;
742 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000743 return true;
744
Daniel Dunbar3f872332009-07-28 16:08:33 +0000745 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000746 return TokError("unexpected token in assignment");
747
748 // Eat the end of statement marker.
749 Lexer.Lex();
750
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000751 // Diagnose assignment to a label.
752 //
753 // FIXME: Diagnostics. Note the location of the definition as a label.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000754 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000755 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar959fd882009-08-26 22:13:22 +0000756 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000757 if (!Sym->isUndefined() && !Sym->isAbsolute())
758 return Error(EqualLoc, "symbol has already been defined");
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000759
760 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000761 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000762
763 return false;
764}
765
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000766/// ParseIdentifier:
767/// ::= identifier
768/// ::= string
769bool AsmParser::ParseIdentifier(StringRef &Res) {
770 if (Lexer.isNot(AsmToken::Identifier) &&
771 Lexer.isNot(AsmToken::String))
772 return true;
773
774 Res = Lexer.getTok().getIdentifier();
775
776 Lexer.Lex(); // Consume the identifier token.
777
778 return false;
779}
780
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000781/// ParseDirectiveSet:
782/// ::= .set identifier ',' expression
783bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000784 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000785
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000786 if (ParseIdentifier(Name))
787 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000788
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000789 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000790 return TokError("unexpected token in '.set'");
791 Lexer.Lex();
792
793 return ParseAssignment(Name, true);
794}
795
Chris Lattner9a023f72009-06-24 04:43:34 +0000796/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000797/// ::= .section identifier (',' identifier)*
798/// FIXME: This should actually parse out the segment, section, attributes and
799/// sizeof_stub fields.
800bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000801 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000802
Daniel Dunbarace63122009-08-11 03:42:33 +0000803 StringRef SectionName;
804 if (ParseIdentifier(SectionName))
805 return Error(Loc, "expected identifier after '.section' directive");
806
807 // Verify there is a following comma.
808 if (!Lexer.is(AsmToken::Comma))
809 return TokError("unexpected token in '.section' directive");
810
Chris Lattnerff4bc462009-08-10 01:39:42 +0000811 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000812 SectionSpec += ",";
813
814 // Add all the tokens until the end of the line, ParseSectionSpecifier will
815 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000816 StringRef EOL = Lexer.LexUntilEndOfStatement();
817 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000818
Chris Lattnerff4bc462009-08-10 01:39:42 +0000819 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000820 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000821 return TokError("unexpected token in '.section' directive");
822 Lexer.Lex();
823
Chris Lattnerff4bc462009-08-10 01:39:42 +0000824
825 StringRef Segment, Section;
826 unsigned TAA, StubSize;
827 std::string ErrorStr =
828 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
829 TAA, StubSize);
830
831 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000832 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000833
Chris Lattner56594f92009-07-31 17:47:16 +0000834 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000835 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
836 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000837 return false;
838}
839
Chris Lattnere15c2d72009-08-10 18:05:55 +0000840/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000841bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
842 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000843 unsigned TAA, unsigned Align,
844 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000846 return TokError("unexpected token in section switching directive");
847 Lexer.Lex();
848
Chris Lattner56594f92009-07-31 17:47:16 +0000849 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000850 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
851 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000852
853 // Set the implicit alignment, if any.
854 //
855 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
856 // alignment on the section (e.g., if one manually inserts bytes into the
857 // section, then just issueing the section switch directive will not realign
858 // the section. However, this is arguably more reasonable behavior, and there
859 // is no good reason for someone to intentionally emit incorrectly sized
860 // values into the implicitly aligned sections.
861 if (Align)
862 Out.EmitValueToAlignment(Align, 0, 1, 0);
863
Chris Lattner529fb542009-06-24 05:13:15 +0000864 return false;
865}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000866
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000867bool AsmParser::ParseEscapedString(std::string &Data) {
868 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
869
870 Data = "";
871 StringRef Str = Lexer.getTok().getStringContents();
872 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
873 if (Str[i] != '\\') {
874 Data += Str[i];
875 continue;
876 }
877
878 // Recognize escaped characters. Note that this escape semantics currently
879 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
880 ++i;
881 if (i == e)
882 return TokError("unexpected backslash at end of string");
883
884 // Recognize octal sequences.
885 if ((unsigned) (Str[i] - '0') <= 7) {
886 // Consume up to three octal characters.
887 unsigned Value = Str[i] - '0';
888
889 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
890 ++i;
891 Value = Value * 8 + (Str[i] - '0');
892
893 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
894 ++i;
895 Value = Value * 8 + (Str[i] - '0');
896 }
897 }
898
899 if (Value > 255)
900 return TokError("invalid octal escape sequence (out of range)");
901
902 Data += (unsigned char) Value;
903 continue;
904 }
905
906 // Otherwise recognize individual escapes.
907 switch (Str[i]) {
908 default:
909 // Just reject invalid escape sequences for now.
910 return TokError("invalid escape sequence (unrecognized character)");
911
912 case 'b': Data += '\b'; break;
913 case 'f': Data += '\f'; break;
914 case 'n': Data += '\n'; break;
915 case 'r': Data += '\r'; break;
916 case 't': Data += '\t'; break;
917 case '"': Data += '"'; break;
918 case '\\': Data += '\\'; break;
919 }
920 }
921
922 return false;
923}
924
Daniel Dunbara0d14262009-06-24 23:30:00 +0000925/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000926/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000927bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000928 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000930 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000931 return TokError("expected string in '.ascii' or '.asciz' directive");
932
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000933 std::string Data;
934 if (ParseEscapedString(Data))
935 return true;
936
937 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000938 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000939 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000940
941 Lexer.Lex();
942
Daniel Dunbar3f872332009-07-28 16:08:33 +0000943 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000944 break;
945
Daniel Dunbar3f872332009-07-28 16:08:33 +0000946 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000947 return TokError("unexpected token in '.ascii' or '.asciz' directive");
948 Lexer.Lex();
949 }
950 }
951
952 Lexer.Lex();
953 return false;
954}
955
956/// ParseDirectiveValue
957/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
958bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000959 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000961 MCValue Expr;
962 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963 return true;
964
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000965 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966
Daniel Dunbar3f872332009-07-28 16:08:33 +0000967 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000968 break;
969
970 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000971 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000972 return TokError("unexpected token in directive");
973 Lexer.Lex();
974 }
975 }
976
977 Lexer.Lex();
978 return false;
979}
980
981/// ParseDirectiveSpace
982/// ::= .space expression [ , expression ]
983bool AsmParser::ParseDirectiveSpace() {
984 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000985 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000986 return true;
987
988 int64_t FillExpr = 0;
989 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000990 if (Lexer.isNot(AsmToken::EndOfStatement)) {
991 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000992 return TokError("unexpected token in '.space' directive");
993 Lexer.Lex();
994
Daniel Dunbar475839e2009-06-29 20:37:27 +0000995 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996 return true;
997
998 HasFillExpr = true;
999
Daniel Dunbar3f872332009-07-28 16:08:33 +00001000 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001001 return TokError("unexpected token in '.space' directive");
1002 }
1003
1004 Lexer.Lex();
1005
1006 if (NumBytes <= 0)
1007 return TokError("invalid number of bytes in '.space' directive");
1008
1009 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1010 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
1011 Out.EmitValue(MCValue::get(FillExpr), 1);
1012
1013 return false;
1014}
1015
1016/// ParseDirectiveFill
1017/// ::= .fill expression , expression , expression
1018bool AsmParser::ParseDirectiveFill() {
1019 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001020 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 return true;
1022
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 return TokError("unexpected token in '.fill' directive");
1025 Lexer.Lex();
1026
1027 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001028 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return true;
1030
Daniel Dunbar3f872332009-07-28 16:08:33 +00001031 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032 return TokError("unexpected token in '.fill' directive");
1033 Lexer.Lex();
1034
1035 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001036 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return true;
1038
Daniel Dunbar3f872332009-07-28 16:08:33 +00001039 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001040 return TokError("unexpected token in '.fill' directive");
1041
1042 Lexer.Lex();
1043
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001044 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1045 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046
1047 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1048 Out.EmitValue(MCValue::get(FillExpr), FillSize);
1049
1050 return false;
1051}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001052
1053/// ParseDirectiveOrg
1054/// ::= .org expression [ , expression ]
1055bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001056 MCValue Offset;
1057 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001058 return true;
1059
1060 // Parse optional fill expression.
1061 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001062 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1063 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001064 return TokError("unexpected token in '.org' directive");
1065 Lexer.Lex();
1066
Daniel Dunbar475839e2009-06-29 20:37:27 +00001067 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001068 return true;
1069
Daniel Dunbar3f872332009-07-28 16:08:33 +00001070 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001071 return TokError("unexpected token in '.org' directive");
1072 }
1073
1074 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001075
1076 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1077 // has to be relative to the current section.
1078 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001079
1080 return false;
1081}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001082
1083/// ParseDirectiveAlign
1084/// ::= {.align, ...} expression [ , expression [ , expression ]]
1085bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001086 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001087 int64_t Alignment;
1088 if (ParseAbsoluteExpression(Alignment))
1089 return true;
1090
1091 SMLoc MaxBytesLoc;
1092 bool HasFillExpr = false;
1093 int64_t FillExpr = 0;
1094 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001095 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1096 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001097 return TokError("unexpected token in directive");
1098 Lexer.Lex();
1099
1100 // The fill expression can be omitted while specifying a maximum number of
1101 // alignment bytes, e.g:
1102 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001103 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001104 HasFillExpr = true;
1105 if (ParseAbsoluteExpression(FillExpr))
1106 return true;
1107 }
1108
Daniel Dunbar3f872332009-07-28 16:08:33 +00001109 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1110 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001111 return TokError("unexpected token in directive");
1112 Lexer.Lex();
1113
1114 MaxBytesLoc = Lexer.getLoc();
1115 if (ParseAbsoluteExpression(MaxBytesToFill))
1116 return true;
1117
Daniel Dunbar3f872332009-07-28 16:08:33 +00001118 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001119 return TokError("unexpected token in directive");
1120 }
1121 }
1122
1123 Lexer.Lex();
1124
1125 if (!HasFillExpr) {
1126 // FIXME: Sometimes fill with nop.
1127 FillExpr = 0;
1128 }
1129
1130 // Compute alignment in bytes.
1131 if (IsPow2) {
1132 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001133 if (Alignment >= 32) {
1134 Error(AlignmentLoc, "invalid alignment value");
1135 Alignment = 31;
1136 }
1137
1138 Alignment = 1 << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001139 }
1140
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001141 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001142 if (MaxBytesLoc.isValid()) {
1143 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001144 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1145 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001146 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001147 }
1148
1149 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001150 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1151 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001152 MaxBytesToFill = 0;
1153 }
1154 }
1155
1156 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1157 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1158
1159 return false;
1160}
1161
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001162/// ParseDirectiveSymbolAttribute
1163/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1164bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001165 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001166 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001167 StringRef Name;
1168
1169 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001170 return TokError("expected identifier in directive");
1171
Daniel Dunbar959fd882009-08-26 22:13:22 +00001172 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001173
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001174 Out.EmitSymbolAttribute(Sym, Attr);
1175
Daniel Dunbar3f872332009-07-28 16:08:33 +00001176 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001177 break;
1178
Daniel Dunbar3f872332009-07-28 16:08:33 +00001179 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001180 return TokError("unexpected token in directive");
1181 Lexer.Lex();
1182 }
1183 }
1184
1185 Lexer.Lex();
1186 return false;
1187}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001188
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001189/// ParseDirectiveDarwinSymbolDesc
1190/// ::= .desc identifier , expression
1191bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001192 StringRef Name;
1193 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001194 return TokError("expected identifier in directive");
1195
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001196 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001197 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001198
Daniel Dunbar3f872332009-07-28 16:08:33 +00001199 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001200 return TokError("unexpected token in '.desc' directive");
1201 Lexer.Lex();
1202
1203 SMLoc DescLoc = Lexer.getLoc();
1204 int64_t DescValue;
1205 if (ParseAbsoluteExpression(DescValue))
1206 return true;
1207
Daniel Dunbar3f872332009-07-28 16:08:33 +00001208 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001209 return TokError("unexpected token in '.desc' directive");
1210
1211 Lexer.Lex();
1212
1213 // Set the n_desc field of this Symbol to this DescValue
1214 Out.EmitSymbolDesc(Sym, DescValue);
1215
1216 return false;
1217}
1218
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001219/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001220/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1221bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001222 SMLoc IDLoc = Lexer.getLoc();
1223 StringRef Name;
1224 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001225 return TokError("expected identifier in directive");
1226
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001227 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001228 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001229
Daniel Dunbar3f872332009-07-28 16:08:33 +00001230 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001231 return TokError("unexpected token in directive");
1232 Lexer.Lex();
1233
1234 int64_t Size;
1235 SMLoc SizeLoc = Lexer.getLoc();
1236 if (ParseAbsoluteExpression(Size))
1237 return true;
1238
1239 int64_t Pow2Alignment = 0;
1240 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001241 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001242 Lexer.Lex();
1243 Pow2AlignmentLoc = Lexer.getLoc();
1244 if (ParseAbsoluteExpression(Pow2Alignment))
1245 return true;
1246 }
1247
Daniel Dunbar3f872332009-07-28 16:08:33 +00001248 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001249 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001250
1251 Lexer.Lex();
1252
Chris Lattner1fc3d752009-07-09 17:25:12 +00001253 // NOTE: a size of zero for a .comm should create a undefined symbol
1254 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001255 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001256 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1257 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001258
1259 // NOTE: The alignment in the directive is a power of 2 value, the assember
1260 // may internally end up wanting an alignment in bytes.
1261 // FIXME: Diagnose overflow.
1262 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001263 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1264 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001265
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001266 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001267 return Error(IDLoc, "invalid symbol redefinition");
1268
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001269 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001270 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001271 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001272 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1273 MCSectionMachO::S_ZEROFILL, 0,
1274 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001275 Sym, Size, 1 << Pow2Alignment);
1276 return false;
1277 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001278
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001279 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001280 return false;
1281}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001282
1283/// ParseDirectiveDarwinZerofill
1284/// ::= .zerofill segname , sectname [, identifier , size_expression [
1285/// , align_expression ]]
1286bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001287 // FIXME: Handle quoted names here.
1288
Daniel Dunbar3f872332009-07-28 16:08:33 +00001289 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001290 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001291 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001292 Lexer.Lex();
1293
Daniel Dunbar3f872332009-07-28 16:08:33 +00001294 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001295 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001296 Lexer.Lex();
1297
Daniel Dunbar3f872332009-07-28 16:08:33 +00001298 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001299 return TokError("expected section name after comma in '.zerofill' "
1300 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001301 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001302 Lexer.Lex();
1303
Chris Lattner9be3fee2009-07-10 22:20:30 +00001304 // If this is the end of the line all that was wanted was to create the
1305 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001306 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001307 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001308 Out.EmitZerofill(getMachOSection(Segment, Section,
1309 MCSectionMachO::S_ZEROFILL, 0,
1310 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001311 return false;
1312 }
1313
Daniel Dunbar3f872332009-07-28 16:08:33 +00001314 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001315 return TokError("unexpected token in directive");
1316 Lexer.Lex();
1317
Daniel Dunbar3f872332009-07-28 16:08:33 +00001318 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001319 return TokError("expected identifier in directive");
1320
1321 // handle the identifier as the key symbol.
1322 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001323 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001324 Lexer.Lex();
1325
Daniel Dunbar3f872332009-07-28 16:08:33 +00001326 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001327 return TokError("unexpected token in directive");
1328 Lexer.Lex();
1329
1330 int64_t Size;
1331 SMLoc SizeLoc = Lexer.getLoc();
1332 if (ParseAbsoluteExpression(Size))
1333 return true;
1334
1335 int64_t Pow2Alignment = 0;
1336 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001337 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001338 Lexer.Lex();
1339 Pow2AlignmentLoc = Lexer.getLoc();
1340 if (ParseAbsoluteExpression(Pow2Alignment))
1341 return true;
1342 }
1343
Daniel Dunbar3f872332009-07-28 16:08:33 +00001344 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001345 return TokError("unexpected token in '.zerofill' directive");
1346
1347 Lexer.Lex();
1348
1349 if (Size < 0)
1350 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1351 "than zero");
1352
1353 // NOTE: The alignment in the directive is a power of 2 value, the assember
1354 // may internally end up wanting an alignment in bytes.
1355 // FIXME: Diagnose overflow.
1356 if (Pow2Alignment < 0)
1357 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1358 "can't be less than zero");
1359
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001360 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001361 return Error(IDLoc, "invalid symbol redefinition");
1362
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001363 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001364 //
1365 // FIXME: Arch specific.
1366 Out.EmitZerofill(getMachOSection(Segment, Section,
1367 MCSectionMachO::S_ZEROFILL, 0,
1368 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001369 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001370
1371 return false;
1372}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001373
1374/// ParseDirectiveDarwinSubsectionsViaSymbols
1375/// ::= .subsections_via_symbols
1376bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001377 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001378 return TokError("unexpected token in '.subsections_via_symbols' directive");
1379
1380 Lexer.Lex();
1381
Kevin Enderbyf96db462009-07-16 17:56:39 +00001382 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001383
1384 return false;
1385}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001386
1387/// ParseDirectiveAbort
1388/// ::= .abort [ "abort_string" ]
1389bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001390 // FIXME: Use loc from directive.
1391 SMLoc Loc = Lexer.getLoc();
1392
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001393 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001394 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1395 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001396 return TokError("expected string in '.abort' directive");
1397
Daniel Dunbar419aded2009-07-28 16:38:40 +00001398 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001399
1400 Lexer.Lex();
1401 }
1402
Daniel Dunbar3f872332009-07-28 16:08:33 +00001403 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001404 return TokError("unexpected token in '.abort' directive");
1405
1406 Lexer.Lex();
1407
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001408 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001409 if (Str.empty())
1410 Error(Loc, ".abort detected. Assembly stopping.");
1411 else
1412 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001413
1414 return false;
1415}
Kevin Enderby71148242009-07-14 21:35:03 +00001416
1417/// ParseDirectiveLsym
1418/// ::= .lsym identifier , expression
1419bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001420 StringRef Name;
1421 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001422 return TokError("expected identifier in directive");
1423
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001424 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001425 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001426
Daniel Dunbar3f872332009-07-28 16:08:33 +00001427 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001428 return TokError("unexpected token in '.lsym' directive");
1429 Lexer.Lex();
1430
1431 MCValue Expr;
1432 if (ParseRelocatableExpression(Expr))
1433 return true;
1434
Daniel Dunbar3f872332009-07-28 16:08:33 +00001435 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001436 return TokError("unexpected token in '.lsym' directive");
1437
1438 Lexer.Lex();
1439
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001440 // We don't currently support this directive.
1441 //
1442 // FIXME: Diagnostic location!
1443 (void) Sym;
1444 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001445}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001446
1447/// ParseDirectiveInclude
1448/// ::= .include "filename"
1449bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001450 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001451 return TokError("expected string in '.include' directive");
1452
Daniel Dunbar419aded2009-07-28 16:38:40 +00001453 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001454 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001455 Lexer.Lex();
1456
Daniel Dunbar3f872332009-07-28 16:08:33 +00001457 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001458 return TokError("unexpected token in '.include' directive");
1459
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001460 // Strip the quotes.
1461 Filename = Filename.substr(1, Filename.size()-2);
1462
1463 // Attempt to switch the lexer to the included file before consuming the end
1464 // of statement to avoid losing it when we switch.
1465 if (Lexer.EnterIncludeFile(Filename)) {
1466 Lexer.PrintMessage(IncludeLoc,
1467 "Could not find include file '" + Filename + "'",
1468 "error");
1469 return true;
1470 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001471
1472 return false;
1473}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001474
1475/// ParseDirectiveDarwinDumpOrLoad
1476/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001477bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001478 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001479 return TokError("expected string in '.dump' or '.load' directive");
1480
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001481 Lexer.Lex();
1482
Daniel Dunbar3f872332009-07-28 16:08:33 +00001483 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001484 return TokError("unexpected token in '.dump' or '.load' directive");
1485
1486 Lexer.Lex();
1487
Kevin Enderby5026ae42009-07-20 20:25:37 +00001488 // FIXME: If/when .dump and .load are implemented they will be done in the
1489 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001490 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001491 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001492 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001493 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001494
1495 return false;
1496}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001497
1498/// ParseDirectiveIf
1499/// ::= .if expression
1500bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1501 // Consume the identifier that was the .if directive
1502 Lexer.Lex();
1503
1504 TheCondStack.push_back(TheCondState);
1505 TheCondState.TheCond = AsmCond::IfCond;
1506 if(TheCondState.Ignore) {
1507 EatToEndOfStatement();
1508 }
1509 else {
1510 int64_t ExprValue;
1511 if (ParseAbsoluteExpression(ExprValue))
1512 return true;
1513
1514 if (Lexer.isNot(AsmToken::EndOfStatement))
1515 return TokError("unexpected token in '.if' directive");
1516
1517 Lexer.Lex();
1518
1519 TheCondState.CondMet = ExprValue;
1520 TheCondState.Ignore = !TheCondState.CondMet;
1521 }
1522
1523 return false;
1524}
1525
1526/// ParseDirectiveElseIf
1527/// ::= .elseif expression
1528bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1529 if (TheCondState.TheCond != AsmCond::IfCond &&
1530 TheCondState.TheCond != AsmCond::ElseIfCond)
1531 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1532 " an .elseif");
1533 TheCondState.TheCond = AsmCond::ElseIfCond;
1534
1535 // Consume the identifier that was the .elseif directive
1536 Lexer.Lex();
1537
1538 bool LastIgnoreState = false;
1539 if (!TheCondStack.empty())
1540 LastIgnoreState = TheCondStack.back().Ignore;
1541 if (LastIgnoreState || TheCondState.CondMet) {
1542 TheCondState.Ignore = true;
1543 EatToEndOfStatement();
1544 }
1545 else {
1546 int64_t ExprValue;
1547 if (ParseAbsoluteExpression(ExprValue))
1548 return true;
1549
1550 if (Lexer.isNot(AsmToken::EndOfStatement))
1551 return TokError("unexpected token in '.elseif' directive");
1552
1553 Lexer.Lex();
1554 TheCondState.CondMet = ExprValue;
1555 TheCondState.Ignore = !TheCondState.CondMet;
1556 }
1557
1558 return false;
1559}
1560
1561/// ParseDirectiveElse
1562/// ::= .else
1563bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1564 // Consume the identifier that was the .else directive
1565 Lexer.Lex();
1566
1567 if (Lexer.isNot(AsmToken::EndOfStatement))
1568 return TokError("unexpected token in '.else' directive");
1569
1570 Lexer.Lex();
1571
1572 if (TheCondState.TheCond != AsmCond::IfCond &&
1573 TheCondState.TheCond != AsmCond::ElseIfCond)
1574 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1575 ".elseif");
1576 TheCondState.TheCond = AsmCond::ElseCond;
1577 bool LastIgnoreState = false;
1578 if (!TheCondStack.empty())
1579 LastIgnoreState = TheCondStack.back().Ignore;
1580 if (LastIgnoreState || TheCondState.CondMet)
1581 TheCondState.Ignore = true;
1582 else
1583 TheCondState.Ignore = false;
1584
1585 return false;
1586}
1587
1588/// ParseDirectiveEndIf
1589/// ::= .endif
1590bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1591 // Consume the identifier that was the .endif directive
1592 Lexer.Lex();
1593
1594 if (Lexer.isNot(AsmToken::EndOfStatement))
1595 return TokError("unexpected token in '.endif' directive");
1596
1597 Lexer.Lex();
1598
1599 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1600 TheCondStack.empty())
1601 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1602 ".else");
1603 if (!TheCondStack.empty()) {
1604 TheCondState = TheCondStack.back();
1605 TheCondStack.pop_back();
1606 }
1607
1608 return false;
1609}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001610
1611/// ParseDirectiveFile
1612/// ::= .file [number] string
1613bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1614 // FIXME: I'm not sure what this is.
1615 int64_t FileNumber = -1;
1616 if (Lexer.is(AsmToken::Integer)) {
1617 FileNumber = Lexer.getTok().getIntVal();
1618 Lexer.Lex();
1619
1620 if (FileNumber < 1)
1621 return TokError("file number less than one");
1622 }
1623
1624 if (Lexer.isNot(AsmToken::String))
1625 return TokError("unexpected token in '.file' directive");
1626
1627 StringRef FileName = Lexer.getTok().getString();
1628 Lexer.Lex();
1629
1630 if (Lexer.isNot(AsmToken::EndOfStatement))
1631 return TokError("unexpected token in '.file' directive");
1632
1633 // FIXME: Do something with the .file.
1634
1635 return false;
1636}
1637
1638/// ParseDirectiveLine
1639/// ::= .line [number]
1640bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1641 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1642 if (Lexer.isNot(AsmToken::Integer))
1643 return TokError("unexpected token in '.line' directive");
1644
1645 int64_t LineNumber = Lexer.getTok().getIntVal();
1646 (void) LineNumber;
1647 Lexer.Lex();
1648
1649 // FIXME: Do something with the .line.
1650 }
1651
1652 if (Lexer.isNot(AsmToken::EndOfStatement))
1653 return TokError("unexpected token in '.file' directive");
1654
1655 return false;
1656}
1657
1658
1659/// ParseDirectiveLoc
1660/// ::= .loc number [number [number]]
1661bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1662 if (Lexer.isNot(AsmToken::Integer))
1663 return TokError("unexpected token in '.loc' directive");
1664
1665 // FIXME: What are these fields?
1666 int64_t FileNumber = Lexer.getTok().getIntVal();
1667 (void) FileNumber;
1668 // FIXME: Validate file.
1669
1670 Lexer.Lex();
1671 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1672 if (Lexer.isNot(AsmToken::Integer))
1673 return TokError("unexpected token in '.loc' directive");
1674
1675 int64_t Param2 = Lexer.getTok().getIntVal();
1676 (void) Param2;
1677 Lexer.Lex();
1678
1679 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1680 if (Lexer.isNot(AsmToken::Integer))
1681 return TokError("unexpected token in '.loc' directive");
1682
1683 int64_t Param3 = Lexer.getTok().getIntVal();
1684 (void) Param3;
1685 Lexer.Lex();
1686
1687 // FIXME: Do something with the .loc.
1688 }
1689 }
1690
1691 if (Lexer.isNot(AsmToken::EndOfStatement))
1692 return TokError("unexpected token in '.file' directive");
1693
1694 return false;
1695}
1696