blob: dd438b76124f4ae1880674cc790f28a6a98421a0 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000016#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000018#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000019#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000022#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfffff912009-10-16 01:34:54 +000023#include "llvm/MC/MCValue.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
27#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000028#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000029using namespace llvm;
30
Chris Lattneraaec2052010-01-19 19:46:13 +000031
32enum { DEFAULT_ADDRSPACE = 0 };
33
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000034// Mach-O section uniquing.
35//
36// FIXME: Figure out where this should live, it should be shared by
37// TargetLoweringObjectFile.
38typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
39
Chris Lattnerebb89b42009-09-27 21:16:52 +000040AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
41 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000042 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
43 CurBuffer(0), SectionUniquingMap(0) {
44 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
45
Chris Lattnerebb89b42009-09-27 21:16:52 +000046 // Debugging directives.
47 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
48 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
49 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
50}
51
52
53
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000054AsmParser::~AsmParser() {
55 // If we have the MachO uniquing map, free it.
56 delete (MachOUniqueMapTy*)SectionUniquingMap;
57}
58
59const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
60 const StringRef &Section,
61 unsigned TypeAndAttributes,
62 unsigned Reserved2,
63 SectionKind Kind) const {
64 // We unique sections by their segment/section pair. The returned section
65 // may not have the same flags as the requested section, if so this should be
66 // diagnosed by the client as an error.
67
68 // Create the map if it doesn't already exist.
69 if (SectionUniquingMap == 0)
70 SectionUniquingMap = new MachOUniqueMapTy();
71 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
72
73 // Form the name to look up.
74 SmallString<64> Name;
75 Name += Segment;
76 Name.push_back(',');
77 Name += Section;
78
79 // Do the lookup, if we have a hit, return it.
80 const MCSectionMachO *&Entry = Map[Name.str()];
81
82 // FIXME: This should validate the type and attributes.
83 if (Entry) return Entry;
84
85 // Otherwise, return a new section.
86 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
87 Reserved2, Kind, Ctx);
88}
89
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000090void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000091 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000092}
93
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000094bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000095 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000096 return true;
97}
98
99bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000100 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000101 return true;
102}
103
Sean Callananbf2013e2010-01-20 23:19:55 +0000104void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
105 const char *Type) const {
106 SrcMgr.PrintMessage(Loc, Msg, Type);
107}
Sean Callananfd0b0282010-01-21 00:19:58 +0000108
109bool AsmParser::EnterIncludeFile(const std::string &Filename) {
110 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
111 if (NewBuf == -1)
112 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000113
Sean Callananfd0b0282010-01-21 00:19:58 +0000114 CurBuffer = NewBuf;
115
116 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
117
118 return false;
119}
120
121const AsmToken &AsmParser::Lex() {
122 const AsmToken *tok = &Lexer.Lex();
123
124 if (tok->is(AsmToken::Eof)) {
125 // If this is the end of an included file, pop the parent file off the
126 // include stack.
127 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
128 if (ParentIncludeLoc != SMLoc()) {
129 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
130 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
131 ParentIncludeLoc.getPointer());
132 tok = &Lexer.Lex();
133 }
134 }
135
136 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000137 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000138
Sean Callananfd0b0282010-01-21 00:19:58 +0000139 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000140}
141
Chris Lattner27aa7d22009-06-21 20:16:42 +0000142bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000143 // Create the initial section.
144 //
145 // FIXME: Support -n.
146 // FIXME: Target hook & command line option for initial section.
147 Out.SwitchSection(getMachOSection("__TEXT", "__text",
148 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
149 0, SectionKind()));
150
151
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000152 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000153 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000154
Chris Lattnerb717fb02009-07-02 21:53:43 +0000155 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000156
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000157 AsmCond StartingCondState = TheCondState;
158
Chris Lattnerb717fb02009-07-02 21:53:43 +0000159 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000160 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000161 // Handle conditional assembly here before calling ParseStatement()
162 if (Lexer.getKind() == AsmToken::Identifier) {
163 // If we have an identifier, handle it as the key symbol.
Sean Callanan18b83232010-01-19 21:44:56 +0000164 AsmToken ID = getTok();
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000165 SMLoc IDLoc = ID.getLoc();
166 StringRef IDVal = ID.getString();
167
168 if (IDVal == ".if" ||
169 IDVal == ".elseif" ||
170 IDVal == ".else" ||
171 IDVal == ".endif") {
172 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
173 continue;
174 HadError = true;
175 EatToEndOfStatement();
176 continue;
177 }
178 }
179 if (TheCondState.Ignore) {
180 EatToEndOfStatement();
181 continue;
182 }
183
Chris Lattnerb717fb02009-07-02 21:53:43 +0000184 if (!ParseStatement()) continue;
185
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000186 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000187 HadError = true;
188 EatToEndOfStatement();
189 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000190
191 if (TheCondState.TheCond != StartingCondState.TheCond ||
192 TheCondState.Ignore != StartingCondState.Ignore)
193 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000194
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000195 if (!HadError)
196 Out.Finish();
197
Chris Lattnerb717fb02009-07-02 21:53:43 +0000198 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000199}
200
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000201/// ParseConditionalAssemblyDirectives - parse the conditional assembly
202/// directives
203bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
204 SMLoc DirectiveLoc) {
205 if (Directive == ".if")
206 return ParseDirectiveIf(DirectiveLoc);
207 if (Directive == ".elseif")
208 return ParseDirectiveElseIf(DirectiveLoc);
209 if (Directive == ".else")
210 return ParseDirectiveElse(DirectiveLoc);
211 if (Directive == ".endif")
212 return ParseDirectiveEndIf(DirectiveLoc);
213 return true;
214}
215
Chris Lattner2cf5f142009-06-22 01:29:09 +0000216/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
217void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000218 while (Lexer.isNot(AsmToken::EndOfStatement) &&
219 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000220 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000221
222 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000223 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000224 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000225}
226
Chris Lattnerc4193832009-06-22 05:51:26 +0000227
Chris Lattner74ec1a32009-06-22 06:32:03 +0000228/// ParseParenExpr - Parse a paren expression and return it.
229/// NOTE: This assumes the leading '(' has already been consumed.
230///
231/// parenexpr ::= expr)
232///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000233bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000234 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000235 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000236 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000237 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000238 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000239 return false;
240}
Chris Lattnerc4193832009-06-22 05:51:26 +0000241
Daniel Dunbar959fd882009-08-26 22:13:22 +0000242MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
243 if (MCSymbol *S = Ctx.LookupSymbol(Name))
244 return S;
245
246 // If the label starts with L it is an assembler temporary label.
247 if (Name.startswith("L"))
248 return Ctx.CreateTemporarySymbol(Name);
249
250 return Ctx.CreateSymbol(Name);
251}
252
Chris Lattner74ec1a32009-06-22 06:32:03 +0000253/// ParsePrimaryExpr - Parse a primary expression and return it.
254/// primaryexpr ::= (parenexpr
255/// primaryexpr ::= symbol
256/// primaryexpr ::= number
257/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000258bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000259 switch (Lexer.getKind()) {
260 default:
261 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000262 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000263 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000264 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000266 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000267 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000268 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000269 case AsmToken::Identifier: {
270 // This is a symbol reference.
Sean Callanan18b83232010-01-19 21:44:56 +0000271 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000272 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000273 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000274
275 // If this is an absolute variable reference, substitute it now to preserve
276 // semantics in the face of reassignment.
277 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
278 Res = Sym->getValue();
279 return false;
280 }
281
282 // Otherwise create a symbol ref.
283 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000284 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000285 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000286 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000287 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000288 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000289 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000290 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000291 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000292 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000293 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000294 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000295 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000296 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000297 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000298 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000299 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000300 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000301 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000302 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000303 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000304 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000306 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000307 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000308 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000310 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000311 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000312 }
313}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000314
Chris Lattnerb4307b32010-01-15 19:28:38 +0000315bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000316 SMLoc EndLoc;
317 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000318}
319
Chris Lattner74ec1a32009-06-22 06:32:03 +0000320/// ParseExpression - Parse an expression and return it.
321///
322/// expr ::= expr +,- expr -> lowest.
323/// expr ::= expr |,^,&,! expr -> middle.
324/// expr ::= expr *,/,%,<<,>> expr -> highest.
325/// expr ::= primaryexpr
326///
Chris Lattner54482b42010-01-15 19:39:23 +0000327bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000328 Res = 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000329 return ParsePrimaryExpr(Res, EndLoc) ||
330 ParseBinOpRHS(1, Res, EndLoc);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000331}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000332
Chris Lattnerb4307b32010-01-15 19:28:38 +0000333bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
334 if (ParseParenExpr(Res, EndLoc))
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000335 return true;
336
337 return false;
338}
339
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000341 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000343 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 if (ParseExpression(Expr))
345 return true;
346
Daniel Dunbare00b0112009-10-16 01:57:52 +0000347 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000348 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000349
350 return false;
351}
352
Daniel Dunbar3f872332009-07-28 16:08:33 +0000353static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000354 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000355 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000356 default:
357 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358
359 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000361 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000362 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 1;
366
367 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000368 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000369 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000370 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000372 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000373 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000374 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000375 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000377 case AsmToken::ExclaimEqual:
378 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000381 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000382 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000383 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000384 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000385 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000386 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000387 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000388 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000389 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000390 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000391 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000392 return 2;
393
394 // Intermediate Precedence: |, &, ^
395 //
396 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000397 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000398 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000399 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000400 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000401 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000402 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000403 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000404 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000405 return 3;
406
407 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000408 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000409 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000410 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000412 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000413 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000414 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000416 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000417 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000418 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000419 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000420 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000421 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000422 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000423 }
424}
425
426
427/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
428/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000429bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
430 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000431 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000432 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000433 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000434
435 // If the next token is lower precedence than we are allowed to eat, return
436 // successfully with what we ate already.
437 if (TokPrec < Precedence)
438 return false;
439
Sean Callanan79ed1a82010-01-19 20:22:31 +0000440 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000441
442 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000443 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000444 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000445
446 // If BinOp binds less tightly with RHS than the operator after RHS, let
447 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000448 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000449 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000450 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000451 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000452 }
453
Daniel Dunbar475839e2009-06-29 20:37:27 +0000454 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000455 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000456 }
457}
458
Chris Lattnerc4193832009-06-22 05:51:26 +0000459
460
461
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000462/// ParseStatement:
463/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000464/// ::= Label* Directive ...Operands... EndOfStatement
465/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000466bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000467 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000468 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000469 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000470 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000471
472 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000473 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000474 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000475 StringRef IDVal;
476 if (ParseIdentifier(IDVal))
477 return TokError("unexpected token at start of statement");
478
479 // FIXME: Recurse on local labels?
480
481 // See what kind of statement we have.
482 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000483 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000484 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000485 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000486
487 // Diagnose attempt to use a variable as a label.
488 //
489 // FIXME: Diagnostics. Note the location of the definition as a label.
490 // FIXME: This doesn't diagnose assignment to a symbol which has been
491 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000492 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000493 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000494 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000495
Daniel Dunbar959fd882009-08-26 22:13:22 +0000496 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000497 Out.EmitLabel(Sym);
498
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000499 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000500 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000501
Daniel Dunbar3f872332009-07-28 16:08:33 +0000502 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000503 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000504 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000505
Daniel Dunbare2ace502009-08-31 08:09:09 +0000506 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000507
508 default: // Normal instruction or directive.
509 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000510 }
511
512 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000513 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000514 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000516 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000518 // FIXME: This changes behavior based on the -static flag to the
519 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000520 return ParseDirectiveSectionSwitch("__TEXT", "__text",
521 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000523 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000525 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000527 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
528 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000530 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000531 MCSectionMachO::S_4BYTE_LITERALS,
532 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000534 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 MCSectionMachO::S_8BYTE_LITERALS,
536 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000538 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000539 MCSectionMachO::S_16BYTE_LITERALS,
540 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
549
550 // FIXME: The assembler manual claims that this has the self modify code
551 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000552 if (IDVal == ".symbol_stub")
553 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
554 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000555 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
556 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 0, 16);
558 // FIXME: PowerPC only?
559 if (IDVal == ".picsymbol_stub")
560 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
561 MCSectionMachO::S_SYMBOL_STUBS |
562 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
563 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000564 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000565 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
568
569 // FIXME: The section names of these two are misspelled in the assembler
570 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000571 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000572 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
573 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
574 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
577 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
578 4);
579
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000580 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000581 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000583 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000584 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
585 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000586 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000587 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000588 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
589 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000592
593
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 return ParseDirectiveSectionSwitch("__OBJC", "__class",
596 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000598 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
599 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
602 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
605 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
608 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000610 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
611 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
614 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000616 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
617 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000619 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
620 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
621 MCSectionMachO::S_LITERAL_POINTERS,
622 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000624 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
625 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
626 MCSectionMachO::S_LITERAL_POINTERS,
627 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000629 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
630 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000632 return ParseDirectiveSectionSwitch("__OBJC", "__category",
633 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000635 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
636 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000638 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
639 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
642 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000644 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
645 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000647 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
648 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000650 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
651 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000653 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
654 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000655
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000656 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000658 return ParseDirectiveSet();
659
Daniel Dunbara0d14262009-06-24 23:30:00 +0000660 // Data directives
661
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000663 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000665 return ParseDirectiveAscii(true);
666
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000668 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000669 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000670 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000672 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000674 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000675
676 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000678 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000680 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000682 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000684 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000686 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000688 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000690 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000692 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
693
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000695 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000696
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000698 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000700 return ParseDirectiveSpace();
701
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000702 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000703
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000705 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000707 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000709 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000711 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000713 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000715 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000717 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000719 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000721 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000723 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000725 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000727 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000728
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000730 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000732 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000734 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000736 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000738 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000739
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000741 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000743 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000745 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000747 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000749 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000750
Chris Lattnerebb89b42009-09-27 21:16:52 +0000751 // Look up the handler in the handler table,
752 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
753 if (Handler)
754 return (this->*Handler)(IDVal, IDLoc);
755
Kevin Enderby9c656452009-09-10 20:51:44 +0000756 // Target hook for parsing target specific directives.
757 if (!getTargetParser().ParseDirective(ID))
758 return false;
759
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000760 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000761 EatToEndOfStatement();
762 return false;
763 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000764
Chris Lattner98986712010-01-14 22:21:20 +0000765
766 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
767 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
768 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000769 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000770
Daniel Dunbar3f872332009-07-28 16:08:33 +0000771 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000772 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000773 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000774
775 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000776 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000777
Chris Lattner98986712010-01-14 22:21:20 +0000778
779 MCInst Inst;
780
781 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
782
783 // Free any parsed operands.
784 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
785 delete ParsedOperands[i];
786
787 if (MatchFail) {
788 // FIXME: We should give nicer diagnostics about the exact failure.
789 Error(IDLoc, "unrecognized instruction");
790 return true;
791 }
792
Chris Lattner2cf5f142009-06-22 01:29:09 +0000793 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000794 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000795
796 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000797 return false;
798}
Chris Lattner9a023f72009-06-24 04:43:34 +0000799
Daniel Dunbare2ace502009-08-31 08:09:09 +0000800bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000801 // FIXME: Use better location, we should use proper tokens.
802 SMLoc EqualLoc = Lexer.getLoc();
803
Daniel Dunbar821e3332009-08-31 08:09:28 +0000804 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000805 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000806 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000807 return true;
808
Daniel Dunbar3f872332009-07-28 16:08:33 +0000809 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000810 return TokError("unexpected token in assignment");
811
812 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000813 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000814
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000815 // Validate that the LHS is allowed to be a variable (either it has not been
816 // used as a symbol, or it is an absolute symbol).
817 MCSymbol *Sym = getContext().LookupSymbol(Name);
818 if (Sym) {
819 // Diagnose assignment to a label.
820 //
821 // FIXME: Diagnostics. Note the location of the definition as a label.
822 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
823 if (!Sym->isUndefined() && !Sym->isAbsolute())
824 return Error(EqualLoc, "redefinition of '" + Name + "'");
825 else if (!Sym->isVariable())
826 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
827 else if (!isa<MCConstantExpr>(Sym->getValue()))
828 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
829 Name + "'");
830 } else
831 Sym = CreateSymbol(Name);
832
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000833 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000834
835 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000836 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000837
838 return false;
839}
840
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000841/// ParseIdentifier:
842/// ::= identifier
843/// ::= string
844bool AsmParser::ParseIdentifier(StringRef &Res) {
845 if (Lexer.isNot(AsmToken::Identifier) &&
846 Lexer.isNot(AsmToken::String))
847 return true;
848
Sean Callanan18b83232010-01-19 21:44:56 +0000849 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000850
Sean Callanan79ed1a82010-01-19 20:22:31 +0000851 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000852
853 return false;
854}
855
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000856/// ParseDirectiveSet:
857/// ::= .set identifier ',' expression
858bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000859 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000860
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000861 if (ParseIdentifier(Name))
862 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000863
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000864 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000865 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000866 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000867
Daniel Dunbare2ace502009-08-31 08:09:09 +0000868 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000869}
870
Chris Lattner9a023f72009-06-24 04:43:34 +0000871/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000872/// ::= .section identifier (',' identifier)*
873/// FIXME: This should actually parse out the segment, section, attributes and
874/// sizeof_stub fields.
875bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000876 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000877
Daniel Dunbarace63122009-08-11 03:42:33 +0000878 StringRef SectionName;
879 if (ParseIdentifier(SectionName))
880 return Error(Loc, "expected identifier after '.section' directive");
881
882 // Verify there is a following comma.
883 if (!Lexer.is(AsmToken::Comma))
884 return TokError("unexpected token in '.section' directive");
885
Chris Lattnerff4bc462009-08-10 01:39:42 +0000886 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000887 SectionSpec += ",";
888
889 // Add all the tokens until the end of the line, ParseSectionSpecifier will
890 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000891 StringRef EOL = Lexer.LexUntilEndOfStatement();
892 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000893
Sean Callanan79ed1a82010-01-19 20:22:31 +0000894 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000895 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000896 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000897 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000898
Chris Lattnerff4bc462009-08-10 01:39:42 +0000899
900 StringRef Segment, Section;
901 unsigned TAA, StubSize;
902 std::string ErrorStr =
903 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
904 TAA, StubSize);
905
906 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000907 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000908
Chris Lattner56594f92009-07-31 17:47:16 +0000909 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000910 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
911 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000912 return false;
913}
914
Chris Lattnere15c2d72009-08-10 18:05:55 +0000915/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000916bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
917 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000918 unsigned TAA, unsigned Align,
919 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000920 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000921 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000922 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000923
Chris Lattner56594f92009-07-31 17:47:16 +0000924 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000925 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
926 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000927
928 // Set the implicit alignment, if any.
929 //
930 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
931 // alignment on the section (e.g., if one manually inserts bytes into the
932 // section, then just issueing the section switch directive will not realign
933 // the section. However, this is arguably more reasonable behavior, and there
934 // is no good reason for someone to intentionally emit incorrectly sized
935 // values into the implicitly aligned sections.
936 if (Align)
937 Out.EmitValueToAlignment(Align, 0, 1, 0);
938
Chris Lattner529fb542009-06-24 05:13:15 +0000939 return false;
940}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000941
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000942bool AsmParser::ParseEscapedString(std::string &Data) {
943 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
944
945 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000946 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000947 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
948 if (Str[i] != '\\') {
949 Data += Str[i];
950 continue;
951 }
952
953 // Recognize escaped characters. Note that this escape semantics currently
954 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
955 ++i;
956 if (i == e)
957 return TokError("unexpected backslash at end of string");
958
959 // Recognize octal sequences.
960 if ((unsigned) (Str[i] - '0') <= 7) {
961 // Consume up to three octal characters.
962 unsigned Value = Str[i] - '0';
963
964 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
965 ++i;
966 Value = Value * 8 + (Str[i] - '0');
967
968 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
969 ++i;
970 Value = Value * 8 + (Str[i] - '0');
971 }
972 }
973
974 if (Value > 255)
975 return TokError("invalid octal escape sequence (out of range)");
976
977 Data += (unsigned char) Value;
978 continue;
979 }
980
981 // Otherwise recognize individual escapes.
982 switch (Str[i]) {
983 default:
984 // Just reject invalid escape sequences for now.
985 return TokError("invalid escape sequence (unrecognized character)");
986
987 case 'b': Data += '\b'; break;
988 case 'f': Data += '\f'; break;
989 case 'n': Data += '\n'; break;
990 case 'r': Data += '\r'; break;
991 case 't': Data += '\t'; break;
992 case '"': Data += '"'; break;
993 case '\\': Data += '\\'; break;
994 }
995 }
996
997 return false;
998}
999
Daniel Dunbara0d14262009-06-24 23:30:00 +00001000/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001001/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001003 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001004 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001005 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001006 return TokError("expected string in '.ascii' or '.asciz' directive");
1007
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001008 std::string Data;
1009 if (ParseEscapedString(Data))
1010 return true;
1011
Chris Lattneraaec2052010-01-19 19:46:13 +00001012 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001014 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015
Sean Callanan79ed1a82010-01-19 20:22:31 +00001016 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001017
Daniel Dunbar3f872332009-07-28 16:08:33 +00001018 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001019 break;
1020
Daniel Dunbar3f872332009-07-28 16:08:33 +00001021 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001023 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024 }
1025 }
1026
Sean Callanan79ed1a82010-01-19 20:22:31 +00001027 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 return false;
1029}
1030
1031/// ParseDirectiveValue
1032/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1033bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001034 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001035 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001036 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001037 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001038 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001039 return true;
1040
Chris Lattneraaec2052010-01-19 19:46:13 +00001041 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001042
Daniel Dunbar3f872332009-07-28 16:08:33 +00001043 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001044 break;
1045
1046 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001047 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001049 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001050 }
1051 }
1052
Sean Callanan79ed1a82010-01-19 20:22:31 +00001053 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054 return false;
1055}
1056
1057/// ParseDirectiveSpace
1058/// ::= .space expression [ , expression ]
1059bool AsmParser::ParseDirectiveSpace() {
1060 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001061 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062 return true;
1063
1064 int64_t FillExpr = 0;
1065 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001066 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1067 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001069 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001070
Daniel Dunbar475839e2009-06-29 20:37:27 +00001071 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001072 return true;
1073
1074 HasFillExpr = true;
1075
Daniel Dunbar3f872332009-07-28 16:08:33 +00001076 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return TokError("unexpected token in '.space' directive");
1078 }
1079
Sean Callanan79ed1a82010-01-19 20:22:31 +00001080 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001081
1082 if (NumBytes <= 0)
1083 return TokError("invalid number of bytes in '.space' directive");
1084
1085 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001086 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087
1088 return false;
1089}
1090
1091/// ParseDirectiveFill
1092/// ::= .fill expression , expression , expression
1093bool AsmParser::ParseDirectiveFill() {
1094 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001095 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001096 return true;
1097
Daniel Dunbar3f872332009-07-28 16:08:33 +00001098 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001099 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001100 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001101
1102 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001103 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001104 return true;
1105
Daniel Dunbar3f872332009-07-28 16:08:33 +00001106 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001107 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001108 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001109
1110 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001111 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001112 return true;
1113
Daniel Dunbar3f872332009-07-28 16:08:33 +00001114 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115 return TokError("unexpected token in '.fill' directive");
1116
Sean Callanan79ed1a82010-01-19 20:22:31 +00001117 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001118
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001119 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1120 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001121
1122 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001123 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1124 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001125
1126 return false;
1127}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001128
1129/// ParseDirectiveOrg
1130/// ::= .org expression [ , expression ]
1131bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001132 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001133 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001134 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001135 return true;
1136
1137 // Parse optional fill expression.
1138 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001139 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1140 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001141 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001142 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001143
Daniel Dunbar475839e2009-06-29 20:37:27 +00001144 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001145 return true;
1146
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001148 return TokError("unexpected token in '.org' directive");
1149 }
1150
Sean Callanan79ed1a82010-01-19 20:22:31 +00001151 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001152
1153 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1154 // has to be relative to the current section.
1155 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001156
1157 return false;
1158}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001159
1160/// ParseDirectiveAlign
1161/// ::= {.align, ...} expression [ , expression [ , expression ]]
1162bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001163 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001164 int64_t Alignment;
1165 if (ParseAbsoluteExpression(Alignment))
1166 return true;
1167
1168 SMLoc MaxBytesLoc;
1169 bool HasFillExpr = false;
1170 int64_t FillExpr = 0;
1171 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001172 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1173 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001174 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001175 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001176
1177 // The fill expression can be omitted while specifying a maximum number of
1178 // alignment bytes, e.g:
1179 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001180 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181 HasFillExpr = true;
1182 if (ParseAbsoluteExpression(FillExpr))
1183 return true;
1184 }
1185
Daniel Dunbar3f872332009-07-28 16:08:33 +00001186 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1187 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001188 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001189 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001190
1191 MaxBytesLoc = Lexer.getLoc();
1192 if (ParseAbsoluteExpression(MaxBytesToFill))
1193 return true;
1194
Daniel Dunbar3f872332009-07-28 16:08:33 +00001195 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001196 return TokError("unexpected token in directive");
1197 }
1198 }
1199
Sean Callanan79ed1a82010-01-19 20:22:31 +00001200 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001201
1202 if (!HasFillExpr) {
1203 // FIXME: Sometimes fill with nop.
1204 FillExpr = 0;
1205 }
1206
1207 // Compute alignment in bytes.
1208 if (IsPow2) {
1209 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001210 if (Alignment >= 32) {
1211 Error(AlignmentLoc, "invalid alignment value");
1212 Alignment = 31;
1213 }
1214
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001215 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001216 }
1217
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001218 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001219 if (MaxBytesLoc.isValid()) {
1220 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001221 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1222 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001223 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224 }
1225
1226 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001227 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1228 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 MaxBytesToFill = 0;
1230 }
1231 }
1232
1233 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1234 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1235
1236 return false;
1237}
1238
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001239/// ParseDirectiveSymbolAttribute
1240/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001241bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001242 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001243 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001244 StringRef Name;
1245
1246 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001247 return TokError("expected identifier in directive");
1248
Daniel Dunbar959fd882009-08-26 22:13:22 +00001249 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001250
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001251 Out.EmitSymbolAttribute(Sym, Attr);
1252
Daniel Dunbar3f872332009-07-28 16:08:33 +00001253 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001254 break;
1255
Daniel Dunbar3f872332009-07-28 16:08:33 +00001256 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001257 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001258 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001259 }
1260 }
1261
Sean Callanan79ed1a82010-01-19 20:22:31 +00001262 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001263 return false;
1264}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001265
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001266/// ParseDirectiveDarwinSymbolDesc
1267/// ::= .desc identifier , expression
1268bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001269 StringRef Name;
1270 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001271 return TokError("expected identifier in directive");
1272
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001273 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001274 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001275
Daniel Dunbar3f872332009-07-28 16:08:33 +00001276 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001277 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001278 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001279
1280 SMLoc DescLoc = Lexer.getLoc();
1281 int64_t DescValue;
1282 if (ParseAbsoluteExpression(DescValue))
1283 return true;
1284
Daniel Dunbar3f872332009-07-28 16:08:33 +00001285 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001286 return TokError("unexpected token in '.desc' directive");
1287
Sean Callanan79ed1a82010-01-19 20:22:31 +00001288 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001289
1290 // Set the n_desc field of this Symbol to this DescValue
1291 Out.EmitSymbolDesc(Sym, DescValue);
1292
1293 return false;
1294}
1295
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001296/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001297/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1298bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001299 SMLoc IDLoc = Lexer.getLoc();
1300 StringRef Name;
1301 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001302 return TokError("expected identifier in directive");
1303
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001304 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001305 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001306
Daniel Dunbar3f872332009-07-28 16:08:33 +00001307 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001308 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001309 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001310
1311 int64_t Size;
1312 SMLoc SizeLoc = Lexer.getLoc();
1313 if (ParseAbsoluteExpression(Size))
1314 return true;
1315
1316 int64_t Pow2Alignment = 0;
1317 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001318 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001319 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001320 Pow2AlignmentLoc = Lexer.getLoc();
1321 if (ParseAbsoluteExpression(Pow2Alignment))
1322 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001323
1324 // If this target takes alignments in bytes (not log) validate and convert.
1325 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1326 if (!isPowerOf2_64(Pow2Alignment))
1327 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1328 Pow2Alignment = Log2_64(Pow2Alignment);
1329 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001330 }
1331
Daniel Dunbar3f872332009-07-28 16:08:33 +00001332 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001333 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001334
Sean Callanan79ed1a82010-01-19 20:22:31 +00001335 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001336
Chris Lattner1fc3d752009-07-09 17:25:12 +00001337 // NOTE: a size of zero for a .comm should create a undefined symbol
1338 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001339 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001340 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1341 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001342
1343 // NOTE: The alignment in the directive is a power of 2 value, the assember
1344 // may internally end up wanting an alignment in bytes.
1345 // FIXME: Diagnose overflow.
1346 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001347 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1348 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001349
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001350 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001351 return Error(IDLoc, "invalid symbol redefinition");
1352
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001353 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001354 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001355 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001356 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1357 MCSectionMachO::S_ZEROFILL, 0,
1358 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001359 Sym, Size, 1 << Pow2Alignment);
1360 return false;
1361 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001362
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001363 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001364 return false;
1365}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001366
1367/// ParseDirectiveDarwinZerofill
1368/// ::= .zerofill segname , sectname [, identifier , size_expression [
1369/// , align_expression ]]
1370bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001371 // FIXME: Handle quoted names here.
1372
Daniel Dunbar3f872332009-07-28 16:08:33 +00001373 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001374 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001375 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001376 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001377
Daniel Dunbar3f872332009-07-28 16:08:33 +00001378 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001379 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001380 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001381
Daniel Dunbar3f872332009-07-28 16:08:33 +00001382 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001383 return TokError("expected section name after comma in '.zerofill' "
1384 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001385 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001386 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001387
Chris Lattner9be3fee2009-07-10 22:20:30 +00001388 // If this is the end of the line all that was wanted was to create the
1389 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001390 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001391 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001392 Out.EmitZerofill(getMachOSection(Segment, Section,
1393 MCSectionMachO::S_ZEROFILL, 0,
1394 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001395 return false;
1396 }
1397
Daniel Dunbar3f872332009-07-28 16:08:33 +00001398 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001399 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001400 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001401
Daniel Dunbar3f872332009-07-28 16:08:33 +00001402 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001403 return TokError("expected identifier in directive");
1404
1405 // handle the identifier as the key symbol.
1406 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001407 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001408 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001409
Daniel Dunbar3f872332009-07-28 16:08:33 +00001410 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001411 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001412 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001413
1414 int64_t Size;
1415 SMLoc SizeLoc = Lexer.getLoc();
1416 if (ParseAbsoluteExpression(Size))
1417 return true;
1418
1419 int64_t Pow2Alignment = 0;
1420 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001421 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001422 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001423 Pow2AlignmentLoc = Lexer.getLoc();
1424 if (ParseAbsoluteExpression(Pow2Alignment))
1425 return true;
1426 }
1427
Daniel Dunbar3f872332009-07-28 16:08:33 +00001428 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001429 return TokError("unexpected token in '.zerofill' directive");
1430
Sean Callanan79ed1a82010-01-19 20:22:31 +00001431 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001432
1433 if (Size < 0)
1434 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1435 "than zero");
1436
1437 // NOTE: The alignment in the directive is a power of 2 value, the assember
1438 // may internally end up wanting an alignment in bytes.
1439 // FIXME: Diagnose overflow.
1440 if (Pow2Alignment < 0)
1441 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1442 "can't be less than zero");
1443
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001444 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001445 return Error(IDLoc, "invalid symbol redefinition");
1446
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001447 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001448 //
1449 // FIXME: Arch specific.
1450 Out.EmitZerofill(getMachOSection(Segment, Section,
1451 MCSectionMachO::S_ZEROFILL, 0,
1452 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001453 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001454
1455 return false;
1456}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001457
1458/// ParseDirectiveDarwinSubsectionsViaSymbols
1459/// ::= .subsections_via_symbols
1460bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001461 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001462 return TokError("unexpected token in '.subsections_via_symbols' directive");
1463
Sean Callanan79ed1a82010-01-19 20:22:31 +00001464 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001465
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001466 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001467
1468 return false;
1469}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001470
1471/// ParseDirectiveAbort
1472/// ::= .abort [ "abort_string" ]
1473bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001474 // FIXME: Use loc from directive.
1475 SMLoc Loc = Lexer.getLoc();
1476
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001477 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001478 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1479 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001480 return TokError("expected string in '.abort' directive");
1481
Sean Callanan18b83232010-01-19 21:44:56 +00001482 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001483
Sean Callanan79ed1a82010-01-19 20:22:31 +00001484 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001485 }
1486
Daniel Dunbar3f872332009-07-28 16:08:33 +00001487 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001488 return TokError("unexpected token in '.abort' directive");
1489
Sean Callanan79ed1a82010-01-19 20:22:31 +00001490 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001491
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001492 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001493 if (Str.empty())
1494 Error(Loc, ".abort detected. Assembly stopping.");
1495 else
1496 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001497
1498 return false;
1499}
Kevin Enderby71148242009-07-14 21:35:03 +00001500
1501/// ParseDirectiveLsym
1502/// ::= .lsym identifier , expression
1503bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001504 StringRef Name;
1505 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001506 return TokError("expected identifier in directive");
1507
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001508 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001509 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001510
Daniel Dunbar3f872332009-07-28 16:08:33 +00001511 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001512 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001513 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001514
Daniel Dunbar821e3332009-08-31 08:09:28 +00001515 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001516 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001517 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001518 return true;
1519
Daniel Dunbar3f872332009-07-28 16:08:33 +00001520 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001521 return TokError("unexpected token in '.lsym' directive");
1522
Sean Callanan79ed1a82010-01-19 20:22:31 +00001523 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001524
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001525 // We don't currently support this directive.
1526 //
1527 // FIXME: Diagnostic location!
1528 (void) Sym;
1529 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001530}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001531
1532/// ParseDirectiveInclude
1533/// ::= .include "filename"
1534bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001535 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001536 return TokError("expected string in '.include' directive");
1537
Sean Callanan18b83232010-01-19 21:44:56 +00001538 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001539 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001540 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001541
Daniel Dunbar3f872332009-07-28 16:08:33 +00001542 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001543 return TokError("unexpected token in '.include' directive");
1544
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001545 // Strip the quotes.
1546 Filename = Filename.substr(1, Filename.size()-2);
1547
1548 // Attempt to switch the lexer to the included file before consuming the end
1549 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001550 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001551 PrintMessage(IncludeLoc,
1552 "Could not find include file '" + Filename + "'",
1553 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001554 return true;
1555 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001556
1557 return false;
1558}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001559
1560/// ParseDirectiveDarwinDumpOrLoad
1561/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001562bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001563 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001564 return TokError("expected string in '.dump' or '.load' directive");
1565
Sean Callanan79ed1a82010-01-19 20:22:31 +00001566 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001567
Daniel Dunbar3f872332009-07-28 16:08:33 +00001568 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001569 return TokError("unexpected token in '.dump' or '.load' directive");
1570
Sean Callanan79ed1a82010-01-19 20:22:31 +00001571 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001572
Kevin Enderby5026ae42009-07-20 20:25:37 +00001573 // FIXME: If/when .dump and .load are implemented they will be done in the
1574 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001575 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001576 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001577 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001578 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001579
1580 return false;
1581}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001582
1583/// ParseDirectiveIf
1584/// ::= .if expression
1585bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1586 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001587 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001588
1589 TheCondStack.push_back(TheCondState);
1590 TheCondState.TheCond = AsmCond::IfCond;
1591 if(TheCondState.Ignore) {
1592 EatToEndOfStatement();
1593 }
1594 else {
1595 int64_t ExprValue;
1596 if (ParseAbsoluteExpression(ExprValue))
1597 return true;
1598
1599 if (Lexer.isNot(AsmToken::EndOfStatement))
1600 return TokError("unexpected token in '.if' directive");
1601
Sean Callanan79ed1a82010-01-19 20:22:31 +00001602 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001603
1604 TheCondState.CondMet = ExprValue;
1605 TheCondState.Ignore = !TheCondState.CondMet;
1606 }
1607
1608 return false;
1609}
1610
1611/// ParseDirectiveElseIf
1612/// ::= .elseif expression
1613bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1614 if (TheCondState.TheCond != AsmCond::IfCond &&
1615 TheCondState.TheCond != AsmCond::ElseIfCond)
1616 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1617 " an .elseif");
1618 TheCondState.TheCond = AsmCond::ElseIfCond;
1619
1620 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001621 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001622
1623 bool LastIgnoreState = false;
1624 if (!TheCondStack.empty())
1625 LastIgnoreState = TheCondStack.back().Ignore;
1626 if (LastIgnoreState || TheCondState.CondMet) {
1627 TheCondState.Ignore = true;
1628 EatToEndOfStatement();
1629 }
1630 else {
1631 int64_t ExprValue;
1632 if (ParseAbsoluteExpression(ExprValue))
1633 return true;
1634
1635 if (Lexer.isNot(AsmToken::EndOfStatement))
1636 return TokError("unexpected token in '.elseif' directive");
1637
Sean Callanan79ed1a82010-01-19 20:22:31 +00001638 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001639 TheCondState.CondMet = ExprValue;
1640 TheCondState.Ignore = !TheCondState.CondMet;
1641 }
1642
1643 return false;
1644}
1645
1646/// ParseDirectiveElse
1647/// ::= .else
1648bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1649 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001650 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001651
1652 if (Lexer.isNot(AsmToken::EndOfStatement))
1653 return TokError("unexpected token in '.else' directive");
1654
Sean Callanan79ed1a82010-01-19 20:22:31 +00001655 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001656
1657 if (TheCondState.TheCond != AsmCond::IfCond &&
1658 TheCondState.TheCond != AsmCond::ElseIfCond)
1659 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1660 ".elseif");
1661 TheCondState.TheCond = AsmCond::ElseCond;
1662 bool LastIgnoreState = false;
1663 if (!TheCondStack.empty())
1664 LastIgnoreState = TheCondStack.back().Ignore;
1665 if (LastIgnoreState || TheCondState.CondMet)
1666 TheCondState.Ignore = true;
1667 else
1668 TheCondState.Ignore = false;
1669
1670 return false;
1671}
1672
1673/// ParseDirectiveEndIf
1674/// ::= .endif
1675bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1676 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001677 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001678
1679 if (Lexer.isNot(AsmToken::EndOfStatement))
1680 return TokError("unexpected token in '.endif' directive");
1681
Sean Callanan79ed1a82010-01-19 20:22:31 +00001682 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001683
1684 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1685 TheCondStack.empty())
1686 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1687 ".else");
1688 if (!TheCondStack.empty()) {
1689 TheCondState = TheCondStack.back();
1690 TheCondStack.pop_back();
1691 }
1692
1693 return false;
1694}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001695
1696/// ParseDirectiveFile
1697/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001698bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001699 // FIXME: I'm not sure what this is.
1700 int64_t FileNumber = -1;
1701 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001702 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001703 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001704
1705 if (FileNumber < 1)
1706 return TokError("file number less than one");
1707 }
1708
1709 if (Lexer.isNot(AsmToken::String))
1710 return TokError("unexpected token in '.file' directive");
1711
Sean Callanan18b83232010-01-19 21:44:56 +00001712 StringRef ATTRIBUTE_UNUSED FileName = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001713 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001714
1715 if (Lexer.isNot(AsmToken::EndOfStatement))
1716 return TokError("unexpected token in '.file' directive");
1717
1718 // FIXME: Do something with the .file.
1719
1720 return false;
1721}
1722
1723/// ParseDirectiveLine
1724/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001725bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001726 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1727 if (Lexer.isNot(AsmToken::Integer))
1728 return TokError("unexpected token in '.line' directive");
1729
Sean Callanan18b83232010-01-19 21:44:56 +00001730 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001731 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001732 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001733
1734 // FIXME: Do something with the .line.
1735 }
1736
1737 if (Lexer.isNot(AsmToken::EndOfStatement))
1738 return TokError("unexpected token in '.file' directive");
1739
1740 return false;
1741}
1742
1743
1744/// ParseDirectiveLoc
1745/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001746bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001747 if (Lexer.isNot(AsmToken::Integer))
1748 return TokError("unexpected token in '.loc' directive");
1749
1750 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001751 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001752 (void) FileNumber;
1753 // FIXME: Validate file.
1754
Sean Callanan79ed1a82010-01-19 20:22:31 +00001755 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001756 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1757 if (Lexer.isNot(AsmToken::Integer))
1758 return TokError("unexpected token in '.loc' directive");
1759
Sean Callanan18b83232010-01-19 21:44:56 +00001760 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001761 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001762 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001763
1764 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1765 if (Lexer.isNot(AsmToken::Integer))
1766 return TokError("unexpected token in '.loc' directive");
1767
Sean Callanan18b83232010-01-19 21:44:56 +00001768 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001769 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001770 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001771
1772 // FIXME: Do something with the .loc.
1773 }
1774 }
1775
1776 if (Lexer.isNot(AsmToken::EndOfStatement))
1777 return TokError("unexpected token in '.file' directive");
1778
1779 return false;
1780}
1781