blob: 068e506be26642ffcdc25ce35660bd802439cd16 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AsmParser.h"
Daniel Dunbar475839e2009-06-29 20:37:27 +000015
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattner98986712010-01-14 22:21:20 +000021#include "llvm/MC/MCParsedAsmOperand.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000022#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000024#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfffff912009-10-16 01:34:54 +000025#include "llvm/MC/MCValue.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000027#include "llvm/Support/SourceMgr.h"
28#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Chris Lattneraaec2052010-01-19 19:46:13 +000032
33enum { DEFAULT_ADDRSPACE = 0 };
34
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000035// Mach-O section uniquing.
36//
37// FIXME: Figure out where this should live, it should be shared by
38// TargetLoweringObjectFile.
39typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
40
Chris Lattnerebb89b42009-09-27 21:16:52 +000041AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
42 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000043 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
44 CurBuffer(0), SectionUniquingMap(0) {
45 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
46
Chris Lattnerebb89b42009-09-27 21:16:52 +000047 // Debugging directives.
48 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
49 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
50 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
51}
52
53
54
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000055AsmParser::~AsmParser() {
56 // If we have the MachO uniquing map, free it.
57 delete (MachOUniqueMapTy*)SectionUniquingMap;
58}
59
60const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
61 const StringRef &Section,
62 unsigned TypeAndAttributes,
63 unsigned Reserved2,
64 SectionKind Kind) const {
65 // We unique sections by their segment/section pair. The returned section
66 // may not have the same flags as the requested section, if so this should be
67 // diagnosed by the client as an error.
68
69 // Create the map if it doesn't already exist.
70 if (SectionUniquingMap == 0)
71 SectionUniquingMap = new MachOUniqueMapTy();
72 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
73
74 // Form the name to look up.
75 SmallString<64> Name;
76 Name += Segment;
77 Name.push_back(',');
78 Name += Section;
79
80 // Do the lookup, if we have a hit, return it.
81 const MCSectionMachO *&Entry = Map[Name.str()];
82
83 // FIXME: This should validate the type and attributes.
84 if (Entry) return Entry;
85
86 // Otherwise, return a new section.
87 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
88 Reserved2, Kind, Ctx);
89}
90
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000091void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000092 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000093}
94
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000095bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000096 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000097 return true;
98}
99
100bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000101 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000102 return true;
103}
104
Sean Callananbf2013e2010-01-20 23:19:55 +0000105void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
106 const char *Type) const {
107 SrcMgr.PrintMessage(Loc, Msg, Type);
108}
Sean Callananfd0b0282010-01-21 00:19:58 +0000109
110bool AsmParser::EnterIncludeFile(const std::string &Filename) {
111 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
112 if (NewBuf == -1)
113 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000114
Sean Callananfd0b0282010-01-21 00:19:58 +0000115 CurBuffer = NewBuf;
116
117 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
118
119 return false;
120}
121
122const AsmToken &AsmParser::Lex() {
123 const AsmToken *tok = &Lexer.Lex();
124
125 if (tok->is(AsmToken::Eof)) {
126 // If this is the end of an included file, pop the parent file off the
127 // include stack.
128 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
129 if (ParentIncludeLoc != SMLoc()) {
130 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
131 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
132 ParentIncludeLoc.getPointer());
133 tok = &Lexer.Lex();
134 }
135 }
136
137 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000138 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000139
Sean Callananfd0b0282010-01-21 00:19:58 +0000140 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000141}
142
Chris Lattner27aa7d22009-06-21 20:16:42 +0000143bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000144 // Create the initial section.
145 //
146 // FIXME: Support -n.
147 // FIXME: Target hook & command line option for initial section.
148 Out.SwitchSection(getMachOSection("__TEXT", "__text",
149 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
150 0, SectionKind()));
151
152
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000153 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000154 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000155
Chris Lattnerb717fb02009-07-02 21:53:43 +0000156 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000157
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000158 AsmCond StartingCondState = TheCondState;
159
Chris Lattnerb717fb02009-07-02 21:53:43 +0000160 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000161 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000162 // Handle conditional assembly here before calling ParseStatement()
163 if (Lexer.getKind() == AsmToken::Identifier) {
164 // If we have an identifier, handle it as the key symbol.
Sean Callanan18b83232010-01-19 21:44:56 +0000165 AsmToken ID = getTok();
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000166 SMLoc IDLoc = ID.getLoc();
167 StringRef IDVal = ID.getString();
168
169 if (IDVal == ".if" ||
170 IDVal == ".elseif" ||
171 IDVal == ".else" ||
172 IDVal == ".endif") {
173 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
174 continue;
175 HadError = true;
176 EatToEndOfStatement();
177 continue;
178 }
179 }
180 if (TheCondState.Ignore) {
181 EatToEndOfStatement();
182 continue;
183 }
184
Chris Lattnerb717fb02009-07-02 21:53:43 +0000185 if (!ParseStatement()) continue;
186
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000187 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000188 HadError = true;
189 EatToEndOfStatement();
190 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000191
192 if (TheCondState.TheCond != StartingCondState.TheCond ||
193 TheCondState.Ignore != StartingCondState.Ignore)
194 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000195
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000196 if (!HadError)
197 Out.Finish();
198
Chris Lattnerb717fb02009-07-02 21:53:43 +0000199 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000200}
201
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000202/// ParseConditionalAssemblyDirectives - parse the conditional assembly
203/// directives
204bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
205 SMLoc DirectiveLoc) {
206 if (Directive == ".if")
207 return ParseDirectiveIf(DirectiveLoc);
208 if (Directive == ".elseif")
209 return ParseDirectiveElseIf(DirectiveLoc);
210 if (Directive == ".else")
211 return ParseDirectiveElse(DirectiveLoc);
212 if (Directive == ".endif")
213 return ParseDirectiveEndIf(DirectiveLoc);
214 return true;
215}
216
Chris Lattner2cf5f142009-06-22 01:29:09 +0000217/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
218void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000219 while (Lexer.isNot(AsmToken::EndOfStatement) &&
220 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000221 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000222
223 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000224 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000225 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000226}
227
Chris Lattnerc4193832009-06-22 05:51:26 +0000228
Chris Lattner74ec1a32009-06-22 06:32:03 +0000229/// ParseParenExpr - Parse a paren expression and return it.
230/// NOTE: This assumes the leading '(' has already been consumed.
231///
232/// parenexpr ::= expr)
233///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000234bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000235 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000236 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000237 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000238 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000239 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000240 return false;
241}
Chris Lattnerc4193832009-06-22 05:51:26 +0000242
Daniel Dunbar959fd882009-08-26 22:13:22 +0000243MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
244 if (MCSymbol *S = Ctx.LookupSymbol(Name))
245 return S;
246
247 // If the label starts with L it is an assembler temporary label.
248 if (Name.startswith("L"))
249 return Ctx.CreateTemporarySymbol(Name);
250
251 return Ctx.CreateSymbol(Name);
252}
253
Chris Lattner74ec1a32009-06-22 06:32:03 +0000254/// ParsePrimaryExpr - Parse a primary expression and return it.
255/// primaryexpr ::= (parenexpr
256/// primaryexpr ::= symbol
257/// primaryexpr ::= number
258/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000259bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000260 switch (Lexer.getKind()) {
261 default:
262 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000263 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000264 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000265 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000266 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000267 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000268 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000269 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000270 case AsmToken::Identifier: {
271 // This is a symbol reference.
Sean Callanan18b83232010-01-19 21:44:56 +0000272 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000273 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000274 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000275
276 // If this is an absolute variable reference, substitute it now to preserve
277 // semantics in the face of reassignment.
278 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
279 Res = Sym->getValue();
280 return false;
281 }
282
283 // Otherwise create a symbol ref.
284 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000285 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000286 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000287 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000288 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000289 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000290 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000291 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000292 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000293 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000294 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000296 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000297 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000299 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000300 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000302 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000303 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000305 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000306 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000307 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000308 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000309 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000311 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000312 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000313 }
314}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000315
Chris Lattnerb4307b32010-01-15 19:28:38 +0000316bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000317 SMLoc EndLoc;
318 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000319}
320
Chris Lattner74ec1a32009-06-22 06:32:03 +0000321/// ParseExpression - Parse an expression and return it.
322///
323/// expr ::= expr +,- expr -> lowest.
324/// expr ::= expr |,^,&,! expr -> middle.
325/// expr ::= expr *,/,%,<<,>> expr -> highest.
326/// expr ::= primaryexpr
327///
Chris Lattner54482b42010-01-15 19:39:23 +0000328bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 Res = 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000330 return ParsePrimaryExpr(Res, EndLoc) ||
331 ParseBinOpRHS(1, Res, EndLoc);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000332}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000333
Chris Lattnerb4307b32010-01-15 19:28:38 +0000334bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
335 if (ParseParenExpr(Res, EndLoc))
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000336 return true;
337
338 return false;
339}
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")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000706 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000708 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000710 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000712 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000714 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000716 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000718 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000720 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000722 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000724 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000726 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000728 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
729
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.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000911 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
912 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000913 return false;
914}
915
Chris Lattnere15c2d72009-08-10 18:05:55 +0000916/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000917bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
918 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000919 unsigned TAA, unsigned Align,
920 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000921 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000922 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000923 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000924
Chris Lattner56594f92009-07-31 17:47:16 +0000925 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000926 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
927 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000928
929 // Set the implicit alignment, if any.
930 //
931 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
932 // alignment on the section (e.g., if one manually inserts bytes into the
933 // section, then just issueing the section switch directive will not realign
934 // the section. However, this is arguably more reasonable behavior, and there
935 // is no good reason for someone to intentionally emit incorrectly sized
936 // values into the implicitly aligned sections.
937 if (Align)
938 Out.EmitValueToAlignment(Align, 0, 1, 0);
939
Chris Lattner529fb542009-06-24 05:13:15 +0000940 return false;
941}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000942
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000943bool AsmParser::ParseEscapedString(std::string &Data) {
944 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
945
946 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000947 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000948 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
949 if (Str[i] != '\\') {
950 Data += Str[i];
951 continue;
952 }
953
954 // Recognize escaped characters. Note that this escape semantics currently
955 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
956 ++i;
957 if (i == e)
958 return TokError("unexpected backslash at end of string");
959
960 // Recognize octal sequences.
961 if ((unsigned) (Str[i] - '0') <= 7) {
962 // Consume up to three octal characters.
963 unsigned Value = Str[i] - '0';
964
965 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
966 ++i;
967 Value = Value * 8 + (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 }
974
975 if (Value > 255)
976 return TokError("invalid octal escape sequence (out of range)");
977
978 Data += (unsigned char) Value;
979 continue;
980 }
981
982 // Otherwise recognize individual escapes.
983 switch (Str[i]) {
984 default:
985 // Just reject invalid escape sequences for now.
986 return TokError("invalid escape sequence (unrecognized character)");
987
988 case 'b': Data += '\b'; break;
989 case 'f': Data += '\f'; break;
990 case 'n': Data += '\n'; break;
991 case 'r': Data += '\r'; break;
992 case 't': Data += '\t'; break;
993 case '"': Data += '"'; break;
994 case '\\': Data += '\\'; break;
995 }
996 }
997
998 return false;
999}
1000
Daniel Dunbara0d14262009-06-24 23:30:00 +00001001/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001002/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001003bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001004 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001005 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001006 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 return TokError("expected string in '.ascii' or '.asciz' directive");
1008
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001009 std::string Data;
1010 if (ParseEscapedString(Data))
1011 return true;
1012
Chris Lattneraaec2052010-01-19 19:46:13 +00001013 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001014 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001015 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001016
Sean Callanan79ed1a82010-01-19 20:22:31 +00001017 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001018
Daniel Dunbar3f872332009-07-28 16:08:33 +00001019 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 break;
1021
Daniel Dunbar3f872332009-07-28 16:08:33 +00001022 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001024 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001025 }
1026 }
1027
Sean Callanan79ed1a82010-01-19 20:22:31 +00001028 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return false;
1030}
1031
1032/// ParseDirectiveValue
1033/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1034bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001035 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001036 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001037 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001038 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001039 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001040 return true;
1041
Chris Lattneraaec2052010-01-19 19:46:13 +00001042 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043
Daniel Dunbar3f872332009-07-28 16:08:33 +00001044 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045 break;
1046
1047 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001050 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 }
1052 }
1053
Sean Callanan79ed1a82010-01-19 20:22:31 +00001054 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 return false;
1056}
1057
1058/// ParseDirectiveSpace
1059/// ::= .space expression [ , expression ]
1060bool AsmParser::ParseDirectiveSpace() {
1061 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001062 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063 return true;
1064
1065 int64_t FillExpr = 0;
1066 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001067 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1068 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001069 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001070 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071
Daniel Dunbar475839e2009-06-29 20:37:27 +00001072 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073 return true;
1074
1075 HasFillExpr = true;
1076
Daniel Dunbar3f872332009-07-28 16:08:33 +00001077 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078 return TokError("unexpected token in '.space' directive");
1079 }
1080
Sean Callanan79ed1a82010-01-19 20:22:31 +00001081 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082
1083 if (NumBytes <= 0)
1084 return TokError("invalid number of bytes in '.space' directive");
1085
1086 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001087 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088
1089 return false;
1090}
1091
1092/// ParseDirectiveFill
1093/// ::= .fill expression , expression , expression
1094bool AsmParser::ParseDirectiveFill() {
1095 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001096 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001097 return true;
1098
Daniel Dunbar3f872332009-07-28 16:08:33 +00001099 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001100 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001101 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001102
1103 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001104 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001105 return true;
1106
Daniel Dunbar3f872332009-07-28 16:08:33 +00001107 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001108 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001109 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001110
1111 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001112 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113 return true;
1114
Daniel Dunbar3f872332009-07-28 16:08:33 +00001115 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001116 return TokError("unexpected token in '.fill' directive");
1117
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001120 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1121 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122
1123 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001124 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1125 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126
1127 return false;
1128}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001129
1130/// ParseDirectiveOrg
1131/// ::= .org expression [ , expression ]
1132bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001133 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001134 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001135 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001136 return true;
1137
1138 // Parse optional fill expression.
1139 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001140 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1141 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001142 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001143 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001144
Daniel Dunbar475839e2009-06-29 20:37:27 +00001145 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001146 return true;
1147
Daniel Dunbar3f872332009-07-28 16:08:33 +00001148 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001149 return TokError("unexpected token in '.org' directive");
1150 }
1151
Sean Callanan79ed1a82010-01-19 20:22:31 +00001152 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001153
1154 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1155 // has to be relative to the current section.
1156 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001157
1158 return false;
1159}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001160
1161/// ParseDirectiveAlign
1162/// ::= {.align, ...} expression [ , expression [ , expression ]]
1163bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001164 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001165 int64_t Alignment;
1166 if (ParseAbsoluteExpression(Alignment))
1167 return true;
1168
1169 SMLoc MaxBytesLoc;
1170 bool HasFillExpr = false;
1171 int64_t FillExpr = 0;
1172 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001173 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1174 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001175 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001176 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177
1178 // The fill expression can be omitted while specifying a maximum number of
1179 // alignment bytes, e.g:
1180 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001181 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001182 HasFillExpr = true;
1183 if (ParseAbsoluteExpression(FillExpr))
1184 return true;
1185 }
1186
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1188 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001189 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001190 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001191
1192 MaxBytesLoc = Lexer.getLoc();
1193 if (ParseAbsoluteExpression(MaxBytesToFill))
1194 return true;
1195
Daniel Dunbar3f872332009-07-28 16:08:33 +00001196 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001197 return TokError("unexpected token in directive");
1198 }
1199 }
1200
Sean Callanan79ed1a82010-01-19 20:22:31 +00001201 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001202
1203 if (!HasFillExpr) {
1204 // FIXME: Sometimes fill with nop.
1205 FillExpr = 0;
1206 }
1207
1208 // Compute alignment in bytes.
1209 if (IsPow2) {
1210 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001211 if (Alignment >= 32) {
1212 Error(AlignmentLoc, "invalid alignment value");
1213 Alignment = 31;
1214 }
1215
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001216 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001217 }
1218
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001219 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001220 if (MaxBytesLoc.isValid()) {
1221 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001222 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1223 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001224 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001225 }
1226
1227 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001228 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1229 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001230 MaxBytesToFill = 0;
1231 }
1232 }
1233
1234 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1235 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1236
1237 return false;
1238}
1239
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001240/// ParseDirectiveSymbolAttribute
1241/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1242bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001243 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001244 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001245 StringRef Name;
1246
1247 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001248 return TokError("expected identifier in directive");
1249
Daniel Dunbar959fd882009-08-26 22:13:22 +00001250 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001251
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001252 Out.EmitSymbolAttribute(Sym, Attr);
1253
Daniel Dunbar3f872332009-07-28 16:08:33 +00001254 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001255 break;
1256
Daniel Dunbar3f872332009-07-28 16:08:33 +00001257 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001258 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001259 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001260 }
1261 }
1262
Sean Callanan79ed1a82010-01-19 20:22:31 +00001263 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001264 return false;
1265}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001266
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001267/// ParseDirectiveDarwinSymbolDesc
1268/// ::= .desc identifier , expression
1269bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001270 StringRef Name;
1271 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001272 return TokError("expected identifier in directive");
1273
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001274 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001275 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001276
Daniel Dunbar3f872332009-07-28 16:08:33 +00001277 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001278 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001279 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001280
1281 SMLoc DescLoc = Lexer.getLoc();
1282 int64_t DescValue;
1283 if (ParseAbsoluteExpression(DescValue))
1284 return true;
1285
Daniel Dunbar3f872332009-07-28 16:08:33 +00001286 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001287 return TokError("unexpected token in '.desc' directive");
1288
Sean Callanan79ed1a82010-01-19 20:22:31 +00001289 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001290
1291 // Set the n_desc field of this Symbol to this DescValue
1292 Out.EmitSymbolDesc(Sym, DescValue);
1293
1294 return false;
1295}
1296
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001297/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001298/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1299bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001300 SMLoc IDLoc = Lexer.getLoc();
1301 StringRef Name;
1302 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001303 return TokError("expected identifier in directive");
1304
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001305 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001306 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001307
Daniel Dunbar3f872332009-07-28 16:08:33 +00001308 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001309 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001310 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001311
1312 int64_t Size;
1313 SMLoc SizeLoc = Lexer.getLoc();
1314 if (ParseAbsoluteExpression(Size))
1315 return true;
1316
1317 int64_t Pow2Alignment = 0;
1318 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001319 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001320 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001321 Pow2AlignmentLoc = Lexer.getLoc();
1322 if (ParseAbsoluteExpression(Pow2Alignment))
1323 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001324
1325 // If this target takes alignments in bytes (not log) validate and convert.
1326 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1327 if (!isPowerOf2_64(Pow2Alignment))
1328 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1329 Pow2Alignment = Log2_64(Pow2Alignment);
1330 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001331 }
1332
Daniel Dunbar3f872332009-07-28 16:08:33 +00001333 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001334 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001335
Sean Callanan79ed1a82010-01-19 20:22:31 +00001336 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001337
Chris Lattner1fc3d752009-07-09 17:25:12 +00001338 // NOTE: a size of zero for a .comm should create a undefined symbol
1339 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001340 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001341 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1342 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001343
1344 // NOTE: The alignment in the directive is a power of 2 value, the assember
1345 // may internally end up wanting an alignment in bytes.
1346 // FIXME: Diagnose overflow.
1347 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001348 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1349 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001350
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001351 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001352 return Error(IDLoc, "invalid symbol redefinition");
1353
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001354 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001355 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001356 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001357 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1358 MCSectionMachO::S_ZEROFILL, 0,
1359 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001360 Sym, Size, 1 << Pow2Alignment);
1361 return false;
1362 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001363
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001364 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001365 return false;
1366}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001367
1368/// ParseDirectiveDarwinZerofill
1369/// ::= .zerofill segname , sectname [, identifier , size_expression [
1370/// , align_expression ]]
1371bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001372 // FIXME: Handle quoted names here.
1373
Daniel Dunbar3f872332009-07-28 16:08:33 +00001374 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001375 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001376 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001377 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001378
Daniel Dunbar3f872332009-07-28 16:08:33 +00001379 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001380 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001381 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001382
Daniel Dunbar3f872332009-07-28 16:08:33 +00001383 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001384 return TokError("expected section name after comma in '.zerofill' "
1385 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001386 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001388
Chris Lattner9be3fee2009-07-10 22:20:30 +00001389 // If this is the end of the line all that was wanted was to create the
1390 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001391 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001392 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001393 Out.EmitZerofill(getMachOSection(Segment, Section,
1394 MCSectionMachO::S_ZEROFILL, 0,
1395 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001396 return false;
1397 }
1398
Daniel Dunbar3f872332009-07-28 16:08:33 +00001399 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001400 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001401 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001402
Daniel Dunbar3f872332009-07-28 16:08:33 +00001403 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001404 return TokError("expected identifier in directive");
1405
1406 // handle the identifier as the key symbol.
1407 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001408 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001409 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001410
Daniel Dunbar3f872332009-07-28 16:08:33 +00001411 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001412 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001414
1415 int64_t Size;
1416 SMLoc SizeLoc = Lexer.getLoc();
1417 if (ParseAbsoluteExpression(Size))
1418 return true;
1419
1420 int64_t Pow2Alignment = 0;
1421 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001422 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001423 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001424 Pow2AlignmentLoc = Lexer.getLoc();
1425 if (ParseAbsoluteExpression(Pow2Alignment))
1426 return true;
1427 }
1428
Daniel Dunbar3f872332009-07-28 16:08:33 +00001429 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001430 return TokError("unexpected token in '.zerofill' directive");
1431
Sean Callanan79ed1a82010-01-19 20:22:31 +00001432 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001433
1434 if (Size < 0)
1435 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1436 "than zero");
1437
1438 // NOTE: The alignment in the directive is a power of 2 value, the assember
1439 // may internally end up wanting an alignment in bytes.
1440 // FIXME: Diagnose overflow.
1441 if (Pow2Alignment < 0)
1442 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1443 "can't be less than zero");
1444
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001445 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001446 return Error(IDLoc, "invalid symbol redefinition");
1447
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001448 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001449 //
1450 // FIXME: Arch specific.
1451 Out.EmitZerofill(getMachOSection(Segment, Section,
1452 MCSectionMachO::S_ZEROFILL, 0,
1453 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001454 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001455
1456 return false;
1457}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001458
1459/// ParseDirectiveDarwinSubsectionsViaSymbols
1460/// ::= .subsections_via_symbols
1461bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001462 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001463 return TokError("unexpected token in '.subsections_via_symbols' directive");
1464
Sean Callanan79ed1a82010-01-19 20:22:31 +00001465 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001466
Kevin Enderbyf96db462009-07-16 17:56:39 +00001467 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001468
1469 return false;
1470}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001471
1472/// ParseDirectiveAbort
1473/// ::= .abort [ "abort_string" ]
1474bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001475 // FIXME: Use loc from directive.
1476 SMLoc Loc = Lexer.getLoc();
1477
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001478 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001479 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1480 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001481 return TokError("expected string in '.abort' directive");
1482
Sean Callanan18b83232010-01-19 21:44:56 +00001483 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001484
Sean Callanan79ed1a82010-01-19 20:22:31 +00001485 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001486 }
1487
Daniel Dunbar3f872332009-07-28 16:08:33 +00001488 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001489 return TokError("unexpected token in '.abort' directive");
1490
Sean Callanan79ed1a82010-01-19 20:22:31 +00001491 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001492
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001493 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001494 if (Str.empty())
1495 Error(Loc, ".abort detected. Assembly stopping.");
1496 else
1497 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001498
1499 return false;
1500}
Kevin Enderby71148242009-07-14 21:35:03 +00001501
1502/// ParseDirectiveLsym
1503/// ::= .lsym identifier , expression
1504bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001505 StringRef Name;
1506 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001507 return TokError("expected identifier in directive");
1508
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001509 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001510 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001511
Daniel Dunbar3f872332009-07-28 16:08:33 +00001512 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001513 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001514 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001515
Daniel Dunbar821e3332009-08-31 08:09:28 +00001516 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001517 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001518 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001519 return true;
1520
Daniel Dunbar3f872332009-07-28 16:08:33 +00001521 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001522 return TokError("unexpected token in '.lsym' directive");
1523
Sean Callanan79ed1a82010-01-19 20:22:31 +00001524 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001525
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001526 // We don't currently support this directive.
1527 //
1528 // FIXME: Diagnostic location!
1529 (void) Sym;
1530 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001531}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001532
1533/// ParseDirectiveInclude
1534/// ::= .include "filename"
1535bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001536 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001537 return TokError("expected string in '.include' directive");
1538
Sean Callanan18b83232010-01-19 21:44:56 +00001539 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001540 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001541 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001542
Daniel Dunbar3f872332009-07-28 16:08:33 +00001543 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001544 return TokError("unexpected token in '.include' directive");
1545
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001546 // Strip the quotes.
1547 Filename = Filename.substr(1, Filename.size()-2);
1548
1549 // Attempt to switch the lexer to the included file before consuming the end
1550 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001551 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001552 PrintMessage(IncludeLoc,
1553 "Could not find include file '" + Filename + "'",
1554 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001555 return true;
1556 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001557
1558 return false;
1559}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001560
1561/// ParseDirectiveDarwinDumpOrLoad
1562/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001563bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001564 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001565 return TokError("expected string in '.dump' or '.load' directive");
1566
Sean Callanan79ed1a82010-01-19 20:22:31 +00001567 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001568
Daniel Dunbar3f872332009-07-28 16:08:33 +00001569 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001570 return TokError("unexpected token in '.dump' or '.load' directive");
1571
Sean Callanan79ed1a82010-01-19 20:22:31 +00001572 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001573
Kevin Enderby5026ae42009-07-20 20:25:37 +00001574 // FIXME: If/when .dump and .load are implemented they will be done in the
1575 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001576 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001577 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001578 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001579 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001580
1581 return false;
1582}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001583
1584/// ParseDirectiveIf
1585/// ::= .if expression
1586bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1587 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001588 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001589
1590 TheCondStack.push_back(TheCondState);
1591 TheCondState.TheCond = AsmCond::IfCond;
1592 if(TheCondState.Ignore) {
1593 EatToEndOfStatement();
1594 }
1595 else {
1596 int64_t ExprValue;
1597 if (ParseAbsoluteExpression(ExprValue))
1598 return true;
1599
1600 if (Lexer.isNot(AsmToken::EndOfStatement))
1601 return TokError("unexpected token in '.if' directive");
1602
Sean Callanan79ed1a82010-01-19 20:22:31 +00001603 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001604
1605 TheCondState.CondMet = ExprValue;
1606 TheCondState.Ignore = !TheCondState.CondMet;
1607 }
1608
1609 return false;
1610}
1611
1612/// ParseDirectiveElseIf
1613/// ::= .elseif expression
1614bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1615 if (TheCondState.TheCond != AsmCond::IfCond &&
1616 TheCondState.TheCond != AsmCond::ElseIfCond)
1617 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1618 " an .elseif");
1619 TheCondState.TheCond = AsmCond::ElseIfCond;
1620
1621 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001622 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001623
1624 bool LastIgnoreState = false;
1625 if (!TheCondStack.empty())
1626 LastIgnoreState = TheCondStack.back().Ignore;
1627 if (LastIgnoreState || TheCondState.CondMet) {
1628 TheCondState.Ignore = true;
1629 EatToEndOfStatement();
1630 }
1631 else {
1632 int64_t ExprValue;
1633 if (ParseAbsoluteExpression(ExprValue))
1634 return true;
1635
1636 if (Lexer.isNot(AsmToken::EndOfStatement))
1637 return TokError("unexpected token in '.elseif' directive");
1638
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001640 TheCondState.CondMet = ExprValue;
1641 TheCondState.Ignore = !TheCondState.CondMet;
1642 }
1643
1644 return false;
1645}
1646
1647/// ParseDirectiveElse
1648/// ::= .else
1649bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1650 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001651 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001652
1653 if (Lexer.isNot(AsmToken::EndOfStatement))
1654 return TokError("unexpected token in '.else' directive");
1655
Sean Callanan79ed1a82010-01-19 20:22:31 +00001656 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001657
1658 if (TheCondState.TheCond != AsmCond::IfCond &&
1659 TheCondState.TheCond != AsmCond::ElseIfCond)
1660 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1661 ".elseif");
1662 TheCondState.TheCond = AsmCond::ElseCond;
1663 bool LastIgnoreState = false;
1664 if (!TheCondStack.empty())
1665 LastIgnoreState = TheCondStack.back().Ignore;
1666 if (LastIgnoreState || TheCondState.CondMet)
1667 TheCondState.Ignore = true;
1668 else
1669 TheCondState.Ignore = false;
1670
1671 return false;
1672}
1673
1674/// ParseDirectiveEndIf
1675/// ::= .endif
1676bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1677 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001678 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001679
1680 if (Lexer.isNot(AsmToken::EndOfStatement))
1681 return TokError("unexpected token in '.endif' directive");
1682
Sean Callanan79ed1a82010-01-19 20:22:31 +00001683 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001684
1685 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1686 TheCondStack.empty())
1687 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1688 ".else");
1689 if (!TheCondStack.empty()) {
1690 TheCondState = TheCondStack.back();
1691 TheCondStack.pop_back();
1692 }
1693
1694 return false;
1695}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001696
1697/// ParseDirectiveFile
1698/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001699bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001700 // FIXME: I'm not sure what this is.
1701 int64_t FileNumber = -1;
1702 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001703 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001704 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001705
1706 if (FileNumber < 1)
1707 return TokError("file number less than one");
1708 }
1709
1710 if (Lexer.isNot(AsmToken::String))
1711 return TokError("unexpected token in '.file' directive");
1712
Sean Callanan18b83232010-01-19 21:44:56 +00001713 StringRef ATTRIBUTE_UNUSED FileName = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001714 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001715
1716 if (Lexer.isNot(AsmToken::EndOfStatement))
1717 return TokError("unexpected token in '.file' directive");
1718
1719 // FIXME: Do something with the .file.
1720
1721 return false;
1722}
1723
1724/// ParseDirectiveLine
1725/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001726bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001727 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1728 if (Lexer.isNot(AsmToken::Integer))
1729 return TokError("unexpected token in '.line' directive");
1730
Sean Callanan18b83232010-01-19 21:44:56 +00001731 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001732 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001733 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001734
1735 // FIXME: Do something with the .line.
1736 }
1737
1738 if (Lexer.isNot(AsmToken::EndOfStatement))
1739 return TokError("unexpected token in '.file' directive");
1740
1741 return false;
1742}
1743
1744
1745/// ParseDirectiveLoc
1746/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001747bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001748 if (Lexer.isNot(AsmToken::Integer))
1749 return TokError("unexpected token in '.loc' directive");
1750
1751 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001752 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001753 (void) FileNumber;
1754 // FIXME: Validate file.
1755
Sean Callanan79ed1a82010-01-19 20:22:31 +00001756 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001757 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1758 if (Lexer.isNot(AsmToken::Integer))
1759 return TokError("unexpected token in '.loc' directive");
1760
Sean Callanan18b83232010-01-19 21:44:56 +00001761 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001762 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001763 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001764
1765 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1766 if (Lexer.isNot(AsmToken::Integer))
1767 return TokError("unexpected token in '.loc' directive");
1768
Sean Callanan18b83232010-01-19 21:44:56 +00001769 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001770 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001771 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001772
1773 // FIXME: Do something with the .loc.
1774 }
1775 }
1776
1777 if (Lexer.isNot(AsmToken::EndOfStatement))
1778 return TokError("unexpected token in '.file' directive");
1779
1780 return false;
1781}
1782