blob: a241106465a0c7b661eed1f7c59f0880dba3758e [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
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000016#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000018#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000019#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000024#include "llvm/Support/Compiler.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
Chris Lattneraaec2052010-01-19 19:46:13 +000030
31enum { DEFAULT_ADDRSPACE = 0 };
32
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000033// Mach-O section uniquing.
34//
35// FIXME: Figure out where this should live, it should be shared by
36// TargetLoweringObjectFile.
37typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
38
Chris Lattnerebb89b42009-09-27 21:16:52 +000039AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
40 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000041 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
42 CurBuffer(0), SectionUniquingMap(0) {
43 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
44
Chris Lattnerebb89b42009-09-27 21:16:52 +000045 // Debugging directives.
46 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
47 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
48 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
49}
50
51
52
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000053AsmParser::~AsmParser() {
54 // If we have the MachO uniquing map, free it.
55 delete (MachOUniqueMapTy*)SectionUniquingMap;
56}
57
58const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
59 const StringRef &Section,
60 unsigned TypeAndAttributes,
61 unsigned Reserved2,
62 SectionKind Kind) const {
63 // We unique sections by their segment/section pair. The returned section
64 // may not have the same flags as the requested section, if so this should be
65 // diagnosed by the client as an error.
66
67 // Create the map if it doesn't already exist.
68 if (SectionUniquingMap == 0)
69 SectionUniquingMap = new MachOUniqueMapTy();
70 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
71
72 // Form the name to look up.
73 SmallString<64> Name;
74 Name += Segment;
75 Name.push_back(',');
76 Name += Section;
77
78 // Do the lookup, if we have a hit, return it.
79 const MCSectionMachO *&Entry = Map[Name.str()];
80
81 // FIXME: This should validate the type and attributes.
82 if (Entry) return Entry;
83
84 // Otherwise, return a new section.
85 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
86 Reserved2, Kind, Ctx);
87}
88
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000089void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000090 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000091}
92
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000093bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000094 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000095 return true;
96}
97
98bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000099 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000100 return true;
101}
102
Sean Callananbf2013e2010-01-20 23:19:55 +0000103void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
104 const char *Type) const {
105 SrcMgr.PrintMessage(Loc, Msg, Type);
106}
Sean Callananfd0b0282010-01-21 00:19:58 +0000107
108bool AsmParser::EnterIncludeFile(const std::string &Filename) {
109 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
110 if (NewBuf == -1)
111 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000112
Sean Callananfd0b0282010-01-21 00:19:58 +0000113 CurBuffer = NewBuf;
114
115 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
116
117 return false;
118}
119
120const AsmToken &AsmParser::Lex() {
121 const AsmToken *tok = &Lexer.Lex();
122
123 if (tok->is(AsmToken::Eof)) {
124 // If this is the end of an included file, pop the parent file off the
125 // include stack.
126 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
127 if (ParentIncludeLoc != SMLoc()) {
128 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
129 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
130 ParentIncludeLoc.getPointer());
131 tok = &Lexer.Lex();
132 }
133 }
134
135 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000136 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000137
Sean Callananfd0b0282010-01-21 00:19:58 +0000138 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000139}
140
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000141bool AsmParser::Run(bool NoInitialTextSection) {
142 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000143 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000144 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000145 if (!NoInitialTextSection)
146 Out.SwitchSection(getMachOSection("__TEXT", "__text",
147 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
148 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000149
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000150 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000151 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000152
Chris Lattnerb717fb02009-07-02 21:53:43 +0000153 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000154
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000155 AsmCond StartingCondState = TheCondState;
156
Chris Lattnerb717fb02009-07-02 21:53:43 +0000157 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000158 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000159 // Handle conditional assembly here before calling ParseStatement()
160 if (Lexer.getKind() == AsmToken::Identifier) {
161 // If we have an identifier, handle it as the key symbol.
Sean Callanan18b83232010-01-19 21:44:56 +0000162 AsmToken ID = getTok();
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000163 SMLoc IDLoc = ID.getLoc();
164 StringRef IDVal = ID.getString();
165
166 if (IDVal == ".if" ||
167 IDVal == ".elseif" ||
168 IDVal == ".else" ||
169 IDVal == ".endif") {
170 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
171 continue;
172 HadError = true;
173 EatToEndOfStatement();
174 continue;
175 }
176 }
177 if (TheCondState.Ignore) {
178 EatToEndOfStatement();
179 continue;
180 }
181
Chris Lattnerb717fb02009-07-02 21:53:43 +0000182 if (!ParseStatement()) continue;
183
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000184 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000185 HadError = true;
186 EatToEndOfStatement();
187 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000188
189 if (TheCondState.TheCond != StartingCondState.TheCond ||
190 TheCondState.Ignore != StartingCondState.Ignore)
191 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000192
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000193 if (!HadError)
194 Out.Finish();
195
Chris Lattnerb717fb02009-07-02 21:53:43 +0000196 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000197}
198
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000199/// ParseConditionalAssemblyDirectives - parse the conditional assembly
200/// directives
201bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
202 SMLoc DirectiveLoc) {
203 if (Directive == ".if")
204 return ParseDirectiveIf(DirectiveLoc);
205 if (Directive == ".elseif")
206 return ParseDirectiveElseIf(DirectiveLoc);
207 if (Directive == ".else")
208 return ParseDirectiveElse(DirectiveLoc);
209 if (Directive == ".endif")
210 return ParseDirectiveEndIf(DirectiveLoc);
211 return true;
212}
213
Chris Lattner2cf5f142009-06-22 01:29:09 +0000214/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
215void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000216 while (Lexer.isNot(AsmToken::EndOfStatement) &&
217 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000218 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000219
220 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000221 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000222 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000223}
224
Chris Lattnerc4193832009-06-22 05:51:26 +0000225
Chris Lattner74ec1a32009-06-22 06:32:03 +0000226/// ParseParenExpr - Parse a paren expression and return it.
227/// NOTE: This assumes the leading '(' has already been consumed.
228///
229/// parenexpr ::= expr)
230///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000231bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000232 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000233 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000234 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000235 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000236 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000237 return false;
238}
Chris Lattnerc4193832009-06-22 05:51:26 +0000239
Daniel Dunbar959fd882009-08-26 22:13:22 +0000240MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Daniel Dunbar959fd882009-08-26 22:13:22 +0000241 // If the label starts with L it is an assembler temporary label.
242 if (Name.startswith("L"))
Chris Lattner00685bb2010-03-10 01:29:27 +0000243 return Ctx.GetOrCreateTemporarySymbol(Name);
244 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000245}
246
Chris Lattner74ec1a32009-06-22 06:32:03 +0000247/// ParsePrimaryExpr - Parse a primary expression and return it.
248/// primaryexpr ::= (parenexpr
249/// primaryexpr ::= symbol
250/// primaryexpr ::= number
251/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000252bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000253 switch (Lexer.getKind()) {
254 default:
255 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000256 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000257 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000258 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000260 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000262 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000263 case AsmToken::Identifier: {
264 // This is a symbol reference.
Sean Callanan18b83232010-01-19 21:44:56 +0000265 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000266 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000267 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000268
269 // If this is an absolute variable reference, substitute it now to preserve
270 // semantics in the face of reassignment.
271 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
272 Res = Sym->getValue();
273 return false;
274 }
275
276 // Otherwise create a symbol ref.
277 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000278 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000279 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000280 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000281 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000282 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000283 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000284 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000285 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000286 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000287 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000288 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000289 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000290 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000291 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000292 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000294 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000295 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000296 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000297 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000298 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000299 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000300 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000301 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000302 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000303 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000304 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000306 }
307}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000308
Chris Lattnerb4307b32010-01-15 19:28:38 +0000309bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000310 SMLoc EndLoc;
311 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000312}
313
Chris Lattner74ec1a32009-06-22 06:32:03 +0000314/// ParseExpression - Parse an expression and return it.
315///
316/// expr ::= expr +,- expr -> lowest.
317/// expr ::= expr |,^,&,! expr -> middle.
318/// expr ::= expr *,/,%,<<,>> expr -> highest.
319/// expr ::= primaryexpr
320///
Chris Lattner54482b42010-01-15 19:39:23 +0000321bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000322 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000323 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000324 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
325 return true;
326
327 // Try to constant fold it up front, if possible.
328 int64_t Value;
329 if (Res->EvaluateAsAbsolute(Value))
330 Res = MCConstantExpr::Create(Value, getContext());
331
332 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000333}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000334
Chris Lattnerb4307b32010-01-15 19:28:38 +0000335bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000336 Res = 0;
337 return ParseParenExpr(Res, EndLoc) ||
338 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000339}
340
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000342 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000343
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000344 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000345 if (ParseExpression(Expr))
346 return true;
347
Daniel Dunbare00b0112009-10-16 01:57:52 +0000348 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000349 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350
351 return false;
352}
353
Daniel Dunbar3f872332009-07-28 16:08:33 +0000354static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000355 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000356 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000357 default:
358 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359
360 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000361 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000362 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000363 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000364 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000365 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000366 return 1;
367
368 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000371 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000372 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 case AsmToken::ExclaimEqual:
379 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000380 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000381 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000382 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000383 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000384 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000386 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000387 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000388 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000389 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000390 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000391 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000392 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000393 return 2;
394
395 // Intermediate Precedence: |, &, ^
396 //
397 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000398 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000399 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000400 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000401 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000402 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000404 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000405 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000406 return 3;
407
408 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000409 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000410 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000411 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000412 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000413 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000414 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000415 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000416 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000417 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000418 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000419 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000420 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000421 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000422 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000423 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000424 }
425}
426
427
428/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
429/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000430bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
431 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000432 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000433 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000434 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000435
436 // If the next token is lower precedence than we are allowed to eat, return
437 // successfully with what we ate already.
438 if (TokPrec < Precedence)
439 return false;
440
Sean Callanan79ed1a82010-01-19 20:22:31 +0000441 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000442
443 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000444 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000445 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000446
447 // If BinOp binds less tightly with RHS than the operator after RHS, let
448 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000449 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000450 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000451 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000452 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000453 }
454
Daniel Dunbar475839e2009-06-29 20:37:27 +0000455 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000456 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000457 }
458}
459
Chris Lattnerc4193832009-06-22 05:51:26 +0000460
461
462
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000463/// ParseStatement:
464/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000465/// ::= Label* Directive ...Operands... EndOfStatement
466/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000467bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000468 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000469 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000470 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000471 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000472
473 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000474 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000475 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000476 StringRef IDVal;
477 if (ParseIdentifier(IDVal))
478 return TokError("unexpected token at start of statement");
479
480 // FIXME: Recurse on local labels?
481
482 // See what kind of statement we have.
483 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000484 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000485 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000486 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000487
488 // Diagnose attempt to use a variable as a label.
489 //
490 // FIXME: Diagnostics. Note the location of the definition as a label.
491 // FIXME: This doesn't diagnose assignment to a symbol which has been
492 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000493 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000494 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000495 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000496
Daniel Dunbar959fd882009-08-26 22:13:22 +0000497 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000498 Out.EmitLabel(Sym);
499
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000500 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000501 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000502
Daniel Dunbar3f872332009-07-28 16:08:33 +0000503 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000504 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000505 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000506
Daniel Dunbare2ace502009-08-31 08:09:09 +0000507 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000508
509 default: // Normal instruction or directive.
510 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000511 }
512
513 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000514 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000515 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000516 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000517 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000519 // FIXME: This changes behavior based on the -static flag to the
520 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000521 return ParseDirectiveSectionSwitch("__TEXT", "__text",
522 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000523 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000524 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000526 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000528 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
529 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000531 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000532 MCSectionMachO::S_4BYTE_LITERALS,
533 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000535 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000536 MCSectionMachO::S_8BYTE_LITERALS,
537 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000538 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000539 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000540 MCSectionMachO::S_16BYTE_LITERALS,
541 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000542 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000543 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000547 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
550
551 // FIXME: The assembler manual claims that this has the self modify code
552 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000553 if (IDVal == ".symbol_stub")
554 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
555 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000556 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
557 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 0, 16);
559 // FIXME: PowerPC only?
560 if (IDVal == ".picsymbol_stub")
561 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
562 MCSectionMachO::S_SYMBOL_STUBS |
563 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
564 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000565 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000566 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
569
570 // FIXME: The section names of these two are misspelled in the assembler
571 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000573 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
574 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
575 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000577 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
578 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
579 4);
580
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000582 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000584 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
586 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000587 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000588 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
590 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000593
594
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000596 return ParseDirectiveSectionSwitch("__OBJC", "__class",
597 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000602 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
603 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000605 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
606 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
612 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000613 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000614 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
615 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000617 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
618 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000620 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
621 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
622 MCSectionMachO::S_LITERAL_POINTERS,
623 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000625 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
626 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
627 MCSectionMachO::S_LITERAL_POINTERS,
628 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000630 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
631 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000633 return ParseDirectiveSectionSwitch("__OBJC", "__category",
634 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000636 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
637 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000639 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
640 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000642 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
643 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000645 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
646 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000648 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
649 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000651 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
652 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000654 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
655 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000656
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000657 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000659 return ParseDirectiveSet();
660
Daniel Dunbara0d14262009-06-24 23:30:00 +0000661 // Data directives
662
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000664 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000666 return ParseDirectiveAscii(true);
667
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000669 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000670 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000671 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000673 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000675 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000676
677 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000679 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000681 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000683 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000685 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000687 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000689 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000691 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000693 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
694
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000696 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000697
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000699 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000701 return ParseDirectiveSpace();
702
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000703 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000704
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000706 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000708 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000710 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000712 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000714 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000716 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000718 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000720 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000722 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000724 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000726 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000728 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000729
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000731 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000732 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000733 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000735 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000737 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000738 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000739 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000740
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000742 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000744 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000746 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000748 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000750 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000751
Chris Lattnerebb89b42009-09-27 21:16:52 +0000752 // Look up the handler in the handler table,
753 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
754 if (Handler)
755 return (this->*Handler)(IDVal, IDLoc);
756
Kevin Enderby9c656452009-09-10 20:51:44 +0000757 // Target hook for parsing target specific directives.
758 if (!getTargetParser().ParseDirective(ID))
759 return false;
760
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000761 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000762 EatToEndOfStatement();
763 return false;
764 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000765
Chris Lattner98986712010-01-14 22:21:20 +0000766
767 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
768 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
769 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000770 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000771
Daniel Dunbar3f872332009-07-28 16:08:33 +0000772 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000773 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000774 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000775
776 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000777 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000778
Chris Lattner98986712010-01-14 22:21:20 +0000779
780 MCInst Inst;
781
782 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
783
784 // Free any parsed operands.
785 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
786 delete ParsedOperands[i];
787
788 if (MatchFail) {
789 // FIXME: We should give nicer diagnostics about the exact failure.
790 Error(IDLoc, "unrecognized instruction");
791 return true;
792 }
793
Chris Lattner2cf5f142009-06-22 01:29:09 +0000794 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000795 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000796
797 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000798 return false;
799}
Chris Lattner9a023f72009-06-24 04:43:34 +0000800
Daniel Dunbare2ace502009-08-31 08:09:09 +0000801bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000802 // FIXME: Use better location, we should use proper tokens.
803 SMLoc EqualLoc = Lexer.getLoc();
804
Daniel Dunbar821e3332009-08-31 08:09:28 +0000805 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000806 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000807 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000808 return true;
809
Daniel Dunbar3f872332009-07-28 16:08:33 +0000810 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000811 return TokError("unexpected token in assignment");
812
813 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000814 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000815
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000816 // Validate that the LHS is allowed to be a variable (either it has not been
817 // used as a symbol, or it is an absolute symbol).
818 MCSymbol *Sym = getContext().LookupSymbol(Name);
819 if (Sym) {
820 // Diagnose assignment to a label.
821 //
822 // FIXME: Diagnostics. Note the location of the definition as a label.
823 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
824 if (!Sym->isUndefined() && !Sym->isAbsolute())
825 return Error(EqualLoc, "redefinition of '" + Name + "'");
826 else if (!Sym->isVariable())
827 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
828 else if (!isa<MCConstantExpr>(Sym->getValue()))
829 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
830 Name + "'");
831 } else
832 Sym = CreateSymbol(Name);
833
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000834 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000835
836 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000837 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000838
839 return false;
840}
841
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000842/// ParseIdentifier:
843/// ::= identifier
844/// ::= string
845bool AsmParser::ParseIdentifier(StringRef &Res) {
846 if (Lexer.isNot(AsmToken::Identifier) &&
847 Lexer.isNot(AsmToken::String))
848 return true;
849
Sean Callanan18b83232010-01-19 21:44:56 +0000850 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000851
Sean Callanan79ed1a82010-01-19 20:22:31 +0000852 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000853
854 return false;
855}
856
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000857/// ParseDirectiveSet:
858/// ::= .set identifier ',' expression
859bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000860 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000861
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000862 if (ParseIdentifier(Name))
863 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000864
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000865 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000866 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000867 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000868
Daniel Dunbare2ace502009-08-31 08:09:09 +0000869 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000870}
871
Chris Lattner9a023f72009-06-24 04:43:34 +0000872/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000873/// ::= .section identifier (',' identifier)*
874/// FIXME: This should actually parse out the segment, section, attributes and
875/// sizeof_stub fields.
876bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000877 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000878
Daniel Dunbarace63122009-08-11 03:42:33 +0000879 StringRef SectionName;
880 if (ParseIdentifier(SectionName))
881 return Error(Loc, "expected identifier after '.section' directive");
882
883 // Verify there is a following comma.
884 if (!Lexer.is(AsmToken::Comma))
885 return TokError("unexpected token in '.section' directive");
886
Chris Lattnerff4bc462009-08-10 01:39:42 +0000887 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000888 SectionSpec += ",";
889
890 // Add all the tokens until the end of the line, ParseSectionSpecifier will
891 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000892 StringRef EOL = Lexer.LexUntilEndOfStatement();
893 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000894
Sean Callanan79ed1a82010-01-19 20:22:31 +0000895 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000896 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000897 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000898 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000899
Chris Lattnerff4bc462009-08-10 01:39:42 +0000900
901 StringRef Segment, Section;
902 unsigned TAA, StubSize;
903 std::string ErrorStr =
904 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
905 TAA, StubSize);
906
907 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000908 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000909
Chris Lattner56594f92009-07-31 17:47:16 +0000910 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000911 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000912 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000913 isText ? SectionKind::getText()
914 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000915 return false;
916}
917
Chris Lattnere15c2d72009-08-10 18:05:55 +0000918/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000919bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
920 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000921 unsigned TAA, unsigned Align,
922 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000923 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000924 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000925 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000926
Chris Lattner56594f92009-07-31 17:47:16 +0000927 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000928 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000929 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000930 isText ? SectionKind::getText()
931 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000932
933 // Set the implicit alignment, if any.
934 //
935 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
936 // alignment on the section (e.g., if one manually inserts bytes into the
937 // section, then just issueing the section switch directive will not realign
938 // the section. However, this is arguably more reasonable behavior, and there
939 // is no good reason for someone to intentionally emit incorrectly sized
940 // values into the implicitly aligned sections.
941 if (Align)
942 Out.EmitValueToAlignment(Align, 0, 1, 0);
943
Chris Lattner529fb542009-06-24 05:13:15 +0000944 return false;
945}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000946
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000947bool AsmParser::ParseEscapedString(std::string &Data) {
948 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
949
950 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000951 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000952 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
953 if (Str[i] != '\\') {
954 Data += Str[i];
955 continue;
956 }
957
958 // Recognize escaped characters. Note that this escape semantics currently
959 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
960 ++i;
961 if (i == e)
962 return TokError("unexpected backslash at end of string");
963
964 // Recognize octal sequences.
965 if ((unsigned) (Str[i] - '0') <= 7) {
966 // Consume up to three octal characters.
967 unsigned Value = Str[i] - '0';
968
969 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
970 ++i;
971 Value = Value * 8 + (Str[i] - '0');
972
973 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
974 ++i;
975 Value = Value * 8 + (Str[i] - '0');
976 }
977 }
978
979 if (Value > 255)
980 return TokError("invalid octal escape sequence (out of range)");
981
982 Data += (unsigned char) Value;
983 continue;
984 }
985
986 // Otherwise recognize individual escapes.
987 switch (Str[i]) {
988 default:
989 // Just reject invalid escape sequences for now.
990 return TokError("invalid escape sequence (unrecognized character)");
991
992 case 'b': Data += '\b'; break;
993 case 'f': Data += '\f'; break;
994 case 'n': Data += '\n'; break;
995 case 'r': Data += '\r'; break;
996 case 't': Data += '\t'; break;
997 case '"': Data += '"'; break;
998 case '\\': Data += '\\'; break;
999 }
1000 }
1001
1002 return false;
1003}
1004
Daniel Dunbara0d14262009-06-24 23:30:00 +00001005/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001006/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001008 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001009 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001010 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001011 return TokError("expected string in '.ascii' or '.asciz' directive");
1012
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001013 std::string Data;
1014 if (ParseEscapedString(Data))
1015 return true;
1016
Chris Lattneraaec2052010-01-19 19:46:13 +00001017 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001019 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020
Sean Callanan79ed1a82010-01-19 20:22:31 +00001021 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 break;
1025
Daniel Dunbar3f872332009-07-28 16:08:33 +00001026 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001027 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001028 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 }
1030 }
1031
Sean Callanan79ed1a82010-01-19 20:22:31 +00001032 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001033 return false;
1034}
1035
1036/// ParseDirectiveValue
1037/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1038bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001039 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001040 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001041 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001042 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001043 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001044 return true;
1045
Chris Lattneraaec2052010-01-19 19:46:13 +00001046 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001047
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 break;
1050
1051 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001052 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001053 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001054 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 }
1056 }
1057
Sean Callanan79ed1a82010-01-19 20:22:31 +00001058 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059 return false;
1060}
1061
1062/// ParseDirectiveSpace
1063/// ::= .space expression [ , expression ]
1064bool AsmParser::ParseDirectiveSpace() {
1065 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001066 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001067 return true;
1068
1069 int64_t FillExpr = 0;
1070 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001071 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1072 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001074 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001075
Daniel Dunbar475839e2009-06-29 20:37:27 +00001076 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return true;
1078
1079 HasFillExpr = true;
1080
Daniel Dunbar3f872332009-07-28 16:08:33 +00001081 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082 return TokError("unexpected token in '.space' directive");
1083 }
1084
Sean Callanan79ed1a82010-01-19 20:22:31 +00001085 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086
1087 if (NumBytes <= 0)
1088 return TokError("invalid number of bytes in '.space' directive");
1089
1090 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001091 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092
1093 return false;
1094}
1095
1096/// ParseDirectiveFill
1097/// ::= .fill expression , expression , expression
1098bool AsmParser::ParseDirectiveFill() {
1099 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001100 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101 return true;
1102
Daniel Dunbar3f872332009-07-28 16:08:33 +00001103 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001104 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001105 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001106
1107 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001108 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001109 return true;
1110
Daniel Dunbar3f872332009-07-28 16:08:33 +00001111 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001112 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001113 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114
1115 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001116 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 return true;
1118
Daniel Dunbar3f872332009-07-28 16:08:33 +00001119 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001120 return TokError("unexpected token in '.fill' directive");
1121
Sean Callanan79ed1a82010-01-19 20:22:31 +00001122 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001123
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001124 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1125 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126
1127 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001128 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1129 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130
1131 return false;
1132}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001133
1134/// ParseDirectiveOrg
1135/// ::= .org expression [ , expression ]
1136bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001137 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001138 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001139 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001140 return true;
1141
1142 // Parse optional fill expression.
1143 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001144 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1145 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001146 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001147 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001148
Daniel Dunbar475839e2009-06-29 20:37:27 +00001149 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001150 return true;
1151
Daniel Dunbar3f872332009-07-28 16:08:33 +00001152 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001153 return TokError("unexpected token in '.org' directive");
1154 }
1155
Sean Callanan79ed1a82010-01-19 20:22:31 +00001156 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001157
1158 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1159 // has to be relative to the current section.
1160 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001161
1162 return false;
1163}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001164
1165/// ParseDirectiveAlign
1166/// ::= {.align, ...} expression [ , expression [ , expression ]]
1167bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001168 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001169 int64_t Alignment;
1170 if (ParseAbsoluteExpression(Alignment))
1171 return true;
1172
1173 SMLoc MaxBytesLoc;
1174 bool HasFillExpr = false;
1175 int64_t FillExpr = 0;
1176 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001177 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1178 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001180 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181
1182 // The fill expression can be omitted while specifying a maximum number of
1183 // alignment bytes, e.g:
1184 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001185 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001186 HasFillExpr = true;
1187 if (ParseAbsoluteExpression(FillExpr))
1188 return true;
1189 }
1190
Daniel Dunbar3f872332009-07-28 16:08:33 +00001191 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1192 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001193 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001194 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001195
1196 MaxBytesLoc = Lexer.getLoc();
1197 if (ParseAbsoluteExpression(MaxBytesToFill))
1198 return true;
1199
Daniel Dunbar3f872332009-07-28 16:08:33 +00001200 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001201 return TokError("unexpected token in directive");
1202 }
1203 }
1204
Sean Callanan79ed1a82010-01-19 20:22:31 +00001205 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001206
1207 if (!HasFillExpr) {
1208 // FIXME: Sometimes fill with nop.
1209 FillExpr = 0;
1210 }
1211
1212 // Compute alignment in bytes.
1213 if (IsPow2) {
1214 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001215 if (Alignment >= 32) {
1216 Error(AlignmentLoc, "invalid alignment value");
1217 Alignment = 31;
1218 }
1219
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001220 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001221 }
1222
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001223 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224 if (MaxBytesLoc.isValid()) {
1225 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001226 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1227 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001228 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 }
1230
1231 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001232 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1233 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001234 MaxBytesToFill = 0;
1235 }
1236 }
1237
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001238 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1239 // the TextAlignFillValue.
1240 if(Out.getCurrentSection()->getKind().isText() &&
1241 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1242 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1243 else
1244 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1245 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001246
1247 return false;
1248}
1249
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001250/// ParseDirectiveSymbolAttribute
1251/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001252bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001253 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001254 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001255 StringRef Name;
1256
1257 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001258 return TokError("expected identifier in directive");
1259
Daniel Dunbar959fd882009-08-26 22:13:22 +00001260 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001261
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001262 Out.EmitSymbolAttribute(Sym, Attr);
1263
Daniel Dunbar3f872332009-07-28 16:08:33 +00001264 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001265 break;
1266
Daniel Dunbar3f872332009-07-28 16:08:33 +00001267 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001268 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001269 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001270 }
1271 }
1272
Sean Callanan79ed1a82010-01-19 20:22:31 +00001273 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001274 return false;
1275}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001276
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001277/// ParseDirectiveDarwinSymbolDesc
1278/// ::= .desc identifier , expression
1279bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001280 StringRef Name;
1281 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001282 return TokError("expected identifier in directive");
1283
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001284 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001285 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001286
Daniel Dunbar3f872332009-07-28 16:08:33 +00001287 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001288 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001289 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001290
1291 SMLoc DescLoc = Lexer.getLoc();
1292 int64_t DescValue;
1293 if (ParseAbsoluteExpression(DescValue))
1294 return true;
1295
Daniel Dunbar3f872332009-07-28 16:08:33 +00001296 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001297 return TokError("unexpected token in '.desc' directive");
1298
Sean Callanan79ed1a82010-01-19 20:22:31 +00001299 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001300
1301 // Set the n_desc field of this Symbol to this DescValue
1302 Out.EmitSymbolDesc(Sym, DescValue);
1303
1304 return false;
1305}
1306
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001307/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001308/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1309bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001310 SMLoc IDLoc = Lexer.getLoc();
1311 StringRef Name;
1312 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001313 return TokError("expected identifier in directive");
1314
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001315 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001316 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001317
Daniel Dunbar3f872332009-07-28 16:08:33 +00001318 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001319 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001320 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001321
1322 int64_t Size;
1323 SMLoc SizeLoc = Lexer.getLoc();
1324 if (ParseAbsoluteExpression(Size))
1325 return true;
1326
1327 int64_t Pow2Alignment = 0;
1328 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001329 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001330 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001331 Pow2AlignmentLoc = Lexer.getLoc();
1332 if (ParseAbsoluteExpression(Pow2Alignment))
1333 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001334
1335 // If this target takes alignments in bytes (not log) validate and convert.
1336 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1337 if (!isPowerOf2_64(Pow2Alignment))
1338 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1339 Pow2Alignment = Log2_64(Pow2Alignment);
1340 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001341 }
1342
Daniel Dunbar3f872332009-07-28 16:08:33 +00001343 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001344 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001345
Sean Callanan79ed1a82010-01-19 20:22:31 +00001346 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001347
Chris Lattner1fc3d752009-07-09 17:25:12 +00001348 // NOTE: a size of zero for a .comm should create a undefined symbol
1349 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001350 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001351 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1352 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001353
1354 // NOTE: The alignment in the directive is a power of 2 value, the assember
1355 // may internally end up wanting an alignment in bytes.
1356 // FIXME: Diagnose overflow.
1357 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001358 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1359 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001360
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001361 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001362 return Error(IDLoc, "invalid symbol redefinition");
1363
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001364 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001365 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001366 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001367 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1368 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001369 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001370 Sym, Size, 1 << Pow2Alignment);
1371 return false;
1372 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001373
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001374 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001375 return false;
1376}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001377
1378/// ParseDirectiveDarwinZerofill
1379/// ::= .zerofill segname , sectname [, identifier , size_expression [
1380/// , align_expression ]]
1381bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001382 // FIXME: Handle quoted names here.
1383
Daniel Dunbar3f872332009-07-28 16:08:33 +00001384 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001385 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001386 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001388
Daniel Dunbar3f872332009-07-28 16:08:33 +00001389 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001390 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001391 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001392
Daniel Dunbar3f872332009-07-28 16:08:33 +00001393 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001394 return TokError("expected section name after comma in '.zerofill' "
1395 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001396 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001397 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001398
Chris Lattner9be3fee2009-07-10 22:20:30 +00001399 // If this is the end of the line all that was wanted was to create the
1400 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001401 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001402 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001403 Out.EmitZerofill(getMachOSection(Segment, Section,
1404 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001405 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001406 return false;
1407 }
1408
Daniel Dunbar3f872332009-07-28 16:08:33 +00001409 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001410 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001411 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001412
Daniel Dunbar3f872332009-07-28 16:08:33 +00001413 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001414 return TokError("expected identifier in directive");
1415
1416 // handle the identifier as the key symbol.
1417 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001418 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001419 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001420
Daniel Dunbar3f872332009-07-28 16:08:33 +00001421 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001422 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001423 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001424
1425 int64_t Size;
1426 SMLoc SizeLoc = Lexer.getLoc();
1427 if (ParseAbsoluteExpression(Size))
1428 return true;
1429
1430 int64_t Pow2Alignment = 0;
1431 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001432 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001433 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001434 Pow2AlignmentLoc = Lexer.getLoc();
1435 if (ParseAbsoluteExpression(Pow2Alignment))
1436 return true;
1437 }
1438
Daniel Dunbar3f872332009-07-28 16:08:33 +00001439 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001440 return TokError("unexpected token in '.zerofill' directive");
1441
Sean Callanan79ed1a82010-01-19 20:22:31 +00001442 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001443
1444 if (Size < 0)
1445 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1446 "than zero");
1447
1448 // NOTE: The alignment in the directive is a power of 2 value, the assember
1449 // may internally end up wanting an alignment in bytes.
1450 // FIXME: Diagnose overflow.
1451 if (Pow2Alignment < 0)
1452 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1453 "can't be less than zero");
1454
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001455 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001456 return Error(IDLoc, "invalid symbol redefinition");
1457
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001458 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001459 //
1460 // FIXME: Arch specific.
1461 Out.EmitZerofill(getMachOSection(Segment, Section,
1462 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001463 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001464 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001465
1466 return false;
1467}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001468
1469/// ParseDirectiveDarwinSubsectionsViaSymbols
1470/// ::= .subsections_via_symbols
1471bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001472 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001473 return TokError("unexpected token in '.subsections_via_symbols' directive");
1474
Sean Callanan79ed1a82010-01-19 20:22:31 +00001475 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001476
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001477 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001478
1479 return false;
1480}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001481
1482/// ParseDirectiveAbort
1483/// ::= .abort [ "abort_string" ]
1484bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001485 // FIXME: Use loc from directive.
1486 SMLoc Loc = Lexer.getLoc();
1487
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001488 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001489 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1490 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001491 return TokError("expected string in '.abort' directive");
1492
Sean Callanan18b83232010-01-19 21:44:56 +00001493 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001494
Sean Callanan79ed1a82010-01-19 20:22:31 +00001495 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001496 }
1497
Daniel Dunbar3f872332009-07-28 16:08:33 +00001498 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001499 return TokError("unexpected token in '.abort' directive");
1500
Sean Callanan79ed1a82010-01-19 20:22:31 +00001501 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001502
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001503 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001504 if (Str.empty())
1505 Error(Loc, ".abort detected. Assembly stopping.");
1506 else
1507 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001508
1509 return false;
1510}
Kevin Enderby71148242009-07-14 21:35:03 +00001511
1512/// ParseDirectiveLsym
1513/// ::= .lsym identifier , expression
1514bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001515 StringRef Name;
1516 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001517 return TokError("expected identifier in directive");
1518
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001519 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001520 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001521
Daniel Dunbar3f872332009-07-28 16:08:33 +00001522 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001523 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001524 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001525
Daniel Dunbar821e3332009-08-31 08:09:28 +00001526 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001527 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001528 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001529 return true;
1530
Daniel Dunbar3f872332009-07-28 16:08:33 +00001531 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001532 return TokError("unexpected token in '.lsym' directive");
1533
Sean Callanan79ed1a82010-01-19 20:22:31 +00001534 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001535
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001536 // We don't currently support this directive.
1537 //
1538 // FIXME: Diagnostic location!
1539 (void) Sym;
1540 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001541}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001542
1543/// ParseDirectiveInclude
1544/// ::= .include "filename"
1545bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001546 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001547 return TokError("expected string in '.include' directive");
1548
Sean Callanan18b83232010-01-19 21:44:56 +00001549 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001550 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001551 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001552
Daniel Dunbar3f872332009-07-28 16:08:33 +00001553 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001554 return TokError("unexpected token in '.include' directive");
1555
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001556 // Strip the quotes.
1557 Filename = Filename.substr(1, Filename.size()-2);
1558
1559 // Attempt to switch the lexer to the included file before consuming the end
1560 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001561 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001562 PrintMessage(IncludeLoc,
1563 "Could not find include file '" + Filename + "'",
1564 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001565 return true;
1566 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001567
1568 return false;
1569}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001570
1571/// ParseDirectiveDarwinDumpOrLoad
1572/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001573bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001574 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001575 return TokError("expected string in '.dump' or '.load' directive");
1576
Sean Callanan79ed1a82010-01-19 20:22:31 +00001577 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001578
Daniel Dunbar3f872332009-07-28 16:08:33 +00001579 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001580 return TokError("unexpected token in '.dump' or '.load' directive");
1581
Sean Callanan79ed1a82010-01-19 20:22:31 +00001582 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001583
Kevin Enderby5026ae42009-07-20 20:25:37 +00001584 // FIXME: If/when .dump and .load are implemented they will be done in the
1585 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001586 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001587 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001588 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001589 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001590
1591 return false;
1592}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001593
1594/// ParseDirectiveIf
1595/// ::= .if expression
1596bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1597 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001598 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001599
1600 TheCondStack.push_back(TheCondState);
1601 TheCondState.TheCond = AsmCond::IfCond;
1602 if(TheCondState.Ignore) {
1603 EatToEndOfStatement();
1604 }
1605 else {
1606 int64_t ExprValue;
1607 if (ParseAbsoluteExpression(ExprValue))
1608 return true;
1609
1610 if (Lexer.isNot(AsmToken::EndOfStatement))
1611 return TokError("unexpected token in '.if' directive");
1612
Sean Callanan79ed1a82010-01-19 20:22:31 +00001613 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001614
1615 TheCondState.CondMet = ExprValue;
1616 TheCondState.Ignore = !TheCondState.CondMet;
1617 }
1618
1619 return false;
1620}
1621
1622/// ParseDirectiveElseIf
1623/// ::= .elseif expression
1624bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1625 if (TheCondState.TheCond != AsmCond::IfCond &&
1626 TheCondState.TheCond != AsmCond::ElseIfCond)
1627 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1628 " an .elseif");
1629 TheCondState.TheCond = AsmCond::ElseIfCond;
1630
1631 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001632 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001633
1634 bool LastIgnoreState = false;
1635 if (!TheCondStack.empty())
1636 LastIgnoreState = TheCondStack.back().Ignore;
1637 if (LastIgnoreState || TheCondState.CondMet) {
1638 TheCondState.Ignore = true;
1639 EatToEndOfStatement();
1640 }
1641 else {
1642 int64_t ExprValue;
1643 if (ParseAbsoluteExpression(ExprValue))
1644 return true;
1645
1646 if (Lexer.isNot(AsmToken::EndOfStatement))
1647 return TokError("unexpected token in '.elseif' directive");
1648
Sean Callanan79ed1a82010-01-19 20:22:31 +00001649 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001650 TheCondState.CondMet = ExprValue;
1651 TheCondState.Ignore = !TheCondState.CondMet;
1652 }
1653
1654 return false;
1655}
1656
1657/// ParseDirectiveElse
1658/// ::= .else
1659bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1660 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001661 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001662
1663 if (Lexer.isNot(AsmToken::EndOfStatement))
1664 return TokError("unexpected token in '.else' directive");
1665
Sean Callanan79ed1a82010-01-19 20:22:31 +00001666 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001667
1668 if (TheCondState.TheCond != AsmCond::IfCond &&
1669 TheCondState.TheCond != AsmCond::ElseIfCond)
1670 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1671 ".elseif");
1672 TheCondState.TheCond = AsmCond::ElseCond;
1673 bool LastIgnoreState = false;
1674 if (!TheCondStack.empty())
1675 LastIgnoreState = TheCondStack.back().Ignore;
1676 if (LastIgnoreState || TheCondState.CondMet)
1677 TheCondState.Ignore = true;
1678 else
1679 TheCondState.Ignore = false;
1680
1681 return false;
1682}
1683
1684/// ParseDirectiveEndIf
1685/// ::= .endif
1686bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1687 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001688 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001689
1690 if (Lexer.isNot(AsmToken::EndOfStatement))
1691 return TokError("unexpected token in '.endif' directive");
1692
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001694
1695 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1696 TheCondStack.empty())
1697 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1698 ".else");
1699 if (!TheCondStack.empty()) {
1700 TheCondState = TheCondStack.back();
1701 TheCondStack.pop_back();
1702 }
1703
1704 return false;
1705}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001706
1707/// ParseDirectiveFile
1708/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001709bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001710 // FIXME: I'm not sure what this is.
1711 int64_t FileNumber = -1;
1712 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001713 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001714 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001715
1716 if (FileNumber < 1)
1717 return TokError("file number less than one");
1718 }
1719
1720 if (Lexer.isNot(AsmToken::String))
1721 return TokError("unexpected token in '.file' directive");
1722
Chris Lattnerd32e8032010-01-25 19:02:58 +00001723 StringRef Filename = getTok().getString();
1724 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001725 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001726
1727 if (Lexer.isNot(AsmToken::EndOfStatement))
1728 return TokError("unexpected token in '.file' directive");
1729
Chris Lattnerd32e8032010-01-25 19:02:58 +00001730 if (FileNumber == -1)
1731 Out.EmitFileDirective(Filename);
1732 else
1733 Out.EmitDwarfFileDirective(FileNumber, Filename);
1734
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001735 return false;
1736}
1737
1738/// ParseDirectiveLine
1739/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001740bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001741 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1742 if (Lexer.isNot(AsmToken::Integer))
1743 return TokError("unexpected token in '.line' directive");
1744
Sean Callanan18b83232010-01-19 21:44:56 +00001745 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001746 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001747 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001748
1749 // FIXME: Do something with the .line.
1750 }
1751
1752 if (Lexer.isNot(AsmToken::EndOfStatement))
1753 return TokError("unexpected token in '.file' directive");
1754
1755 return false;
1756}
1757
1758
1759/// ParseDirectiveLoc
1760/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001761bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001762 if (Lexer.isNot(AsmToken::Integer))
1763 return TokError("unexpected token in '.loc' directive");
1764
1765 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001766 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001767 (void) FileNumber;
1768 // FIXME: Validate file.
1769
Sean Callanan79ed1a82010-01-19 20:22:31 +00001770 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001771 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1772 if (Lexer.isNot(AsmToken::Integer))
1773 return TokError("unexpected token in '.loc' directive");
1774
Sean Callanan18b83232010-01-19 21:44:56 +00001775 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001776 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001777 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001778
1779 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1780 if (Lexer.isNot(AsmToken::Integer))
1781 return TokError("unexpected token in '.loc' directive");
1782
Sean Callanan18b83232010-01-19 21:44:56 +00001783 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001784 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001786
1787 // FIXME: Do something with the .loc.
1788 }
1789 }
1790
1791 if (Lexer.isNot(AsmToken::EndOfStatement))
1792 return TokError("unexpected token in '.file' directive");
1793
1794 return false;
1795}
1796