blob: 2eb75a76dd2150693e59c0d5590fafbb5af1f240 [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 Lattner9d3c7552010-01-14 22:29:57 +000032/// getStartLoc - Get the location of the first token of this operand.
33SMLoc MCParsedAsmOperand::getStartLoc() const { return SMLoc(); }
34SMLoc MCParsedAsmOperand::getEndLoc() const { return SMLoc(); }
35
36
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000037// Mach-O section uniquing.
38//
39// FIXME: Figure out where this should live, it should be shared by
40// TargetLoweringObjectFile.
41typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
42
Chris Lattnerebb89b42009-09-27 21:16:52 +000043AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
44 const MCAsmInfo &_MAI)
45 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
46 SectionUniquingMap(0) {
47 // Debugging directives.
48 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
49 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
50 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
51}
52
53
54
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000055AsmParser::~AsmParser() {
56 // If we have the MachO uniquing map, free it.
57 delete (MachOUniqueMapTy*)SectionUniquingMap;
58}
59
60const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
61 const StringRef &Section,
62 unsigned TypeAndAttributes,
63 unsigned Reserved2,
64 SectionKind Kind) const {
65 // We unique sections by their segment/section pair. The returned section
66 // may not have the same flags as the requested section, if so this should be
67 // diagnosed by the client as an error.
68
69 // Create the map if it doesn't already exist.
70 if (SectionUniquingMap == 0)
71 SectionUniquingMap = new MachOUniqueMapTy();
72 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
73
74 // Form the name to look up.
75 SmallString<64> Name;
76 Name += Segment;
77 Name.push_back(',');
78 Name += Section;
79
80 // Do the lookup, if we have a hit, return it.
81 const MCSectionMachO *&Entry = Map[Name.str()];
82
83 // FIXME: This should validate the type and attributes.
84 if (Entry) return Entry;
85
86 // Otherwise, return a new section.
87 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
88 Reserved2, Kind, Ctx);
89}
90
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000091void AsmParser::Warning(SMLoc L, const Twine &Msg) {
92 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000093}
94
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000095bool AsmParser::Error(SMLoc L, const Twine &Msg) {
96 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000097 return true;
98}
99
100bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000101 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000102 return true;
103}
104
Chris Lattner27aa7d22009-06-21 20:16:42 +0000105bool AsmParser::Run() {
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000106 // Create the initial section.
107 //
108 // FIXME: Support -n.
109 // FIXME: Target hook & command line option for initial section.
110 Out.SwitchSection(getMachOSection("__TEXT", "__text",
111 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
112 0, SectionKind()));
113
114
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000115 // Prime the lexer.
116 Lexer.Lex();
117
Chris Lattnerb717fb02009-07-02 21:53:43 +0000118 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000119
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000120 AsmCond StartingCondState = TheCondState;
121
Chris Lattnerb717fb02009-07-02 21:53:43 +0000122 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000123 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000124 // Handle conditional assembly here before calling ParseStatement()
125 if (Lexer.getKind() == AsmToken::Identifier) {
126 // If we have an identifier, handle it as the key symbol.
127 AsmToken ID = Lexer.getTok();
128 SMLoc IDLoc = ID.getLoc();
129 StringRef IDVal = ID.getString();
130
131 if (IDVal == ".if" ||
132 IDVal == ".elseif" ||
133 IDVal == ".else" ||
134 IDVal == ".endif") {
135 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
136 continue;
137 HadError = true;
138 EatToEndOfStatement();
139 continue;
140 }
141 }
142 if (TheCondState.Ignore) {
143 EatToEndOfStatement();
144 continue;
145 }
146
Chris Lattnerb717fb02009-07-02 21:53:43 +0000147 if (!ParseStatement()) continue;
148
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000149 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000150 HadError = true;
151 EatToEndOfStatement();
152 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000153
154 if (TheCondState.TheCond != StartingCondState.TheCond ||
155 TheCondState.Ignore != StartingCondState.Ignore)
156 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000157
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000158 if (!HadError)
159 Out.Finish();
160
Chris Lattnerb717fb02009-07-02 21:53:43 +0000161 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000162}
163
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000164/// ParseConditionalAssemblyDirectives - parse the conditional assembly
165/// directives
166bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
167 SMLoc DirectiveLoc) {
168 if (Directive == ".if")
169 return ParseDirectiveIf(DirectiveLoc);
170 if (Directive == ".elseif")
171 return ParseDirectiveElseIf(DirectiveLoc);
172 if (Directive == ".else")
173 return ParseDirectiveElse(DirectiveLoc);
174 if (Directive == ".endif")
175 return ParseDirectiveEndIf(DirectiveLoc);
176 return true;
177}
178
Chris Lattner2cf5f142009-06-22 01:29:09 +0000179/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
180void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000181 while (Lexer.isNot(AsmToken::EndOfStatement) &&
182 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000183 Lexer.Lex();
184
185 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000186 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000187 Lexer.Lex();
188}
189
Chris Lattnerc4193832009-06-22 05:51:26 +0000190
Chris Lattner74ec1a32009-06-22 06:32:03 +0000191/// ParseParenExpr - Parse a paren expression and return it.
192/// NOTE: This assumes the leading '(' has already been consumed.
193///
194/// parenexpr ::= expr)
195///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000196bool AsmParser::ParseParenExpr(const MCExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000197 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000198 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000199 return TokError("expected ')' in parentheses expression");
200 Lexer.Lex();
201 return false;
202}
Chris Lattnerc4193832009-06-22 05:51:26 +0000203
Daniel Dunbar959fd882009-08-26 22:13:22 +0000204MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
205 if (MCSymbol *S = Ctx.LookupSymbol(Name))
206 return S;
207
208 // If the label starts with L it is an assembler temporary label.
209 if (Name.startswith("L"))
210 return Ctx.CreateTemporarySymbol(Name);
211
212 return Ctx.CreateSymbol(Name);
213}
214
Chris Lattner74ec1a32009-06-22 06:32:03 +0000215/// ParsePrimaryExpr - Parse a primary expression and return it.
216/// primaryexpr ::= (parenexpr
217/// primaryexpr ::= symbol
218/// primaryexpr ::= number
219/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000220bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000221 switch (Lexer.getKind()) {
222 default:
223 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000224 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000225 Lexer.Lex(); // Eat the operator.
226 if (ParsePrimaryExpr(Res))
227 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000228 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000229 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000230 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000231 case AsmToken::Identifier: {
232 // This is a symbol reference.
233 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
Chris Lattnerc4193832009-06-22 05:51:26 +0000234 Lexer.Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000235
236 // If this is an absolute variable reference, substitute it now to preserve
237 // semantics in the face of reassignment.
238 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
239 Res = Sym->getValue();
240 return false;
241 }
242
243 // Otherwise create a symbol ref.
244 Res = MCSymbolRefExpr::Create(Sym, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000245 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000246 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000247 case AsmToken::Integer:
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000248 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000249 Lexer.Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000250 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000252 Lexer.Lex(); // Eat the '('.
253 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000254 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000255 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 if (ParsePrimaryExpr(Res))
257 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000258 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000260 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 Lexer.Lex(); // Eat the operator.
262 if (ParsePrimaryExpr(Res))
263 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000264 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000265 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000266 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000267 Lexer.Lex(); // Eat the operator.
268 if (ParsePrimaryExpr(Res))
269 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000270 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000271 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000272 }
273}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000274
275/// ParseExpression - Parse an expression and return it.
276///
277/// expr ::= expr +,- expr -> lowest.
278/// expr ::= expr |,^,&,! expr -> middle.
279/// expr ::= expr *,/,%,<<,>> expr -> highest.
280/// expr ::= primaryexpr
281///
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000282bool AsmParser::ParseExpression(const MCExpr *&Res) {
Daniel Dunbar475839e2009-06-29 20:37:27 +0000283 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000284 return ParsePrimaryExpr(Res) ||
285 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000286}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000287
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000288bool AsmParser::ParseParenExpression(const MCExpr *&Res) {
289 if (ParseParenExpr(Res))
290 return true;
291
292 return false;
293}
294
Daniel Dunbar475839e2009-06-29 20:37:27 +0000295bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000296 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000297
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000298 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000299 if (ParseExpression(Expr))
300 return true;
301
Daniel Dunbare00b0112009-10-16 01:57:52 +0000302 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000303 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304
305 return false;
306}
307
Daniel Dunbar3f872332009-07-28 16:08:33 +0000308static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000309 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000310 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000311 default:
312 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000313
314 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000315 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000316 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000317 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000318 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000319 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000320 return 1;
321
322 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000324 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000325 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000327 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000328 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000330 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000331 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::ExclaimEqual:
333 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000334 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000337 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000339 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000340 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000342 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000343 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000346 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return 2;
348
349 // Intermediate Precedence: |, &, ^
350 //
351 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000352 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000353 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000354 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000357 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000358 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000359 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360 return 3;
361
362 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000363 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000364 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000365 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000366 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000367 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000369 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000370 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000371 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000372 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000373 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000376 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000378 }
379}
380
381
382/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
383/// Res contains the LHS of the expression on input.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000384bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000385 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000386 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000387 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000388
389 // If the next token is lower precedence than we are allowed to eat, return
390 // successfully with what we ate already.
391 if (TokPrec < Precedence)
392 return false;
393
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000394 Lexer.Lex();
395
396 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000397 const MCExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 if (ParsePrimaryExpr(RHS)) return true;
399
400 // If BinOp binds less tightly with RHS than the operator after RHS, let
401 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000402 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000404 if (TokPrec < NextTokPrec) {
405 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
406 }
407
Daniel Dunbar475839e2009-06-29 20:37:27 +0000408 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000409 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000410 }
411}
412
Chris Lattnerc4193832009-06-22 05:51:26 +0000413
414
415
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000416/// ParseStatement:
417/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000418/// ::= Label* Directive ...Operands... EndOfStatement
419/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000420bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000421 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000422 Lexer.Lex();
423 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000424 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000425
426 // Statements always start with an identifier.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000427 AsmToken ID = Lexer.getTok();
428 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000429 StringRef IDVal;
430 if (ParseIdentifier(IDVal))
431 return TokError("unexpected token at start of statement");
432
433 // FIXME: Recurse on local labels?
434
435 // See what kind of statement we have.
436 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000437 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000438 // identifier ':' -> Label.
439 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000440
441 // Diagnose attempt to use a variable as a label.
442 //
443 // FIXME: Diagnostics. Note the location of the definition as a label.
444 // FIXME: This doesn't diagnose assignment to a symbol which has been
445 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000446 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000447 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000448 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000449
Daniel Dunbar959fd882009-08-26 22:13:22 +0000450 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000451 Out.EmitLabel(Sym);
452
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000453 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000454 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000455
Daniel Dunbar3f872332009-07-28 16:08:33 +0000456 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000457 // identifier '=' ... -> assignment statement
458 Lexer.Lex();
459
Daniel Dunbare2ace502009-08-31 08:09:09 +0000460 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000461
462 default: // Normal instruction or directive.
463 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000464 }
465
466 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000467 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000468 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000470 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000471 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000472 // FIXME: This changes behavior based on the -static flag to the
473 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000474 return ParseDirectiveSectionSwitch("__TEXT", "__text",
475 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000476 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000477 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000478 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000479 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000480 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000481 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
482 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000483 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000484 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000485 MCSectionMachO::S_4BYTE_LITERALS,
486 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000488 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000489 MCSectionMachO::S_8BYTE_LITERALS,
490 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000491 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000492 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000493 MCSectionMachO::S_16BYTE_LITERALS,
494 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000495 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000496 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000497 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000498 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000499 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000502 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
503
504 // FIXME: The assembler manual claims that this has the self modify code
505 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000506 if (IDVal == ".symbol_stub")
507 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
508 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000509 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
510 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000511 0, 16);
512 // FIXME: PowerPC only?
513 if (IDVal == ".picsymbol_stub")
514 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
515 MCSectionMachO::S_SYMBOL_STUBS |
516 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
517 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000518 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000519 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000520 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000521 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
522
523 // FIXME: The section names of these two are misspelled in the assembler
524 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000525 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000526 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
527 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
528 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000529 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000530 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
531 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
532 4);
533
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000535 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000536 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000537 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000538 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
539 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000541 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
543 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000545 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000546
547
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000549 return ParseDirectiveSectionSwitch("__OBJC", "__class",
550 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000551 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000552 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
553 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
556 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000557 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000558 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
559 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000560 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000561 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
562 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000563 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000564 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
565 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000566 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000567 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
568 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
571 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000572 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000573 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
574 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
575 MCSectionMachO::S_LITERAL_POINTERS,
576 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000577 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000578 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
579 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
580 MCSectionMachO::S_LITERAL_POINTERS,
581 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 return ParseDirectiveSectionSwitch("__OBJC", "__category",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000588 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000589 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
590 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
593 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000594 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
596 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000598 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
599 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000600 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000601 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
602 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000604 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
605 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000607 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
608 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000609
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000610 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000612 return ParseDirectiveSet();
613
Daniel Dunbara0d14262009-06-24 23:30:00 +0000614 // Data directives
615
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000617 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000619 return ParseDirectiveAscii(true);
620
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000622 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000623 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000624 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000626 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000628 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000629
630 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000632 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000634 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000636 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000638 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000640 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000642 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000644 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000646 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
647
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000649 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000650
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000652 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000654 return ParseDirectiveSpace();
655
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000656 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000657
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000659 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000661 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000662 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000663 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000664 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000665 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000667 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000669 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000671 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000673 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000675 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000677 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000679 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000681 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
682
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000684 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000686 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000688 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000689 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000690 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000692 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000693
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000695 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000697 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000699 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000701 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000703 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000704
Chris Lattnerebb89b42009-09-27 21:16:52 +0000705 // Look up the handler in the handler table,
706 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
707 if (Handler)
708 return (this->*Handler)(IDVal, IDLoc);
709
Kevin Enderby9c656452009-09-10 20:51:44 +0000710 // Target hook for parsing target specific directives.
711 if (!getTargetParser().ParseDirective(ID))
712 return false;
713
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000714 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000715 EatToEndOfStatement();
716 return false;
717 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000718
Chris Lattner98986712010-01-14 22:21:20 +0000719
720 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
721 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
722 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000723 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000724
Daniel Dunbar3f872332009-07-28 16:08:33 +0000725 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000726 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000727 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000728
729 // Eat the end of statement marker.
730 Lexer.Lex();
731
Chris Lattner98986712010-01-14 22:21:20 +0000732
733 MCInst Inst;
734
735 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
736
737 // Free any parsed operands.
738 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
739 delete ParsedOperands[i];
740
741 if (MatchFail) {
742 // FIXME: We should give nicer diagnostics about the exact failure.
743 Error(IDLoc, "unrecognized instruction");
744 return true;
745 }
746
Chris Lattner2cf5f142009-06-22 01:29:09 +0000747 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000748 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000749
750 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000751 return false;
752}
Chris Lattner9a023f72009-06-24 04:43:34 +0000753
Daniel Dunbare2ace502009-08-31 08:09:09 +0000754bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000755 // FIXME: Use better location, we should use proper tokens.
756 SMLoc EqualLoc = Lexer.getLoc();
757
Daniel Dunbar821e3332009-08-31 08:09:28 +0000758 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000759 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000760 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000761 return true;
762
Daniel Dunbar3f872332009-07-28 16:08:33 +0000763 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000764 return TokError("unexpected token in assignment");
765
766 // Eat the end of statement marker.
767 Lexer.Lex();
768
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000769 // Validate that the LHS is allowed to be a variable (either it has not been
770 // used as a symbol, or it is an absolute symbol).
771 MCSymbol *Sym = getContext().LookupSymbol(Name);
772 if (Sym) {
773 // Diagnose assignment to a label.
774 //
775 // FIXME: Diagnostics. Note the location of the definition as a label.
776 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
777 if (!Sym->isUndefined() && !Sym->isAbsolute())
778 return Error(EqualLoc, "redefinition of '" + Name + "'");
779 else if (!Sym->isVariable())
780 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
781 else if (!isa<MCConstantExpr>(Sym->getValue()))
782 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
783 Name + "'");
784 } else
785 Sym = CreateSymbol(Name);
786
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000787 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000788
789 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000790 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000791
792 return false;
793}
794
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000795/// ParseIdentifier:
796/// ::= identifier
797/// ::= string
798bool AsmParser::ParseIdentifier(StringRef &Res) {
799 if (Lexer.isNot(AsmToken::Identifier) &&
800 Lexer.isNot(AsmToken::String))
801 return true;
802
803 Res = Lexer.getTok().getIdentifier();
804
805 Lexer.Lex(); // Consume the identifier token.
806
807 return false;
808}
809
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000810/// ParseDirectiveSet:
811/// ::= .set identifier ',' expression
812bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000813 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000814
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000815 if (ParseIdentifier(Name))
816 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000817
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000818 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000819 return TokError("unexpected token in '.set'");
820 Lexer.Lex();
821
Daniel Dunbare2ace502009-08-31 08:09:09 +0000822 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000823}
824
Chris Lattner9a023f72009-06-24 04:43:34 +0000825/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000826/// ::= .section identifier (',' identifier)*
827/// FIXME: This should actually parse out the segment, section, attributes and
828/// sizeof_stub fields.
829bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000830 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000831
Daniel Dunbarace63122009-08-11 03:42:33 +0000832 StringRef SectionName;
833 if (ParseIdentifier(SectionName))
834 return Error(Loc, "expected identifier after '.section' directive");
835
836 // Verify there is a following comma.
837 if (!Lexer.is(AsmToken::Comma))
838 return TokError("unexpected token in '.section' directive");
839
Chris Lattnerff4bc462009-08-10 01:39:42 +0000840 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000841 SectionSpec += ",";
842
843 // Add all the tokens until the end of the line, ParseSectionSpecifier will
844 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000845 StringRef EOL = Lexer.LexUntilEndOfStatement();
846 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000847
Chris Lattnerff4bc462009-08-10 01:39:42 +0000848 Lexer.Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000849 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000850 return TokError("unexpected token in '.section' directive");
851 Lexer.Lex();
852
Chris Lattnerff4bc462009-08-10 01:39:42 +0000853
854 StringRef Segment, Section;
855 unsigned TAA, StubSize;
856 std::string ErrorStr =
857 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
858 TAA, StubSize);
859
860 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000861 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000862
Chris Lattner56594f92009-07-31 17:47:16 +0000863 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000864 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
865 SectionKind()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000866 return false;
867}
868
Chris Lattnere15c2d72009-08-10 18:05:55 +0000869/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000870bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
871 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000872 unsigned TAA, unsigned Align,
873 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000874 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000875 return TokError("unexpected token in section switching directive");
876 Lexer.Lex();
877
Chris Lattner56594f92009-07-31 17:47:16 +0000878 // FIXME: Arch specific.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000879 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
880 SectionKind()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000881
882 // Set the implicit alignment, if any.
883 //
884 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
885 // alignment on the section (e.g., if one manually inserts bytes into the
886 // section, then just issueing the section switch directive will not realign
887 // the section. However, this is arguably more reasonable behavior, and there
888 // is no good reason for someone to intentionally emit incorrectly sized
889 // values into the implicitly aligned sections.
890 if (Align)
891 Out.EmitValueToAlignment(Align, 0, 1, 0);
892
Chris Lattner529fb542009-06-24 05:13:15 +0000893 return false;
894}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000895
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000896bool AsmParser::ParseEscapedString(std::string &Data) {
897 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
898
899 Data = "";
900 StringRef Str = Lexer.getTok().getStringContents();
901 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
902 if (Str[i] != '\\') {
903 Data += Str[i];
904 continue;
905 }
906
907 // Recognize escaped characters. Note that this escape semantics currently
908 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
909 ++i;
910 if (i == e)
911 return TokError("unexpected backslash at end of string");
912
913 // Recognize octal sequences.
914 if ((unsigned) (Str[i] - '0') <= 7) {
915 // Consume up to three octal characters.
916 unsigned Value = Str[i] - '0';
917
918 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
919 ++i;
920 Value = Value * 8 + (Str[i] - '0');
921
922 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
923 ++i;
924 Value = Value * 8 + (Str[i] - '0');
925 }
926 }
927
928 if (Value > 255)
929 return TokError("invalid octal escape sequence (out of range)");
930
931 Data += (unsigned char) Value;
932 continue;
933 }
934
935 // Otherwise recognize individual escapes.
936 switch (Str[i]) {
937 default:
938 // Just reject invalid escape sequences for now.
939 return TokError("invalid escape sequence (unrecognized character)");
940
941 case 'b': Data += '\b'; break;
942 case 'f': Data += '\f'; break;
943 case 'n': Data += '\n'; break;
944 case 'r': Data += '\r'; break;
945 case 't': Data += '\t'; break;
946 case '"': Data += '"'; break;
947 case '\\': Data += '\\'; break;
948 }
949 }
950
951 return false;
952}
953
Daniel Dunbara0d14262009-06-24 23:30:00 +0000954/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000955/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000956bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000957 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000958 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000959 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960 return TokError("expected string in '.ascii' or '.asciz' directive");
961
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000962 std::string Data;
963 if (ParseEscapedString(Data))
964 return true;
965
966 Out.EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000967 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000968 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969
970 Lexer.Lex();
971
Daniel Dunbar3f872332009-07-28 16:08:33 +0000972 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000973 break;
974
Daniel Dunbar3f872332009-07-28 16:08:33 +0000975 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976 return TokError("unexpected token in '.ascii' or '.asciz' directive");
977 Lexer.Lex();
978 }
979 }
980
981 Lexer.Lex();
982 return false;
983}
984
985/// ParseDirectiveValue
986/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
987bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000988 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000989 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000990 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +0000991 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000992 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000993 return true;
994
Daniel Dunbar883f9202009-08-31 08:08:50 +0000995 Out.EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996
Daniel Dunbar3f872332009-07-28 16:08:33 +0000997 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998 break;
999
1000 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001001 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001002 return TokError("unexpected token in directive");
1003 Lexer.Lex();
1004 }
1005 }
1006
1007 Lexer.Lex();
1008 return false;
1009}
1010
1011/// ParseDirectiveSpace
1012/// ::= .space expression [ , expression ]
1013bool AsmParser::ParseDirectiveSpace() {
1014 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001015 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001016 return true;
1017
1018 int64_t FillExpr = 0;
1019 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001020 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1021 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001022 return TokError("unexpected token in '.space' directive");
1023 Lexer.Lex();
1024
Daniel Dunbar475839e2009-06-29 20:37:27 +00001025 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001026 return true;
1027
1028 HasFillExpr = true;
1029
Daniel Dunbar3f872332009-07-28 16:08:33 +00001030 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001031 return TokError("unexpected token in '.space' directive");
1032 }
1033
1034 Lexer.Lex();
1035
1036 if (NumBytes <= 0)
1037 return TokError("invalid number of bytes in '.space' directive");
1038
1039 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1040 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001041 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001042
1043 return false;
1044}
1045
1046/// ParseDirectiveFill
1047/// ::= .fill expression , expression , expression
1048bool AsmParser::ParseDirectiveFill() {
1049 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001050 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001051 return true;
1052
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054 return TokError("unexpected token in '.fill' directive");
1055 Lexer.Lex();
1056
1057 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001058 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059 return true;
1060
Daniel Dunbar3f872332009-07-28 16:08:33 +00001061 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062 return TokError("unexpected token in '.fill' directive");
1063 Lexer.Lex();
1064
1065 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001066 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001067 return true;
1068
Daniel Dunbar3f872332009-07-28 16:08:33 +00001069 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001070 return TokError("unexpected token in '.fill' directive");
1071
1072 Lexer.Lex();
1073
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001074 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1075 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076
1077 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar821e3332009-08-31 08:09:28 +00001078 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001079
1080 return false;
1081}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001082
1083/// ParseDirectiveOrg
1084/// ::= .org expression [ , expression ]
1085bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001086 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001087 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001088 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001089 return true;
1090
1091 // Parse optional fill expression.
1092 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001093 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1094 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001095 return TokError("unexpected token in '.org' directive");
1096 Lexer.Lex();
1097
Daniel Dunbar475839e2009-06-29 20:37:27 +00001098 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001099 return true;
1100
Daniel Dunbar3f872332009-07-28 16:08:33 +00001101 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001102 return TokError("unexpected token in '.org' directive");
1103 }
1104
1105 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001106
1107 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1108 // has to be relative to the current section.
1109 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001110
1111 return false;
1112}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001113
1114/// ParseDirectiveAlign
1115/// ::= {.align, ...} expression [ , expression [ , expression ]]
1116bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001117 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001118 int64_t Alignment;
1119 if (ParseAbsoluteExpression(Alignment))
1120 return true;
1121
1122 SMLoc MaxBytesLoc;
1123 bool HasFillExpr = false;
1124 int64_t FillExpr = 0;
1125 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001126 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1127 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001128 return TokError("unexpected token in directive");
1129 Lexer.Lex();
1130
1131 // The fill expression can be omitted while specifying a maximum number of
1132 // alignment bytes, e.g:
1133 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001134 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001135 HasFillExpr = true;
1136 if (ParseAbsoluteExpression(FillExpr))
1137 return true;
1138 }
1139
Daniel Dunbar3f872332009-07-28 16:08:33 +00001140 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1141 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001142 return TokError("unexpected token in directive");
1143 Lexer.Lex();
1144
1145 MaxBytesLoc = Lexer.getLoc();
1146 if (ParseAbsoluteExpression(MaxBytesToFill))
1147 return true;
1148
Daniel Dunbar3f872332009-07-28 16:08:33 +00001149 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001150 return TokError("unexpected token in directive");
1151 }
1152 }
1153
1154 Lexer.Lex();
1155
1156 if (!HasFillExpr) {
1157 // FIXME: Sometimes fill with nop.
1158 FillExpr = 0;
1159 }
1160
1161 // Compute alignment in bytes.
1162 if (IsPow2) {
1163 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001164 if (Alignment >= 32) {
1165 Error(AlignmentLoc, "invalid alignment value");
1166 Alignment = 31;
1167 }
1168
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001169 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001170 }
1171
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001172 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001173 if (MaxBytesLoc.isValid()) {
1174 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001175 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1176 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001177 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001178 }
1179
1180 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001181 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1182 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001183 MaxBytesToFill = 0;
1184 }
1185 }
1186
1187 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1188 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1189
1190 return false;
1191}
1192
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001193/// ParseDirectiveSymbolAttribute
1194/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1195bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001196 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001197 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001198 StringRef Name;
1199
1200 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001201 return TokError("expected identifier in directive");
1202
Daniel Dunbar959fd882009-08-26 22:13:22 +00001203 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001204
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001205 Out.EmitSymbolAttribute(Sym, Attr);
1206
Daniel Dunbar3f872332009-07-28 16:08:33 +00001207 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001208 break;
1209
Daniel Dunbar3f872332009-07-28 16:08:33 +00001210 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001211 return TokError("unexpected token in directive");
1212 Lexer.Lex();
1213 }
1214 }
1215
1216 Lexer.Lex();
1217 return false;
1218}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001219
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001220/// ParseDirectiveDarwinSymbolDesc
1221/// ::= .desc identifier , expression
1222bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001223 StringRef Name;
1224 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001225 return TokError("expected identifier in directive");
1226
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001227 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001228 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001229
Daniel Dunbar3f872332009-07-28 16:08:33 +00001230 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001231 return TokError("unexpected token in '.desc' directive");
1232 Lexer.Lex();
1233
1234 SMLoc DescLoc = Lexer.getLoc();
1235 int64_t DescValue;
1236 if (ParseAbsoluteExpression(DescValue))
1237 return true;
1238
Daniel Dunbar3f872332009-07-28 16:08:33 +00001239 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001240 return TokError("unexpected token in '.desc' directive");
1241
1242 Lexer.Lex();
1243
1244 // Set the n_desc field of this Symbol to this DescValue
1245 Out.EmitSymbolDesc(Sym, DescValue);
1246
1247 return false;
1248}
1249
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001250/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001251/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1252bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001253 SMLoc IDLoc = Lexer.getLoc();
1254 StringRef Name;
1255 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001256 return TokError("expected identifier in directive");
1257
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001258 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001259 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001260
Daniel Dunbar3f872332009-07-28 16:08:33 +00001261 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001262 return TokError("unexpected token in directive");
1263 Lexer.Lex();
1264
1265 int64_t Size;
1266 SMLoc SizeLoc = Lexer.getLoc();
1267 if (ParseAbsoluteExpression(Size))
1268 return true;
1269
1270 int64_t Pow2Alignment = 0;
1271 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001272 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001273 Lexer.Lex();
1274 Pow2AlignmentLoc = Lexer.getLoc();
1275 if (ParseAbsoluteExpression(Pow2Alignment))
1276 return true;
1277 }
1278
Daniel Dunbar3f872332009-07-28 16:08:33 +00001279 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001280 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001281
1282 Lexer.Lex();
1283
Chris Lattner1fc3d752009-07-09 17:25:12 +00001284 // NOTE: a size of zero for a .comm should create a undefined symbol
1285 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001286 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001287 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1288 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001289
1290 // NOTE: The alignment in the directive is a power of 2 value, the assember
1291 // may internally end up wanting an alignment in bytes.
1292 // FIXME: Diagnose overflow.
1293 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001294 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1295 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001296
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001297 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001298 return Error(IDLoc, "invalid symbol redefinition");
1299
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001300 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001301 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001302 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001303 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1304 MCSectionMachO::S_ZEROFILL, 0,
1305 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001306 Sym, Size, 1 << Pow2Alignment);
1307 return false;
1308 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001309
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001310 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001311 return false;
1312}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001313
1314/// ParseDirectiveDarwinZerofill
1315/// ::= .zerofill segname , sectname [, identifier , size_expression [
1316/// , align_expression ]]
1317bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001318 // FIXME: Handle quoted names here.
1319
Daniel Dunbar3f872332009-07-28 16:08:33 +00001320 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001321 return TokError("expected segment name after '.zerofill' directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001322 StringRef Segment = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001323 Lexer.Lex();
1324
Daniel Dunbar3f872332009-07-28 16:08:33 +00001325 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001326 return TokError("unexpected token in directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001327 Lexer.Lex();
1328
Daniel Dunbar3f872332009-07-28 16:08:33 +00001329 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001330 return TokError("expected section name after comma in '.zerofill' "
1331 "directive");
Chris Lattnerff4bc462009-08-10 01:39:42 +00001332 StringRef Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001333 Lexer.Lex();
1334
Chris Lattner9be3fee2009-07-10 22:20:30 +00001335 // If this is the end of the line all that was wanted was to create the
1336 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001337 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001338 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001339 Out.EmitZerofill(getMachOSection(Segment, Section,
1340 MCSectionMachO::S_ZEROFILL, 0,
1341 SectionKind()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001342 return false;
1343 }
1344
Daniel Dunbar3f872332009-07-28 16:08:33 +00001345 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001346 return TokError("unexpected token in directive");
1347 Lexer.Lex();
1348
Daniel Dunbar3f872332009-07-28 16:08:33 +00001349 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001350 return TokError("expected identifier in directive");
1351
1352 // handle the identifier as the key symbol.
1353 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar959fd882009-08-26 22:13:22 +00001354 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001355 Lexer.Lex();
1356
Daniel Dunbar3f872332009-07-28 16:08:33 +00001357 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001358 return TokError("unexpected token in directive");
1359 Lexer.Lex();
1360
1361 int64_t Size;
1362 SMLoc SizeLoc = Lexer.getLoc();
1363 if (ParseAbsoluteExpression(Size))
1364 return true;
1365
1366 int64_t Pow2Alignment = 0;
1367 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001368 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001369 Lexer.Lex();
1370 Pow2AlignmentLoc = Lexer.getLoc();
1371 if (ParseAbsoluteExpression(Pow2Alignment))
1372 return true;
1373 }
1374
Daniel Dunbar3f872332009-07-28 16:08:33 +00001375 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001376 return TokError("unexpected token in '.zerofill' directive");
1377
1378 Lexer.Lex();
1379
1380 if (Size < 0)
1381 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1382 "than zero");
1383
1384 // NOTE: The alignment in the directive is a power of 2 value, the assember
1385 // may internally end up wanting an alignment in bytes.
1386 // FIXME: Diagnose overflow.
1387 if (Pow2Alignment < 0)
1388 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1389 "can't be less than zero");
1390
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001391 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001392 return Error(IDLoc, "invalid symbol redefinition");
1393
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001394 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001395 //
1396 // FIXME: Arch specific.
1397 Out.EmitZerofill(getMachOSection(Segment, Section,
1398 MCSectionMachO::S_ZEROFILL, 0,
1399 SectionKind()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001400 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001401
1402 return false;
1403}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001404
1405/// ParseDirectiveDarwinSubsectionsViaSymbols
1406/// ::= .subsections_via_symbols
1407bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001408 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001409 return TokError("unexpected token in '.subsections_via_symbols' directive");
1410
1411 Lexer.Lex();
1412
Kevin Enderbyf96db462009-07-16 17:56:39 +00001413 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001414
1415 return false;
1416}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001417
1418/// ParseDirectiveAbort
1419/// ::= .abort [ "abort_string" ]
1420bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001421 // FIXME: Use loc from directive.
1422 SMLoc Loc = Lexer.getLoc();
1423
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001424 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001425 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1426 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001427 return TokError("expected string in '.abort' directive");
1428
Daniel Dunbar419aded2009-07-28 16:38:40 +00001429 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001430
1431 Lexer.Lex();
1432 }
1433
Daniel Dunbar3f872332009-07-28 16:08:33 +00001434 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001435 return TokError("unexpected token in '.abort' directive");
1436
1437 Lexer.Lex();
1438
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001439 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001440 if (Str.empty())
1441 Error(Loc, ".abort detected. Assembly stopping.");
1442 else
1443 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001444
1445 return false;
1446}
Kevin Enderby71148242009-07-14 21:35:03 +00001447
1448/// ParseDirectiveLsym
1449/// ::= .lsym identifier , expression
1450bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001451 StringRef Name;
1452 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001453 return TokError("expected identifier in directive");
1454
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001455 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001456 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001457
Daniel Dunbar3f872332009-07-28 16:08:33 +00001458 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001459 return TokError("unexpected token in '.lsym' directive");
1460 Lexer.Lex();
1461
Daniel Dunbar821e3332009-08-31 08:09:28 +00001462 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001463 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001464 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001465 return true;
1466
Daniel Dunbar3f872332009-07-28 16:08:33 +00001467 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001468 return TokError("unexpected token in '.lsym' directive");
1469
1470 Lexer.Lex();
1471
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001472 // We don't currently support this directive.
1473 //
1474 // FIXME: Diagnostic location!
1475 (void) Sym;
1476 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001477}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001478
1479/// ParseDirectiveInclude
1480/// ::= .include "filename"
1481bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001482 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001483 return TokError("expected string in '.include' directive");
1484
Daniel Dunbar419aded2009-07-28 16:38:40 +00001485 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001486 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001487 Lexer.Lex();
1488
Daniel Dunbar3f872332009-07-28 16:08:33 +00001489 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001490 return TokError("unexpected token in '.include' directive");
1491
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001492 // Strip the quotes.
1493 Filename = Filename.substr(1, Filename.size()-2);
1494
1495 // Attempt to switch the lexer to the included file before consuming the end
1496 // of statement to avoid losing it when we switch.
1497 if (Lexer.EnterIncludeFile(Filename)) {
1498 Lexer.PrintMessage(IncludeLoc,
1499 "Could not find include file '" + Filename + "'",
1500 "error");
1501 return true;
1502 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001503
1504 return false;
1505}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001506
1507/// ParseDirectiveDarwinDumpOrLoad
1508/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001509bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001510 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001511 return TokError("expected string in '.dump' or '.load' directive");
1512
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001513 Lexer.Lex();
1514
Daniel Dunbar3f872332009-07-28 16:08:33 +00001515 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001516 return TokError("unexpected token in '.dump' or '.load' directive");
1517
1518 Lexer.Lex();
1519
Kevin Enderby5026ae42009-07-20 20:25:37 +00001520 // FIXME: If/when .dump and .load are implemented they will be done in the
1521 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001522 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001523 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001524 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001525 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001526
1527 return false;
1528}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001529
1530/// ParseDirectiveIf
1531/// ::= .if expression
1532bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1533 // Consume the identifier that was the .if directive
1534 Lexer.Lex();
1535
1536 TheCondStack.push_back(TheCondState);
1537 TheCondState.TheCond = AsmCond::IfCond;
1538 if(TheCondState.Ignore) {
1539 EatToEndOfStatement();
1540 }
1541 else {
1542 int64_t ExprValue;
1543 if (ParseAbsoluteExpression(ExprValue))
1544 return true;
1545
1546 if (Lexer.isNot(AsmToken::EndOfStatement))
1547 return TokError("unexpected token in '.if' directive");
1548
1549 Lexer.Lex();
1550
1551 TheCondState.CondMet = ExprValue;
1552 TheCondState.Ignore = !TheCondState.CondMet;
1553 }
1554
1555 return false;
1556}
1557
1558/// ParseDirectiveElseIf
1559/// ::= .elseif expression
1560bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1561 if (TheCondState.TheCond != AsmCond::IfCond &&
1562 TheCondState.TheCond != AsmCond::ElseIfCond)
1563 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1564 " an .elseif");
1565 TheCondState.TheCond = AsmCond::ElseIfCond;
1566
1567 // Consume the identifier that was the .elseif directive
1568 Lexer.Lex();
1569
1570 bool LastIgnoreState = false;
1571 if (!TheCondStack.empty())
1572 LastIgnoreState = TheCondStack.back().Ignore;
1573 if (LastIgnoreState || TheCondState.CondMet) {
1574 TheCondState.Ignore = true;
1575 EatToEndOfStatement();
1576 }
1577 else {
1578 int64_t ExprValue;
1579 if (ParseAbsoluteExpression(ExprValue))
1580 return true;
1581
1582 if (Lexer.isNot(AsmToken::EndOfStatement))
1583 return TokError("unexpected token in '.elseif' directive");
1584
1585 Lexer.Lex();
1586 TheCondState.CondMet = ExprValue;
1587 TheCondState.Ignore = !TheCondState.CondMet;
1588 }
1589
1590 return false;
1591}
1592
1593/// ParseDirectiveElse
1594/// ::= .else
1595bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1596 // Consume the identifier that was the .else directive
1597 Lexer.Lex();
1598
1599 if (Lexer.isNot(AsmToken::EndOfStatement))
1600 return TokError("unexpected token in '.else' directive");
1601
1602 Lexer.Lex();
1603
1604 if (TheCondState.TheCond != AsmCond::IfCond &&
1605 TheCondState.TheCond != AsmCond::ElseIfCond)
1606 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1607 ".elseif");
1608 TheCondState.TheCond = AsmCond::ElseCond;
1609 bool LastIgnoreState = false;
1610 if (!TheCondStack.empty())
1611 LastIgnoreState = TheCondStack.back().Ignore;
1612 if (LastIgnoreState || TheCondState.CondMet)
1613 TheCondState.Ignore = true;
1614 else
1615 TheCondState.Ignore = false;
1616
1617 return false;
1618}
1619
1620/// ParseDirectiveEndIf
1621/// ::= .endif
1622bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1623 // Consume the identifier that was the .endif directive
1624 Lexer.Lex();
1625
1626 if (Lexer.isNot(AsmToken::EndOfStatement))
1627 return TokError("unexpected token in '.endif' directive");
1628
1629 Lexer.Lex();
1630
1631 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1632 TheCondStack.empty())
1633 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1634 ".else");
1635 if (!TheCondStack.empty()) {
1636 TheCondState = TheCondStack.back();
1637 TheCondStack.pop_back();
1638 }
1639
1640 return false;
1641}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001642
1643/// ParseDirectiveFile
1644/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001645bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001646 // FIXME: I'm not sure what this is.
1647 int64_t FileNumber = -1;
1648 if (Lexer.is(AsmToken::Integer)) {
1649 FileNumber = Lexer.getTok().getIntVal();
1650 Lexer.Lex();
1651
1652 if (FileNumber < 1)
1653 return TokError("file number less than one");
1654 }
1655
1656 if (Lexer.isNot(AsmToken::String))
1657 return TokError("unexpected token in '.file' directive");
1658
Bill Wendling9bc0af82009-12-28 01:34:57 +00001659 StringRef ATTRIBUTE_UNUSED FileName = Lexer.getTok().getString();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001660 Lexer.Lex();
1661
1662 if (Lexer.isNot(AsmToken::EndOfStatement))
1663 return TokError("unexpected token in '.file' directive");
1664
1665 // FIXME: Do something with the .file.
1666
1667 return false;
1668}
1669
1670/// ParseDirectiveLine
1671/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001672bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001673 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1674 if (Lexer.isNot(AsmToken::Integer))
1675 return TokError("unexpected token in '.line' directive");
1676
1677 int64_t LineNumber = Lexer.getTok().getIntVal();
1678 (void) LineNumber;
1679 Lexer.Lex();
1680
1681 // FIXME: Do something with the .line.
1682 }
1683
1684 if (Lexer.isNot(AsmToken::EndOfStatement))
1685 return TokError("unexpected token in '.file' directive");
1686
1687 return false;
1688}
1689
1690
1691/// ParseDirectiveLoc
1692/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001693bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001694 if (Lexer.isNot(AsmToken::Integer))
1695 return TokError("unexpected token in '.loc' directive");
1696
1697 // FIXME: What are these fields?
1698 int64_t FileNumber = Lexer.getTok().getIntVal();
1699 (void) FileNumber;
1700 // FIXME: Validate file.
1701
1702 Lexer.Lex();
1703 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1704 if (Lexer.isNot(AsmToken::Integer))
1705 return TokError("unexpected token in '.loc' directive");
1706
1707 int64_t Param2 = Lexer.getTok().getIntVal();
1708 (void) Param2;
1709 Lexer.Lex();
1710
1711 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1712 if (Lexer.isNot(AsmToken::Integer))
1713 return TokError("unexpected token in '.loc' directive");
1714
1715 int64_t Param3 = Lexer.getTok().getIntVal();
1716 (void) Param3;
1717 Lexer.Lex();
1718
1719 // FIXME: Do something with the .loc.
1720 }
1721 }
1722
1723 if (Lexer.isNot(AsmToken::EndOfStatement))
1724 return TokError("unexpected token in '.file' directive");
1725
1726 return false;
1727}
1728