blob: 02b7c396a4b814623a95a6943e853ed4d49c76b1 [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 Dunbarfffff912009-10-16 01:34:54 +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
Chris Lattnerebb89b42009-09-27 21:16:52 +000036AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
37 const MCAsmInfo &_MAI)
38 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
39 SectionUniquingMap(0) {
40 // Debugging directives.
41 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
42 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
43 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
44}
45
46
47
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000048AsmParser::~AsmParser() {
49 // If we have the MachO uniquing map, free it.
50 delete (MachOUniqueMapTy*)SectionUniquingMap;
51}
52
53const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
54 const StringRef &Section,
55 unsigned TypeAndAttributes,
56 unsigned Reserved2,
57 SectionKind Kind) const {
58 // We unique sections by their segment/section pair. The returned section
59 // may not have the same flags as the requested section, if so this should be
60 // diagnosed by the client as an error.
61
62 // Create the map if it doesn't already exist.
63 if (SectionUniquingMap == 0)
64 SectionUniquingMap = new MachOUniqueMapTy();
65 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
66
67 // Form the name to look up.
68 SmallString<64> Name;
69 Name += Segment;
70 Name.push_back(',');
71 Name += Section;
72
73 // Do the lookup, if we have a hit, return it.
74 const MCSectionMachO *&Entry = Map[Name.str()];
75
76 // FIXME: This should validate the type and attributes.
77 if (Entry) return Entry;
78
79 // Otherwise, return a new section.
80 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
81 Reserved2, Kind, Ctx);
82}
83
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000084void AsmParser::Warning(SMLoc L, const Twine &Msg) {
85 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000086}
87
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000088bool AsmParser::Error(SMLoc L, const Twine &Msg) {
89 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000090 return true;
91}
92
93bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000094 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000095 return true;
96}
97
Chris Lattner27aa7d22009-06-21 20:16:42 +000098bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000099 // Create the initial section.
100 //
101 // FIXME: Support -n.
102 // FIXME: Target hook & command line option for initial section.
103 Out.SwitchSection(getMachOSection("__TEXT", "__text",
104 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
105 0, SectionKind()));
106
107
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000108 // Prime the lexer.
109 Lexer.Lex();
110
Chris Lattnerb717fb02009-07-02 21:53:43 +0000111 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000112
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000113 AsmCond StartingCondState = TheCondState;
114
Chris Lattnerb717fb02009-07-02 21:53:43 +0000115 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000116 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000117 // Handle conditional assembly here before calling ParseStatement()
118 if (Lexer.getKind() == AsmToken::Identifier) {
119 // If we have an identifier, handle it as the key symbol.
120 AsmToken ID = Lexer.getTok();
121 SMLoc IDLoc = ID.getLoc();
122 StringRef IDVal = ID.getString();
123
124 if (IDVal == ".if" ||
125 IDVal == ".elseif" ||
126 IDVal == ".else" ||
127 IDVal == ".endif") {
128 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
129 continue;
130 HadError = true;
131 EatToEndOfStatement();
132 continue;
133 }
134 }
135 if (TheCondState.Ignore) {
136 EatToEndOfStatement();
137 continue;
138 }
139
Chris Lattnerb717fb02009-07-02 21:53:43 +0000140 if (!ParseStatement()) continue;
141
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000142 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000143 HadError = true;
144 EatToEndOfStatement();
145 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000146
147 if (TheCondState.TheCond != StartingCondState.TheCond ||
148 TheCondState.Ignore != StartingCondState.Ignore)
149 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000150
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000151 if (!HadError)
152 Out.Finish();
153
Chris Lattnerb717fb02009-07-02 21:53:43 +0000154 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000155}
156
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000157/// ParseConditionalAssemblyDirectives - parse the conditional assembly
158/// directives
159bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
160 SMLoc DirectiveLoc) {
161 if (Directive == ".if")
162 return ParseDirectiveIf(DirectiveLoc);
163 if (Directive == ".elseif")
164 return ParseDirectiveElseIf(DirectiveLoc);
165 if (Directive == ".else")
166 return ParseDirectiveElse(DirectiveLoc);
167 if (Directive == ".endif")
168 return ParseDirectiveEndIf(DirectiveLoc);
169 return true;
170}
171
Chris Lattner2cf5f142009-06-22 01:29:09 +0000172/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
173void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000174 while (Lexer.isNot(AsmToken::EndOfStatement) &&
175 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000176 Lexer.Lex();
177
178 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000179 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000180 Lexer.Lex();
181}
182
Chris Lattnerc4193832009-06-22 05:51:26 +0000183
Chris Lattner74ec1a32009-06-22 06:32:03 +0000184/// ParseParenExpr - Parse a paren expression and return it.
185/// NOTE: This assumes the leading '(' has already been consumed.
186///
187/// parenexpr ::= expr)
188///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000189bool AsmParser::ParseParenExpr(const MCExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000190 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000191 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000192 return TokError("expected ')' in parentheses expression");
193 Lexer.Lex();
194 return false;
195}
Chris Lattnerc4193832009-06-22 05:51:26 +0000196
Daniel Dunbar959fd882009-08-26 22:13:22 +0000197MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
198 if (MCSymbol *S = Ctx.LookupSymbol(Name))
199 return S;
200
201 // If the label starts with L it is an assembler temporary label.
202 if (Name.startswith("L"))
203 return Ctx.CreateTemporarySymbol(Name);
204
205 return Ctx.CreateSymbol(Name);
206}
207
Chris Lattner74ec1a32009-06-22 06:32:03 +0000208/// ParsePrimaryExpr - Parse a primary expression and return it.
209/// primaryexpr ::= (parenexpr
210/// primaryexpr ::= symbol
211/// primaryexpr ::= number
212/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000213bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000214 switch (Lexer.getKind()) {
215 default:
216 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000218 Lexer.Lex(); // Eat the operator.
219 if (ParsePrimaryExpr(Res))
220 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000221 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000222 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000223 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000224 case AsmToken::Identifier: {
225 // This is a symbol reference.
226 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Chris Lattnerc4193832009-06-22 05:51:26 +0000227 Lexer.Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000228
229 // If this is an absolute variable reference, substitute it now to preserve
230 // semantics in the face of reassignment.
231 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
232 Res = Sym->getValue();
233 return false;
234 }
235
236 // Otherwise create a symbol ref.
237 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000238 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000239 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000240 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000241 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000242 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000243 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000244 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000245 Lexer.Lex(); // Eat the '('.
246 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000247 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000248 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000249 if (ParsePrimaryExpr(Res))
250 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000251 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000252 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000253 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000254 Lexer.Lex(); // Eat the operator.
255 if (ParsePrimaryExpr(Res))
256 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000257 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000258 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000259 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000260 Lexer.Lex(); // Eat the operator.
261 if (ParsePrimaryExpr(Res))
262 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000263 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000264 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000265 }
266}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000267
268/// ParseExpression - Parse an expression and return it.
269///
270/// expr ::= expr +,- expr -> lowest.
271/// expr ::= expr |,^,&,! expr -> middle.
272/// expr ::= expr *,/,%,<<,>> expr -> highest.
273/// expr ::= primaryexpr
274///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000275bool AsmParser::ParseExpression(const MCExpr *&Res) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000276 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000277 return ParsePrimaryExpr(Res) ||
278 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000279}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000280
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000281bool AsmParser::ParseParenExpression(const MCExpr *&Res) {
282 if (ParseParenExpr(Res))
283 return true;
284
285 return false;
286}
287
Daniel Dunbar475839e2009-06-29 20:37:27 +0000288bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000289 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000290
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000291 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000292 if (ParseExpression(Expr))
293 return true;
294
295 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000296 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000297
298 return false;
299}
300
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000302 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000303 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000304 default:
305 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000306
307 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000308 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000309 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000310 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000311 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000312 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000313 return 1;
314
315 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000316 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000317 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000318 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000319 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000320 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000321 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000322 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000323 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000324 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000325 case AsmToken::ExclaimEqual:
326 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000327 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000328 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000333 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000336 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000337 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000338 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000339 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return 2;
341
342 // Intermediate Precedence: |, &, ^
343 //
344 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000347 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000348 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000349 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000351 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000352 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000353 return 3;
354
355 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000356 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000357 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000359 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000360 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000361 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000363 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000364 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000366 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000368 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000369 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000370 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000371 }
372}
373
374
375/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
376/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000377bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000378 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000381
382 // If the next token is lower precedence than we are allowed to eat, return
383 // successfully with what we ate already.
384 if (TokPrec < Precedence)
385 return false;
386
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000387 Lexer.Lex();
388
389 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000390 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000391 if (ParsePrimaryExpr(RHS)) return true;
392
393 // If BinOp binds less tightly with RHS than the operator after RHS, let
394 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000395 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000396 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000397 if (TokPrec < NextTokPrec) {
398 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
399 }
400
Daniel Dunbar475839e2009-06-29 20:37:27 +0000401 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000402 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000403 }
404}
405
Chris Lattnerc4193832009-06-22 05:51:26 +0000406
407
408
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000409/// ParseStatement:
410/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000411/// ::= Label* Directive ...Operands... EndOfStatement
412/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000413bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000414 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000415 Lexer.Lex();
416 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000417 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000418
419 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000420 AsmToken ID = Lexer.getTok();
421 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000422 StringRef IDVal;
423 if (ParseIdentifier(IDVal))
424 return TokError("unexpected token at start of statement");
425
426 // FIXME: Recurse on local labels?
427
428 // See what kind of statement we have.
429 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000430 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000431 // identifier ':' -> Label.
432 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000433
434 // Diagnose attempt to use a variable as a label.
435 //
436 // FIXME: Diagnostics. Note the location of the definition as a label.
437 // FIXME: This doesn't diagnose assignment to a symbol which has been
438 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000439 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000440 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000441 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000442
Daniel Dunbar959fd882009-08-26 22:13:22 +0000443 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000444 Out.EmitLabel(Sym);
445
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000446 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000447 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000448
Daniel Dunbar3f872332009-07-28 16:08:33 +0000449 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000450 // identifier '=' ... -> assignment statement
451 Lexer.Lex();
452
Daniel Dunbare2ace502009-08-31 08:09:09 +0000453 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000454
455 default: // Normal instruction or directive.
456 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000457 }
458
459 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000460 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000461 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000462 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000463 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000464 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000465 // FIXME: This changes behavior based on the -static flag to the
466 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000467 return ParseDirectiveSectionSwitch("__TEXT", "__text",
468 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000470 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000471 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000472 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000473 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000474 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
475 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000477 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000478 MCSectionMachO::S_4BYTE_LITERALS,
479 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000481 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000482 MCSectionMachO::S_8BYTE_LITERALS,
483 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000485 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000486 MCSectionMachO::S_16BYTE_LITERALS,
487 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000488 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000489 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000491 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000494 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000495 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
496
497 // FIXME: The assembler manual claims that this has the self modify code
498 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000499 if (IDVal == ".symbol_stub")
500 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
501 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000502 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
503 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000504 0, 16);
505 // FIXME: PowerPC only?
506 if (IDVal == ".picsymbol_stub")
507 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
508 MCSectionMachO::S_SYMBOL_STUBS |
509 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
510 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000512 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000514 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
515
516 // FIXME: The section names of these two are misspelled in the assembler
517 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000519 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
520 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
521 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000523 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
524 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
525 4);
526
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000528 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000530 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000531 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
532 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000534 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
536 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000538 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000539
540
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__OBJC", "__class",
543 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
546 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
549 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
552 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000553 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000554 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
555 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
558 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000559 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000560 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
561 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000562 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000563 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
564 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000565 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000566 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
567 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
568 MCSectionMachO::S_LITERAL_POINTERS,
569 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000570 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000571 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
573 MCSectionMachO::S_LITERAL_POINTERS,
574 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
577 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000578 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000579 return ParseDirectiveSectionSwitch("__OBJC", "__category",
580 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000582 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
583 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
586 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000587 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000588 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
589 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000591 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
592 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000594 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
595 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000596 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000597 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
598 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000600 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
601 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000602
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000603 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000605 return ParseDirectiveSet();
606
Daniel Dunbara0d14262009-06-24 23:30:00 +0000607 // Data directives
608
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000610 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000612 return ParseDirectiveAscii(true);
613
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000615 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000616 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000617 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000619 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000621 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000622
623 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000625 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000627 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000629 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000631 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000633 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000635 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000637 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000639 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
640
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000642 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000643
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000645 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000647 return ParseDirectiveSpace();
648
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000649 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000650
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000652 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000654 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000656 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000658 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000660 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000662 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000664 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000666 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000668 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000670 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000672 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000674 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
675
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000677 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000679 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000681 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000683 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000685 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000686
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000688 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000690 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000692 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000694 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000696 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000697
Chris Lattnerebb89b42009-09-27 21:16:52 +0000698 // Look up the handler in the handler table,
699 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
700 if (Handler)
701 return (this->*Handler)(IDVal, IDLoc);
702
Kevin Enderby9c656452009-09-10 20:51:44 +0000703 // Target hook for parsing target specific directives.
704 if (!getTargetParser().ParseDirective(ID))
705 return false;
706
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000707 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000708 EatToEndOfStatement();
709 return false;
710 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000711
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000712 MCInst Inst;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000713 if (getTargetParser().ParseInstruction(IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000714 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000715
Daniel Dunbar3f872332009-07-28 16:08:33 +0000716 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000717 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000718
719 // Eat the end of statement marker.
720 Lexer.Lex();
721
722 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000723 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000724
725 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000726 return false;
727}
Chris Lattner9a023f72009-06-24 04:43:34 +0000728
Daniel Dunbare2ace502009-08-31 08:09:09 +0000729bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000730 // FIXME: Use better location, we should use proper tokens.
731 SMLoc EqualLoc = Lexer.getLoc();
732
Daniel Dunbar821e3332009-08-31 08:09:28 +0000733 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000734 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000735 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000736 return true;
737
Daniel Dunbar3f872332009-07-28 16:08:33 +0000738 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000739 return TokError("unexpected token in assignment");
740
741 // Eat the end of statement marker.
742 Lexer.Lex();
743
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000744 // Validate that the LHS is allowed to be a variable (either it has not been
745 // used as a symbol, or it is an absolute symbol).
746 MCSymbol *Sym = getContext().LookupSymbol(Name);
747 if (Sym) {
748 // Diagnose assignment to a label.
749 //
750 // FIXME: Diagnostics. Note the location of the definition as a label.
751 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
752 if (!Sym->isUndefined() && !Sym->isAbsolute())
753 return Error(EqualLoc, "redefinition of '" + Name + "'");
754 else if (!Sym->isVariable())
755 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
756 else if (!isa<MCConstantExpr>(Sym->getValue()))
757 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
758 Name + "'");
759 } else
760 Sym = CreateSymbol(Name);
761
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000762 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000763
764 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000765 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000766
767 return false;
768}
769
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000770/// ParseIdentifier:
771/// ::= identifier
772/// ::= string
773bool AsmParser::ParseIdentifier(StringRef &Res) {
774 if (Lexer.isNot(AsmToken::Identifier) &&
775 Lexer.isNot(AsmToken::String))
776 return true;
777
778 Res = Lexer.getTok().getIdentifier();
779
780 Lexer.Lex(); // Consume the identifier token.
781
782 return false;
783}
784
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000785/// ParseDirectiveSet:
786/// ::= .set identifier ',' expression
787bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000788 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000789
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000790 if (ParseIdentifier(Name))
791 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000792
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000793 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000794 return TokError("unexpected token in '.set'");
795 Lexer.Lex();
796
Daniel Dunbare2ace502009-08-31 08:09:09 +0000797 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000798}
799
Chris Lattner9a023f72009-06-24 04:43:34 +0000800/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000801/// ::= .section identifier (',' identifier)*
802/// FIXME: This should actually parse out the segment, section, attributes and
803/// sizeof_stub fields.
804bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000805 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000806
Daniel Dunbarace63122009-08-11 03:42:33 +0000807 StringRef SectionName;
808 if (ParseIdentifier(SectionName))
809 return Error(Loc, "expected identifier after '.section' directive");
810
811 // Verify there is a following comma.
812 if (!Lexer.is(AsmToken::Comma))
813 return TokError("unexpected token in '.section' directive");
814
Chris Lattnerff4bc462009-08-10 01:39:42 +0000815 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000816 SectionSpec += ",";
817
818 // Add all the tokens until the end of the line, ParseSectionSpecifier will
819 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000820 StringRef EOL = Lexer.LexUntilEndOfStatement();
821 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000822
Chris Lattnerff4bc462009-08-10 01:39:42 +0000823 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000824 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000825 return TokError("unexpected token in '.section' directive");
826 Lexer.Lex();
827
Chris Lattnerff4bc462009-08-10 01:39:42 +0000828
829 StringRef Segment, Section;
830 unsigned TAA, StubSize;
831 std::string ErrorStr =
832 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
833 TAA, StubSize);
834
835 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000836 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000837
Chris Lattner56594f92009-07-31 17:47:16 +0000838 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000839 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
840 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000841 return false;
842}
843
Chris Lattnere15c2d72009-08-10 18:05:55 +0000844/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000845bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
846 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000847 unsigned TAA, unsigned Align,
848 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000849 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000850 return TokError("unexpected token in section switching directive");
851 Lexer.Lex();
852
Chris Lattner56594f92009-07-31 17:47:16 +0000853 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000854 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
855 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000856
857 // Set the implicit alignment, if any.
858 //
859 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
860 // alignment on the section (e.g., if one manually inserts bytes into the
861 // section, then just issueing the section switch directive will not realign
862 // the section. However, this is arguably more reasonable behavior, and there
863 // is no good reason for someone to intentionally emit incorrectly sized
864 // values into the implicitly aligned sections.
865 if (Align)
866 Out.EmitValueToAlignment(Align, 0, 1, 0);
867
Chris Lattner529fb542009-06-24 05:13:15 +0000868 return false;
869}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000870
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000871bool AsmParser::ParseEscapedString(std::string &Data) {
872 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
873
874 Data = "";
875 StringRef Str = Lexer.getTok().getStringContents();
876 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
877 if (Str[i] != '\\') {
878 Data += Str[i];
879 continue;
880 }
881
882 // Recognize escaped characters. Note that this escape semantics currently
883 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
884 ++i;
885 if (i == e)
886 return TokError("unexpected backslash at end of string");
887
888 // Recognize octal sequences.
889 if ((unsigned) (Str[i] - '0') <= 7) {
890 // Consume up to three octal characters.
891 unsigned Value = 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 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
898 ++i;
899 Value = Value * 8 + (Str[i] - '0');
900 }
901 }
902
903 if (Value > 255)
904 return TokError("invalid octal escape sequence (out of range)");
905
906 Data += (unsigned char) Value;
907 continue;
908 }
909
910 // Otherwise recognize individual escapes.
911 switch (Str[i]) {
912 default:
913 // Just reject invalid escape sequences for now.
914 return TokError("invalid escape sequence (unrecognized character)");
915
916 case 'b': Data += '\b'; break;
917 case 'f': Data += '\f'; break;
918 case 'n': Data += '\n'; break;
919 case 'r': Data += '\r'; break;
920 case 't': Data += '\t'; break;
921 case '"': Data += '"'; break;
922 case '\\': Data += '\\'; break;
923 }
924 }
925
926 return false;
927}
928
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000930/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000931bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000932 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000933 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000934 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000935 return TokError("expected string in '.ascii' or '.asciz' directive");
936
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000937 std::string Data;
938 if (ParseEscapedString(Data))
939 return true;
940
941 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000942 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000943 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000944
945 Lexer.Lex();
946
Daniel Dunbar3f872332009-07-28 16:08:33 +0000947 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000948 break;
949
Daniel Dunbar3f872332009-07-28 16:08:33 +0000950 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000951 return TokError("unexpected token in '.ascii' or '.asciz' directive");
952 Lexer.Lex();
953 }
954 }
955
956 Lexer.Lex();
957 return false;
958}
959
960/// ParseDirectiveValue
961/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
962bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000965 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000966 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000967 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000968 return true;
969
Daniel Dunbar883f9202009-08-31 08:08:50 +0000970 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000971
Daniel Dunbar3f872332009-07-28 16:08:33 +0000972 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000973 break;
974
975 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000976 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977 return TokError("unexpected token in directive");
978 Lexer.Lex();
979 }
980 }
981
982 Lexer.Lex();
983 return false;
984}
985
986/// ParseDirectiveSpace
987/// ::= .space expression [ , expression ]
988bool AsmParser::ParseDirectiveSpace() {
989 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000990 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000991 return true;
992
993 int64_t FillExpr = 0;
994 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000995 if (Lexer.isNot(AsmToken::EndOfStatement)) {
996 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000997 return TokError("unexpected token in '.space' directive");
998 Lexer.Lex();
999
Daniel Dunbar475839e2009-06-29 20:37:27 +00001000 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001001 return true;
1002
1003 HasFillExpr = true;
1004
Daniel Dunbar3f872332009-07-28 16:08:33 +00001005 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001006 return TokError("unexpected token in '.space' directive");
1007 }
1008
1009 Lexer.Lex();
1010
1011 if (NumBytes <= 0)
1012 return TokError("invalid number of bytes in '.space' directive");
1013
1014 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1015 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001016 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001017
1018 return false;
1019}
1020
1021/// ParseDirectiveFill
1022/// ::= .fill expression , expression , expression
1023bool AsmParser::ParseDirectiveFill() {
1024 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001025 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 return true;
1027
Daniel Dunbar3f872332009-07-28 16:08:33 +00001028 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return TokError("unexpected token in '.fill' directive");
1030 Lexer.Lex();
1031
1032 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001033 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001034 return true;
1035
Daniel Dunbar3f872332009-07-28 16:08:33 +00001036 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return TokError("unexpected token in '.fill' directive");
1038 Lexer.Lex();
1039
1040 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001041 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001042 return true;
1043
Daniel Dunbar3f872332009-07-28 16:08:33 +00001044 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045 return TokError("unexpected token in '.fill' directive");
1046
1047 Lexer.Lex();
1048
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001049 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1050 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051
1052 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001053 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054
1055 return false;
1056}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001057
1058/// ParseDirectiveOrg
1059/// ::= .org expression [ , expression ]
1060bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001061 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001062 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001063 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001064 return true;
1065
1066 // Parse optional fill expression.
1067 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001068 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1069 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001070 return TokError("unexpected token in '.org' directive");
1071 Lexer.Lex();
1072
Daniel Dunbar475839e2009-06-29 20:37:27 +00001073 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001074 return true;
1075
Daniel Dunbar3f872332009-07-28 16:08:33 +00001076 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001077 return TokError("unexpected token in '.org' directive");
1078 }
1079
1080 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001081
1082 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1083 // has to be relative to the current section.
1084 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001085
1086 return false;
1087}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001088
1089/// ParseDirectiveAlign
1090/// ::= {.align, ...} expression [ , expression [ , expression ]]
1091bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001092 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001093 int64_t Alignment;
1094 if (ParseAbsoluteExpression(Alignment))
1095 return true;
1096
1097 SMLoc MaxBytesLoc;
1098 bool HasFillExpr = false;
1099 int64_t FillExpr = 0;
1100 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001101 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1102 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001103 return TokError("unexpected token in directive");
1104 Lexer.Lex();
1105
1106 // The fill expression can be omitted while specifying a maximum number of
1107 // alignment bytes, e.g:
1108 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001109 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001110 HasFillExpr = true;
1111 if (ParseAbsoluteExpression(FillExpr))
1112 return true;
1113 }
1114
Daniel Dunbar3f872332009-07-28 16:08:33 +00001115 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1116 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001117 return TokError("unexpected token in directive");
1118 Lexer.Lex();
1119
1120 MaxBytesLoc = Lexer.getLoc();
1121 if (ParseAbsoluteExpression(MaxBytesToFill))
1122 return true;
1123
Daniel Dunbar3f872332009-07-28 16:08:33 +00001124 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001125 return TokError("unexpected token in directive");
1126 }
1127 }
1128
1129 Lexer.Lex();
1130
1131 if (!HasFillExpr) {
1132 // FIXME: Sometimes fill with nop.
1133 FillExpr = 0;
1134 }
1135
1136 // Compute alignment in bytes.
1137 if (IsPow2) {
1138 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001139 if (Alignment >= 32) {
1140 Error(AlignmentLoc, "invalid alignment value");
1141 Alignment = 31;
1142 }
1143
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001144 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001145 }
1146
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001147 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001148 if (MaxBytesLoc.isValid()) {
1149 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001150 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1151 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001152 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001153 }
1154
1155 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001156 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1157 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001158 MaxBytesToFill = 0;
1159 }
1160 }
1161
1162 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1163 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1164
1165 return false;
1166}
1167
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001168/// ParseDirectiveSymbolAttribute
1169/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1170bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001171 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001172 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001173 StringRef Name;
1174
1175 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001176 return TokError("expected identifier in directive");
1177
Daniel Dunbar959fd882009-08-26 22:13:22 +00001178 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001179
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001180 Out.EmitSymbolAttribute(Sym, Attr);
1181
Daniel Dunbar3f872332009-07-28 16:08:33 +00001182 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001183 break;
1184
Daniel Dunbar3f872332009-07-28 16:08:33 +00001185 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001186 return TokError("unexpected token in directive");
1187 Lexer.Lex();
1188 }
1189 }
1190
1191 Lexer.Lex();
1192 return false;
1193}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001194
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001195/// ParseDirectiveDarwinSymbolDesc
1196/// ::= .desc identifier , expression
1197bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001198 StringRef Name;
1199 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001200 return TokError("expected identifier in directive");
1201
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001202 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001203 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001204
Daniel Dunbar3f872332009-07-28 16:08:33 +00001205 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001206 return TokError("unexpected token in '.desc' directive");
1207 Lexer.Lex();
1208
1209 SMLoc DescLoc = Lexer.getLoc();
1210 int64_t DescValue;
1211 if (ParseAbsoluteExpression(DescValue))
1212 return true;
1213
Daniel Dunbar3f872332009-07-28 16:08:33 +00001214 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001215 return TokError("unexpected token in '.desc' directive");
1216
1217 Lexer.Lex();
1218
1219 // Set the n_desc field of this Symbol to this DescValue
1220 Out.EmitSymbolDesc(Sym, DescValue);
1221
1222 return false;
1223}
1224
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001225/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001226/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1227bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001228 SMLoc IDLoc = Lexer.getLoc();
1229 StringRef Name;
1230 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001231 return TokError("expected identifier in directive");
1232
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001233 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001234 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001235
Daniel Dunbar3f872332009-07-28 16:08:33 +00001236 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001237 return TokError("unexpected token in directive");
1238 Lexer.Lex();
1239
1240 int64_t Size;
1241 SMLoc SizeLoc = Lexer.getLoc();
1242 if (ParseAbsoluteExpression(Size))
1243 return true;
1244
1245 int64_t Pow2Alignment = 0;
1246 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001247 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001248 Lexer.Lex();
1249 Pow2AlignmentLoc = Lexer.getLoc();
1250 if (ParseAbsoluteExpression(Pow2Alignment))
1251 return true;
1252 }
1253
Daniel Dunbar3f872332009-07-28 16:08:33 +00001254 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001255 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001256
1257 Lexer.Lex();
1258
Chris Lattner1fc3d752009-07-09 17:25:12 +00001259 // NOTE: a size of zero for a .comm should create a undefined symbol
1260 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001261 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001262 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1263 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001264
1265 // NOTE: The alignment in the directive is a power of 2 value, the assember
1266 // may internally end up wanting an alignment in bytes.
1267 // FIXME: Diagnose overflow.
1268 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001269 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1270 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001271
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001272 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001273 return Error(IDLoc, "invalid symbol redefinition");
1274
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001275 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001276 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001277 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001278 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1279 MCSectionMachO::S_ZEROFILL, 0,
1280 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001281 Sym, Size, 1 << Pow2Alignment);
1282 return false;
1283 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001284
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001285 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001286 return false;
1287}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001288
1289/// ParseDirectiveDarwinZerofill
1290/// ::= .zerofill segname , sectname [, identifier , size_expression [
1291/// , align_expression ]]
1292bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001293 // FIXME: Handle quoted names here.
1294
Daniel Dunbar3f872332009-07-28 16:08:33 +00001295 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001296 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001297 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001298 Lexer.Lex();
1299
Daniel Dunbar3f872332009-07-28 16:08:33 +00001300 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001301 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001302 Lexer.Lex();
1303
Daniel Dunbar3f872332009-07-28 16:08:33 +00001304 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001305 return TokError("expected section name after comma in '.zerofill' "
1306 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001307 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001308 Lexer.Lex();
1309
Chris Lattner9be3fee2009-07-10 22:20:30 +00001310 // If this is the end of the line all that was wanted was to create the
1311 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001312 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001313 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001314 Out.EmitZerofill(getMachOSection(Segment, Section,
1315 MCSectionMachO::S_ZEROFILL, 0,
1316 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001317 return false;
1318 }
1319
Daniel Dunbar3f872332009-07-28 16:08:33 +00001320 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001321 return TokError("unexpected token in directive");
1322 Lexer.Lex();
1323
Daniel Dunbar3f872332009-07-28 16:08:33 +00001324 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001325 return TokError("expected identifier in directive");
1326
1327 // handle the identifier as the key symbol.
1328 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001329 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001330 Lexer.Lex();
1331
Daniel Dunbar3f872332009-07-28 16:08:33 +00001332 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001333 return TokError("unexpected token in directive");
1334 Lexer.Lex();
1335
1336 int64_t Size;
1337 SMLoc SizeLoc = Lexer.getLoc();
1338 if (ParseAbsoluteExpression(Size))
1339 return true;
1340
1341 int64_t Pow2Alignment = 0;
1342 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001343 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001344 Lexer.Lex();
1345 Pow2AlignmentLoc = Lexer.getLoc();
1346 if (ParseAbsoluteExpression(Pow2Alignment))
1347 return true;
1348 }
1349
Daniel Dunbar3f872332009-07-28 16:08:33 +00001350 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001351 return TokError("unexpected token in '.zerofill' directive");
1352
1353 Lexer.Lex();
1354
1355 if (Size < 0)
1356 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1357 "than zero");
1358
1359 // NOTE: The alignment in the directive is a power of 2 value, the assember
1360 // may internally end up wanting an alignment in bytes.
1361 // FIXME: Diagnose overflow.
1362 if (Pow2Alignment < 0)
1363 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1364 "can't be less than zero");
1365
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001366 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001367 return Error(IDLoc, "invalid symbol redefinition");
1368
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001369 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001370 //
1371 // FIXME: Arch specific.
1372 Out.EmitZerofill(getMachOSection(Segment, Section,
1373 MCSectionMachO::S_ZEROFILL, 0,
1374 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001375 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001376
1377 return false;
1378}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001379
1380/// ParseDirectiveDarwinSubsectionsViaSymbols
1381/// ::= .subsections_via_symbols
1382bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001383 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001384 return TokError("unexpected token in '.subsections_via_symbols' directive");
1385
1386 Lexer.Lex();
1387
Kevin Enderbyf96db462009-07-16 17:56:39 +00001388 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001389
1390 return false;
1391}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001392
1393/// ParseDirectiveAbort
1394/// ::= .abort [ "abort_string" ]
1395bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001396 // FIXME: Use loc from directive.
1397 SMLoc Loc = Lexer.getLoc();
1398
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001399 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001400 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1401 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001402 return TokError("expected string in '.abort' directive");
1403
Daniel Dunbar419aded2009-07-28 16:38:40 +00001404 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001405
1406 Lexer.Lex();
1407 }
1408
Daniel Dunbar3f872332009-07-28 16:08:33 +00001409 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001410 return TokError("unexpected token in '.abort' directive");
1411
1412 Lexer.Lex();
1413
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001414 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001415 if (Str.empty())
1416 Error(Loc, ".abort detected. Assembly stopping.");
1417 else
1418 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001419
1420 return false;
1421}
Kevin Enderby71148242009-07-14 21:35:03 +00001422
1423/// ParseDirectiveLsym
1424/// ::= .lsym identifier , expression
1425bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001426 StringRef Name;
1427 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001428 return TokError("expected identifier in directive");
1429
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001430 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001431 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001432
Daniel Dunbar3f872332009-07-28 16:08:33 +00001433 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001434 return TokError("unexpected token in '.lsym' directive");
1435 Lexer.Lex();
1436
Daniel Dunbar821e3332009-08-31 08:09:28 +00001437 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001438 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001439 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001440 return true;
1441
Daniel Dunbar3f872332009-07-28 16:08:33 +00001442 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001443 return TokError("unexpected token in '.lsym' directive");
1444
1445 Lexer.Lex();
1446
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001447 // We don't currently support this directive.
1448 //
1449 // FIXME: Diagnostic location!
1450 (void) Sym;
1451 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001452}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001453
1454/// ParseDirectiveInclude
1455/// ::= .include "filename"
1456bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001457 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001458 return TokError("expected string in '.include' directive");
1459
Daniel Dunbar419aded2009-07-28 16:38:40 +00001460 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001461 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001462 Lexer.Lex();
1463
Daniel Dunbar3f872332009-07-28 16:08:33 +00001464 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001465 return TokError("unexpected token in '.include' directive");
1466
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001467 // Strip the quotes.
1468 Filename = Filename.substr(1, Filename.size()-2);
1469
1470 // Attempt to switch the lexer to the included file before consuming the end
1471 // of statement to avoid losing it when we switch.
1472 if (Lexer.EnterIncludeFile(Filename)) {
1473 Lexer.PrintMessage(IncludeLoc,
1474 "Could not find include file '" + Filename + "'",
1475 "error");
1476 return true;
1477 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001478
1479 return false;
1480}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001481
1482/// ParseDirectiveDarwinDumpOrLoad
1483/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001484bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001485 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001486 return TokError("expected string in '.dump' or '.load' directive");
1487
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001488 Lexer.Lex();
1489
Daniel Dunbar3f872332009-07-28 16:08:33 +00001490 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001491 return TokError("unexpected token in '.dump' or '.load' directive");
1492
1493 Lexer.Lex();
1494
Kevin Enderby5026ae42009-07-20 20:25:37 +00001495 // FIXME: If/when .dump and .load are implemented they will be done in the
1496 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001497 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001498 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001499 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001500 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001501
1502 return false;
1503}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001504
1505/// ParseDirectiveIf
1506/// ::= .if expression
1507bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1508 // Consume the identifier that was the .if directive
1509 Lexer.Lex();
1510
1511 TheCondStack.push_back(TheCondState);
1512 TheCondState.TheCond = AsmCond::IfCond;
1513 if(TheCondState.Ignore) {
1514 EatToEndOfStatement();
1515 }
1516 else {
1517 int64_t ExprValue;
1518 if (ParseAbsoluteExpression(ExprValue))
1519 return true;
1520
1521 if (Lexer.isNot(AsmToken::EndOfStatement))
1522 return TokError("unexpected token in '.if' directive");
1523
1524 Lexer.Lex();
1525
1526 TheCondState.CondMet = ExprValue;
1527 TheCondState.Ignore = !TheCondState.CondMet;
1528 }
1529
1530 return false;
1531}
1532
1533/// ParseDirectiveElseIf
1534/// ::= .elseif expression
1535bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1536 if (TheCondState.TheCond != AsmCond::IfCond &&
1537 TheCondState.TheCond != AsmCond::ElseIfCond)
1538 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1539 " an .elseif");
1540 TheCondState.TheCond = AsmCond::ElseIfCond;
1541
1542 // Consume the identifier that was the .elseif directive
1543 Lexer.Lex();
1544
1545 bool LastIgnoreState = false;
1546 if (!TheCondStack.empty())
1547 LastIgnoreState = TheCondStack.back().Ignore;
1548 if (LastIgnoreState || TheCondState.CondMet) {
1549 TheCondState.Ignore = true;
1550 EatToEndOfStatement();
1551 }
1552 else {
1553 int64_t ExprValue;
1554 if (ParseAbsoluteExpression(ExprValue))
1555 return true;
1556
1557 if (Lexer.isNot(AsmToken::EndOfStatement))
1558 return TokError("unexpected token in '.elseif' directive");
1559
1560 Lexer.Lex();
1561 TheCondState.CondMet = ExprValue;
1562 TheCondState.Ignore = !TheCondState.CondMet;
1563 }
1564
1565 return false;
1566}
1567
1568/// ParseDirectiveElse
1569/// ::= .else
1570bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1571 // Consume the identifier that was the .else directive
1572 Lexer.Lex();
1573
1574 if (Lexer.isNot(AsmToken::EndOfStatement))
1575 return TokError("unexpected token in '.else' directive");
1576
1577 Lexer.Lex();
1578
1579 if (TheCondState.TheCond != AsmCond::IfCond &&
1580 TheCondState.TheCond != AsmCond::ElseIfCond)
1581 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1582 ".elseif");
1583 TheCondState.TheCond = AsmCond::ElseCond;
1584 bool LastIgnoreState = false;
1585 if (!TheCondStack.empty())
1586 LastIgnoreState = TheCondStack.back().Ignore;
1587 if (LastIgnoreState || TheCondState.CondMet)
1588 TheCondState.Ignore = true;
1589 else
1590 TheCondState.Ignore = false;
1591
1592 return false;
1593}
1594
1595/// ParseDirectiveEndIf
1596/// ::= .endif
1597bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1598 // Consume the identifier that was the .endif directive
1599 Lexer.Lex();
1600
1601 if (Lexer.isNot(AsmToken::EndOfStatement))
1602 return TokError("unexpected token in '.endif' directive");
1603
1604 Lexer.Lex();
1605
1606 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1607 TheCondStack.empty())
1608 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1609 ".else");
1610 if (!TheCondStack.empty()) {
1611 TheCondState = TheCondStack.back();
1612 TheCondStack.pop_back();
1613 }
1614
1615 return false;
1616}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001617
1618/// ParseDirectiveFile
1619/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001620bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001621 // FIXME: I'm not sure what this is.
1622 int64_t FileNumber = -1;
1623 if (Lexer.is(AsmToken::Integer)) {
1624 FileNumber = Lexer.getTok().getIntVal();
1625 Lexer.Lex();
1626
1627 if (FileNumber < 1)
1628 return TokError("file number less than one");
1629 }
1630
1631 if (Lexer.isNot(AsmToken::String))
1632 return TokError("unexpected token in '.file' directive");
1633
1634 StringRef FileName = Lexer.getTok().getString();
1635 Lexer.Lex();
1636
1637 if (Lexer.isNot(AsmToken::EndOfStatement))
1638 return TokError("unexpected token in '.file' directive");
1639
1640 // FIXME: Do something with the .file.
1641
1642 return false;
1643}
1644
1645/// ParseDirectiveLine
1646/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001647bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001648 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1649 if (Lexer.isNot(AsmToken::Integer))
1650 return TokError("unexpected token in '.line' directive");
1651
1652 int64_t LineNumber = Lexer.getTok().getIntVal();
1653 (void) LineNumber;
1654 Lexer.Lex();
1655
1656 // FIXME: Do something with the .line.
1657 }
1658
1659 if (Lexer.isNot(AsmToken::EndOfStatement))
1660 return TokError("unexpected token in '.file' directive");
1661
1662 return false;
1663}
1664
1665
1666/// ParseDirectiveLoc
1667/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001668bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001669 if (Lexer.isNot(AsmToken::Integer))
1670 return TokError("unexpected token in '.loc' directive");
1671
1672 // FIXME: What are these fields?
1673 int64_t FileNumber = Lexer.getTok().getIntVal();
1674 (void) FileNumber;
1675 // FIXME: Validate file.
1676
1677 Lexer.Lex();
1678 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1679 if (Lexer.isNot(AsmToken::Integer))
1680 return TokError("unexpected token in '.loc' directive");
1681
1682 int64_t Param2 = Lexer.getTok().getIntVal();
1683 (void) Param2;
1684 Lexer.Lex();
1685
1686 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1687 if (Lexer.isNot(AsmToken::Integer))
1688 return TokError("unexpected token in '.loc' directive");
1689
1690 int64_t Param3 = Lexer.getTok().getIntVal();
1691 (void) Param3;
1692 Lexer.Lex();
1693
1694 // FIXME: Do something with the .loc.
1695 }
1696 }
1697
1698 if (Lexer.isNot(AsmToken::EndOfStatement))
1699 return TokError("unexpected token in '.file' directive");
1700
1701 return false;
1702}
1703