blob: 50964aea42b1b41ca039a9c2e228a13a4dae7e5e [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000016#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000018#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000019#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000024#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000025#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000027#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000028using namespace llvm;
29
Chris Lattneraaec2052010-01-19 19:46:13 +000030
31enum { DEFAULT_ADDRSPACE = 0 };
32
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000033// Mach-O section uniquing.
34//
35// FIXME: Figure out where this should live, it should be shared by
36// TargetLoweringObjectFile.
37typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
38
Chris Lattnerebb89b42009-09-27 21:16:52 +000039AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
40 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000041 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
42 CurBuffer(0), SectionUniquingMap(0) {
43 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
44
Chris Lattnerebb89b42009-09-27 21:16:52 +000045 // Debugging directives.
46 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
47 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
48 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
49}
50
51
52
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000053AsmParser::~AsmParser() {
54 // If we have the MachO uniquing map, free it.
55 delete (MachOUniqueMapTy*)SectionUniquingMap;
56}
57
58const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
59 const StringRef &Section,
60 unsigned TypeAndAttributes,
61 unsigned Reserved2,
62 SectionKind Kind) const {
63 // We unique sections by their segment/section pair. The returned section
64 // may not have the same flags as the requested section, if so this should be
65 // diagnosed by the client as an error.
66
67 // Create the map if it doesn't already exist.
68 if (SectionUniquingMap == 0)
69 SectionUniquingMap = new MachOUniqueMapTy();
70 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
71
72 // Form the name to look up.
73 SmallString<64> Name;
74 Name += Segment;
75 Name.push_back(',');
76 Name += Section;
77
78 // Do the lookup, if we have a hit, return it.
79 const MCSectionMachO *&Entry = Map[Name.str()];
80
81 // FIXME: This should validate the type and attributes.
82 if (Entry) return Entry;
83
84 // Otherwise, return a new section.
85 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
86 Reserved2, Kind, Ctx);
87}
88
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000089void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000090 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000091}
92
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000093bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000094 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000095 return true;
96}
97
98bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000099 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000100 return true;
101}
102
Sean Callananbf2013e2010-01-20 23:19:55 +0000103void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
104 const char *Type) const {
105 SrcMgr.PrintMessage(Loc, Msg, Type);
106}
Sean Callananfd0b0282010-01-21 00:19:58 +0000107
108bool AsmParser::EnterIncludeFile(const std::string &Filename) {
109 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
110 if (NewBuf == -1)
111 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000112
Sean Callananfd0b0282010-01-21 00:19:58 +0000113 CurBuffer = NewBuf;
114
115 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
116
117 return false;
118}
119
120const AsmToken &AsmParser::Lex() {
121 const AsmToken *tok = &Lexer.Lex();
122
123 if (tok->is(AsmToken::Eof)) {
124 // If this is the end of an included file, pop the parent file off the
125 // include stack.
126 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
127 if (ParentIncludeLoc != SMLoc()) {
128 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
129 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
130 ParentIncludeLoc.getPointer());
131 tok = &Lexer.Lex();
132 }
133 }
134
135 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000136 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000137
Sean Callananfd0b0282010-01-21 00:19:58 +0000138 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000139}
140
Chris Lattner27aa7d22009-06-21 20:16:42 +0000141bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000142 // Create the initial section.
143 //
144 // FIXME: Support -n.
145 // FIXME: Target hook & command line option for initial section.
146 Out.SwitchSection(getMachOSection("__TEXT", "__text",
147 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
Kevin Enderbyd74acb02010-02-25 18:46:04 +0000148 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000149
150
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000151 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000152 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000153
Chris Lattnerb717fb02009-07-02 21:53:43 +0000154 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000155
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000156 AsmCond StartingCondState = TheCondState;
157
Chris Lattnerb717fb02009-07-02 21:53:43 +0000158 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000159 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000160 // Handle conditional assembly here before calling ParseStatement()
161 if (Lexer.getKind() == AsmToken::Identifier) {
162 // If we have an identifier, handle it as the key symbol.
Sean Callanan18b83232010-01-19 21:44:56 +0000163 AsmToken ID = getTok();
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000164 SMLoc IDLoc = ID.getLoc();
165 StringRef IDVal = ID.getString();
166
167 if (IDVal == ".if" ||
168 IDVal == ".elseif" ||
169 IDVal == ".else" ||
170 IDVal == ".endif") {
171 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
172 continue;
173 HadError = true;
174 EatToEndOfStatement();
175 continue;
176 }
177 }
178 if (TheCondState.Ignore) {
179 EatToEndOfStatement();
180 continue;
181 }
182
Chris Lattnerb717fb02009-07-02 21:53:43 +0000183 if (!ParseStatement()) continue;
184
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000185 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000186 HadError = true;
187 EatToEndOfStatement();
188 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000189
190 if (TheCondState.TheCond != StartingCondState.TheCond ||
191 TheCondState.Ignore != StartingCondState.Ignore)
192 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000193
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000194 if (!HadError)
195 Out.Finish();
196
Chris Lattnerb717fb02009-07-02 21:53:43 +0000197 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000198}
199
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000200/// ParseConditionalAssemblyDirectives - parse the conditional assembly
201/// directives
202bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
203 SMLoc DirectiveLoc) {
204 if (Directive == ".if")
205 return ParseDirectiveIf(DirectiveLoc);
206 if (Directive == ".elseif")
207 return ParseDirectiveElseIf(DirectiveLoc);
208 if (Directive == ".else")
209 return ParseDirectiveElse(DirectiveLoc);
210 if (Directive == ".endif")
211 return ParseDirectiveEndIf(DirectiveLoc);
212 return true;
213}
214
Chris Lattner2cf5f142009-06-22 01:29:09 +0000215/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
216void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 while (Lexer.isNot(AsmToken::EndOfStatement) &&
218 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000219 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000220
221 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000222 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000223 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000224}
225
Chris Lattnerc4193832009-06-22 05:51:26 +0000226
Chris Lattner74ec1a32009-06-22 06:32:03 +0000227/// ParseParenExpr - Parse a paren expression and return it.
228/// NOTE: This assumes the leading '(' has already been consumed.
229///
230/// parenexpr ::= expr)
231///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000232bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000233 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000234 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000235 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000236 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000237 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000238 return false;
239}
Chris Lattnerc4193832009-06-22 05:51:26 +0000240
Daniel Dunbar959fd882009-08-26 22:13:22 +0000241MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Daniel Dunbar959fd882009-08-26 22:13:22 +0000242 // If the label starts with L it is an assembler temporary label.
243 if (Name.startswith("L"))
Chris Lattner00685bb2010-03-10 01:29:27 +0000244 return Ctx.GetOrCreateTemporarySymbol(Name);
245 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000246}
247
Chris Lattner74ec1a32009-06-22 06:32:03 +0000248/// ParsePrimaryExpr - Parse a primary expression and return it.
249/// primaryexpr ::= (parenexpr
250/// primaryexpr ::= symbol
251/// primaryexpr ::= number
252/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000253bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000254 switch (Lexer.getKind()) {
255 default:
256 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000257 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000258 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000259 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000260 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000261 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000263 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000264 case AsmToken::Identifier: {
265 // This is a symbol reference.
Sean Callanan18b83232010-01-19 21:44:56 +0000266 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000267 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000268 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000269
270 // If this is an absolute variable reference, substitute it now to preserve
271 // semantics in the face of reassignment.
272 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
273 Res = Sym->getValue();
274 return false;
275 }
276
277 // Otherwise create a symbol ref.
278 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000279 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000280 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000281 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000282 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000283 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000284 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000285 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000286 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000287 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000288 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000289 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000290 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000291 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000292 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000293 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000294 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000296 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000297 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000299 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000300 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000302 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000303 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000305 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000306 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000307 }
308}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000309
Chris Lattnerb4307b32010-01-15 19:28:38 +0000310bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000311 SMLoc EndLoc;
312 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000313}
314
Chris Lattner74ec1a32009-06-22 06:32:03 +0000315/// ParseExpression - Parse an expression and return it.
316///
317/// expr ::= expr +,- expr -> lowest.
318/// expr ::= expr |,^,&,! expr -> middle.
319/// expr ::= expr *,/,%,<<,>> expr -> highest.
320/// expr ::= primaryexpr
321///
Chris Lattner54482b42010-01-15 19:39:23 +0000322bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000323 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000324 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000325 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
326 return true;
327
328 // Try to constant fold it up front, if possible.
329 int64_t Value;
330 if (Res->EvaluateAsAbsolute(Value))
331 Res = MCConstantExpr::Create(Value, getContext());
332
333 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000334}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000335
Chris Lattnerb4307b32010-01-15 19:28:38 +0000336bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000337 Res = 0;
338 return ParseParenExpr(Res, EndLoc) ||
339 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000340}
341
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000343 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000345 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000346 if (ParseExpression(Expr))
347 return true;
348
Daniel Dunbare00b0112009-10-16 01:57:52 +0000349 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000350 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351
352 return false;
353}
354
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000357 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000358 default:
359 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360
361 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000363 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000364 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000366 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 return 1;
368
369 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000371 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000372 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000373 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000374 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000375 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000376 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000377 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000378 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000379 case AsmToken::ExclaimEqual:
380 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000383 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000384 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000386 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000387 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000392 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000393 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 return 2;
395
396 // Intermediate Precedence: |, &, ^
397 //
398 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000399 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000400 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000402 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000403 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000404 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000405 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000407 return 3;
408
409 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000410 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000411 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000412 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000413 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000414 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000415 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000416 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000417 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000418 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000420 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000423 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000424 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000425 }
426}
427
428
429/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
430/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000431bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
432 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000433 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000434 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000435 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000436
437 // If the next token is lower precedence than we are allowed to eat, return
438 // successfully with what we ate already.
439 if (TokPrec < Precedence)
440 return false;
441
Sean Callanan79ed1a82010-01-19 20:22:31 +0000442 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000443
444 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000445 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000446 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000447
448 // If BinOp binds less tightly with RHS than the operator after RHS, let
449 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000450 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000451 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000452 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000453 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000454 }
455
Daniel Dunbar475839e2009-06-29 20:37:27 +0000456 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000457 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000458 }
459}
460
Chris Lattnerc4193832009-06-22 05:51:26 +0000461
462
463
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000464/// ParseStatement:
465/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000466/// ::= Label* Directive ...Operands... EndOfStatement
467/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000468bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000469 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000470 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000471 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000472 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000473
474 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000475 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000476 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000477 StringRef IDVal;
478 if (ParseIdentifier(IDVal))
479 return TokError("unexpected token at start of statement");
480
481 // FIXME: Recurse on local labels?
482
483 // See what kind of statement we have.
484 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000485 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000486 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000487 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000488
489 // Diagnose attempt to use a variable as a label.
490 //
491 // FIXME: Diagnostics. Note the location of the definition as a label.
492 // FIXME: This doesn't diagnose assignment to a symbol which has been
493 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000494 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000495 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000496 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000497
Daniel Dunbar959fd882009-08-26 22:13:22 +0000498 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000499 Out.EmitLabel(Sym);
500
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000501 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000502 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000503
Daniel Dunbar3f872332009-07-28 16:08:33 +0000504 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000505 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000506 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000507
Daniel Dunbare2ace502009-08-31 08:09:09 +0000508 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000509
510 default: // Normal instruction or directive.
511 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000512 }
513
514 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000515 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000516 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000518 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000519 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000520 // FIXME: This changes behavior based on the -static flag to the
521 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000522 return ParseDirectiveSectionSwitch("__TEXT", "__text",
523 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000524 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000525 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000527 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000529 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
530 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000531 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000532 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000533 MCSectionMachO::S_4BYTE_LITERALS,
534 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000536 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000537 MCSectionMachO::S_8BYTE_LITERALS,
538 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000540 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000541 MCSectionMachO::S_16BYTE_LITERALS,
542 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000544 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000547 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000548 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000549 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000550 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
551
552 // FIXME: The assembler manual claims that this has the self modify code
553 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000554 if (IDVal == ".symbol_stub")
555 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
556 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000557 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
558 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 0, 16);
560 // FIXME: PowerPC only?
561 if (IDVal == ".picsymbol_stub")
562 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
563 MCSectionMachO::S_SYMBOL_STUBS |
564 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
565 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000568 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000569 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
570
571 // FIXME: The section names of these two are misspelled in the assembler
572 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000573 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000574 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
575 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
576 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000577 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000578 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
579 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
580 4);
581
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000585 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
587 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000589 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000590 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
591 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000592 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000593 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000594
595
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000596 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000597 return ParseDirectiveSectionSwitch("__OBJC", "__class",
598 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000600 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
601 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000602 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000603 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
604 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000606 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
607 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000608 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000609 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
610 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000612 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
613 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000615 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
616 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000618 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
619 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000621 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
622 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
623 MCSectionMachO::S_LITERAL_POINTERS,
624 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000626 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
627 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
628 MCSectionMachO::S_LITERAL_POINTERS,
629 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000631 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
632 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000634 return ParseDirectiveSectionSwitch("__OBJC", "__category",
635 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000637 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
638 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000640 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
641 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000643 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
644 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000646 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
647 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000649 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
650 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000652 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
653 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000655 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
656 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000657
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000658 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000660 return ParseDirectiveSet();
661
Daniel Dunbara0d14262009-06-24 23:30:00 +0000662 // Data directives
663
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000665 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000667 return ParseDirectiveAscii(true);
668
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000670 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000671 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000672 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000674 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000676 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000677
678 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000680 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000682 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000684 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000686 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000688 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000690 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000692 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000694 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
695
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000697 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000698
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000700 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000702 return ParseDirectiveSpace();
703
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000704 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000705
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000707 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000709 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000711 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000713 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000715 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000717 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000719 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000721 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000723 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000725 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000726 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000727 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000729 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000730
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000732 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000734 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000736 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000738 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000740 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000741
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000743 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000745 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000747 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000749 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000750 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000751 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000752
Chris Lattnerebb89b42009-09-27 21:16:52 +0000753 // Look up the handler in the handler table,
754 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
755 if (Handler)
756 return (this->*Handler)(IDVal, IDLoc);
757
Kevin Enderby9c656452009-09-10 20:51:44 +0000758 // Target hook for parsing target specific directives.
759 if (!getTargetParser().ParseDirective(ID))
760 return false;
761
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000762 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000763 EatToEndOfStatement();
764 return false;
765 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000766
Chris Lattner98986712010-01-14 22:21:20 +0000767
768 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
769 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
770 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000771 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000772
Daniel Dunbar3f872332009-07-28 16:08:33 +0000773 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000774 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000775 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000776
777 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000778 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000779
Chris Lattner98986712010-01-14 22:21:20 +0000780
781 MCInst Inst;
782
783 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
784
785 // Free any parsed operands.
786 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
787 delete ParsedOperands[i];
788
789 if (MatchFail) {
790 // FIXME: We should give nicer diagnostics about the exact failure.
791 Error(IDLoc, "unrecognized instruction");
792 return true;
793 }
794
Chris Lattner2cf5f142009-06-22 01:29:09 +0000795 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000796 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000797
798 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000799 return false;
800}
Chris Lattner9a023f72009-06-24 04:43:34 +0000801
Daniel Dunbare2ace502009-08-31 08:09:09 +0000802bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000803 // FIXME: Use better location, we should use proper tokens.
804 SMLoc EqualLoc = Lexer.getLoc();
805
Daniel Dunbar821e3332009-08-31 08:09:28 +0000806 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000807 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000808 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000809 return true;
810
Daniel Dunbar3f872332009-07-28 16:08:33 +0000811 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000812 return TokError("unexpected token in assignment");
813
814 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000815 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000816
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000817 // Validate that the LHS is allowed to be a variable (either it has not been
818 // used as a symbol, or it is an absolute symbol).
819 MCSymbol *Sym = getContext().LookupSymbol(Name);
820 if (Sym) {
821 // Diagnose assignment to a label.
822 //
823 // FIXME: Diagnostics. Note the location of the definition as a label.
824 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
825 if (!Sym->isUndefined() && !Sym->isAbsolute())
826 return Error(EqualLoc, "redefinition of '" + Name + "'");
827 else if (!Sym->isVariable())
828 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
829 else if (!isa<MCConstantExpr>(Sym->getValue()))
830 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
831 Name + "'");
832 } else
833 Sym = CreateSymbol(Name);
834
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000835 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000836
837 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000838 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000839
840 return false;
841}
842
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000843/// ParseIdentifier:
844/// ::= identifier
845/// ::= string
846bool AsmParser::ParseIdentifier(StringRef &Res) {
847 if (Lexer.isNot(AsmToken::Identifier) &&
848 Lexer.isNot(AsmToken::String))
849 return true;
850
Sean Callanan18b83232010-01-19 21:44:56 +0000851 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000852
Sean Callanan79ed1a82010-01-19 20:22:31 +0000853 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000854
855 return false;
856}
857
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000858/// ParseDirectiveSet:
859/// ::= .set identifier ',' expression
860bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000861 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000862
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000863 if (ParseIdentifier(Name))
864 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000865
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000866 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000867 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000868 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000869
Daniel Dunbare2ace502009-08-31 08:09:09 +0000870 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000871}
872
Chris Lattner9a023f72009-06-24 04:43:34 +0000873/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000874/// ::= .section identifier (',' identifier)*
875/// FIXME: This should actually parse out the segment, section, attributes and
876/// sizeof_stub fields.
877bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000878 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000879
Daniel Dunbarace63122009-08-11 03:42:33 +0000880 StringRef SectionName;
881 if (ParseIdentifier(SectionName))
882 return Error(Loc, "expected identifier after '.section' directive");
883
884 // Verify there is a following comma.
885 if (!Lexer.is(AsmToken::Comma))
886 return TokError("unexpected token in '.section' directive");
887
Chris Lattnerff4bc462009-08-10 01:39:42 +0000888 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000889 SectionSpec += ",";
890
891 // Add all the tokens until the end of the line, ParseSectionSpecifier will
892 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000893 StringRef EOL = Lexer.LexUntilEndOfStatement();
894 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000895
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000897 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000898 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000899 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000900
Chris Lattnerff4bc462009-08-10 01:39:42 +0000901
902 StringRef Segment, Section;
903 unsigned TAA, StubSize;
904 std::string ErrorStr =
905 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
906 TAA, StubSize);
907
908 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000909 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000910
Chris Lattner56594f92009-07-31 17:47:16 +0000911 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000912 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000913 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000914 isText ? SectionKind::getText()
915 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000916 return false;
917}
918
Chris Lattnere15c2d72009-08-10 18:05:55 +0000919/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000920bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
921 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000922 unsigned TAA, unsigned Align,
923 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000924 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000925 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000926 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000927
Chris Lattner56594f92009-07-31 17:47:16 +0000928 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000929 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000930 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000931 isText ? SectionKind::getText()
932 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000933
934 // Set the implicit alignment, if any.
935 //
936 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
937 // alignment on the section (e.g., if one manually inserts bytes into the
938 // section, then just issueing the section switch directive will not realign
939 // the section. However, this is arguably more reasonable behavior, and there
940 // is no good reason for someone to intentionally emit incorrectly sized
941 // values into the implicitly aligned sections.
942 if (Align)
943 Out.EmitValueToAlignment(Align, 0, 1, 0);
944
Chris Lattner529fb542009-06-24 05:13:15 +0000945 return false;
946}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000947
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000948bool AsmParser::ParseEscapedString(std::string &Data) {
949 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
950
951 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000952 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000953 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
954 if (Str[i] != '\\') {
955 Data += Str[i];
956 continue;
957 }
958
959 // Recognize escaped characters. Note that this escape semantics currently
960 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
961 ++i;
962 if (i == e)
963 return TokError("unexpected backslash at end of string");
964
965 // Recognize octal sequences.
966 if ((unsigned) (Str[i] - '0') <= 7) {
967 // Consume up to three octal characters.
968 unsigned Value = Str[i] - '0';
969
970 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
971 ++i;
972 Value = Value * 8 + (Str[i] - '0');
973
974 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
975 ++i;
976 Value = Value * 8 + (Str[i] - '0');
977 }
978 }
979
980 if (Value > 255)
981 return TokError("invalid octal escape sequence (out of range)");
982
983 Data += (unsigned char) Value;
984 continue;
985 }
986
987 // Otherwise recognize individual escapes.
988 switch (Str[i]) {
989 default:
990 // Just reject invalid escape sequences for now.
991 return TokError("invalid escape sequence (unrecognized character)");
992
993 case 'b': Data += '\b'; break;
994 case 'f': Data += '\f'; break;
995 case 'n': Data += '\n'; break;
996 case 'r': Data += '\r'; break;
997 case 't': Data += '\t'; break;
998 case '"': Data += '"'; break;
999 case '\\': Data += '\\'; break;
1000 }
1001 }
1002
1003 return false;
1004}
1005
Daniel Dunbara0d14262009-06-24 23:30:00 +00001006/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001007/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001008bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001009 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001010 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001011 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001012 return TokError("expected string in '.ascii' or '.asciz' directive");
1013
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001014 std::string Data;
1015 if (ParseEscapedString(Data))
1016 return true;
1017
Chris Lattneraaec2052010-01-19 19:46:13 +00001018 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001019 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001020 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021
Sean Callanan79ed1a82010-01-19 20:22:31 +00001022 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023
Daniel Dunbar3f872332009-07-28 16:08:33 +00001024 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001025 break;
1026
Daniel Dunbar3f872332009-07-28 16:08:33 +00001027 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001029 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030 }
1031 }
1032
Sean Callanan79ed1a82010-01-19 20:22:31 +00001033 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001034 return false;
1035}
1036
1037/// ParseDirectiveValue
1038/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1039bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001040 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001041 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001042 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001043 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001044 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045 return true;
1046
Chris Lattneraaec2052010-01-19 19:46:13 +00001047 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001048
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001050 break;
1051
1052 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001055 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001056 }
1057 }
1058
Sean Callanan79ed1a82010-01-19 20:22:31 +00001059 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060 return false;
1061}
1062
1063/// ParseDirectiveSpace
1064/// ::= .space expression [ , expression ]
1065bool AsmParser::ParseDirectiveSpace() {
1066 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001067 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return true;
1069
1070 int64_t FillExpr = 0;
1071 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001072 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1073 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001074 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001075 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076
Daniel Dunbar475839e2009-06-29 20:37:27 +00001077 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078 return true;
1079
1080 HasFillExpr = true;
1081
Daniel Dunbar3f872332009-07-28 16:08:33 +00001082 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083 return TokError("unexpected token in '.space' directive");
1084 }
1085
Sean Callanan79ed1a82010-01-19 20:22:31 +00001086 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087
1088 if (NumBytes <= 0)
1089 return TokError("invalid number of bytes in '.space' directive");
1090
1091 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001092 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093
1094 return false;
1095}
1096
1097/// ParseDirectiveFill
1098/// ::= .fill expression , expression , expression
1099bool AsmParser::ParseDirectiveFill() {
1100 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001101 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001102 return true;
1103
Daniel Dunbar3f872332009-07-28 16:08:33 +00001104 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001105 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001106 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001107
1108 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001109 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001110 return true;
1111
Daniel Dunbar3f872332009-07-28 16:08:33 +00001112 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001114 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115
1116 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001117 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001118 return true;
1119
Daniel Dunbar3f872332009-07-28 16:08:33 +00001120 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001121 return TokError("unexpected token in '.fill' directive");
1122
Sean Callanan79ed1a82010-01-19 20:22:31 +00001123 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001125 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1126 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127
1128 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001129 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1130 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131
1132 return false;
1133}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001134
1135/// ParseDirectiveOrg
1136/// ::= .org expression [ , expression ]
1137bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001138 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001139 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001140 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001141 return true;
1142
1143 // Parse optional fill expression.
1144 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001145 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1146 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001147 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001148 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001149
Daniel Dunbar475839e2009-06-29 20:37:27 +00001150 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001151 return true;
1152
Daniel Dunbar3f872332009-07-28 16:08:33 +00001153 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001154 return TokError("unexpected token in '.org' directive");
1155 }
1156
Sean Callanan79ed1a82010-01-19 20:22:31 +00001157 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001158
1159 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1160 // has to be relative to the current section.
1161 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001162
1163 return false;
1164}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001165
1166/// ParseDirectiveAlign
1167/// ::= {.align, ...} expression [ , expression [ , expression ]]
1168bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001169 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001170 int64_t Alignment;
1171 if (ParseAbsoluteExpression(Alignment))
1172 return true;
1173
1174 SMLoc MaxBytesLoc;
1175 bool HasFillExpr = false;
1176 int64_t FillExpr = 0;
1177 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001178 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1179 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001180 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001181 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001182
1183 // The fill expression can be omitted while specifying a maximum number of
1184 // alignment bytes, e.g:
1185 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001186 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001187 HasFillExpr = true;
1188 if (ParseAbsoluteExpression(FillExpr))
1189 return true;
1190 }
1191
Daniel Dunbar3f872332009-07-28 16:08:33 +00001192 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1193 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001194 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001195 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001196
1197 MaxBytesLoc = Lexer.getLoc();
1198 if (ParseAbsoluteExpression(MaxBytesToFill))
1199 return true;
1200
Daniel Dunbar3f872332009-07-28 16:08:33 +00001201 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001202 return TokError("unexpected token in directive");
1203 }
1204 }
1205
Sean Callanan79ed1a82010-01-19 20:22:31 +00001206 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207
1208 if (!HasFillExpr) {
1209 // FIXME: Sometimes fill with nop.
1210 FillExpr = 0;
1211 }
1212
1213 // Compute alignment in bytes.
1214 if (IsPow2) {
1215 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001216 if (Alignment >= 32) {
1217 Error(AlignmentLoc, "invalid alignment value");
1218 Alignment = 31;
1219 }
1220
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001221 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001222 }
1223
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001224 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001225 if (MaxBytesLoc.isValid()) {
1226 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001227 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1228 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001229 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001230 }
1231
1232 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001233 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1234 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001235 MaxBytesToFill = 0;
1236 }
1237 }
1238
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001239 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1240 // the TextAlignFillValue.
1241 if(Out.getCurrentSection()->getKind().isText() &&
1242 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1243 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1244 else
1245 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1246 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001247
1248 return false;
1249}
1250
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001251/// ParseDirectiveSymbolAttribute
1252/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001253bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001254 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001255 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001256 StringRef Name;
1257
1258 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001259 return TokError("expected identifier in directive");
1260
Daniel Dunbar959fd882009-08-26 22:13:22 +00001261 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001262
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001263 Out.EmitSymbolAttribute(Sym, Attr);
1264
Daniel Dunbar3f872332009-07-28 16:08:33 +00001265 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001266 break;
1267
Daniel Dunbar3f872332009-07-28 16:08:33 +00001268 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001269 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001270 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001271 }
1272 }
1273
Sean Callanan79ed1a82010-01-19 20:22:31 +00001274 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001275 return false;
1276}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001277
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001278/// ParseDirectiveDarwinSymbolDesc
1279/// ::= .desc identifier , expression
1280bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001281 StringRef Name;
1282 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001283 return TokError("expected identifier in directive");
1284
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001285 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001286 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001287
Daniel Dunbar3f872332009-07-28 16:08:33 +00001288 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001289 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001290 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001291
1292 SMLoc DescLoc = Lexer.getLoc();
1293 int64_t DescValue;
1294 if (ParseAbsoluteExpression(DescValue))
1295 return true;
1296
Daniel Dunbar3f872332009-07-28 16:08:33 +00001297 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001298 return TokError("unexpected token in '.desc' directive");
1299
Sean Callanan79ed1a82010-01-19 20:22:31 +00001300 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001301
1302 // Set the n_desc field of this Symbol to this DescValue
1303 Out.EmitSymbolDesc(Sym, DescValue);
1304
1305 return false;
1306}
1307
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001308/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001309/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1310bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001311 SMLoc IDLoc = Lexer.getLoc();
1312 StringRef Name;
1313 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001314 return TokError("expected identifier in directive");
1315
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001316 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001317 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001318
Daniel Dunbar3f872332009-07-28 16:08:33 +00001319 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001320 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001321 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001322
1323 int64_t Size;
1324 SMLoc SizeLoc = Lexer.getLoc();
1325 if (ParseAbsoluteExpression(Size))
1326 return true;
1327
1328 int64_t Pow2Alignment = 0;
1329 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001330 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001331 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001332 Pow2AlignmentLoc = Lexer.getLoc();
1333 if (ParseAbsoluteExpression(Pow2Alignment))
1334 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001335
1336 // If this target takes alignments in bytes (not log) validate and convert.
1337 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1338 if (!isPowerOf2_64(Pow2Alignment))
1339 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1340 Pow2Alignment = Log2_64(Pow2Alignment);
1341 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001342 }
1343
Daniel Dunbar3f872332009-07-28 16:08:33 +00001344 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001345 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001346
Sean Callanan79ed1a82010-01-19 20:22:31 +00001347 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001348
Chris Lattner1fc3d752009-07-09 17:25:12 +00001349 // NOTE: a size of zero for a .comm should create a undefined symbol
1350 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001351 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001352 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1353 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001354
1355 // NOTE: The alignment in the directive is a power of 2 value, the assember
1356 // may internally end up wanting an alignment in bytes.
1357 // FIXME: Diagnose overflow.
1358 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001359 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1360 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001361
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001362 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001363 return Error(IDLoc, "invalid symbol redefinition");
1364
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001365 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001366 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001367 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001368 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1369 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001370 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001371 Sym, Size, 1 << Pow2Alignment);
1372 return false;
1373 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001374
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001375 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001376 return false;
1377}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001378
1379/// ParseDirectiveDarwinZerofill
1380/// ::= .zerofill segname , sectname [, identifier , size_expression [
1381/// , align_expression ]]
1382bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001383 // FIXME: Handle quoted names here.
1384
Daniel Dunbar3f872332009-07-28 16:08:33 +00001385 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001386 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001387 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001388 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001389
Daniel Dunbar3f872332009-07-28 16:08:33 +00001390 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001391 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001392 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001393
Daniel Dunbar3f872332009-07-28 16:08:33 +00001394 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001395 return TokError("expected section name after comma in '.zerofill' "
1396 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001397 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001398 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001399
Chris Lattner9be3fee2009-07-10 22:20:30 +00001400 // If this is the end of the line all that was wanted was to create the
1401 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001402 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001403 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001404 Out.EmitZerofill(getMachOSection(Segment, Section,
1405 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001406 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001407 return false;
1408 }
1409
Daniel Dunbar3f872332009-07-28 16:08:33 +00001410 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001411 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001412 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001413
Daniel Dunbar3f872332009-07-28 16:08:33 +00001414 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001415 return TokError("expected identifier in directive");
1416
1417 // handle the identifier as the key symbol.
1418 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001419 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001420 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001421
Daniel Dunbar3f872332009-07-28 16:08:33 +00001422 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001423 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001424 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001425
1426 int64_t Size;
1427 SMLoc SizeLoc = Lexer.getLoc();
1428 if (ParseAbsoluteExpression(Size))
1429 return true;
1430
1431 int64_t Pow2Alignment = 0;
1432 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001433 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001434 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001435 Pow2AlignmentLoc = Lexer.getLoc();
1436 if (ParseAbsoluteExpression(Pow2Alignment))
1437 return true;
1438 }
1439
Daniel Dunbar3f872332009-07-28 16:08:33 +00001440 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001441 return TokError("unexpected token in '.zerofill' directive");
1442
Sean Callanan79ed1a82010-01-19 20:22:31 +00001443 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001444
1445 if (Size < 0)
1446 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1447 "than zero");
1448
1449 // NOTE: The alignment in the directive is a power of 2 value, the assember
1450 // may internally end up wanting an alignment in bytes.
1451 // FIXME: Diagnose overflow.
1452 if (Pow2Alignment < 0)
1453 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1454 "can't be less than zero");
1455
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001456 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001457 return Error(IDLoc, "invalid symbol redefinition");
1458
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001459 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001460 //
1461 // FIXME: Arch specific.
1462 Out.EmitZerofill(getMachOSection(Segment, Section,
1463 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001464 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001465 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001466
1467 return false;
1468}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001469
1470/// ParseDirectiveDarwinSubsectionsViaSymbols
1471/// ::= .subsections_via_symbols
1472bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001473 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001474 return TokError("unexpected token in '.subsections_via_symbols' directive");
1475
Sean Callanan79ed1a82010-01-19 20:22:31 +00001476 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001477
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001478 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001479
1480 return false;
1481}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001482
1483/// ParseDirectiveAbort
1484/// ::= .abort [ "abort_string" ]
1485bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001486 // FIXME: Use loc from directive.
1487 SMLoc Loc = Lexer.getLoc();
1488
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001489 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001490 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1491 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001492 return TokError("expected string in '.abort' directive");
1493
Sean Callanan18b83232010-01-19 21:44:56 +00001494 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001495
Sean Callanan79ed1a82010-01-19 20:22:31 +00001496 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001497 }
1498
Daniel Dunbar3f872332009-07-28 16:08:33 +00001499 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001500 return TokError("unexpected token in '.abort' directive");
1501
Sean Callanan79ed1a82010-01-19 20:22:31 +00001502 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001503
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001504 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001505 if (Str.empty())
1506 Error(Loc, ".abort detected. Assembly stopping.");
1507 else
1508 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001509
1510 return false;
1511}
Kevin Enderby71148242009-07-14 21:35:03 +00001512
1513/// ParseDirectiveLsym
1514/// ::= .lsym identifier , expression
1515bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001516 StringRef Name;
1517 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001518 return TokError("expected identifier in directive");
1519
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001520 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001521 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001522
Daniel Dunbar3f872332009-07-28 16:08:33 +00001523 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001524 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001525 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001526
Daniel Dunbar821e3332009-08-31 08:09:28 +00001527 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001528 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001529 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001530 return true;
1531
Daniel Dunbar3f872332009-07-28 16:08:33 +00001532 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001533 return TokError("unexpected token in '.lsym' directive");
1534
Sean Callanan79ed1a82010-01-19 20:22:31 +00001535 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001536
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001537 // We don't currently support this directive.
1538 //
1539 // FIXME: Diagnostic location!
1540 (void) Sym;
1541 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001542}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001543
1544/// ParseDirectiveInclude
1545/// ::= .include "filename"
1546bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001547 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001548 return TokError("expected string in '.include' directive");
1549
Sean Callanan18b83232010-01-19 21:44:56 +00001550 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001551 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001552 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001553
Daniel Dunbar3f872332009-07-28 16:08:33 +00001554 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001555 return TokError("unexpected token in '.include' directive");
1556
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001557 // Strip the quotes.
1558 Filename = Filename.substr(1, Filename.size()-2);
1559
1560 // Attempt to switch the lexer to the included file before consuming the end
1561 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001562 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001563 PrintMessage(IncludeLoc,
1564 "Could not find include file '" + Filename + "'",
1565 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001566 return true;
1567 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001568
1569 return false;
1570}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001571
1572/// ParseDirectiveDarwinDumpOrLoad
1573/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001574bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001575 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001576 return TokError("expected string in '.dump' or '.load' directive");
1577
Sean Callanan79ed1a82010-01-19 20:22:31 +00001578 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001579
Daniel Dunbar3f872332009-07-28 16:08:33 +00001580 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001581 return TokError("unexpected token in '.dump' or '.load' directive");
1582
Sean Callanan79ed1a82010-01-19 20:22:31 +00001583 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001584
Kevin Enderby5026ae42009-07-20 20:25:37 +00001585 // FIXME: If/when .dump and .load are implemented they will be done in the
1586 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001587 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001588 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001589 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001590 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001591
1592 return false;
1593}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001594
1595/// ParseDirectiveIf
1596/// ::= .if expression
1597bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1598 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001599 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001600
1601 TheCondStack.push_back(TheCondState);
1602 TheCondState.TheCond = AsmCond::IfCond;
1603 if(TheCondState.Ignore) {
1604 EatToEndOfStatement();
1605 }
1606 else {
1607 int64_t ExprValue;
1608 if (ParseAbsoluteExpression(ExprValue))
1609 return true;
1610
1611 if (Lexer.isNot(AsmToken::EndOfStatement))
1612 return TokError("unexpected token in '.if' directive");
1613
Sean Callanan79ed1a82010-01-19 20:22:31 +00001614 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001615
1616 TheCondState.CondMet = ExprValue;
1617 TheCondState.Ignore = !TheCondState.CondMet;
1618 }
1619
1620 return false;
1621}
1622
1623/// ParseDirectiveElseIf
1624/// ::= .elseif expression
1625bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1626 if (TheCondState.TheCond != AsmCond::IfCond &&
1627 TheCondState.TheCond != AsmCond::ElseIfCond)
1628 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1629 " an .elseif");
1630 TheCondState.TheCond = AsmCond::ElseIfCond;
1631
1632 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001633 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001634
1635 bool LastIgnoreState = false;
1636 if (!TheCondStack.empty())
1637 LastIgnoreState = TheCondStack.back().Ignore;
1638 if (LastIgnoreState || TheCondState.CondMet) {
1639 TheCondState.Ignore = true;
1640 EatToEndOfStatement();
1641 }
1642 else {
1643 int64_t ExprValue;
1644 if (ParseAbsoluteExpression(ExprValue))
1645 return true;
1646
1647 if (Lexer.isNot(AsmToken::EndOfStatement))
1648 return TokError("unexpected token in '.elseif' directive");
1649
Sean Callanan79ed1a82010-01-19 20:22:31 +00001650 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001651 TheCondState.CondMet = ExprValue;
1652 TheCondState.Ignore = !TheCondState.CondMet;
1653 }
1654
1655 return false;
1656}
1657
1658/// ParseDirectiveElse
1659/// ::= .else
1660bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1661 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001662 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001663
1664 if (Lexer.isNot(AsmToken::EndOfStatement))
1665 return TokError("unexpected token in '.else' directive");
1666
Sean Callanan79ed1a82010-01-19 20:22:31 +00001667 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001668
1669 if (TheCondState.TheCond != AsmCond::IfCond &&
1670 TheCondState.TheCond != AsmCond::ElseIfCond)
1671 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1672 ".elseif");
1673 TheCondState.TheCond = AsmCond::ElseCond;
1674 bool LastIgnoreState = false;
1675 if (!TheCondStack.empty())
1676 LastIgnoreState = TheCondStack.back().Ignore;
1677 if (LastIgnoreState || TheCondState.CondMet)
1678 TheCondState.Ignore = true;
1679 else
1680 TheCondState.Ignore = false;
1681
1682 return false;
1683}
1684
1685/// ParseDirectiveEndIf
1686/// ::= .endif
1687bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1688 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001689 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001690
1691 if (Lexer.isNot(AsmToken::EndOfStatement))
1692 return TokError("unexpected token in '.endif' directive");
1693
Sean Callanan79ed1a82010-01-19 20:22:31 +00001694 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001695
1696 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1697 TheCondStack.empty())
1698 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1699 ".else");
1700 if (!TheCondStack.empty()) {
1701 TheCondState = TheCondStack.back();
1702 TheCondStack.pop_back();
1703 }
1704
1705 return false;
1706}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001707
1708/// ParseDirectiveFile
1709/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001710bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001711 // FIXME: I'm not sure what this is.
1712 int64_t FileNumber = -1;
1713 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001714 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001715 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001716
1717 if (FileNumber < 1)
1718 return TokError("file number less than one");
1719 }
1720
1721 if (Lexer.isNot(AsmToken::String))
1722 return TokError("unexpected token in '.file' directive");
1723
Chris Lattnerd32e8032010-01-25 19:02:58 +00001724 StringRef Filename = getTok().getString();
1725 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001726 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001727
1728 if (Lexer.isNot(AsmToken::EndOfStatement))
1729 return TokError("unexpected token in '.file' directive");
1730
Chris Lattnerd32e8032010-01-25 19:02:58 +00001731 if (FileNumber == -1)
1732 Out.EmitFileDirective(Filename);
1733 else
1734 Out.EmitDwarfFileDirective(FileNumber, Filename);
1735
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001736 return false;
1737}
1738
1739/// ParseDirectiveLine
1740/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001741bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001742 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1743 if (Lexer.isNot(AsmToken::Integer))
1744 return TokError("unexpected token in '.line' directive");
1745
Sean Callanan18b83232010-01-19 21:44:56 +00001746 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001747 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001748 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001749
1750 // FIXME: Do something with the .line.
1751 }
1752
1753 if (Lexer.isNot(AsmToken::EndOfStatement))
1754 return TokError("unexpected token in '.file' directive");
1755
1756 return false;
1757}
1758
1759
1760/// ParseDirectiveLoc
1761/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001762bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001763 if (Lexer.isNot(AsmToken::Integer))
1764 return TokError("unexpected token in '.loc' directive");
1765
1766 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001767 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001768 (void) FileNumber;
1769 // FIXME: Validate file.
1770
Sean Callanan79ed1a82010-01-19 20:22:31 +00001771 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001772 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1773 if (Lexer.isNot(AsmToken::Integer))
1774 return TokError("unexpected token in '.loc' directive");
1775
Sean Callanan18b83232010-01-19 21:44:56 +00001776 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001777 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001778 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001779
1780 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1781 if (Lexer.isNot(AsmToken::Integer))
1782 return TokError("unexpected token in '.loc' directive");
1783
Sean Callanan18b83232010-01-19 21:44:56 +00001784 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001785 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001786 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001787
1788 // FIXME: Do something with the .loc.
1789 }
1790 }
1791
1792 if (Lexer.isNot(AsmToken::EndOfStatement))
1793 return TokError("unexpected token in '.file' directive");
1794
1795 return false;
1796}
1797