blob: 4e03646fec65d45464ee4d4db52080649d1f680c [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 Lattner98986712010-01-14 22:21:20 +000021#include "llvm/MC/MCParsedAsmOperand.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000022#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000024#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfffff912009-10-16 01:34:54 +000025#include "llvm/MC/MCValue.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000027#include "llvm/Support/SourceMgr.h"
28#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000032// Mach-O section uniquing.
33//
34// FIXME: Figure out where this should live, it should be shared by
35// TargetLoweringObjectFile.
36typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
37
Chris Lattnerebb89b42009-09-27 21:16:52 +000038AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
39 const MCAsmInfo &_MAI)
40 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
41 SectionUniquingMap(0) {
42 // Debugging directives.
43 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
44 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
45 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
46}
47
48
49
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000050AsmParser::~AsmParser() {
51 // If we have the MachO uniquing map, free it.
52 delete (MachOUniqueMapTy*)SectionUniquingMap;
53}
54
55const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
56 const StringRef &Section,
57 unsigned TypeAndAttributes,
58 unsigned Reserved2,
59 SectionKind Kind) const {
60 // We unique sections by their segment/section pair. The returned section
61 // may not have the same flags as the requested section, if so this should be
62 // diagnosed by the client as an error.
63
64 // Create the map if it doesn't already exist.
65 if (SectionUniquingMap == 0)
66 SectionUniquingMap = new MachOUniqueMapTy();
67 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
68
69 // Form the name to look up.
70 SmallString<64> Name;
71 Name += Segment;
72 Name.push_back(',');
73 Name += Section;
74
75 // Do the lookup, if we have a hit, return it.
76 const MCSectionMachO *&Entry = Map[Name.str()];
77
78 // FIXME: This should validate the type and attributes.
79 if (Entry) return Entry;
80
81 // Otherwise, return a new section.
82 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
83 Reserved2, Kind, Ctx);
84}
85
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000086void AsmParser::Warning(SMLoc L, const Twine &Msg) {
87 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000088}
89
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000090bool AsmParser::Error(SMLoc L, const Twine &Msg) {
91 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000092 return true;
93}
94
95bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000096 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000097 return true;
98}
99
Chris Lattner27aa7d22009-06-21 20:16:42 +0000100bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000101 // Create the initial section.
102 //
103 // FIXME: Support -n.
104 // FIXME: Target hook & command line option for initial section.
105 Out.SwitchSection(getMachOSection("__TEXT", "__text",
106 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
107 0, SectionKind()));
108
109
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000110 // Prime the lexer.
111 Lexer.Lex();
112
Chris Lattnerb717fb02009-07-02 21:53:43 +0000113 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000114
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000115 AsmCond StartingCondState = TheCondState;
116
Chris Lattnerb717fb02009-07-02 21:53:43 +0000117 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000118 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000119 // Handle conditional assembly here before calling ParseStatement()
120 if (Lexer.getKind() == AsmToken::Identifier) {
121 // If we have an identifier, handle it as the key symbol.
122 AsmToken ID = Lexer.getTok();
123 SMLoc IDLoc = ID.getLoc();
124 StringRef IDVal = ID.getString();
125
126 if (IDVal == ".if" ||
127 IDVal == ".elseif" ||
128 IDVal == ".else" ||
129 IDVal == ".endif") {
130 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
131 continue;
132 HadError = true;
133 EatToEndOfStatement();
134 continue;
135 }
136 }
137 if (TheCondState.Ignore) {
138 EatToEndOfStatement();
139 continue;
140 }
141
Chris Lattnerb717fb02009-07-02 21:53:43 +0000142 if (!ParseStatement()) continue;
143
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000144 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000145 HadError = true;
146 EatToEndOfStatement();
147 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000148
149 if (TheCondState.TheCond != StartingCondState.TheCond ||
150 TheCondState.Ignore != StartingCondState.Ignore)
151 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000152
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000153 if (!HadError)
154 Out.Finish();
155
Chris Lattnerb717fb02009-07-02 21:53:43 +0000156 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000157}
158
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000159/// ParseConditionalAssemblyDirectives - parse the conditional assembly
160/// directives
161bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
162 SMLoc DirectiveLoc) {
163 if (Directive == ".if")
164 return ParseDirectiveIf(DirectiveLoc);
165 if (Directive == ".elseif")
166 return ParseDirectiveElseIf(DirectiveLoc);
167 if (Directive == ".else")
168 return ParseDirectiveElse(DirectiveLoc);
169 if (Directive == ".endif")
170 return ParseDirectiveEndIf(DirectiveLoc);
171 return true;
172}
173
Chris Lattner2cf5f142009-06-22 01:29:09 +0000174/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
175void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000176 while (Lexer.isNot(AsmToken::EndOfStatement) &&
177 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000178 Lexer.Lex();
179
180 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000181 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000182 Lexer.Lex();
183}
184
Chris Lattnerc4193832009-06-22 05:51:26 +0000185
Chris Lattner74ec1a32009-06-22 06:32:03 +0000186/// ParseParenExpr - Parse a paren expression and return it.
187/// NOTE: This assumes the leading '(' has already been consumed.
188///
189/// parenexpr ::= expr)
190///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000191bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000192 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000193 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000194 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000195 EndLoc = Lexer.getLoc();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000196 Lexer.Lex();
197 return false;
198}
Chris Lattnerc4193832009-06-22 05:51:26 +0000199
Daniel Dunbar959fd882009-08-26 22:13:22 +0000200MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
201 if (MCSymbol *S = Ctx.LookupSymbol(Name))
202 return S;
203
204 // If the label starts with L it is an assembler temporary label.
205 if (Name.startswith("L"))
206 return Ctx.CreateTemporarySymbol(Name);
207
208 return Ctx.CreateSymbol(Name);
209}
210
Chris Lattner74ec1a32009-06-22 06:32:03 +0000211/// ParsePrimaryExpr - Parse a primary expression and return it.
212/// primaryexpr ::= (parenexpr
213/// primaryexpr ::= symbol
214/// primaryexpr ::= number
215/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000216bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000217 switch (Lexer.getKind()) {
218 default:
219 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000220 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000221 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000222 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000223 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000224 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000225 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000226 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000227 case AsmToken::Identifier: {
228 // This is a symbol reference.
229 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000230 EndLoc = Lexer.getLoc();
Chris Lattnerc4193832009-06-22 05:51:26 +0000231 Lexer.Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000232
233 // If this is an absolute variable reference, substitute it now to preserve
234 // semantics in the face of reassignment.
235 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
236 Res = Sym->getValue();
237 return false;
238 }
239
240 // Otherwise create a symbol ref.
241 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000242 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000243 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000244 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000245 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000246 EndLoc = Lexer.getLoc();
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000247 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000248 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000249 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000250 Lexer.Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000251 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000252 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000253 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000256 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000257 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000260 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000262 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000264 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 Lexer.Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000266 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000267 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000268 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000270 }
271}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000272
Chris Lattnerb4307b32010-01-15 19:28:38 +0000273bool AsmParser::ParseExpression(const MCExpr *&Res) {
274 SMLoc L;
275 return ParseExpression(Res, L, L);
276}
277
Chris Lattner74ec1a32009-06-22 06:32:03 +0000278/// ParseExpression - Parse an expression and return it.
279///
280/// expr ::= expr +,- expr -> lowest.
281/// expr ::= expr |,^,&,! expr -> middle.
282/// expr ::= expr *,/,%,<<,>> expr -> highest.
283/// expr ::= primaryexpr
284///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000285bool AsmParser::ParseExpression(const MCExpr *&Res,
286 SMLoc &StartLoc, SMLoc &EndLoc) {
287 StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000288 Res = 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000289 return ParsePrimaryExpr(Res, EndLoc) ||
290 ParseBinOpRHS(1, Res, EndLoc);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000291}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000292
Chris Lattnerb4307b32010-01-15 19:28:38 +0000293bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
294 if (ParseParenExpr(Res, EndLoc))
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000295 return true;
296
297 return false;
298}
299
Daniel Dunbar475839e2009-06-29 20:37:27 +0000300bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000301 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000302
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000303 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304 if (ParseExpression(Expr))
305 return true;
306
Daniel Dunbare00b0112009-10-16 01:57:52 +0000307 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000308 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309
310 return false;
311}
312
Daniel Dunbar3f872332009-07-28 16:08:33 +0000313static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000314 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000315 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000316 default:
317 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000318
319 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000321 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000322 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000324 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325 return 1;
326
327 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000328 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000329 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000330 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000331 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000332 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000334 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000335 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000336 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000337 case AsmToken::ExclaimEqual:
338 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000339 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000341 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000342 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000343 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000344 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000345 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000346 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000347 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000348 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000349 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000350 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000351 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 return 2;
353
354 // Intermediate Precedence: |, &, ^
355 //
356 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000357 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000358 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000359 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000361 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000362 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 3;
366
367 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000368 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000369 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000370 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000372 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000373 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000374 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000375 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000377 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000378 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000379 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000383 }
384}
385
386
387/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
388/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000389bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
390 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000391 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000392 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000393 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000394
395 // If the next token is lower precedence than we are allowed to eat, return
396 // successfully with what we ate already.
397 if (TokPrec < Precedence)
398 return false;
399
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000400 Lexer.Lex();
401
402 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000403 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000404 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000405
406 // If BinOp binds less tightly with RHS than the operator after RHS, let
407 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000408 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000409 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000410 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000411 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000412 }
413
Daniel Dunbar475839e2009-06-29 20:37:27 +0000414 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000415 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000416 }
417}
418
Chris Lattnerc4193832009-06-22 05:51:26 +0000419
420
421
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000422/// ParseStatement:
423/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000424/// ::= Label* Directive ...Operands... EndOfStatement
425/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000426bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000427 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000428 Lexer.Lex();
429 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000430 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000431
432 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000433 AsmToken ID = Lexer.getTok();
434 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000435 StringRef IDVal;
436 if (ParseIdentifier(IDVal))
437 return TokError("unexpected token at start of statement");
438
439 // FIXME: Recurse on local labels?
440
441 // See what kind of statement we have.
442 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000443 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000444 // identifier ':' -> Label.
445 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000446
447 // Diagnose attempt to use a variable as a label.
448 //
449 // FIXME: Diagnostics. Note the location of the definition as a label.
450 // FIXME: This doesn't diagnose assignment to a symbol which has been
451 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000452 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000453 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000454 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000455
Daniel Dunbar959fd882009-08-26 22:13:22 +0000456 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000457 Out.EmitLabel(Sym);
458
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000459 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000460 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000461
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000463 // identifier '=' ... -> assignment statement
464 Lexer.Lex();
465
Daniel Dunbare2ace502009-08-31 08:09:09 +0000466 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000467
468 default: // Normal instruction or directive.
469 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000470 }
471
472 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000473 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000474 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000475 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000476 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000477 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000478 // FIXME: This changes behavior based on the -static flag to the
479 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000480 return ParseDirectiveSectionSwitch("__TEXT", "__text",
481 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000482 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000483 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000484 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000485 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000486 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000487 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
488 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000489 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000490 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000491 MCSectionMachO::S_4BYTE_LITERALS,
492 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000493 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000494 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000495 MCSectionMachO::S_8BYTE_LITERALS,
496 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000497 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000498 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000499 MCSectionMachO::S_16BYTE_LITERALS,
500 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000502 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000504 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000505 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000506 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000508 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
509
510 // FIXME: The assembler manual claims that this has the self modify code
511 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000512 if (IDVal == ".symbol_stub")
513 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
514 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000515 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
516 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000517 0, 16);
518 // FIXME: PowerPC only?
519 if (IDVal == ".picsymbol_stub")
520 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
521 MCSectionMachO::S_SYMBOL_STUBS |
522 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
523 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000525 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000527 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
528
529 // FIXME: The section names of these two are misspelled in the assembler
530 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000532 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
533 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
534 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000536 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
537 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
538 4);
539
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000541 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000543 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
545 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000547 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
549 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000552
553
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__OBJC", "__class",
556 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
559 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
562 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000564 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
565 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
568 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
571 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000573 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
574 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
577 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000578 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000579 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
580 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
581 MCSectionMachO::S_LITERAL_POINTERS,
582 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000584 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
585 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
586 MCSectionMachO::S_LITERAL_POINTERS,
587 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
590 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__OBJC", "__category",
593 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
596 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000598 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
599 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
602 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_class_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_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000607 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
608 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000610 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
611 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
614 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000615
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000616 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000618 return ParseDirectiveSet();
619
Daniel Dunbara0d14262009-06-24 23:30:00 +0000620 // Data directives
621
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000623 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000625 return ParseDirectiveAscii(true);
626
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000628 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000629 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000630 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000632 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000634 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000635
636 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000638 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000640 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000642 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000644 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000646 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000648 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000650 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000652 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
653
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000655 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000656
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000658 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000660 return ParseDirectiveSpace();
661
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000662 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000663
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000665 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000667 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000669 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000671 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000673 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000675 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000677 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000679 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000681 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000683 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000685 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000687 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
688
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000690 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000692 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000694 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000696 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000698 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000699
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000701 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000703 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000705 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000707 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000709 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000710
Chris Lattnerebb89b42009-09-27 21:16:52 +0000711 // Look up the handler in the handler table,
712 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
713 if (Handler)
714 return (this->*Handler)(IDVal, IDLoc);
715
Kevin Enderby9c656452009-09-10 20:51:44 +0000716 // Target hook for parsing target specific directives.
717 if (!getTargetParser().ParseDirective(ID))
718 return false;
719
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000720 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000721 EatToEndOfStatement();
722 return false;
723 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000724
Chris Lattner98986712010-01-14 22:21:20 +0000725
726 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
727 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
728 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000729 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000730
Daniel Dunbar3f872332009-07-28 16:08:33 +0000731 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000732 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000733 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000734
735 // Eat the end of statement marker.
736 Lexer.Lex();
737
Chris Lattner98986712010-01-14 22:21:20 +0000738
739 MCInst Inst;
740
741 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
742
743 // Free any parsed operands.
744 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
745 delete ParsedOperands[i];
746
747 if (MatchFail) {
748 // FIXME: We should give nicer diagnostics about the exact failure.
749 Error(IDLoc, "unrecognized instruction");
750 return true;
751 }
752
Chris Lattner2cf5f142009-06-22 01:29:09 +0000753 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000754 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000755
756 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000757 return false;
758}
Chris Lattner9a023f72009-06-24 04:43:34 +0000759
Daniel Dunbare2ace502009-08-31 08:09:09 +0000760bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000761 // FIXME: Use better location, we should use proper tokens.
762 SMLoc EqualLoc = Lexer.getLoc();
763
Daniel Dunbar821e3332009-08-31 08:09:28 +0000764 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000765 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000766 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000767 return true;
768
Daniel Dunbar3f872332009-07-28 16:08:33 +0000769 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000770 return TokError("unexpected token in assignment");
771
772 // Eat the end of statement marker.
773 Lexer.Lex();
774
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000775 // Validate that the LHS is allowed to be a variable (either it has not been
776 // used as a symbol, or it is an absolute symbol).
777 MCSymbol *Sym = getContext().LookupSymbol(Name);
778 if (Sym) {
779 // Diagnose assignment to a label.
780 //
781 // FIXME: Diagnostics. Note the location of the definition as a label.
782 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
783 if (!Sym->isUndefined() && !Sym->isAbsolute())
784 return Error(EqualLoc, "redefinition of '" + Name + "'");
785 else if (!Sym->isVariable())
786 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
787 else if (!isa<MCConstantExpr>(Sym->getValue()))
788 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
789 Name + "'");
790 } else
791 Sym = CreateSymbol(Name);
792
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000793 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000794
795 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000796 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000797
798 return false;
799}
800
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000801/// ParseIdentifier:
802/// ::= identifier
803/// ::= string
804bool AsmParser::ParseIdentifier(StringRef &Res) {
805 if (Lexer.isNot(AsmToken::Identifier) &&
806 Lexer.isNot(AsmToken::String))
807 return true;
808
809 Res = Lexer.getTok().getIdentifier();
810
811 Lexer.Lex(); // Consume the identifier token.
812
813 return false;
814}
815
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000816/// ParseDirectiveSet:
817/// ::= .set identifier ',' expression
818bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000819 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000820
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000821 if (ParseIdentifier(Name))
822 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000823
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000824 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000825 return TokError("unexpected token in '.set'");
826 Lexer.Lex();
827
Daniel Dunbare2ace502009-08-31 08:09:09 +0000828 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000829}
830
Chris Lattner9a023f72009-06-24 04:43:34 +0000831/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000832/// ::= .section identifier (',' identifier)*
833/// FIXME: This should actually parse out the segment, section, attributes and
834/// sizeof_stub fields.
835bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000836 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000837
Daniel Dunbarace63122009-08-11 03:42:33 +0000838 StringRef SectionName;
839 if (ParseIdentifier(SectionName))
840 return Error(Loc, "expected identifier after '.section' directive");
841
842 // Verify there is a following comma.
843 if (!Lexer.is(AsmToken::Comma))
844 return TokError("unexpected token in '.section' directive");
845
Chris Lattnerff4bc462009-08-10 01:39:42 +0000846 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000847 SectionSpec += ",";
848
849 // Add all the tokens until the end of the line, ParseSectionSpecifier will
850 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000851 StringRef EOL = Lexer.LexUntilEndOfStatement();
852 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000853
Chris Lattnerff4bc462009-08-10 01:39:42 +0000854 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000855 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000856 return TokError("unexpected token in '.section' directive");
857 Lexer.Lex();
858
Chris Lattnerff4bc462009-08-10 01:39:42 +0000859
860 StringRef Segment, Section;
861 unsigned TAA, StubSize;
862 std::string ErrorStr =
863 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
864 TAA, StubSize);
865
866 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000867 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000868
Chris Lattner56594f92009-07-31 17:47:16 +0000869 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000870 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
871 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000872 return false;
873}
874
Chris Lattnere15c2d72009-08-10 18:05:55 +0000875/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000876bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
877 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000878 unsigned TAA, unsigned Align,
879 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000880 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000881 return TokError("unexpected token in section switching directive");
882 Lexer.Lex();
883
Chris Lattner56594f92009-07-31 17:47:16 +0000884 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000885 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
886 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000887
888 // Set the implicit alignment, if any.
889 //
890 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
891 // alignment on the section (e.g., if one manually inserts bytes into the
892 // section, then just issueing the section switch directive will not realign
893 // the section. However, this is arguably more reasonable behavior, and there
894 // is no good reason for someone to intentionally emit incorrectly sized
895 // values into the implicitly aligned sections.
896 if (Align)
897 Out.EmitValueToAlignment(Align, 0, 1, 0);
898
Chris Lattner529fb542009-06-24 05:13:15 +0000899 return false;
900}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000901
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000902bool AsmParser::ParseEscapedString(std::string &Data) {
903 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
904
905 Data = "";
906 StringRef Str = Lexer.getTok().getStringContents();
907 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
908 if (Str[i] != '\\') {
909 Data += Str[i];
910 continue;
911 }
912
913 // Recognize escaped characters. Note that this escape semantics currently
914 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
915 ++i;
916 if (i == e)
917 return TokError("unexpected backslash at end of string");
918
919 // Recognize octal sequences.
920 if ((unsigned) (Str[i] - '0') <= 7) {
921 // Consume up to three octal characters.
922 unsigned Value = Str[i] - '0';
923
924 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
925 ++i;
926 Value = Value * 8 + (Str[i] - '0');
927
928 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
929 ++i;
930 Value = Value * 8 + (Str[i] - '0');
931 }
932 }
933
934 if (Value > 255)
935 return TokError("invalid octal escape sequence (out of range)");
936
937 Data += (unsigned char) Value;
938 continue;
939 }
940
941 // Otherwise recognize individual escapes.
942 switch (Str[i]) {
943 default:
944 // Just reject invalid escape sequences for now.
945 return TokError("invalid escape sequence (unrecognized character)");
946
947 case 'b': Data += '\b'; break;
948 case 'f': Data += '\f'; break;
949 case 'n': Data += '\n'; break;
950 case 'r': Data += '\r'; break;
951 case 't': Data += '\t'; break;
952 case '"': Data += '"'; break;
953 case '\\': Data += '\\'; break;
954 }
955 }
956
957 return false;
958}
959
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000961/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000962bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000964 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000965 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966 return TokError("expected string in '.ascii' or '.asciz' directive");
967
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000968 std::string Data;
969 if (ParseEscapedString(Data))
970 return true;
971
972 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000973 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000974 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000975
976 Lexer.Lex();
977
Daniel Dunbar3f872332009-07-28 16:08:33 +0000978 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000979 break;
980
Daniel Dunbar3f872332009-07-28 16:08:33 +0000981 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000982 return TokError("unexpected token in '.ascii' or '.asciz' directive");
983 Lexer.Lex();
984 }
985 }
986
987 Lexer.Lex();
988 return false;
989}
990
991/// ParseDirectiveValue
992/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
993bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000994 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000995 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000996 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +0000997 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000998 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000999 return true;
1000
Daniel Dunbar883f9202009-08-31 08:08:50 +00001001 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002
Daniel Dunbar3f872332009-07-28 16:08:33 +00001003 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001004 break;
1005
1006 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001007 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001008 return TokError("unexpected token in directive");
1009 Lexer.Lex();
1010 }
1011 }
1012
1013 Lexer.Lex();
1014 return false;
1015}
1016
1017/// ParseDirectiveSpace
1018/// ::= .space expression [ , expression ]
1019bool AsmParser::ParseDirectiveSpace() {
1020 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001021 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022 return true;
1023
1024 int64_t FillExpr = 0;
1025 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001026 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1027 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 return TokError("unexpected token in '.space' directive");
1029 Lexer.Lex();
1030
Daniel Dunbar475839e2009-06-29 20:37:27 +00001031 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032 return true;
1033
1034 HasFillExpr = true;
1035
Daniel Dunbar3f872332009-07-28 16:08:33 +00001036 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return TokError("unexpected token in '.space' directive");
1038 }
1039
1040 Lexer.Lex();
1041
1042 if (NumBytes <= 0)
1043 return TokError("invalid number of bytes in '.space' directive");
1044
1045 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1046 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001047 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048
1049 return false;
1050}
1051
1052/// ParseDirectiveFill
1053/// ::= .fill expression , expression , expression
1054bool AsmParser::ParseDirectiveFill() {
1055 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001056 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057 return true;
1058
Daniel Dunbar3f872332009-07-28 16:08:33 +00001059 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060 return TokError("unexpected token in '.fill' directive");
1061 Lexer.Lex();
1062
1063 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001064 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001065 return true;
1066
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return TokError("unexpected token in '.fill' directive");
1069 Lexer.Lex();
1070
1071 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001072 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 return true;
1074
Daniel Dunbar3f872332009-07-28 16:08:33 +00001075 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076 return TokError("unexpected token in '.fill' directive");
1077
1078 Lexer.Lex();
1079
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001080 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1081 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082
1083 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001084 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001085
1086 return false;
1087}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001088
1089/// ParseDirectiveOrg
1090/// ::= .org expression [ , expression ]
1091bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001092 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001093 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001094 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001095 return true;
1096
1097 // Parse optional fill expression.
1098 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001099 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1100 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001101 return TokError("unexpected token in '.org' directive");
1102 Lexer.Lex();
1103
Daniel Dunbar475839e2009-06-29 20:37:27 +00001104 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001105 return true;
1106
Daniel Dunbar3f872332009-07-28 16:08:33 +00001107 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001108 return TokError("unexpected token in '.org' directive");
1109 }
1110
1111 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001112
1113 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1114 // has to be relative to the current section.
1115 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001116
1117 return false;
1118}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001119
1120/// ParseDirectiveAlign
1121/// ::= {.align, ...} expression [ , expression [ , expression ]]
1122bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001123 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001124 int64_t Alignment;
1125 if (ParseAbsoluteExpression(Alignment))
1126 return true;
1127
1128 SMLoc MaxBytesLoc;
1129 bool HasFillExpr = false;
1130 int64_t FillExpr = 0;
1131 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001132 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1133 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001134 return TokError("unexpected token in directive");
1135 Lexer.Lex();
1136
1137 // The fill expression can be omitted while specifying a maximum number of
1138 // alignment bytes, e.g:
1139 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001140 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001141 HasFillExpr = true;
1142 if (ParseAbsoluteExpression(FillExpr))
1143 return true;
1144 }
1145
Daniel Dunbar3f872332009-07-28 16:08:33 +00001146 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1147 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001148 return TokError("unexpected token in directive");
1149 Lexer.Lex();
1150
1151 MaxBytesLoc = Lexer.getLoc();
1152 if (ParseAbsoluteExpression(MaxBytesToFill))
1153 return true;
1154
Daniel Dunbar3f872332009-07-28 16:08:33 +00001155 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001156 return TokError("unexpected token in directive");
1157 }
1158 }
1159
1160 Lexer.Lex();
1161
1162 if (!HasFillExpr) {
1163 // FIXME: Sometimes fill with nop.
1164 FillExpr = 0;
1165 }
1166
1167 // Compute alignment in bytes.
1168 if (IsPow2) {
1169 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001170 if (Alignment >= 32) {
1171 Error(AlignmentLoc, "invalid alignment value");
1172 Alignment = 31;
1173 }
1174
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001175 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001176 }
1177
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001178 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179 if (MaxBytesLoc.isValid()) {
1180 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001181 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1182 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001183 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001184 }
1185
1186 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001187 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1188 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001189 MaxBytesToFill = 0;
1190 }
1191 }
1192
1193 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1194 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1195
1196 return false;
1197}
1198
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001199/// ParseDirectiveSymbolAttribute
1200/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1201bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001202 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001203 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001204 StringRef Name;
1205
1206 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001207 return TokError("expected identifier in directive");
1208
Daniel Dunbar959fd882009-08-26 22:13:22 +00001209 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001210
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001211 Out.EmitSymbolAttribute(Sym, Attr);
1212
Daniel Dunbar3f872332009-07-28 16:08:33 +00001213 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001214 break;
1215
Daniel Dunbar3f872332009-07-28 16:08:33 +00001216 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001217 return TokError("unexpected token in directive");
1218 Lexer.Lex();
1219 }
1220 }
1221
1222 Lexer.Lex();
1223 return false;
1224}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001225
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001226/// ParseDirectiveDarwinSymbolDesc
1227/// ::= .desc identifier , expression
1228bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001229 StringRef Name;
1230 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001231 return TokError("expected identifier in directive");
1232
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001233 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001234 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001235
Daniel Dunbar3f872332009-07-28 16:08:33 +00001236 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001237 return TokError("unexpected token in '.desc' directive");
1238 Lexer.Lex();
1239
1240 SMLoc DescLoc = Lexer.getLoc();
1241 int64_t DescValue;
1242 if (ParseAbsoluteExpression(DescValue))
1243 return true;
1244
Daniel Dunbar3f872332009-07-28 16:08:33 +00001245 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001246 return TokError("unexpected token in '.desc' directive");
1247
1248 Lexer.Lex();
1249
1250 // Set the n_desc field of this Symbol to this DescValue
1251 Out.EmitSymbolDesc(Sym, DescValue);
1252
1253 return false;
1254}
1255
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001256/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001257/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1258bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001259 SMLoc IDLoc = Lexer.getLoc();
1260 StringRef Name;
1261 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001262 return TokError("expected identifier in directive");
1263
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001264 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001265 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001266
Daniel Dunbar3f872332009-07-28 16:08:33 +00001267 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001268 return TokError("unexpected token in directive");
1269 Lexer.Lex();
1270
1271 int64_t Size;
1272 SMLoc SizeLoc = Lexer.getLoc();
1273 if (ParseAbsoluteExpression(Size))
1274 return true;
1275
1276 int64_t Pow2Alignment = 0;
1277 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001278 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001279 Lexer.Lex();
1280 Pow2AlignmentLoc = Lexer.getLoc();
1281 if (ParseAbsoluteExpression(Pow2Alignment))
1282 return true;
1283 }
1284
Daniel Dunbar3f872332009-07-28 16:08:33 +00001285 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001286 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001287
1288 Lexer.Lex();
1289
Chris Lattner1fc3d752009-07-09 17:25:12 +00001290 // NOTE: a size of zero for a .comm should create a undefined symbol
1291 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001292 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001293 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1294 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001295
1296 // NOTE: The alignment in the directive is a power of 2 value, the assember
1297 // may internally end up wanting an alignment in bytes.
1298 // FIXME: Diagnose overflow.
1299 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001300 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1301 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001302
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001303 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001304 return Error(IDLoc, "invalid symbol redefinition");
1305
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001306 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001307 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001308 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001309 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1310 MCSectionMachO::S_ZEROFILL, 0,
1311 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001312 Sym, Size, 1 << Pow2Alignment);
1313 return false;
1314 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001315
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001316 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001317 return false;
1318}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001319
1320/// ParseDirectiveDarwinZerofill
1321/// ::= .zerofill segname , sectname [, identifier , size_expression [
1322/// , align_expression ]]
1323bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001324 // FIXME: Handle quoted names here.
1325
Daniel Dunbar3f872332009-07-28 16:08:33 +00001326 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001327 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001328 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001329 Lexer.Lex();
1330
Daniel Dunbar3f872332009-07-28 16:08:33 +00001331 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001332 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001333 Lexer.Lex();
1334
Daniel Dunbar3f872332009-07-28 16:08:33 +00001335 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001336 return TokError("expected section name after comma in '.zerofill' "
1337 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001338 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001339 Lexer.Lex();
1340
Chris Lattner9be3fee2009-07-10 22:20:30 +00001341 // If this is the end of the line all that was wanted was to create the
1342 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001343 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001344 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001345 Out.EmitZerofill(getMachOSection(Segment, Section,
1346 MCSectionMachO::S_ZEROFILL, 0,
1347 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001348 return false;
1349 }
1350
Daniel Dunbar3f872332009-07-28 16:08:33 +00001351 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001352 return TokError("unexpected token in directive");
1353 Lexer.Lex();
1354
Daniel Dunbar3f872332009-07-28 16:08:33 +00001355 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001356 return TokError("expected identifier in directive");
1357
1358 // handle the identifier as the key symbol.
1359 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001360 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001361 Lexer.Lex();
1362
Daniel Dunbar3f872332009-07-28 16:08:33 +00001363 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001364 return TokError("unexpected token in directive");
1365 Lexer.Lex();
1366
1367 int64_t Size;
1368 SMLoc SizeLoc = Lexer.getLoc();
1369 if (ParseAbsoluteExpression(Size))
1370 return true;
1371
1372 int64_t Pow2Alignment = 0;
1373 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001374 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001375 Lexer.Lex();
1376 Pow2AlignmentLoc = Lexer.getLoc();
1377 if (ParseAbsoluteExpression(Pow2Alignment))
1378 return true;
1379 }
1380
Daniel Dunbar3f872332009-07-28 16:08:33 +00001381 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001382 return TokError("unexpected token in '.zerofill' directive");
1383
1384 Lexer.Lex();
1385
1386 if (Size < 0)
1387 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1388 "than zero");
1389
1390 // NOTE: The alignment in the directive is a power of 2 value, the assember
1391 // may internally end up wanting an alignment in bytes.
1392 // FIXME: Diagnose overflow.
1393 if (Pow2Alignment < 0)
1394 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1395 "can't be less than zero");
1396
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001397 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001398 return Error(IDLoc, "invalid symbol redefinition");
1399
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001400 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001401 //
1402 // FIXME: Arch specific.
1403 Out.EmitZerofill(getMachOSection(Segment, Section,
1404 MCSectionMachO::S_ZEROFILL, 0,
1405 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001406 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001407
1408 return false;
1409}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001410
1411/// ParseDirectiveDarwinSubsectionsViaSymbols
1412/// ::= .subsections_via_symbols
1413bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001414 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001415 return TokError("unexpected token in '.subsections_via_symbols' directive");
1416
1417 Lexer.Lex();
1418
Kevin Enderbyf96db462009-07-16 17:56:39 +00001419 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001420
1421 return false;
1422}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001423
1424/// ParseDirectiveAbort
1425/// ::= .abort [ "abort_string" ]
1426bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001427 // FIXME: Use loc from directive.
1428 SMLoc Loc = Lexer.getLoc();
1429
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001430 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001431 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1432 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001433 return TokError("expected string in '.abort' directive");
1434
Daniel Dunbar419aded2009-07-28 16:38:40 +00001435 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001436
1437 Lexer.Lex();
1438 }
1439
Daniel Dunbar3f872332009-07-28 16:08:33 +00001440 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001441 return TokError("unexpected token in '.abort' directive");
1442
1443 Lexer.Lex();
1444
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001445 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001446 if (Str.empty())
1447 Error(Loc, ".abort detected. Assembly stopping.");
1448 else
1449 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001450
1451 return false;
1452}
Kevin Enderby71148242009-07-14 21:35:03 +00001453
1454/// ParseDirectiveLsym
1455/// ::= .lsym identifier , expression
1456bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001457 StringRef Name;
1458 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001459 return TokError("expected identifier in directive");
1460
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001461 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001462 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001463
Daniel Dunbar3f872332009-07-28 16:08:33 +00001464 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001465 return TokError("unexpected token in '.lsym' directive");
1466 Lexer.Lex();
1467
Daniel Dunbar821e3332009-08-31 08:09:28 +00001468 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001469 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001470 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001471 return true;
1472
Daniel Dunbar3f872332009-07-28 16:08:33 +00001473 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001474 return TokError("unexpected token in '.lsym' directive");
1475
1476 Lexer.Lex();
1477
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001478 // We don't currently support this directive.
1479 //
1480 // FIXME: Diagnostic location!
1481 (void) Sym;
1482 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001483}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001484
1485/// ParseDirectiveInclude
1486/// ::= .include "filename"
1487bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001488 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001489 return TokError("expected string in '.include' directive");
1490
Daniel Dunbar419aded2009-07-28 16:38:40 +00001491 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001492 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001493 Lexer.Lex();
1494
Daniel Dunbar3f872332009-07-28 16:08:33 +00001495 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001496 return TokError("unexpected token in '.include' directive");
1497
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001498 // Strip the quotes.
1499 Filename = Filename.substr(1, Filename.size()-2);
1500
1501 // Attempt to switch the lexer to the included file before consuming the end
1502 // of statement to avoid losing it when we switch.
1503 if (Lexer.EnterIncludeFile(Filename)) {
1504 Lexer.PrintMessage(IncludeLoc,
1505 "Could not find include file '" + Filename + "'",
1506 "error");
1507 return true;
1508 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001509
1510 return false;
1511}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001512
1513/// ParseDirectiveDarwinDumpOrLoad
1514/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001515bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001516 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001517 return TokError("expected string in '.dump' or '.load' directive");
1518
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001519 Lexer.Lex();
1520
Daniel Dunbar3f872332009-07-28 16:08:33 +00001521 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001522 return TokError("unexpected token in '.dump' or '.load' directive");
1523
1524 Lexer.Lex();
1525
Kevin Enderby5026ae42009-07-20 20:25:37 +00001526 // FIXME: If/when .dump and .load are implemented they will be done in the
1527 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001528 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001529 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001530 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001531 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001532
1533 return false;
1534}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001535
1536/// ParseDirectiveIf
1537/// ::= .if expression
1538bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1539 // Consume the identifier that was the .if directive
1540 Lexer.Lex();
1541
1542 TheCondStack.push_back(TheCondState);
1543 TheCondState.TheCond = AsmCond::IfCond;
1544 if(TheCondState.Ignore) {
1545 EatToEndOfStatement();
1546 }
1547 else {
1548 int64_t ExprValue;
1549 if (ParseAbsoluteExpression(ExprValue))
1550 return true;
1551
1552 if (Lexer.isNot(AsmToken::EndOfStatement))
1553 return TokError("unexpected token in '.if' directive");
1554
1555 Lexer.Lex();
1556
1557 TheCondState.CondMet = ExprValue;
1558 TheCondState.Ignore = !TheCondState.CondMet;
1559 }
1560
1561 return false;
1562}
1563
1564/// ParseDirectiveElseIf
1565/// ::= .elseif expression
1566bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1567 if (TheCondState.TheCond != AsmCond::IfCond &&
1568 TheCondState.TheCond != AsmCond::ElseIfCond)
1569 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1570 " an .elseif");
1571 TheCondState.TheCond = AsmCond::ElseIfCond;
1572
1573 // Consume the identifier that was the .elseif directive
1574 Lexer.Lex();
1575
1576 bool LastIgnoreState = false;
1577 if (!TheCondStack.empty())
1578 LastIgnoreState = TheCondStack.back().Ignore;
1579 if (LastIgnoreState || TheCondState.CondMet) {
1580 TheCondState.Ignore = true;
1581 EatToEndOfStatement();
1582 }
1583 else {
1584 int64_t ExprValue;
1585 if (ParseAbsoluteExpression(ExprValue))
1586 return true;
1587
1588 if (Lexer.isNot(AsmToken::EndOfStatement))
1589 return TokError("unexpected token in '.elseif' directive");
1590
1591 Lexer.Lex();
1592 TheCondState.CondMet = ExprValue;
1593 TheCondState.Ignore = !TheCondState.CondMet;
1594 }
1595
1596 return false;
1597}
1598
1599/// ParseDirectiveElse
1600/// ::= .else
1601bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1602 // Consume the identifier that was the .else directive
1603 Lexer.Lex();
1604
1605 if (Lexer.isNot(AsmToken::EndOfStatement))
1606 return TokError("unexpected token in '.else' directive");
1607
1608 Lexer.Lex();
1609
1610 if (TheCondState.TheCond != AsmCond::IfCond &&
1611 TheCondState.TheCond != AsmCond::ElseIfCond)
1612 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1613 ".elseif");
1614 TheCondState.TheCond = AsmCond::ElseCond;
1615 bool LastIgnoreState = false;
1616 if (!TheCondStack.empty())
1617 LastIgnoreState = TheCondStack.back().Ignore;
1618 if (LastIgnoreState || TheCondState.CondMet)
1619 TheCondState.Ignore = true;
1620 else
1621 TheCondState.Ignore = false;
1622
1623 return false;
1624}
1625
1626/// ParseDirectiveEndIf
1627/// ::= .endif
1628bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1629 // Consume the identifier that was the .endif directive
1630 Lexer.Lex();
1631
1632 if (Lexer.isNot(AsmToken::EndOfStatement))
1633 return TokError("unexpected token in '.endif' directive");
1634
1635 Lexer.Lex();
1636
1637 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1638 TheCondStack.empty())
1639 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1640 ".else");
1641 if (!TheCondStack.empty()) {
1642 TheCondState = TheCondStack.back();
1643 TheCondStack.pop_back();
1644 }
1645
1646 return false;
1647}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001648
1649/// ParseDirectiveFile
1650/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001651bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001652 // FIXME: I'm not sure what this is.
1653 int64_t FileNumber = -1;
1654 if (Lexer.is(AsmToken::Integer)) {
1655 FileNumber = Lexer.getTok().getIntVal();
1656 Lexer.Lex();
1657
1658 if (FileNumber < 1)
1659 return TokError("file number less than one");
1660 }
1661
1662 if (Lexer.isNot(AsmToken::String))
1663 return TokError("unexpected token in '.file' directive");
1664
Bill Wendling9bc0af82009-12-28 01:34:57 +00001665 StringRef ATTRIBUTE_UNUSED FileName = Lexer.getTok().getString();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001666 Lexer.Lex();
1667
1668 if (Lexer.isNot(AsmToken::EndOfStatement))
1669 return TokError("unexpected token in '.file' directive");
1670
1671 // FIXME: Do something with the .file.
1672
1673 return false;
1674}
1675
1676/// ParseDirectiveLine
1677/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001678bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001679 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1680 if (Lexer.isNot(AsmToken::Integer))
1681 return TokError("unexpected token in '.line' directive");
1682
1683 int64_t LineNumber = Lexer.getTok().getIntVal();
1684 (void) LineNumber;
1685 Lexer.Lex();
1686
1687 // FIXME: Do something with the .line.
1688 }
1689
1690 if (Lexer.isNot(AsmToken::EndOfStatement))
1691 return TokError("unexpected token in '.file' directive");
1692
1693 return false;
1694}
1695
1696
1697/// ParseDirectiveLoc
1698/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001699bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001700 if (Lexer.isNot(AsmToken::Integer))
1701 return TokError("unexpected token in '.loc' directive");
1702
1703 // FIXME: What are these fields?
1704 int64_t FileNumber = Lexer.getTok().getIntVal();
1705 (void) FileNumber;
1706 // FIXME: Validate file.
1707
1708 Lexer.Lex();
1709 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1710 if (Lexer.isNot(AsmToken::Integer))
1711 return TokError("unexpected token in '.loc' directive");
1712
1713 int64_t Param2 = Lexer.getTok().getIntVal();
1714 (void) Param2;
1715 Lexer.Lex();
1716
1717 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1718 if (Lexer.isNot(AsmToken::Integer))
1719 return TokError("unexpected token in '.loc' directive");
1720
1721 int64_t Param3 = Lexer.getTok().getIntVal();
1722 (void) Param3;
1723 Lexer.Lex();
1724
1725 // FIXME: Do something with the .loc.
1726 }
1727 }
1728
1729 if (Lexer.isNot(AsmToken::EndOfStatement))
1730 return TokError("unexpected token in '.file' directive");
1731
1732 return false;
1733}
1734