blob: fc8d549b8a12ca44588623aa1cd7c183d61f247b [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,
Kevin Enderbyd74acb02010-02-25 18:46:04 +0000149 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000150
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) {
Daniel Dunbar959fd882009-08-26 22:13:22 +0000243 // If the label starts with L it is an assembler temporary label.
244 if (Name.startswith("L"))
Chris Lattner00685bb2010-03-10 01:29:27 +0000245 return Ctx.GetOrCreateTemporarySymbol(Name);
246 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000247}
248
Chris Lattner74ec1a32009-06-22 06:32:03 +0000249/// ParsePrimaryExpr - Parse a primary expression and return it.
250/// primaryexpr ::= (parenexpr
251/// primaryexpr ::= symbol
252/// primaryexpr ::= number
253/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000255 switch (Lexer.getKind()) {
256 default:
257 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000259 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000260 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000262 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000263 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000264 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000265 case AsmToken::Identifier: {
266 // This is a symbol reference.
Sean Callanan18b83232010-01-19 21:44:56 +0000267 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000268 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000269 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000270
271 // If this is an absolute variable reference, substitute it now to preserve
272 // semantics in the face of reassignment.
273 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
274 Res = Sym->getValue();
275 return false;
276 }
277
278 // Otherwise create a symbol ref.
279 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000280 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000281 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000282 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000283 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000284 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000285 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000286 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000287 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000288 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000289 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000290 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000291 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000292 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000293 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000294 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000295 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000296 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000297 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000298 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000299 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000300 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000301 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000302 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000303 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000304 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000305 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000306 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000308 }
309}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000310
Chris Lattnerb4307b32010-01-15 19:28:38 +0000311bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000312 SMLoc EndLoc;
313 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000314}
315
Chris Lattner74ec1a32009-06-22 06:32:03 +0000316/// ParseExpression - Parse an expression and return it.
317///
318/// expr ::= expr +,- expr -> lowest.
319/// expr ::= expr |,^,&,! expr -> middle.
320/// expr ::= expr *,/,%,<<,>> expr -> highest.
321/// expr ::= primaryexpr
322///
Chris Lattner54482b42010-01-15 19:39:23 +0000323bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000324 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000325 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000326 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
327 return true;
328
329 // Try to constant fold it up front, if possible.
330 int64_t Value;
331 if (Res->EvaluateAsAbsolute(Value))
332 Res = MCConstantExpr::Create(Value, getContext());
333
334 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000335}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000336
Chris Lattnerb4307b32010-01-15 19:28:38 +0000337bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000338 Res = 0;
339 return ParseParenExpr(Res, EndLoc) ||
340 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000341}
342
Daniel Dunbar475839e2009-06-29 20:37:27 +0000343bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000344 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000345
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000346 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 if (ParseExpression(Expr))
348 return true;
349
Daniel Dunbare00b0112009-10-16 01:57:52 +0000350 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000351 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352
353 return false;
354}
355
Daniel Dunbar3f872332009-07-28 16:08:33 +0000356static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000357 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000358 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000359 default:
360 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000361
362 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000365 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 1;
369
370 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000372 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000373 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000374 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000375 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000377 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000378 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000379 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 case AsmToken::ExclaimEqual:
381 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000382 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000383 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000384 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000385 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000386 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000387 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000388 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000389 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000390 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000391 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000392 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000393 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000394 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000395 return 2;
396
397 // Intermediate Precedence: |, &, ^
398 //
399 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000400 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000401 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000402 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000403 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000404 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000405 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000406 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000407 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000408 return 3;
409
410 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000412 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000413 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000414 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000416 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000417 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000418 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000419 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000420 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000421 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000422 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000423 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000424 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000425 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000426 }
427}
428
429
430/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
431/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000432bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
433 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000434 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000435 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000436 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000437
438 // If the next token is lower precedence than we are allowed to eat, return
439 // successfully with what we ate already.
440 if (TokPrec < Precedence)
441 return false;
442
Sean Callanan79ed1a82010-01-19 20:22:31 +0000443 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000444
445 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000446 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000447 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000448
449 // If BinOp binds less tightly with RHS than the operator after RHS, let
450 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000451 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000452 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000453 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000454 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000455 }
456
Daniel Dunbar475839e2009-06-29 20:37:27 +0000457 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000458 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000459 }
460}
461
Chris Lattnerc4193832009-06-22 05:51:26 +0000462
463
464
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000465/// ParseStatement:
466/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000467/// ::= Label* Directive ...Operands... EndOfStatement
468/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000469bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000470 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000471 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000472 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000473 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000474
475 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000476 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000477 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000478 StringRef IDVal;
479 if (ParseIdentifier(IDVal))
480 return TokError("unexpected token at start of statement");
481
482 // FIXME: Recurse on local labels?
483
484 // See what kind of statement we have.
485 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000486 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000487 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000488 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000489
490 // Diagnose attempt to use a variable as a label.
491 //
492 // FIXME: Diagnostics. Note the location of the definition as a label.
493 // FIXME: This doesn't diagnose assignment to a symbol which has been
494 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000495 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000496 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000497 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000498
Daniel Dunbar959fd882009-08-26 22:13:22 +0000499 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000500 Out.EmitLabel(Sym);
501
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000502 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000503 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000504
Daniel Dunbar3f872332009-07-28 16:08:33 +0000505 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000506 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000507 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000508
Daniel Dunbare2ace502009-08-31 08:09:09 +0000509 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000510
511 default: // Normal instruction or directive.
512 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000513 }
514
515 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000516 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000517 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000519 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000521 // FIXME: This changes behavior based on the -static flag to the
522 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000523 return ParseDirectiveSectionSwitch("__TEXT", "__text",
524 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000526 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000527 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000528 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000530 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
531 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000533 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 MCSectionMachO::S_4BYTE_LITERALS,
535 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000537 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000538 MCSectionMachO::S_8BYTE_LITERALS,
539 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000541 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 MCSectionMachO::S_16BYTE_LITERALS,
543 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000547 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
552
553 // FIXME: The assembler manual claims that this has the self modify code
554 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000555 if (IDVal == ".symbol_stub")
556 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
557 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000558 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
559 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000560 0, 16);
561 // FIXME: PowerPC only?
562 if (IDVal == ".picsymbol_stub")
563 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
564 MCSectionMachO::S_SYMBOL_STUBS |
565 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
566 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000567 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
571
572 // FIXME: The section names of these two are misspelled in the assembler
573 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000575 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
576 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
577 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000578 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000579 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
580 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
581 4);
582
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000584 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000586 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000587 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
588 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000590 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
592 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000595
596
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000598 return ParseDirectiveSectionSwitch("__OBJC", "__class",
599 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000601 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
602 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
605 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
608 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000610 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
611 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
614 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000616 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
617 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000619 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
620 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000622 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
623 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
624 MCSectionMachO::S_LITERAL_POINTERS,
625 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000627 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
628 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
629 MCSectionMachO::S_LITERAL_POINTERS,
630 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000632 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
633 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000635 return ParseDirectiveSectionSwitch("__OBJC", "__category",
636 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000638 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
639 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
642 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000644 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
645 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".objc_class_names")
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_types")
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_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000653 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
654 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000656 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
657 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000658
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000659 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000661 return ParseDirectiveSet();
662
Daniel Dunbara0d14262009-06-24 23:30:00 +0000663 // Data directives
664
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000666 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000668 return ParseDirectiveAscii(true);
669
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000671 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000672 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000673 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000675 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000677 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000678
679 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000681 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000683 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000685 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000686 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000687 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000689 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000691 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000693 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000695 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
696
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000698 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000699
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000701 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000703 return ParseDirectiveSpace();
704
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000705 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000706
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000707 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000708 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000710 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000712 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000713 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000714 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000716 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000718 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000720 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000722 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000724 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000726 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000728 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000730 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000731
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000732 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000733 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000735 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000737 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000738 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000739 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000741 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000742
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000744 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000746 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000747 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000748 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000750 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000752 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000753
Chris Lattnerebb89b42009-09-27 21:16:52 +0000754 // Look up the handler in the handler table,
755 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
756 if (Handler)
757 return (this->*Handler)(IDVal, IDLoc);
758
Kevin Enderby9c656452009-09-10 20:51:44 +0000759 // Target hook for parsing target specific directives.
760 if (!getTargetParser().ParseDirective(ID))
761 return false;
762
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000763 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000764 EatToEndOfStatement();
765 return false;
766 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000767
Chris Lattner98986712010-01-14 22:21:20 +0000768
769 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
770 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
771 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000772 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000773
Daniel Dunbar3f872332009-07-28 16:08:33 +0000774 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000775 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000776 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000777
778 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000779 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000780
Chris Lattner98986712010-01-14 22:21:20 +0000781
782 MCInst Inst;
783
784 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
785
786 // Free any parsed operands.
787 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
788 delete ParsedOperands[i];
789
790 if (MatchFail) {
791 // FIXME: We should give nicer diagnostics about the exact failure.
792 Error(IDLoc, "unrecognized instruction");
793 return true;
794 }
795
Chris Lattner2cf5f142009-06-22 01:29:09 +0000796 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000797 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000798
799 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000800 return false;
801}
Chris Lattner9a023f72009-06-24 04:43:34 +0000802
Daniel Dunbare2ace502009-08-31 08:09:09 +0000803bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000804 // FIXME: Use better location, we should use proper tokens.
805 SMLoc EqualLoc = Lexer.getLoc();
806
Daniel Dunbar821e3332009-08-31 08:09:28 +0000807 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000808 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000809 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000810 return true;
811
Daniel Dunbar3f872332009-07-28 16:08:33 +0000812 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000813 return TokError("unexpected token in assignment");
814
815 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000816 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000817
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000818 // Validate that the LHS is allowed to be a variable (either it has not been
819 // used as a symbol, or it is an absolute symbol).
820 MCSymbol *Sym = getContext().LookupSymbol(Name);
821 if (Sym) {
822 // Diagnose assignment to a label.
823 //
824 // FIXME: Diagnostics. Note the location of the definition as a label.
825 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
826 if (!Sym->isUndefined() && !Sym->isAbsolute())
827 return Error(EqualLoc, "redefinition of '" + Name + "'");
828 else if (!Sym->isVariable())
829 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
830 else if (!isa<MCConstantExpr>(Sym->getValue()))
831 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
832 Name + "'");
833 } else
834 Sym = CreateSymbol(Name);
835
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000836 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000837
838 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000839 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000840
841 return false;
842}
843
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000844/// ParseIdentifier:
845/// ::= identifier
846/// ::= string
847bool AsmParser::ParseIdentifier(StringRef &Res) {
848 if (Lexer.isNot(AsmToken::Identifier) &&
849 Lexer.isNot(AsmToken::String))
850 return true;
851
Sean Callanan18b83232010-01-19 21:44:56 +0000852 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000853
Sean Callanan79ed1a82010-01-19 20:22:31 +0000854 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000855
856 return false;
857}
858
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000859/// ParseDirectiveSet:
860/// ::= .set identifier ',' expression
861bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000862 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000863
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000864 if (ParseIdentifier(Name))
865 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000866
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000867 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000868 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000870
Daniel Dunbare2ace502009-08-31 08:09:09 +0000871 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000872}
873
Chris Lattner9a023f72009-06-24 04:43:34 +0000874/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000875/// ::= .section identifier (',' identifier)*
876/// FIXME: This should actually parse out the segment, section, attributes and
877/// sizeof_stub fields.
878bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000879 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000880
Daniel Dunbarace63122009-08-11 03:42:33 +0000881 StringRef SectionName;
882 if (ParseIdentifier(SectionName))
883 return Error(Loc, "expected identifier after '.section' directive");
884
885 // Verify there is a following comma.
886 if (!Lexer.is(AsmToken::Comma))
887 return TokError("unexpected token in '.section' directive");
888
Chris Lattnerff4bc462009-08-10 01:39:42 +0000889 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000890 SectionSpec += ",";
891
892 // Add all the tokens until the end of the line, ParseSectionSpecifier will
893 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000894 StringRef EOL = Lexer.LexUntilEndOfStatement();
895 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000896
Sean Callanan79ed1a82010-01-19 20:22:31 +0000897 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000898 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000899 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000900 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000901
Chris Lattnerff4bc462009-08-10 01:39:42 +0000902
903 StringRef Segment, Section;
904 unsigned TAA, StubSize;
905 std::string ErrorStr =
906 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
907 TAA, StubSize);
908
909 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000910 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000911
Chris Lattner56594f92009-07-31 17:47:16 +0000912 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000913 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000914 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000915 isText ? SectionKind::getText()
916 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000917 return false;
918}
919
Chris Lattnere15c2d72009-08-10 18:05:55 +0000920/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000921bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
922 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000923 unsigned TAA, unsigned Align,
924 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000925 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000926 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000927 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000928
Chris Lattner56594f92009-07-31 17:47:16 +0000929 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000930 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000931 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000932 isText ? SectionKind::getText()
933 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000934
935 // Set the implicit alignment, if any.
936 //
937 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
938 // alignment on the section (e.g., if one manually inserts bytes into the
939 // section, then just issueing the section switch directive will not realign
940 // the section. However, this is arguably more reasonable behavior, and there
941 // is no good reason for someone to intentionally emit incorrectly sized
942 // values into the implicitly aligned sections.
943 if (Align)
944 Out.EmitValueToAlignment(Align, 0, 1, 0);
945
Chris Lattner529fb542009-06-24 05:13:15 +0000946 return false;
947}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000948
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000949bool AsmParser::ParseEscapedString(std::string &Data) {
950 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
951
952 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000953 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000954 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
955 if (Str[i] != '\\') {
956 Data += Str[i];
957 continue;
958 }
959
960 // Recognize escaped characters. Note that this escape semantics currently
961 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
962 ++i;
963 if (i == e)
964 return TokError("unexpected backslash at end of string");
965
966 // Recognize octal sequences.
967 if ((unsigned) (Str[i] - '0') <= 7) {
968 // Consume up to three octal characters.
969 unsigned Value = Str[i] - '0';
970
971 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
972 ++i;
973 Value = Value * 8 + (Str[i] - '0');
974
975 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
976 ++i;
977 Value = Value * 8 + (Str[i] - '0');
978 }
979 }
980
981 if (Value > 255)
982 return TokError("invalid octal escape sequence (out of range)");
983
984 Data += (unsigned char) Value;
985 continue;
986 }
987
988 // Otherwise recognize individual escapes.
989 switch (Str[i]) {
990 default:
991 // Just reject invalid escape sequences for now.
992 return TokError("invalid escape sequence (unrecognized character)");
993
994 case 'b': Data += '\b'; break;
995 case 'f': Data += '\f'; break;
996 case 'n': Data += '\n'; break;
997 case 'r': Data += '\r'; break;
998 case 't': Data += '\t'; break;
999 case '"': Data += '"'; break;
1000 case '\\': Data += '\\'; break;
1001 }
1002 }
1003
1004 return false;
1005}
1006
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001008/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001009bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001010 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001011 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001012 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013 return TokError("expected string in '.ascii' or '.asciz' directive");
1014
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001015 std::string Data;
1016 if (ParseEscapedString(Data))
1017 return true;
1018
Chris Lattneraaec2052010-01-19 19:46:13 +00001019 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001020 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001021 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022
Sean Callanan79ed1a82010-01-19 20:22:31 +00001023 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001024
Daniel Dunbar3f872332009-07-28 16:08:33 +00001025 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 break;
1027
Daniel Dunbar3f872332009-07-28 16:08:33 +00001028 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001030 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001031 }
1032 }
1033
Sean Callanan79ed1a82010-01-19 20:22:31 +00001034 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001035 return false;
1036}
1037
1038/// ParseDirectiveValue
1039/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1040bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001041 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001042 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001043 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001044 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001045 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001046 return true;
1047
Chris Lattneraaec2052010-01-19 19:46:13 +00001048 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001049
Daniel Dunbar3f872332009-07-28 16:08:33 +00001050 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 break;
1052
1053 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001056 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057 }
1058 }
1059
Sean Callanan79ed1a82010-01-19 20:22:31 +00001060 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001061 return false;
1062}
1063
1064/// ParseDirectiveSpace
1065/// ::= .space expression [ , expression ]
1066bool AsmParser::ParseDirectiveSpace() {
1067 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001068 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001069 return true;
1070
1071 int64_t FillExpr = 0;
1072 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001073 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1074 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001075 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001076 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077
Daniel Dunbar475839e2009-06-29 20:37:27 +00001078 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079 return true;
1080
1081 HasFillExpr = true;
1082
Daniel Dunbar3f872332009-07-28 16:08:33 +00001083 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 return TokError("unexpected token in '.space' directive");
1085 }
1086
Sean Callanan79ed1a82010-01-19 20:22:31 +00001087 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001088
1089 if (NumBytes <= 0)
1090 return TokError("invalid number of bytes in '.space' directive");
1091
1092 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001093 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001094
1095 return false;
1096}
1097
1098/// ParseDirectiveFill
1099/// ::= .fill expression , expression , expression
1100bool AsmParser::ParseDirectiveFill() {
1101 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001102 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001103 return true;
1104
Daniel Dunbar3f872332009-07-28 16:08:33 +00001105 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001106 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001107 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001108
1109 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001110 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001111 return true;
1112
Daniel Dunbar3f872332009-07-28 16:08:33 +00001113 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001115 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001116
1117 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001118 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119 return true;
1120
Daniel Dunbar3f872332009-07-28 16:08:33 +00001121 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122 return TokError("unexpected token in '.fill' directive");
1123
Sean Callanan79ed1a82010-01-19 20:22:31 +00001124 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001125
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001126 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1127 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001128
1129 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001130 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1131 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132
1133 return false;
1134}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001135
1136/// ParseDirectiveOrg
1137/// ::= .org expression [ , expression ]
1138bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001139 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001140 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001141 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001142 return true;
1143
1144 // Parse optional fill expression.
1145 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001146 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1147 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001148 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001149 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001150
Daniel Dunbar475839e2009-06-29 20:37:27 +00001151 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001152 return true;
1153
Daniel Dunbar3f872332009-07-28 16:08:33 +00001154 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001155 return TokError("unexpected token in '.org' directive");
1156 }
1157
Sean Callanan79ed1a82010-01-19 20:22:31 +00001158 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001159
1160 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1161 // has to be relative to the current section.
1162 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001163
1164 return false;
1165}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001166
1167/// ParseDirectiveAlign
1168/// ::= {.align, ...} expression [ , expression [ , expression ]]
1169bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001170 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001171 int64_t Alignment;
1172 if (ParseAbsoluteExpression(Alignment))
1173 return true;
1174
1175 SMLoc MaxBytesLoc;
1176 bool HasFillExpr = false;
1177 int64_t FillExpr = 0;
1178 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001179 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1180 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001182 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001183
1184 // The fill expression can be omitted while specifying a maximum number of
1185 // alignment bytes, e.g:
1186 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001188 HasFillExpr = true;
1189 if (ParseAbsoluteExpression(FillExpr))
1190 return true;
1191 }
1192
Daniel Dunbar3f872332009-07-28 16:08:33 +00001193 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1194 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001195 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001196 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001197
1198 MaxBytesLoc = Lexer.getLoc();
1199 if (ParseAbsoluteExpression(MaxBytesToFill))
1200 return true;
1201
Daniel Dunbar3f872332009-07-28 16:08:33 +00001202 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001203 return TokError("unexpected token in directive");
1204 }
1205 }
1206
Sean Callanan79ed1a82010-01-19 20:22:31 +00001207 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001208
1209 if (!HasFillExpr) {
1210 // FIXME: Sometimes fill with nop.
1211 FillExpr = 0;
1212 }
1213
1214 // Compute alignment in bytes.
1215 if (IsPow2) {
1216 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001217 if (Alignment >= 32) {
1218 Error(AlignmentLoc, "invalid alignment value");
1219 Alignment = 31;
1220 }
1221
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001222 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001223 }
1224
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001225 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001226 if (MaxBytesLoc.isValid()) {
1227 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001228 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1229 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001230 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001231 }
1232
1233 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001234 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1235 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001236 MaxBytesToFill = 0;
1237 }
1238 }
1239
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001240 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1241 // the TextAlignFillValue.
1242 if(Out.getCurrentSection()->getKind().isText() &&
1243 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1244 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1245 else
1246 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1247 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001248
1249 return false;
1250}
1251
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001252/// ParseDirectiveSymbolAttribute
1253/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001254bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001255 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001256 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001257 StringRef Name;
1258
1259 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001260 return TokError("expected identifier in directive");
1261
Daniel Dunbar959fd882009-08-26 22:13:22 +00001262 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001263
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001264 Out.EmitSymbolAttribute(Sym, Attr);
1265
Daniel Dunbar3f872332009-07-28 16:08:33 +00001266 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001267 break;
1268
Daniel Dunbar3f872332009-07-28 16:08:33 +00001269 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001270 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001271 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001272 }
1273 }
1274
Sean Callanan79ed1a82010-01-19 20:22:31 +00001275 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001276 return false;
1277}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001278
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001279/// ParseDirectiveDarwinSymbolDesc
1280/// ::= .desc identifier , expression
1281bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001282 StringRef Name;
1283 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001284 return TokError("expected identifier in directive");
1285
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001286 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001287 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001288
Daniel Dunbar3f872332009-07-28 16:08:33 +00001289 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001290 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001292
1293 SMLoc DescLoc = Lexer.getLoc();
1294 int64_t DescValue;
1295 if (ParseAbsoluteExpression(DescValue))
1296 return true;
1297
Daniel Dunbar3f872332009-07-28 16:08:33 +00001298 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001299 return TokError("unexpected token in '.desc' directive");
1300
Sean Callanan79ed1a82010-01-19 20:22:31 +00001301 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001302
1303 // Set the n_desc field of this Symbol to this DescValue
1304 Out.EmitSymbolDesc(Sym, DescValue);
1305
1306 return false;
1307}
1308
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001309/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001310/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1311bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001312 SMLoc IDLoc = Lexer.getLoc();
1313 StringRef Name;
1314 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001315 return TokError("expected identifier in directive");
1316
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001317 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001318 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001319
Daniel Dunbar3f872332009-07-28 16:08:33 +00001320 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001321 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001322 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001323
1324 int64_t Size;
1325 SMLoc SizeLoc = Lexer.getLoc();
1326 if (ParseAbsoluteExpression(Size))
1327 return true;
1328
1329 int64_t Pow2Alignment = 0;
1330 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001331 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001332 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001333 Pow2AlignmentLoc = Lexer.getLoc();
1334 if (ParseAbsoluteExpression(Pow2Alignment))
1335 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001336
1337 // If this target takes alignments in bytes (not log) validate and convert.
1338 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1339 if (!isPowerOf2_64(Pow2Alignment))
1340 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1341 Pow2Alignment = Log2_64(Pow2Alignment);
1342 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001343 }
1344
Daniel Dunbar3f872332009-07-28 16:08:33 +00001345 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001346 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001347
Sean Callanan79ed1a82010-01-19 20:22:31 +00001348 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001349
Chris Lattner1fc3d752009-07-09 17:25:12 +00001350 // NOTE: a size of zero for a .comm should create a undefined symbol
1351 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001352 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001353 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1354 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001355
1356 // NOTE: The alignment in the directive is a power of 2 value, the assember
1357 // may internally end up wanting an alignment in bytes.
1358 // FIXME: Diagnose overflow.
1359 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001360 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1361 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001362
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001363 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001364 return Error(IDLoc, "invalid symbol redefinition");
1365
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001366 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001367 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001368 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001369 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1370 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001371 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001372 Sym, Size, 1 << Pow2Alignment);
1373 return false;
1374 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001375
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001376 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001377 return false;
1378}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001379
1380/// ParseDirectiveDarwinZerofill
1381/// ::= .zerofill segname , sectname [, identifier , size_expression [
1382/// , align_expression ]]
1383bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001384 // FIXME: Handle quoted names here.
1385
Daniel Dunbar3f872332009-07-28 16:08:33 +00001386 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001387 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001388 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001389 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001390
Daniel Dunbar3f872332009-07-28 16:08:33 +00001391 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001392 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001393 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001394
Daniel Dunbar3f872332009-07-28 16:08:33 +00001395 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001396 return TokError("expected section name after comma in '.zerofill' "
1397 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001398 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001399 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001400
Chris Lattner9be3fee2009-07-10 22:20:30 +00001401 // If this is the end of the line all that was wanted was to create the
1402 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001403 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001404 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001405 Out.EmitZerofill(getMachOSection(Segment, Section,
1406 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001407 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001408 return false;
1409 }
1410
Daniel Dunbar3f872332009-07-28 16:08:33 +00001411 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001412 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001414
Daniel Dunbar3f872332009-07-28 16:08:33 +00001415 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001416 return TokError("expected identifier in directive");
1417
1418 // handle the identifier as the key symbol.
1419 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001420 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001421 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001422
Daniel Dunbar3f872332009-07-28 16:08:33 +00001423 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001424 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001425 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001426
1427 int64_t Size;
1428 SMLoc SizeLoc = Lexer.getLoc();
1429 if (ParseAbsoluteExpression(Size))
1430 return true;
1431
1432 int64_t Pow2Alignment = 0;
1433 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001434 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001435 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001436 Pow2AlignmentLoc = Lexer.getLoc();
1437 if (ParseAbsoluteExpression(Pow2Alignment))
1438 return true;
1439 }
1440
Daniel Dunbar3f872332009-07-28 16:08:33 +00001441 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001442 return TokError("unexpected token in '.zerofill' directive");
1443
Sean Callanan79ed1a82010-01-19 20:22:31 +00001444 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001445
1446 if (Size < 0)
1447 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1448 "than zero");
1449
1450 // NOTE: The alignment in the directive is a power of 2 value, the assember
1451 // may internally end up wanting an alignment in bytes.
1452 // FIXME: Diagnose overflow.
1453 if (Pow2Alignment < 0)
1454 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1455 "can't be less than zero");
1456
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001457 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001458 return Error(IDLoc, "invalid symbol redefinition");
1459
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001460 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001461 //
1462 // FIXME: Arch specific.
1463 Out.EmitZerofill(getMachOSection(Segment, Section,
1464 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001465 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001466 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001467
1468 return false;
1469}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001470
1471/// ParseDirectiveDarwinSubsectionsViaSymbols
1472/// ::= .subsections_via_symbols
1473bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001474 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001475 return TokError("unexpected token in '.subsections_via_symbols' directive");
1476
Sean Callanan79ed1a82010-01-19 20:22:31 +00001477 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001478
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001479 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001480
1481 return false;
1482}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001483
1484/// ParseDirectiveAbort
1485/// ::= .abort [ "abort_string" ]
1486bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001487 // FIXME: Use loc from directive.
1488 SMLoc Loc = Lexer.getLoc();
1489
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001490 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001491 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1492 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001493 return TokError("expected string in '.abort' directive");
1494
Sean Callanan18b83232010-01-19 21:44:56 +00001495 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001496
Sean Callanan79ed1a82010-01-19 20:22:31 +00001497 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001498 }
1499
Daniel Dunbar3f872332009-07-28 16:08:33 +00001500 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001501 return TokError("unexpected token in '.abort' directive");
1502
Sean Callanan79ed1a82010-01-19 20:22:31 +00001503 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001504
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001505 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001506 if (Str.empty())
1507 Error(Loc, ".abort detected. Assembly stopping.");
1508 else
1509 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001510
1511 return false;
1512}
Kevin Enderby71148242009-07-14 21:35:03 +00001513
1514/// ParseDirectiveLsym
1515/// ::= .lsym identifier , expression
1516bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001517 StringRef Name;
1518 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001519 return TokError("expected identifier in directive");
1520
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001521 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001522 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001523
Daniel Dunbar3f872332009-07-28 16:08:33 +00001524 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001525 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001526 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001527
Daniel Dunbar821e3332009-08-31 08:09:28 +00001528 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001529 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001530 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001531 return true;
1532
Daniel Dunbar3f872332009-07-28 16:08:33 +00001533 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001534 return TokError("unexpected token in '.lsym' directive");
1535
Sean Callanan79ed1a82010-01-19 20:22:31 +00001536 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001537
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001538 // We don't currently support this directive.
1539 //
1540 // FIXME: Diagnostic location!
1541 (void) Sym;
1542 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001543}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001544
1545/// ParseDirectiveInclude
1546/// ::= .include "filename"
1547bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001548 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001549 return TokError("expected string in '.include' directive");
1550
Sean Callanan18b83232010-01-19 21:44:56 +00001551 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001552 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001553 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001554
Daniel Dunbar3f872332009-07-28 16:08:33 +00001555 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001556 return TokError("unexpected token in '.include' directive");
1557
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001558 // Strip the quotes.
1559 Filename = Filename.substr(1, Filename.size()-2);
1560
1561 // Attempt to switch the lexer to the included file before consuming the end
1562 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001563 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001564 PrintMessage(IncludeLoc,
1565 "Could not find include file '" + Filename + "'",
1566 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001567 return true;
1568 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001569
1570 return false;
1571}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001572
1573/// ParseDirectiveDarwinDumpOrLoad
1574/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001575bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001576 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001577 return TokError("expected string in '.dump' or '.load' directive");
1578
Sean Callanan79ed1a82010-01-19 20:22:31 +00001579 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001580
Daniel Dunbar3f872332009-07-28 16:08:33 +00001581 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001582 return TokError("unexpected token in '.dump' or '.load' directive");
1583
Sean Callanan79ed1a82010-01-19 20:22:31 +00001584 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001585
Kevin Enderby5026ae42009-07-20 20:25:37 +00001586 // FIXME: If/when .dump and .load are implemented they will be done in the
1587 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001588 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001589 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001590 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001591 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001592
1593 return false;
1594}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001595
1596/// ParseDirectiveIf
1597/// ::= .if expression
1598bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1599 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001600 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001601
1602 TheCondStack.push_back(TheCondState);
1603 TheCondState.TheCond = AsmCond::IfCond;
1604 if(TheCondState.Ignore) {
1605 EatToEndOfStatement();
1606 }
1607 else {
1608 int64_t ExprValue;
1609 if (ParseAbsoluteExpression(ExprValue))
1610 return true;
1611
1612 if (Lexer.isNot(AsmToken::EndOfStatement))
1613 return TokError("unexpected token in '.if' directive");
1614
Sean Callanan79ed1a82010-01-19 20:22:31 +00001615 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001616
1617 TheCondState.CondMet = ExprValue;
1618 TheCondState.Ignore = !TheCondState.CondMet;
1619 }
1620
1621 return false;
1622}
1623
1624/// ParseDirectiveElseIf
1625/// ::= .elseif expression
1626bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1627 if (TheCondState.TheCond != AsmCond::IfCond &&
1628 TheCondState.TheCond != AsmCond::ElseIfCond)
1629 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1630 " an .elseif");
1631 TheCondState.TheCond = AsmCond::ElseIfCond;
1632
1633 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001634 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001635
1636 bool LastIgnoreState = false;
1637 if (!TheCondStack.empty())
1638 LastIgnoreState = TheCondStack.back().Ignore;
1639 if (LastIgnoreState || TheCondState.CondMet) {
1640 TheCondState.Ignore = true;
1641 EatToEndOfStatement();
1642 }
1643 else {
1644 int64_t ExprValue;
1645 if (ParseAbsoluteExpression(ExprValue))
1646 return true;
1647
1648 if (Lexer.isNot(AsmToken::EndOfStatement))
1649 return TokError("unexpected token in '.elseif' directive");
1650
Sean Callanan79ed1a82010-01-19 20:22:31 +00001651 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001652 TheCondState.CondMet = ExprValue;
1653 TheCondState.Ignore = !TheCondState.CondMet;
1654 }
1655
1656 return false;
1657}
1658
1659/// ParseDirectiveElse
1660/// ::= .else
1661bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1662 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001663 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001664
1665 if (Lexer.isNot(AsmToken::EndOfStatement))
1666 return TokError("unexpected token in '.else' directive");
1667
Sean Callanan79ed1a82010-01-19 20:22:31 +00001668 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001669
1670 if (TheCondState.TheCond != AsmCond::IfCond &&
1671 TheCondState.TheCond != AsmCond::ElseIfCond)
1672 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1673 ".elseif");
1674 TheCondState.TheCond = AsmCond::ElseCond;
1675 bool LastIgnoreState = false;
1676 if (!TheCondStack.empty())
1677 LastIgnoreState = TheCondStack.back().Ignore;
1678 if (LastIgnoreState || TheCondState.CondMet)
1679 TheCondState.Ignore = true;
1680 else
1681 TheCondState.Ignore = false;
1682
1683 return false;
1684}
1685
1686/// ParseDirectiveEndIf
1687/// ::= .endif
1688bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1689 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001690 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001691
1692 if (Lexer.isNot(AsmToken::EndOfStatement))
1693 return TokError("unexpected token in '.endif' directive");
1694
Sean Callanan79ed1a82010-01-19 20:22:31 +00001695 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001696
1697 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1698 TheCondStack.empty())
1699 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1700 ".else");
1701 if (!TheCondStack.empty()) {
1702 TheCondState = TheCondStack.back();
1703 TheCondStack.pop_back();
1704 }
1705
1706 return false;
1707}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001708
1709/// ParseDirectiveFile
1710/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001711bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001712 // FIXME: I'm not sure what this is.
1713 int64_t FileNumber = -1;
1714 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001715 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001716 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001717
1718 if (FileNumber < 1)
1719 return TokError("file number less than one");
1720 }
1721
1722 if (Lexer.isNot(AsmToken::String))
1723 return TokError("unexpected token in '.file' directive");
1724
Chris Lattnerd32e8032010-01-25 19:02:58 +00001725 StringRef Filename = getTok().getString();
1726 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001727 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001728
1729 if (Lexer.isNot(AsmToken::EndOfStatement))
1730 return TokError("unexpected token in '.file' directive");
1731
Chris Lattnerd32e8032010-01-25 19:02:58 +00001732 if (FileNumber == -1)
1733 Out.EmitFileDirective(Filename);
1734 else
1735 Out.EmitDwarfFileDirective(FileNumber, Filename);
1736
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001737 return false;
1738}
1739
1740/// ParseDirectiveLine
1741/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001742bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001743 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1744 if (Lexer.isNot(AsmToken::Integer))
1745 return TokError("unexpected token in '.line' directive");
1746
Sean Callanan18b83232010-01-19 21:44:56 +00001747 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001748 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001749 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001750
1751 // FIXME: Do something with the .line.
1752 }
1753
1754 if (Lexer.isNot(AsmToken::EndOfStatement))
1755 return TokError("unexpected token in '.file' directive");
1756
1757 return false;
1758}
1759
1760
1761/// ParseDirectiveLoc
1762/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001763bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001764 if (Lexer.isNot(AsmToken::Integer))
1765 return TokError("unexpected token in '.loc' directive");
1766
1767 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001768 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001769 (void) FileNumber;
1770 // FIXME: Validate file.
1771
Sean Callanan79ed1a82010-01-19 20:22:31 +00001772 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001773 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1774 if (Lexer.isNot(AsmToken::Integer))
1775 return TokError("unexpected token in '.loc' directive");
1776
Sean Callanan18b83232010-01-19 21:44:56 +00001777 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001778 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001779 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001780
1781 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1782 if (Lexer.isNot(AsmToken::Integer))
1783 return TokError("unexpected token in '.loc' directive");
1784
Sean Callanan18b83232010-01-19 21:44:56 +00001785 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001786 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001787 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001788
1789 // FIXME: Do something with the .loc.
1790 }
1791 }
1792
1793 if (Lexer.isNot(AsmToken::EndOfStatement))
1794 return TokError("unexpected token in '.file' directive");
1795
1796 return false;
1797}
1798