blob: 0e0c1a4de7a5fddd565e4764b4a8d4e28bb28984 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AsmParser.h"
Daniel Dunbar475839e2009-06-29 20:37:27 +000015
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattner98986712010-01-14 22:21:20 +000021#include "llvm/MC/MCParsedAsmOperand.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000022#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000024#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfffff912009-10-16 01:34:54 +000025#include "llvm/MC/MCValue.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000027#include "llvm/Support/SourceMgr.h"
28#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Chris Lattneraaec2052010-01-19 19:46:13 +000032
33enum { DEFAULT_ADDRSPACE = 0 };
34
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000035// Mach-O section uniquing.
36//
37// FIXME: Figure out where this should live, it should be shared by
38// TargetLoweringObjectFile.
39typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
40
Chris Lattnerebb89b42009-09-27 21:16:52 +000041AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
42 const MCAsmInfo &_MAI)
Sean Callanan10d33a42010-01-20 22:45:23 +000043 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
Chris Lattnerebb89b42009-09-27 21:16:52 +000044 SectionUniquingMap(0) {
45 // 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}
107
Sean Callanan79ed1a82010-01-19 20:22:31 +0000108const AsmToken &AsmParser::Lex() {
Sean Callanan79036e42010-01-20 22:18:24 +0000109 const AsmToken &tok = Lexer.Lex();
110
111 if (tok.is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000112 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000113
114 return tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000115}
116
Chris Lattner27aa7d22009-06-21 20:16:42 +0000117bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000118 // Create the initial section.
119 //
120 // FIXME: Support -n.
121 // FIXME: Target hook & command line option for initial section.
122 Out.SwitchSection(getMachOSection("__TEXT", "__text",
123 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
124 0, SectionKind()));
125
126
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000127 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000128 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000129
Chris Lattnerb717fb02009-07-02 21:53:43 +0000130 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000131
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000132 AsmCond StartingCondState = TheCondState;
133
Chris Lattnerb717fb02009-07-02 21:53:43 +0000134 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000135 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000136 // Handle conditional assembly here before calling ParseStatement()
137 if (Lexer.getKind() == AsmToken::Identifier) {
138 // If we have an identifier, handle it as the key symbol.
Sean Callanan18b83232010-01-19 21:44:56 +0000139 AsmToken ID = getTok();
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000140 SMLoc IDLoc = ID.getLoc();
141 StringRef IDVal = ID.getString();
142
143 if (IDVal == ".if" ||
144 IDVal == ".elseif" ||
145 IDVal == ".else" ||
146 IDVal == ".endif") {
147 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
148 continue;
149 HadError = true;
150 EatToEndOfStatement();
151 continue;
152 }
153 }
154 if (TheCondState.Ignore) {
155 EatToEndOfStatement();
156 continue;
157 }
158
Chris Lattnerb717fb02009-07-02 21:53:43 +0000159 if (!ParseStatement()) continue;
160
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000161 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000162 HadError = true;
163 EatToEndOfStatement();
164 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000165
166 if (TheCondState.TheCond != StartingCondState.TheCond ||
167 TheCondState.Ignore != StartingCondState.Ignore)
168 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000169
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000170 if (!HadError)
171 Out.Finish();
172
Chris Lattnerb717fb02009-07-02 21:53:43 +0000173 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000174}
175
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000176/// ParseConditionalAssemblyDirectives - parse the conditional assembly
177/// directives
178bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
179 SMLoc DirectiveLoc) {
180 if (Directive == ".if")
181 return ParseDirectiveIf(DirectiveLoc);
182 if (Directive == ".elseif")
183 return ParseDirectiveElseIf(DirectiveLoc);
184 if (Directive == ".else")
185 return ParseDirectiveElse(DirectiveLoc);
186 if (Directive == ".endif")
187 return ParseDirectiveEndIf(DirectiveLoc);
188 return true;
189}
190
Chris Lattner2cf5f142009-06-22 01:29:09 +0000191/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
192void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000193 while (Lexer.isNot(AsmToken::EndOfStatement) &&
194 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000195 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000196
197 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000198 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000199 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000200}
201
Chris Lattnerc4193832009-06-22 05:51:26 +0000202
Chris Lattner74ec1a32009-06-22 06:32:03 +0000203/// ParseParenExpr - Parse a paren expression and return it.
204/// NOTE: This assumes the leading '(' has already been consumed.
205///
206/// parenexpr ::= expr)
207///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000208bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000209 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000210 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000211 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000212 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000213 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000214 return false;
215}
Chris Lattnerc4193832009-06-22 05:51:26 +0000216
Daniel Dunbar959fd882009-08-26 22:13:22 +0000217MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
218 if (MCSymbol *S = Ctx.LookupSymbol(Name))
219 return S;
220
221 // If the label starts with L it is an assembler temporary label.
222 if (Name.startswith("L"))
223 return Ctx.CreateTemporarySymbol(Name);
224
225 return Ctx.CreateSymbol(Name);
226}
227
Chris Lattner74ec1a32009-06-22 06:32:03 +0000228/// ParsePrimaryExpr - Parse a primary expression and return it.
229/// primaryexpr ::= (parenexpr
230/// primaryexpr ::= symbol
231/// primaryexpr ::= number
232/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000233bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000234 switch (Lexer.getKind()) {
235 default:
236 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000237 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000238 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000239 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000240 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000241 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000242 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000243 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000244 case AsmToken::Identifier: {
245 // This is a symbol reference.
Sean Callanan18b83232010-01-19 21:44:56 +0000246 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000247 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000248 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000249
250 // If this is an absolute variable reference, substitute it now to preserve
251 // semantics in the face of reassignment.
252 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
253 Res = Sym->getValue();
254 return false;
255 }
256
257 // Otherwise create a symbol ref.
258 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000259 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000260 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000261 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000262 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000263 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000264 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000265 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000266 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000267 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000268 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000269 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000270 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000271 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000272 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000273 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000274 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000275 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000276 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000277 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000278 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000279 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000280 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000281 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000282 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000283 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000284 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000285 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000286 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000287 }
288}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000289
Chris Lattnerb4307b32010-01-15 19:28:38 +0000290bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000291 SMLoc EndLoc;
292 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000293}
294
Chris Lattner74ec1a32009-06-22 06:32:03 +0000295/// ParseExpression - Parse an expression and return it.
296///
297/// expr ::= expr +,- expr -> lowest.
298/// expr ::= expr |,^,&,! expr -> middle.
299/// expr ::= expr *,/,%,<<,>> expr -> highest.
300/// expr ::= primaryexpr
301///
Chris Lattner54482b42010-01-15 19:39:23 +0000302bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000303 Res = 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000304 return ParsePrimaryExpr(Res, EndLoc) ||
305 ParseBinOpRHS(1, Res, EndLoc);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000306}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000307
Chris Lattnerb4307b32010-01-15 19:28:38 +0000308bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
309 if (ParseParenExpr(Res, EndLoc))
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000310 return true;
311
312 return false;
313}
314
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000316 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000317
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000318 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000319 if (ParseExpression(Expr))
320 return true;
321
Daniel Dunbare00b0112009-10-16 01:57:52 +0000322 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000323 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000324
325 return false;
326}
327
Daniel Dunbar3f872332009-07-28 16:08:33 +0000328static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000329 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000330 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000331 default:
332 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333
334 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000336 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000337 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000338 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000339 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return 1;
341
342 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000344 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000345 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000346 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000347 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000350 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::ExclaimEqual:
353 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000354 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000355 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000356 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000357 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000359 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000360 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000361 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000363 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000364 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000366 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 return 2;
368
369 // Intermediate Precedence: |, &, ^
370 //
371 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000372 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000374 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000379 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return 3;
381
382 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000383 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000384 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000386 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000387 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000392 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000393 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000395 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000396 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000397 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 }
399}
400
401
402/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
403/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000404bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
405 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000406 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000407 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000408 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000409
410 // If the next token is lower precedence than we are allowed to eat, return
411 // successfully with what we ate already.
412 if (TokPrec < Precedence)
413 return false;
414
Sean Callanan79ed1a82010-01-19 20:22:31 +0000415 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000416
417 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000418 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000419 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000420
421 // If BinOp binds less tightly with RHS than the operator after RHS, let
422 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000423 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000424 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000425 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000426 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000427 }
428
Daniel Dunbar475839e2009-06-29 20:37:27 +0000429 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000430 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000431 }
432}
433
Chris Lattnerc4193832009-06-22 05:51:26 +0000434
435
436
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000437/// ParseStatement:
438/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000439/// ::= Label* Directive ...Operands... EndOfStatement
440/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000441bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000442 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000443 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000444 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000445 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000446
447 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000448 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000449 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000450 StringRef IDVal;
451 if (ParseIdentifier(IDVal))
452 return TokError("unexpected token at start of statement");
453
454 // FIXME: Recurse on local labels?
455
456 // See what kind of statement we have.
457 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000458 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000459 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000460 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000461
462 // Diagnose attempt to use a variable as a label.
463 //
464 // FIXME: Diagnostics. Note the location of the definition as a label.
465 // FIXME: This doesn't diagnose assignment to a symbol which has been
466 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000467 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000468 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000469 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000470
Daniel Dunbar959fd882009-08-26 22:13:22 +0000471 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000472 Out.EmitLabel(Sym);
473
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000474 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000475 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000476
Daniel Dunbar3f872332009-07-28 16:08:33 +0000477 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000478 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000479 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000480
Daniel Dunbare2ace502009-08-31 08:09:09 +0000481 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000482
483 default: // Normal instruction or directive.
484 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000485 }
486
487 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000488 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000489 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000490 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000491 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000493 // FIXME: This changes behavior based on the -static flag to the
494 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000495 return ParseDirectiveSectionSwitch("__TEXT", "__text",
496 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000497 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000498 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000502 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
503 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000504 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000505 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000506 MCSectionMachO::S_4BYTE_LITERALS,
507 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000508 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000509 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000510 MCSectionMachO::S_8BYTE_LITERALS,
511 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000512 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000513 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000514 MCSectionMachO::S_16BYTE_LITERALS,
515 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000516 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000517 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000519 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000521 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000522 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000523 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
524
525 // FIXME: The assembler manual claims that this has the self modify code
526 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000527 if (IDVal == ".symbol_stub")
528 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
529 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000530 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
531 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000532 0, 16);
533 // FIXME: PowerPC only?
534 if (IDVal == ".picsymbol_stub")
535 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
536 MCSectionMachO::S_SYMBOL_STUBS |
537 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
538 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000540 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
543
544 // FIXME: The section names of these two are misspelled in the assembler
545 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000546 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000547 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
548 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
549 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000550 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000551 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
552 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
553 4);
554
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000555 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000558 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
560 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000561 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000562 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000563 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
564 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000565 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000566 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000567
568
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 return ParseDirectiveSectionSwitch("__OBJC", "__class",
571 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000573 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
574 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
577 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000578 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000579 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
580 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000581 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000582 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
583 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000585 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
586 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000587 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000588 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
589 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000590 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000591 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
592 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
595 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
596 MCSectionMachO::S_LITERAL_POINTERS,
597 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
601 MCSectionMachO::S_LITERAL_POINTERS,
602 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
605 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 return ParseDirectiveSectionSwitch("__OBJC", "__category",
608 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000610 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
611 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
614 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000616 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
617 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000619 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
620 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000622 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
623 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000625 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
626 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000628 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
629 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000630
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000631 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000633 return ParseDirectiveSet();
634
Daniel Dunbara0d14262009-06-24 23:30:00 +0000635 // Data directives
636
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000638 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000640 return ParseDirectiveAscii(true);
641
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000643 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000644 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000645 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000647 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000649 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000650
651 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000653 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000655 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000657 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000659 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000661 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000663 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000665 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000667 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
668
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000670 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000671
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000673 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000675 return ParseDirectiveSpace();
676
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000677 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000678
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000680 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000682 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000684 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000686 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000688 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000690 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000692 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000693 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000694 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000695 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000696 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000698 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000700 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000702 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
703
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000704 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000705 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000707 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000709 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000711 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000713 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000714
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000716 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000718 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000720 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000722 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000724 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000725
Chris Lattnerebb89b42009-09-27 21:16:52 +0000726 // Look up the handler in the handler table,
727 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
728 if (Handler)
729 return (this->*Handler)(IDVal, IDLoc);
730
Kevin Enderby9c656452009-09-10 20:51:44 +0000731 // Target hook for parsing target specific directives.
732 if (!getTargetParser().ParseDirective(ID))
733 return false;
734
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000735 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000736 EatToEndOfStatement();
737 return false;
738 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000739
Chris Lattner98986712010-01-14 22:21:20 +0000740
741 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
742 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
743 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000744 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000745
Daniel Dunbar3f872332009-07-28 16:08:33 +0000746 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000747 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000748 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000749
750 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000751 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000752
Chris Lattner98986712010-01-14 22:21:20 +0000753
754 MCInst Inst;
755
756 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
757
758 // Free any parsed operands.
759 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
760 delete ParsedOperands[i];
761
762 if (MatchFail) {
763 // FIXME: We should give nicer diagnostics about the exact failure.
764 Error(IDLoc, "unrecognized instruction");
765 return true;
766 }
767
Chris Lattner2cf5f142009-06-22 01:29:09 +0000768 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000769 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000770
771 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000772 return false;
773}
Chris Lattner9a023f72009-06-24 04:43:34 +0000774
Daniel Dunbare2ace502009-08-31 08:09:09 +0000775bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000776 // FIXME: Use better location, we should use proper tokens.
777 SMLoc EqualLoc = Lexer.getLoc();
778
Daniel Dunbar821e3332009-08-31 08:09:28 +0000779 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000780 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000781 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000782 return true;
783
Daniel Dunbar3f872332009-07-28 16:08:33 +0000784 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000785 return TokError("unexpected token in assignment");
786
787 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000788 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000789
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000790 // Validate that the LHS is allowed to be a variable (either it has not been
791 // used as a symbol, or it is an absolute symbol).
792 MCSymbol *Sym = getContext().LookupSymbol(Name);
793 if (Sym) {
794 // Diagnose assignment to a label.
795 //
796 // FIXME: Diagnostics. Note the location of the definition as a label.
797 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
798 if (!Sym->isUndefined() && !Sym->isAbsolute())
799 return Error(EqualLoc, "redefinition of '" + Name + "'");
800 else if (!Sym->isVariable())
801 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
802 else if (!isa<MCConstantExpr>(Sym->getValue()))
803 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
804 Name + "'");
805 } else
806 Sym = CreateSymbol(Name);
807
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000808 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000809
810 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000811 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000812
813 return false;
814}
815
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000816/// ParseIdentifier:
817/// ::= identifier
818/// ::= string
819bool AsmParser::ParseIdentifier(StringRef &Res) {
820 if (Lexer.isNot(AsmToken::Identifier) &&
821 Lexer.isNot(AsmToken::String))
822 return true;
823
Sean Callanan18b83232010-01-19 21:44:56 +0000824 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000825
Sean Callanan79ed1a82010-01-19 20:22:31 +0000826 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000827
828 return false;
829}
830
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000831/// ParseDirectiveSet:
832/// ::= .set identifier ',' expression
833bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000834 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000835
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000836 if (ParseIdentifier(Name))
837 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000838
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000839 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000840 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000841 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000842
Daniel Dunbare2ace502009-08-31 08:09:09 +0000843 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000844}
845
Chris Lattner9a023f72009-06-24 04:43:34 +0000846/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000847/// ::= .section identifier (',' identifier)*
848/// FIXME: This should actually parse out the segment, section, attributes and
849/// sizeof_stub fields.
850bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000851 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000852
Daniel Dunbarace63122009-08-11 03:42:33 +0000853 StringRef SectionName;
854 if (ParseIdentifier(SectionName))
855 return Error(Loc, "expected identifier after '.section' directive");
856
857 // Verify there is a following comma.
858 if (!Lexer.is(AsmToken::Comma))
859 return TokError("unexpected token in '.section' directive");
860
Chris Lattnerff4bc462009-08-10 01:39:42 +0000861 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000862 SectionSpec += ",";
863
864 // Add all the tokens until the end of the line, ParseSectionSpecifier will
865 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000866 StringRef EOL = Lexer.LexUntilEndOfStatement();
867 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000868
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000870 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000871 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000872 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000873
Chris Lattnerff4bc462009-08-10 01:39:42 +0000874
875 StringRef Segment, Section;
876 unsigned TAA, StubSize;
877 std::string ErrorStr =
878 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
879 TAA, StubSize);
880
881 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000882 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000883
Chris Lattner56594f92009-07-31 17:47:16 +0000884 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000885 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
886 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000887 return false;
888}
889
Chris Lattnere15c2d72009-08-10 18:05:55 +0000890/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000891bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
892 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000893 unsigned TAA, unsigned Align,
894 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000895 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000896 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000897 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000898
Chris Lattner56594f92009-07-31 17:47:16 +0000899 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000900 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
901 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000902
903 // Set the implicit alignment, if any.
904 //
905 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
906 // alignment on the section (e.g., if one manually inserts bytes into the
907 // section, then just issueing the section switch directive will not realign
908 // the section. However, this is arguably more reasonable behavior, and there
909 // is no good reason for someone to intentionally emit incorrectly sized
910 // values into the implicitly aligned sections.
911 if (Align)
912 Out.EmitValueToAlignment(Align, 0, 1, 0);
913
Chris Lattner529fb542009-06-24 05:13:15 +0000914 return false;
915}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000916
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000917bool AsmParser::ParseEscapedString(std::string &Data) {
918 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
919
920 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000921 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000922 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
923 if (Str[i] != '\\') {
924 Data += Str[i];
925 continue;
926 }
927
928 // Recognize escaped characters. Note that this escape semantics currently
929 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
930 ++i;
931 if (i == e)
932 return TokError("unexpected backslash at end of string");
933
934 // Recognize octal sequences.
935 if ((unsigned) (Str[i] - '0') <= 7) {
936 // Consume up to three octal characters.
937 unsigned Value = Str[i] - '0';
938
939 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
940 ++i;
941 Value = Value * 8 + (Str[i] - '0');
942
943 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
944 ++i;
945 Value = Value * 8 + (Str[i] - '0');
946 }
947 }
948
949 if (Value > 255)
950 return TokError("invalid octal escape sequence (out of range)");
951
952 Data += (unsigned char) Value;
953 continue;
954 }
955
956 // Otherwise recognize individual escapes.
957 switch (Str[i]) {
958 default:
959 // Just reject invalid escape sequences for now.
960 return TokError("invalid escape sequence (unrecognized character)");
961
962 case 'b': Data += '\b'; break;
963 case 'f': Data += '\f'; break;
964 case 'n': Data += '\n'; break;
965 case 'r': Data += '\r'; break;
966 case 't': Data += '\t'; break;
967 case '"': Data += '"'; break;
968 case '\\': Data += '\\'; break;
969 }
970 }
971
972 return false;
973}
974
Daniel Dunbara0d14262009-06-24 23:30:00 +0000975/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000976/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000977bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000978 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000979 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000980 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000981 return TokError("expected string in '.ascii' or '.asciz' directive");
982
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000983 std::string Data;
984 if (ParseEscapedString(Data))
985 return true;
986
Chris Lattneraaec2052010-01-19 19:46:13 +0000987 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000988 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +0000989 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000990
Sean Callanan79ed1a82010-01-19 20:22:31 +0000991 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000992
Daniel Dunbar3f872332009-07-28 16:08:33 +0000993 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000994 break;
995
Daniel Dunbar3f872332009-07-28 16:08:33 +0000996 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000997 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000998 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000999 }
1000 }
1001
Sean Callanan79ed1a82010-01-19 20:22:31 +00001002 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001003 return false;
1004}
1005
1006/// ParseDirectiveValue
1007/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1008bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001009 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001010 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001011 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001012 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001013 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001014 return true;
1015
Chris Lattneraaec2052010-01-19 19:46:13 +00001016 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001017
Daniel Dunbar3f872332009-07-28 16:08:33 +00001018 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001019 break;
1020
1021 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001022 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001023 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001024 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001025 }
1026 }
1027
Sean Callanan79ed1a82010-01-19 20:22:31 +00001028 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001029 return false;
1030}
1031
1032/// ParseDirectiveSpace
1033/// ::= .space expression [ , expression ]
1034bool AsmParser::ParseDirectiveSpace() {
1035 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001036 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return true;
1038
1039 int64_t FillExpr = 0;
1040 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001041 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1042 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001044 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001045
Daniel Dunbar475839e2009-06-29 20:37:27 +00001046 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001047 return true;
1048
1049 HasFillExpr = true;
1050
Daniel Dunbar3f872332009-07-28 16:08:33 +00001051 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052 return TokError("unexpected token in '.space' directive");
1053 }
1054
Sean Callanan79ed1a82010-01-19 20:22:31 +00001055 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001056
1057 if (NumBytes <= 0)
1058 return TokError("invalid number of bytes in '.space' directive");
1059
1060 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001061 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062
1063 return false;
1064}
1065
1066/// ParseDirectiveFill
1067/// ::= .fill expression , expression , expression
1068bool AsmParser::ParseDirectiveFill() {
1069 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001070 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return true;
1072
Daniel Dunbar3f872332009-07-28 16:08:33 +00001073 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001074 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001075 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076
1077 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001078 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079 return true;
1080
Daniel Dunbar3f872332009-07-28 16:08:33 +00001081 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001083 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084
1085 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001086 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087 return true;
1088
Daniel Dunbar3f872332009-07-28 16:08:33 +00001089 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001090 return TokError("unexpected token in '.fill' directive");
1091
Sean Callanan79ed1a82010-01-19 20:22:31 +00001092 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001093
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001094 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1095 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001096
1097 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001098 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1099 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001100
1101 return false;
1102}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001103
1104/// ParseDirectiveOrg
1105/// ::= .org expression [ , expression ]
1106bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001107 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001108 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001109 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001110 return true;
1111
1112 // Parse optional fill expression.
1113 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001114 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1115 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001116 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001117 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001118
Daniel Dunbar475839e2009-06-29 20:37:27 +00001119 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001120 return true;
1121
Daniel Dunbar3f872332009-07-28 16:08:33 +00001122 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001123 return TokError("unexpected token in '.org' directive");
1124 }
1125
Sean Callanan79ed1a82010-01-19 20:22:31 +00001126 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001127
1128 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1129 // has to be relative to the current section.
1130 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001131
1132 return false;
1133}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001134
1135/// ParseDirectiveAlign
1136/// ::= {.align, ...} expression [ , expression [ , expression ]]
1137bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001138 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001139 int64_t Alignment;
1140 if (ParseAbsoluteExpression(Alignment))
1141 return true;
1142
1143 SMLoc MaxBytesLoc;
1144 bool HasFillExpr = false;
1145 int64_t FillExpr = 0;
1146 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1148 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001149 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001150 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001151
1152 // The fill expression can be omitted while specifying a maximum number of
1153 // alignment bytes, e.g:
1154 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001155 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001156 HasFillExpr = true;
1157 if (ParseAbsoluteExpression(FillExpr))
1158 return true;
1159 }
1160
Daniel Dunbar3f872332009-07-28 16:08:33 +00001161 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1162 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001163 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001164 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001165
1166 MaxBytesLoc = Lexer.getLoc();
1167 if (ParseAbsoluteExpression(MaxBytesToFill))
1168 return true;
1169
Daniel Dunbar3f872332009-07-28 16:08:33 +00001170 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001171 return TokError("unexpected token in directive");
1172 }
1173 }
1174
Sean Callanan79ed1a82010-01-19 20:22:31 +00001175 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001176
1177 if (!HasFillExpr) {
1178 // FIXME: Sometimes fill with nop.
1179 FillExpr = 0;
1180 }
1181
1182 // Compute alignment in bytes.
1183 if (IsPow2) {
1184 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001185 if (Alignment >= 32) {
1186 Error(AlignmentLoc, "invalid alignment value");
1187 Alignment = 31;
1188 }
1189
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001190 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001191 }
1192
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001193 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001194 if (MaxBytesLoc.isValid()) {
1195 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001196 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1197 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001198 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001199 }
1200
1201 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001202 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1203 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001204 MaxBytesToFill = 0;
1205 }
1206 }
1207
1208 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1209 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1210
1211 return false;
1212}
1213
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001214/// ParseDirectiveSymbolAttribute
1215/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1216bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001217 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001218 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001219 StringRef Name;
1220
1221 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001222 return TokError("expected identifier in directive");
1223
Daniel Dunbar959fd882009-08-26 22:13:22 +00001224 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001225
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001226 Out.EmitSymbolAttribute(Sym, Attr);
1227
Daniel Dunbar3f872332009-07-28 16:08:33 +00001228 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001229 break;
1230
Daniel Dunbar3f872332009-07-28 16:08:33 +00001231 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001232 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001233 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001234 }
1235 }
1236
Sean Callanan79ed1a82010-01-19 20:22:31 +00001237 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001238 return false;
1239}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001240
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001241/// ParseDirectiveDarwinSymbolDesc
1242/// ::= .desc identifier , expression
1243bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001244 StringRef Name;
1245 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001246 return TokError("expected identifier in directive");
1247
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001248 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001249 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001250
Daniel Dunbar3f872332009-07-28 16:08:33 +00001251 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001252 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001253 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001254
1255 SMLoc DescLoc = Lexer.getLoc();
1256 int64_t DescValue;
1257 if (ParseAbsoluteExpression(DescValue))
1258 return true;
1259
Daniel Dunbar3f872332009-07-28 16:08:33 +00001260 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001261 return TokError("unexpected token in '.desc' directive");
1262
Sean Callanan79ed1a82010-01-19 20:22:31 +00001263 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001264
1265 // Set the n_desc field of this Symbol to this DescValue
1266 Out.EmitSymbolDesc(Sym, DescValue);
1267
1268 return false;
1269}
1270
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001271/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001272/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1273bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001274 SMLoc IDLoc = Lexer.getLoc();
1275 StringRef Name;
1276 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001277 return TokError("expected identifier in directive");
1278
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001279 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001280 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001281
Daniel Dunbar3f872332009-07-28 16:08:33 +00001282 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001283 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001284 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001285
1286 int64_t Size;
1287 SMLoc SizeLoc = Lexer.getLoc();
1288 if (ParseAbsoluteExpression(Size))
1289 return true;
1290
1291 int64_t Pow2Alignment = 0;
1292 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001293 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001294 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001295 Pow2AlignmentLoc = Lexer.getLoc();
1296 if (ParseAbsoluteExpression(Pow2Alignment))
1297 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001298
1299 // If this target takes alignments in bytes (not log) validate and convert.
1300 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1301 if (!isPowerOf2_64(Pow2Alignment))
1302 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1303 Pow2Alignment = Log2_64(Pow2Alignment);
1304 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001305 }
1306
Daniel Dunbar3f872332009-07-28 16:08:33 +00001307 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001308 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001309
Sean Callanan79ed1a82010-01-19 20:22:31 +00001310 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001311
Chris Lattner1fc3d752009-07-09 17:25:12 +00001312 // NOTE: a size of zero for a .comm should create a undefined symbol
1313 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001314 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001315 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1316 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001317
1318 // NOTE: The alignment in the directive is a power of 2 value, the assember
1319 // may internally end up wanting an alignment in bytes.
1320 // FIXME: Diagnose overflow.
1321 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001322 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1323 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001324
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001325 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001326 return Error(IDLoc, "invalid symbol redefinition");
1327
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001328 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001329 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001330 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001331 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1332 MCSectionMachO::S_ZEROFILL, 0,
1333 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001334 Sym, Size, 1 << Pow2Alignment);
1335 return false;
1336 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001337
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001338 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001339 return false;
1340}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001341
1342/// ParseDirectiveDarwinZerofill
1343/// ::= .zerofill segname , sectname [, identifier , size_expression [
1344/// , align_expression ]]
1345bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001346 // FIXME: Handle quoted names here.
1347
Daniel Dunbar3f872332009-07-28 16:08:33 +00001348 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001349 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001350 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001351 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001352
Daniel Dunbar3f872332009-07-28 16:08:33 +00001353 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001354 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001355 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001356
Daniel Dunbar3f872332009-07-28 16:08:33 +00001357 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001358 return TokError("expected section name after comma in '.zerofill' "
1359 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001360 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001361 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001362
Chris Lattner9be3fee2009-07-10 22:20:30 +00001363 // If this is the end of the line all that was wanted was to create the
1364 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001365 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001366 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001367 Out.EmitZerofill(getMachOSection(Segment, Section,
1368 MCSectionMachO::S_ZEROFILL, 0,
1369 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001370 return false;
1371 }
1372
Daniel Dunbar3f872332009-07-28 16:08:33 +00001373 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001374 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001375 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001376
Daniel Dunbar3f872332009-07-28 16:08:33 +00001377 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001378 return TokError("expected identifier in directive");
1379
1380 // handle the identifier as the key symbol.
1381 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001382 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001383 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001384
Daniel Dunbar3f872332009-07-28 16:08:33 +00001385 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001386 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001388
1389 int64_t Size;
1390 SMLoc SizeLoc = Lexer.getLoc();
1391 if (ParseAbsoluteExpression(Size))
1392 return true;
1393
1394 int64_t Pow2Alignment = 0;
1395 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001396 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001397 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001398 Pow2AlignmentLoc = Lexer.getLoc();
1399 if (ParseAbsoluteExpression(Pow2Alignment))
1400 return true;
1401 }
1402
Daniel Dunbar3f872332009-07-28 16:08:33 +00001403 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001404 return TokError("unexpected token in '.zerofill' directive");
1405
Sean Callanan79ed1a82010-01-19 20:22:31 +00001406 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001407
1408 if (Size < 0)
1409 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1410 "than zero");
1411
1412 // NOTE: The alignment in the directive is a power of 2 value, the assember
1413 // may internally end up wanting an alignment in bytes.
1414 // FIXME: Diagnose overflow.
1415 if (Pow2Alignment < 0)
1416 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1417 "can't be less than zero");
1418
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001419 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001420 return Error(IDLoc, "invalid symbol redefinition");
1421
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001422 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001423 //
1424 // FIXME: Arch specific.
1425 Out.EmitZerofill(getMachOSection(Segment, Section,
1426 MCSectionMachO::S_ZEROFILL, 0,
1427 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001428 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001429
1430 return false;
1431}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001432
1433/// ParseDirectiveDarwinSubsectionsViaSymbols
1434/// ::= .subsections_via_symbols
1435bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001436 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001437 return TokError("unexpected token in '.subsections_via_symbols' directive");
1438
Sean Callanan79ed1a82010-01-19 20:22:31 +00001439 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001440
Kevin Enderbyf96db462009-07-16 17:56:39 +00001441 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001442
1443 return false;
1444}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001445
1446/// ParseDirectiveAbort
1447/// ::= .abort [ "abort_string" ]
1448bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001449 // FIXME: Use loc from directive.
1450 SMLoc Loc = Lexer.getLoc();
1451
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001452 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001453 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1454 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001455 return TokError("expected string in '.abort' directive");
1456
Sean Callanan18b83232010-01-19 21:44:56 +00001457 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001458
Sean Callanan79ed1a82010-01-19 20:22:31 +00001459 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001460 }
1461
Daniel Dunbar3f872332009-07-28 16:08:33 +00001462 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001463 return TokError("unexpected token in '.abort' directive");
1464
Sean Callanan79ed1a82010-01-19 20:22:31 +00001465 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001466
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001467 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001468 if (Str.empty())
1469 Error(Loc, ".abort detected. Assembly stopping.");
1470 else
1471 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001472
1473 return false;
1474}
Kevin Enderby71148242009-07-14 21:35:03 +00001475
1476/// ParseDirectiveLsym
1477/// ::= .lsym identifier , expression
1478bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001479 StringRef Name;
1480 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001481 return TokError("expected identifier in directive");
1482
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001483 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001484 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001485
Daniel Dunbar3f872332009-07-28 16:08:33 +00001486 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001487 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001488 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001489
Daniel Dunbar821e3332009-08-31 08:09:28 +00001490 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001491 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001492 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001493 return true;
1494
Daniel Dunbar3f872332009-07-28 16:08:33 +00001495 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001496 return TokError("unexpected token in '.lsym' directive");
1497
Sean Callanan79ed1a82010-01-19 20:22:31 +00001498 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001499
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001500 // We don't currently support this directive.
1501 //
1502 // FIXME: Diagnostic location!
1503 (void) Sym;
1504 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001505}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001506
1507/// ParseDirectiveInclude
1508/// ::= .include "filename"
1509bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001510 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001511 return TokError("expected string in '.include' directive");
1512
Sean Callanan18b83232010-01-19 21:44:56 +00001513 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001514 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001515 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001516
Daniel Dunbar3f872332009-07-28 16:08:33 +00001517 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001518 return TokError("unexpected token in '.include' directive");
1519
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001520 // Strip the quotes.
1521 Filename = Filename.substr(1, Filename.size()-2);
1522
1523 // Attempt to switch the lexer to the included file before consuming the end
1524 // of statement to avoid losing it when we switch.
1525 if (Lexer.EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001526 PrintMessage(IncludeLoc,
1527 "Could not find include file '" + Filename + "'",
1528 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001529 return true;
1530 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001531
1532 return false;
1533}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001534
1535/// ParseDirectiveDarwinDumpOrLoad
1536/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001537bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001538 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001539 return TokError("expected string in '.dump' or '.load' directive");
1540
Sean Callanan79ed1a82010-01-19 20:22:31 +00001541 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001542
Daniel Dunbar3f872332009-07-28 16:08:33 +00001543 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001544 return TokError("unexpected token in '.dump' or '.load' directive");
1545
Sean Callanan79ed1a82010-01-19 20:22:31 +00001546 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001547
Kevin Enderby5026ae42009-07-20 20:25:37 +00001548 // FIXME: If/when .dump and .load are implemented they will be done in the
1549 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001550 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001551 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001552 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001553 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001554
1555 return false;
1556}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001557
1558/// ParseDirectiveIf
1559/// ::= .if expression
1560bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1561 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001562 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001563
1564 TheCondStack.push_back(TheCondState);
1565 TheCondState.TheCond = AsmCond::IfCond;
1566 if(TheCondState.Ignore) {
1567 EatToEndOfStatement();
1568 }
1569 else {
1570 int64_t ExprValue;
1571 if (ParseAbsoluteExpression(ExprValue))
1572 return true;
1573
1574 if (Lexer.isNot(AsmToken::EndOfStatement))
1575 return TokError("unexpected token in '.if' directive");
1576
Sean Callanan79ed1a82010-01-19 20:22:31 +00001577 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001578
1579 TheCondState.CondMet = ExprValue;
1580 TheCondState.Ignore = !TheCondState.CondMet;
1581 }
1582
1583 return false;
1584}
1585
1586/// ParseDirectiveElseIf
1587/// ::= .elseif expression
1588bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1589 if (TheCondState.TheCond != AsmCond::IfCond &&
1590 TheCondState.TheCond != AsmCond::ElseIfCond)
1591 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1592 " an .elseif");
1593 TheCondState.TheCond = AsmCond::ElseIfCond;
1594
1595 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001596 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001597
1598 bool LastIgnoreState = false;
1599 if (!TheCondStack.empty())
1600 LastIgnoreState = TheCondStack.back().Ignore;
1601 if (LastIgnoreState || TheCondState.CondMet) {
1602 TheCondState.Ignore = true;
1603 EatToEndOfStatement();
1604 }
1605 else {
1606 int64_t ExprValue;
1607 if (ParseAbsoluteExpression(ExprValue))
1608 return true;
1609
1610 if (Lexer.isNot(AsmToken::EndOfStatement))
1611 return TokError("unexpected token in '.elseif' directive");
1612
Sean Callanan79ed1a82010-01-19 20:22:31 +00001613 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001614 TheCondState.CondMet = ExprValue;
1615 TheCondState.Ignore = !TheCondState.CondMet;
1616 }
1617
1618 return false;
1619}
1620
1621/// ParseDirectiveElse
1622/// ::= .else
1623bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1624 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001625 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001626
1627 if (Lexer.isNot(AsmToken::EndOfStatement))
1628 return TokError("unexpected token in '.else' directive");
1629
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001631
1632 if (TheCondState.TheCond != AsmCond::IfCond &&
1633 TheCondState.TheCond != AsmCond::ElseIfCond)
1634 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1635 ".elseif");
1636 TheCondState.TheCond = AsmCond::ElseCond;
1637 bool LastIgnoreState = false;
1638 if (!TheCondStack.empty())
1639 LastIgnoreState = TheCondStack.back().Ignore;
1640 if (LastIgnoreState || TheCondState.CondMet)
1641 TheCondState.Ignore = true;
1642 else
1643 TheCondState.Ignore = false;
1644
1645 return false;
1646}
1647
1648/// ParseDirectiveEndIf
1649/// ::= .endif
1650bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1651 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001652 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001653
1654 if (Lexer.isNot(AsmToken::EndOfStatement))
1655 return TokError("unexpected token in '.endif' directive");
1656
Sean Callanan79ed1a82010-01-19 20:22:31 +00001657 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001658
1659 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1660 TheCondStack.empty())
1661 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1662 ".else");
1663 if (!TheCondStack.empty()) {
1664 TheCondState = TheCondStack.back();
1665 TheCondStack.pop_back();
1666 }
1667
1668 return false;
1669}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001670
1671/// ParseDirectiveFile
1672/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001673bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001674 // FIXME: I'm not sure what this is.
1675 int64_t FileNumber = -1;
1676 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001677 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001678 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001679
1680 if (FileNumber < 1)
1681 return TokError("file number less than one");
1682 }
1683
1684 if (Lexer.isNot(AsmToken::String))
1685 return TokError("unexpected token in '.file' directive");
1686
Sean Callanan18b83232010-01-19 21:44:56 +00001687 StringRef ATTRIBUTE_UNUSED FileName = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001688 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001689
1690 if (Lexer.isNot(AsmToken::EndOfStatement))
1691 return TokError("unexpected token in '.file' directive");
1692
1693 // FIXME: Do something with the .file.
1694
1695 return false;
1696}
1697
1698/// ParseDirectiveLine
1699/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001700bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001701 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1702 if (Lexer.isNot(AsmToken::Integer))
1703 return TokError("unexpected token in '.line' directive");
1704
Sean Callanan18b83232010-01-19 21:44:56 +00001705 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001706 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001707 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001708
1709 // FIXME: Do something with the .line.
1710 }
1711
1712 if (Lexer.isNot(AsmToken::EndOfStatement))
1713 return TokError("unexpected token in '.file' directive");
1714
1715 return false;
1716}
1717
1718
1719/// ParseDirectiveLoc
1720/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001721bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001722 if (Lexer.isNot(AsmToken::Integer))
1723 return TokError("unexpected token in '.loc' directive");
1724
1725 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001726 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001727 (void) FileNumber;
1728 // FIXME: Validate file.
1729
Sean Callanan79ed1a82010-01-19 20:22:31 +00001730 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001731 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1732 if (Lexer.isNot(AsmToken::Integer))
1733 return TokError("unexpected token in '.loc' directive");
1734
Sean Callanan18b83232010-01-19 21:44:56 +00001735 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001736 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001737 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001738
1739 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1740 if (Lexer.isNot(AsmToken::Integer))
1741 return TokError("unexpected token in '.loc' directive");
1742
Sean Callanan18b83232010-01-19 21:44:56 +00001743 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001744 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001745 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001746
1747 // FIXME: Do something with the .loc.
1748 }
1749 }
1750
1751 if (Lexer.isNot(AsmToken::EndOfStatement))
1752 return TokError("unexpected token in '.file' directive");
1753
1754 return false;
1755}
1756