blob: e4c85a791424bbcee430016a21435cc2359ae213 [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
16#include "AsmExpr.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"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000019#include "llvm/MC/MCInst.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000021#include "llvm/MC/MCSymbol.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000022#include "llvm/Support/SourceMgr.h"
23#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000024#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000025using namespace llvm;
26
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000027void AsmParser::Warning(SMLoc L, const Twine &Msg) {
28 Lexer.PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000029}
30
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000031bool AsmParser::Error(SMLoc L, const Twine &Msg) {
32 Lexer.PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000033 return true;
34}
35
36bool AsmParser::TokError(const char *Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000037 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000038 return true;
39}
40
Chris Lattner27aa7d22009-06-21 20:16:42 +000041bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000042 // Prime the lexer.
43 Lexer.Lex();
44
Chris Lattnerb717fb02009-07-02 21:53:43 +000045 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000046
Chris Lattnerb717fb02009-07-02 21:53:43 +000047 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +000048 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +000049 if (!ParseStatement()) continue;
50
51 // If we had an error, remember it and recover by skipping to the next line.
52 HadError = true;
53 EatToEndOfStatement();
54 }
55
56 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +000057}
58
Chris Lattner2cf5f142009-06-22 01:29:09 +000059/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
60void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +000061 while (Lexer.isNot(AsmToken::EndOfStatement) &&
62 Lexer.isNot(AsmToken::Eof))
Chris Lattner2cf5f142009-06-22 01:29:09 +000063 Lexer.Lex();
64
65 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +000066 if (Lexer.is(AsmToken::EndOfStatement))
Chris Lattner2cf5f142009-06-22 01:29:09 +000067 Lexer.Lex();
68}
69
Chris Lattnerc4193832009-06-22 05:51:26 +000070
Chris Lattner74ec1a32009-06-22 06:32:03 +000071/// ParseParenExpr - Parse a paren expression and return it.
72/// NOTE: This assumes the leading '(' has already been consumed.
73///
74/// parenexpr ::= expr)
75///
Daniel Dunbar475839e2009-06-29 20:37:27 +000076bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
Chris Lattner74ec1a32009-06-22 06:32:03 +000077 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +000078 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +000079 return TokError("expected ')' in parentheses expression");
80 Lexer.Lex();
81 return false;
82}
Chris Lattnerc4193832009-06-22 05:51:26 +000083
Chris Lattner74ec1a32009-06-22 06:32:03 +000084/// ParsePrimaryExpr - Parse a primary expression and return it.
85/// primaryexpr ::= (parenexpr
86/// primaryexpr ::= symbol
87/// primaryexpr ::= number
88/// primaryexpr ::= ~,+,- primaryexpr
Daniel Dunbar475839e2009-06-29 20:37:27 +000089bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +000090 switch (Lexer.getKind()) {
91 default:
92 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +000093 case AsmToken::Exclaim:
Daniel Dunbar475839e2009-06-29 20:37:27 +000094 Lexer.Lex(); // Eat the operator.
95 if (ParsePrimaryExpr(Res))
96 return true;
97 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
98 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +000099 case AsmToken::Identifier: {
Chris Lattnerc4193832009-06-22 05:51:26 +0000100 // This is a label, this should be parsed as part of an expression, to
Daniel Dunbar475839e2009-06-29 20:37:27 +0000101 // handle things like LFOO+4.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000102 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000103
104 // If this is use of an undefined symbol then mark it external.
105 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
106 Sym->setExternal(true);
107
108 Res = new AsmSymbolRefExpr(Sym);
Chris Lattnerc4193832009-06-22 05:51:26 +0000109 Lexer.Lex(); // Eat identifier.
110 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000111 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000112 case AsmToken::Integer:
Daniel Dunbar419aded2009-07-28 16:38:40 +0000113 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
Chris Lattnerc4193832009-06-22 05:51:26 +0000114 Lexer.Lex(); // Eat identifier.
115 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000116 case AsmToken::LParen:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000117 Lexer.Lex(); // Eat the '('.
118 return ParseParenExpr(Res);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000119 case AsmToken::Minus:
Chris Lattner74ec1a32009-06-22 06:32:03 +0000120 Lexer.Lex(); // Eat the operator.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000121 if (ParsePrimaryExpr(Res))
122 return true;
123 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
124 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000125 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000126 Lexer.Lex(); // Eat the operator.
127 if (ParsePrimaryExpr(Res))
128 return true;
129 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
130 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000131 case AsmToken::Tilde:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000132 Lexer.Lex(); // Eat the operator.
133 if (ParsePrimaryExpr(Res))
134 return true;
135 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
136 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000137 }
138}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000139
140/// ParseExpression - Parse an expression and return it.
141///
142/// expr ::= expr +,- expr -> lowest.
143/// expr ::= expr |,^,&,! expr -> middle.
144/// expr ::= expr *,/,%,<<,>> expr -> highest.
145/// expr ::= primaryexpr
146///
Daniel Dunbar475839e2009-06-29 20:37:27 +0000147bool AsmParser::ParseExpression(AsmExpr *&Res) {
148 Res = 0;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000149 return ParsePrimaryExpr(Res) ||
150 ParseBinOpRHS(1, Res);
Chris Lattner74ec1a32009-06-22 06:32:03 +0000151}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000152
Daniel Dunbar475839e2009-06-29 20:37:27 +0000153bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
154 AsmExpr *Expr;
155
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000156 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000157 if (ParseExpression(Expr))
158 return true;
159
160 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000161 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000162
163 return false;
164}
165
Daniel Dunbar15d17072009-06-30 01:49:52 +0000166bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
167 AsmExpr *Expr;
168
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000169 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000170 if (ParseExpression(Expr))
171 return true;
172
173 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000174 return Error(StartLoc, "expected relocatable expression");
Daniel Dunbar15d17072009-06-30 01:49:52 +0000175
176 return false;
177}
178
Daniel Dunbar2c3f00c2009-07-02 02:09:07 +0000179bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
180 AsmExpr *Expr;
181
182 SMLoc StartLoc = Lexer.getLoc();
183 if (ParseParenExpr(Expr))
184 return true;
185
186 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
187 return Error(StartLoc, "expected relocatable expression");
188
189 return false;
190}
191
Daniel Dunbar3f872332009-07-28 16:08:33 +0000192static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar475839e2009-06-29 20:37:27 +0000193 AsmBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000194 switch (K) {
195 default: return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000196
197 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000198 case AsmToken::AmpAmp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000199 Kind = AsmBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000200 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000201 case AsmToken::PipePipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000202 Kind = AsmBinaryExpr::LOr;
203 return 1;
204
205 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000206 case AsmToken::Plus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000207 Kind = AsmBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000208 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000209 case AsmToken::Minus:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000210 Kind = AsmBinaryExpr::Sub;
211 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000212 case AsmToken::EqualEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000213 Kind = AsmBinaryExpr::EQ;
214 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000215 case AsmToken::ExclaimEqual:
216 case AsmToken::LessGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000217 Kind = AsmBinaryExpr::NE;
218 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000219 case AsmToken::Less:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000220 Kind = AsmBinaryExpr::LT;
221 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000222 case AsmToken::LessEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000223 Kind = AsmBinaryExpr::LTE;
224 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000225 case AsmToken::Greater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000226 Kind = AsmBinaryExpr::GT;
227 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000228 case AsmToken::GreaterEqual:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000229 Kind = AsmBinaryExpr::GTE;
230 return 2;
231
232 // Intermediate Precedence: |, &, ^
233 //
234 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000235 case AsmToken::Pipe:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000236 Kind = AsmBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000237 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000238 case AsmToken::Caret:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000239 Kind = AsmBinaryExpr::Xor;
240 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000241 case AsmToken::Amp:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000242 Kind = AsmBinaryExpr::And;
243 return 3;
244
245 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000246 case AsmToken::Star:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000247 Kind = AsmBinaryExpr::Mul;
248 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000249 case AsmToken::Slash:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000250 Kind = AsmBinaryExpr::Div;
251 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000252 case AsmToken::Percent:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 Kind = AsmBinaryExpr::Mod;
254 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000255 case AsmToken::LessLess:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 Kind = AsmBinaryExpr::Shl;
257 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 case AsmToken::GreaterGreater:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 Kind = AsmBinaryExpr::Shr;
260 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000261 }
262}
263
264
265/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
266/// Res contains the LHS of the expression on input.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000267bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000268 while (1) {
Daniel Dunbar51330632009-06-29 21:14:21 +0000269 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000270 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000271
272 // If the next token is lower precedence than we are allowed to eat, return
273 // successfully with what we ate already.
274 if (TokPrec < Precedence)
275 return false;
276
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000277 Lexer.Lex();
278
279 // Eat the next primary expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000280 AsmExpr *RHS;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000281 if (ParsePrimaryExpr(RHS)) return true;
282
283 // If BinOp binds less tightly with RHS than the operator after RHS, let
284 // the pending operator take RHS as its LHS.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000285 AsmBinaryExpr::Opcode Dummy;
286 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000287 if (TokPrec < NextTokPrec) {
288 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
289 }
290
Daniel Dunbar475839e2009-06-29 20:37:27 +0000291 // Merge LHS and RHS according to operator.
292 Res = new AsmBinaryExpr(Kind, Res, RHS);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000293 }
294}
295
Chris Lattnerc4193832009-06-22 05:51:26 +0000296
297
298
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000299/// ParseStatement:
300/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000301/// ::= Label* Directive ...Operands... EndOfStatement
302/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000303bool AsmParser::ParseStatement() {
304 switch (Lexer.getKind()) {
305 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000306 return TokError("unexpected token at start of statement");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000307 case AsmToken::EndOfStatement:
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000308 Lexer.Lex();
309 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000310 case AsmToken::Identifier:
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000311 break;
312 // TODO: Recurse on local labels etc.
313 }
314
315 // If we have an identifier, handle it as the key symbol.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000316 AsmToken ID = Lexer.getTok();
317 SMLoc IDLoc = ID.getLoc();
318 StringRef IDVal = ID.getString();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000319
320 // Consume the identifier, see what is after it.
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000321 switch (Lexer.Lex().getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000322 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000323 // identifier ':' -> Label.
324 Lexer.Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000325
326 // Diagnose attempt to use a variable as a label.
327 //
328 // FIXME: Diagnostics. Note the location of the definition as a label.
329 // FIXME: This doesn't diagnose assignment to a symbol which has been
330 // implicitly marked as external.
331 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
332 if (Sym->getSection())
333 return Error(IDLoc, "invalid symbol redefinition");
334 if (Ctx.GetSymbolValue(Sym))
335 return Error(IDLoc, "symbol already used as assembler variable");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000336
337 // Since we saw a label, create a symbol and emit it.
338 // FIXME: If the label starts with L it is an assembler temporary label.
339 // Why does the client of this api need to know this?
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000340 Out.EmitLabel(Sym);
341
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000342 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000343 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000344
Daniel Dunbar3f872332009-07-28 16:08:33 +0000345 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000346 // identifier '=' ... -> assignment statement
347 Lexer.Lex();
348
349 return ParseAssignment(IDVal, false);
350
351 default: // Normal instruction or directive.
352 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000353 }
354
355 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000356 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000357 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000358 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000359 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000360 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000361 // FIXME: This changes behavior based on the -static flag to the
362 // assembler.
363 return ParseDirectiveSectionSwitch("__TEXT,__text",
364 "regular,pure_instructions");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000365 if (IDVal == ".const")
Chris Lattner529fb542009-06-24 05:13:15 +0000366 return ParseDirectiveSectionSwitch("__TEXT,__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000367 if (IDVal == ".static_const")
Chris Lattner529fb542009-06-24 05:13:15 +0000368 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000369 if (IDVal == ".cstring")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000370 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
371 "cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000372 if (IDVal == ".literal4")
Chris Lattner529fb542009-06-24 05:13:15 +0000373 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000374 if (IDVal == ".literal8")
Chris Lattner529fb542009-06-24 05:13:15 +0000375 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000376 if (IDVal == ".literal16")
Chris Lattner529fb542009-06-24 05:13:15 +0000377 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
378 "16byte_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000379 if (IDVal == ".constructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000380 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000381 if (IDVal == ".destructor")
Chris Lattner529fb542009-06-24 05:13:15 +0000382 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000383 if (IDVal == ".fvmlib_init0")
Chris Lattner529fb542009-06-24 05:13:15 +0000384 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000385 if (IDVal == ".fvmlib_init1")
Chris Lattner529fb542009-06-24 05:13:15 +0000386 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000387 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
Chris Lattner529fb542009-06-24 05:13:15 +0000388 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
389 "self_modifying_code+pure_instructions,5");
390 // FIXME: .picsymbol_stub on PPC.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000391 if (IDVal == ".data")
Chris Lattner529fb542009-06-24 05:13:15 +0000392 return ParseDirectiveSectionSwitch("__DATA,__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000393 if (IDVal == ".static_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000394 return ParseDirectiveSectionSwitch("__DATA,__static_data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000395 if (IDVal == ".non_lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000396 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
397 "non_lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000398 if (IDVal == ".lazy_symbol_pointer")
Chris Lattner529fb542009-06-24 05:13:15 +0000399 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
400 "lazy_symbol_pointers");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000401 if (IDVal == ".dyld")
Chris Lattner529fb542009-06-24 05:13:15 +0000402 return ParseDirectiveSectionSwitch("__DATA,__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000403 if (IDVal == ".mod_init_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000404 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
405 "mod_init_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000406 if (IDVal == ".mod_term_func")
Chris Lattner529fb542009-06-24 05:13:15 +0000407 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
408 "mod_term_funcs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000409 if (IDVal == ".const_data")
Chris Lattner529fb542009-06-24 05:13:15 +0000410 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
411
412
413 // FIXME: Verify attributes on sections.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000414 if (IDVal == ".objc_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000415 return ParseDirectiveSectionSwitch("__OBJC,__class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000416 if (IDVal == ".objc_meta_class")
Chris Lattner529fb542009-06-24 05:13:15 +0000417 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000418 if (IDVal == ".objc_cat_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000419 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000420 if (IDVal == ".objc_cat_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000421 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000422 if (IDVal == ".objc_protocol")
Chris Lattner529fb542009-06-24 05:13:15 +0000423 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000424 if (IDVal == ".objc_string_object")
Chris Lattner529fb542009-06-24 05:13:15 +0000425 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000426 if (IDVal == ".objc_cls_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000427 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000428 if (IDVal == ".objc_inst_meth")
Chris Lattner529fb542009-06-24 05:13:15 +0000429 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000430 if (IDVal == ".objc_cls_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000431 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000432 if (IDVal == ".objc_message_refs")
Chris Lattner529fb542009-06-24 05:13:15 +0000433 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000434 if (IDVal == ".objc_symbols")
Chris Lattner529fb542009-06-24 05:13:15 +0000435 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000436 if (IDVal == ".objc_category")
Chris Lattner529fb542009-06-24 05:13:15 +0000437 return ParseDirectiveSectionSwitch("__OBJC,__category");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000438 if (IDVal == ".objc_class_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000439 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000440 if (IDVal == ".objc_instance_vars")
Chris Lattner529fb542009-06-24 05:13:15 +0000441 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000442 if (IDVal == ".objc_module_info")
Chris Lattner529fb542009-06-24 05:13:15 +0000443 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000444 if (IDVal == ".objc_class_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000445 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000446 if (IDVal == ".objc_meth_var_types")
Chris Lattner529fb542009-06-24 05:13:15 +0000447 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000448 if (IDVal == ".objc_meth_var_names")
Chris Lattner529fb542009-06-24 05:13:15 +0000449 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000450 if (IDVal == ".objc_selector_strs")
Chris Lattner529fb542009-06-24 05:13:15 +0000451 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
Chris Lattner9a023f72009-06-24 04:43:34 +0000452
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000453 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000454 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000455 return ParseDirectiveSet();
456
Daniel Dunbara0d14262009-06-24 23:30:00 +0000457 // Data directives
458
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000459 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000460 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000461 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000462 return ParseDirectiveAscii(true);
463
464 // FIXME: Target hooks for size? Also for "word", "hword".
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000465 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000466 return ParseDirectiveValue(1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000467 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000468 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000469 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000470 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000471 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000472 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000473
474 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000475 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000476 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000477 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000478 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000479 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000480 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000481 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000482 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000483 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000484 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000485 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000486 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000487 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000488 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000489 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000490 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
491
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000492 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000493 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000494
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000495 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000496 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000497 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000498 return ParseDirectiveSpace();
499
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000500 // Symbol attribute directives
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000501 if (IDVal == ".globl" || IDVal == ".global")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000502 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000503 if (IDVal == ".hidden")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000504 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000505 if (IDVal == ".indirect_symbol")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000506 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000507 if (IDVal == ".internal")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000508 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000509 if (IDVal == ".lazy_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000510 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000511 if (IDVal == ".no_dead_strip")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000512 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000513 if (IDVal == ".private_extern")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000514 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000515 if (IDVal == ".protected")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000516 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000517 if (IDVal == ".reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000518 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000519 if (IDVal == ".weak")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000520 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000521 if (IDVal == ".weak_definition")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000522 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000523 if (IDVal == ".weak_reference")
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000524 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
525
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000527 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000529 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000530 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000531 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000532 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000533 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000534 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000535 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000536
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000538 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000539 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000540 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000541 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000542 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000543 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000544 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000545 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000546 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000547
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000548 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000549 EatToEndOfStatement();
550 return false;
551 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000552
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000553 MCInst Inst;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000554 if (ParseX86InstOperands(IDVal, Inst) &&
555 getTargetParser().ParseInstruction(*this, IDVal, Inst))
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000556 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000557
Daniel Dunbar3f872332009-07-28 16:08:33 +0000558 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000559 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000560
561 // Eat the end of statement marker.
562 Lexer.Lex();
563
564 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000565 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000566
567 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000568 return false;
569}
Chris Lattner9a023f72009-06-24 04:43:34 +0000570
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000571bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000572 // FIXME: Use better location, we should use proper tokens.
573 SMLoc EqualLoc = Lexer.getLoc();
574
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000575 MCValue Value;
576 if (ParseRelocatableExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000577 return true;
578
Daniel Dunbar3f872332009-07-28 16:08:33 +0000579 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000580 return TokError("unexpected token in assignment");
581
582 // Eat the end of statement marker.
583 Lexer.Lex();
584
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000585 // Diagnose assignment to a label.
586 //
587 // FIXME: Diagnostics. Note the location of the definition as a label.
588 // FIXME: This doesn't diagnose assignment to a symbol which has been
589 // implicitly marked as external.
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000590 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000591 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000592 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000593 if (Sym->getSection())
594 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
595 if (Sym->isExternal())
596 return Error(EqualLoc, "invalid assignment to external symbol");
597
598 // Do the assignment.
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000599 Out.EmitAssignment(Sym, Value, IsDotSet);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000600
601 return false;
602}
603
604/// ParseDirectiveSet:
605/// ::= .set identifier ',' expression
606bool AsmParser::ParseDirectiveSet() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000607 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000608 return TokError("expected identifier after '.set' directive");
609
Daniel Dunbar419aded2009-07-28 16:38:40 +0000610 StringRef Name = Lexer.getTok().getString();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000611
Daniel Dunbara3c924f2009-07-28 16:56:42 +0000612 if (Lexer.Lex().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000613 return TokError("unexpected token in '.set'");
614 Lexer.Lex();
615
616 return ParseAssignment(Name, true);
617}
618
Chris Lattner9a023f72009-06-24 04:43:34 +0000619/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000620/// ::= .section identifier (',' identifier)*
621/// FIXME: This should actually parse out the segment, section, attributes and
622/// sizeof_stub fields.
623bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000624 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000625 return TokError("expected identifier after '.section' directive");
626
Daniel Dunbar419aded2009-07-28 16:38:40 +0000627 std::string Section = Lexer.getTok().getString();
Chris Lattner9a023f72009-06-24 04:43:34 +0000628 Lexer.Lex();
629
630 // Accept a comma separated list of modifiers.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 while (Lexer.is(AsmToken::Comma)) {
Chris Lattner9a023f72009-06-24 04:43:34 +0000632 Lexer.Lex();
633
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9a023f72009-06-24 04:43:34 +0000635 return TokError("expected identifier in '.section' directive");
636 Section += ',';
Daniel Dunbar419aded2009-07-28 16:38:40 +0000637 Section += Lexer.getTok().getString().str();
Chris Lattner9a023f72009-06-24 04:43:34 +0000638 Lexer.Lex();
639 }
640
Daniel Dunbar3f872332009-07-28 16:08:33 +0000641 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000642 return TokError("unexpected token in '.section' directive");
643 Lexer.Lex();
644
645 Out.SwitchSection(Ctx.GetSection(Section.c_str()));
646 return false;
647}
648
Chris Lattner529fb542009-06-24 05:13:15 +0000649bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
650 const char *Directives) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000651 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000652 return TokError("unexpected token in section switching directive");
653 Lexer.Lex();
654
655 std::string SectionStr = Section;
656 if (Directives && Directives[0]) {
657 SectionStr += ",";
658 SectionStr += Directives;
659 }
660
661 Out.SwitchSection(Ctx.GetSection(Section));
662 return false;
663}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000664
665/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000666/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000667bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000668 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000669 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000670 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000671 return TokError("expected string in '.ascii' or '.asciz' directive");
672
673 // FIXME: This shouldn't use a const char* + strlen, the string could have
674 // embedded nulls.
675 // FIXME: Should have accessor for getting string contents.
Daniel Dunbar419aded2009-07-28 16:38:40 +0000676 StringRef Str = Lexer.getTok().getString();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 Out.EmitBytes(Str.substr(1, Str.size() - 2));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000678 if (ZeroTerminated)
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 Out.EmitBytes(StringRef("\0", 1));
Daniel Dunbara0d14262009-06-24 23:30:00 +0000680
681 Lexer.Lex();
682
Daniel Dunbar3f872332009-07-28 16:08:33 +0000683 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000684 break;
685
Daniel Dunbar3f872332009-07-28 16:08:33 +0000686 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000687 return TokError("unexpected token in '.ascii' or '.asciz' directive");
688 Lexer.Lex();
689 }
690 }
691
692 Lexer.Lex();
693 return false;
694}
695
696/// ParseDirectiveValue
697/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
698bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000699 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000700 for (;;) {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000701 MCValue Expr;
702 if (ParseRelocatableExpression(Expr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000703 return true;
704
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000705 Out.EmitValue(Expr, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000706
Daniel Dunbar3f872332009-07-28 16:08:33 +0000707 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000708 break;
709
710 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000711 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000712 return TokError("unexpected token in directive");
713 Lexer.Lex();
714 }
715 }
716
717 Lexer.Lex();
718 return false;
719}
720
721/// ParseDirectiveSpace
722/// ::= .space expression [ , expression ]
723bool AsmParser::ParseDirectiveSpace() {
724 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000725 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000726 return true;
727
728 int64_t FillExpr = 0;
729 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000730 if (Lexer.isNot(AsmToken::EndOfStatement)) {
731 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000732 return TokError("unexpected token in '.space' directive");
733 Lexer.Lex();
734
Daniel Dunbar475839e2009-06-29 20:37:27 +0000735 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000736 return true;
737
738 HasFillExpr = true;
739
Daniel Dunbar3f872332009-07-28 16:08:33 +0000740 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000741 return TokError("unexpected token in '.space' directive");
742 }
743
744 Lexer.Lex();
745
746 if (NumBytes <= 0)
747 return TokError("invalid number of bytes in '.space' directive");
748
749 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
750 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
751 Out.EmitValue(MCValue::get(FillExpr), 1);
752
753 return false;
754}
755
756/// ParseDirectiveFill
757/// ::= .fill expression , expression , expression
758bool AsmParser::ParseDirectiveFill() {
759 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000760 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000761 return true;
762
Daniel Dunbar3f872332009-07-28 16:08:33 +0000763 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000764 return TokError("unexpected token in '.fill' directive");
765 Lexer.Lex();
766
767 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000768 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000769 return true;
770
Daniel Dunbar3f872332009-07-28 16:08:33 +0000771 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000772 return TokError("unexpected token in '.fill' directive");
773 Lexer.Lex();
774
775 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000776 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000777 return true;
778
Daniel Dunbar3f872332009-07-28 16:08:33 +0000779 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000780 return TokError("unexpected token in '.fill' directive");
781
782 Lexer.Lex();
783
784 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
785 return TokError("invalid '.fill' size, expected 1, 2, or 4");
786
787 for (uint64_t i = 0, e = NumValues; i != e; ++i)
788 Out.EmitValue(MCValue::get(FillExpr), FillSize);
789
790 return false;
791}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000792
793/// ParseDirectiveOrg
794/// ::= .org expression [ , expression ]
795bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000796 MCValue Offset;
797 if (ParseRelocatableExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000798 return true;
799
800 // Parse optional fill expression.
801 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000802 if (Lexer.isNot(AsmToken::EndOfStatement)) {
803 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000804 return TokError("unexpected token in '.org' directive");
805 Lexer.Lex();
806
Daniel Dunbar475839e2009-06-29 20:37:27 +0000807 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000808 return true;
809
Daniel Dunbar3f872332009-07-28 16:08:33 +0000810 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000811 return TokError("unexpected token in '.org' directive");
812 }
813
814 Lexer.Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000815
816 // FIXME: Only limited forms of relocatable expressions are accepted here, it
817 // has to be relative to the current section.
818 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000819
820 return false;
821}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000822
823/// ParseDirectiveAlign
824/// ::= {.align, ...} expression [ , expression [ , expression ]]
825bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
826 int64_t Alignment;
827 if (ParseAbsoluteExpression(Alignment))
828 return true;
829
830 SMLoc MaxBytesLoc;
831 bool HasFillExpr = false;
832 int64_t FillExpr = 0;
833 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000834 if (Lexer.isNot(AsmToken::EndOfStatement)) {
835 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000836 return TokError("unexpected token in directive");
837 Lexer.Lex();
838
839 // The fill expression can be omitted while specifying a maximum number of
840 // alignment bytes, e.g:
841 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000843 HasFillExpr = true;
844 if (ParseAbsoluteExpression(FillExpr))
845 return true;
846 }
847
Daniel Dunbar3f872332009-07-28 16:08:33 +0000848 if (Lexer.isNot(AsmToken::EndOfStatement)) {
849 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000850 return TokError("unexpected token in directive");
851 Lexer.Lex();
852
853 MaxBytesLoc = Lexer.getLoc();
854 if (ParseAbsoluteExpression(MaxBytesToFill))
855 return true;
856
Daniel Dunbar3f872332009-07-28 16:08:33 +0000857 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000858 return TokError("unexpected token in directive");
859 }
860 }
861
862 Lexer.Lex();
863
864 if (!HasFillExpr) {
865 // FIXME: Sometimes fill with nop.
866 FillExpr = 0;
867 }
868
869 // Compute alignment in bytes.
870 if (IsPow2) {
871 // FIXME: Diagnose overflow.
Chris Lattner39750252009-07-11 22:32:37 +0000872 Alignment = 1LL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000873 }
874
875 // Diagnose non-sensical max bytes to fill.
876 if (MaxBytesLoc.isValid()) {
877 if (MaxBytesToFill < 1) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000878 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
879 "many bytes, ignoring");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000880 return false;
881 }
882
883 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000884 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
885 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000886 MaxBytesToFill = 0;
887 }
888 }
889
890 // FIXME: Target specific behavior about how the "extra" bytes are filled.
891 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
892
893 return false;
894}
895
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000896/// ParseDirectiveSymbolAttribute
897/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
898bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000899 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000900 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000901 if (Lexer.isNot(AsmToken::Identifier))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000902 return TokError("expected identifier in directive");
903
Daniel Dunbar419aded2009-07-28 16:38:40 +0000904 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000905 Lexer.Lex();
906
907 // If this is use of an undefined symbol then mark it external.
908 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
909 Sym->setExternal(true);
910
911 Out.EmitSymbolAttribute(Sym, Attr);
912
Daniel Dunbar3f872332009-07-28 16:08:33 +0000913 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000914 break;
915
Daniel Dunbar3f872332009-07-28 16:08:33 +0000916 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000917 return TokError("unexpected token in directive");
918 Lexer.Lex();
919 }
920 }
921
922 Lexer.Lex();
923 return false;
924}
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000925
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000926/// ParseDirectiveDarwinSymbolDesc
927/// ::= .desc identifier , expression
928bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000929 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000930 return TokError("expected identifier in directive");
931
932 // handle the identifier as the key symbol.
933 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000934 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000935 Lexer.Lex();
936
Daniel Dunbar3f872332009-07-28 16:08:33 +0000937 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000938 return TokError("unexpected token in '.desc' directive");
939 Lexer.Lex();
940
941 SMLoc DescLoc = Lexer.getLoc();
942 int64_t DescValue;
943 if (ParseAbsoluteExpression(DescValue))
944 return true;
945
Daniel Dunbar3f872332009-07-28 16:08:33 +0000946 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000947 return TokError("unexpected token in '.desc' directive");
948
949 Lexer.Lex();
950
951 // Set the n_desc field of this Symbol to this DescValue
952 Out.EmitSymbolDesc(Sym, DescValue);
953
954 return false;
955}
956
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000957/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +0000958/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
959bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000960 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000961 return TokError("expected identifier in directive");
962
963 // handle the identifier as the key symbol.
964 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000965 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000966 Lexer.Lex();
967
Daniel Dunbar3f872332009-07-28 16:08:33 +0000968 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000969 return TokError("unexpected token in directive");
970 Lexer.Lex();
971
972 int64_t Size;
973 SMLoc SizeLoc = Lexer.getLoc();
974 if (ParseAbsoluteExpression(Size))
975 return true;
976
977 int64_t Pow2Alignment = 0;
978 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000979 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000980 Lexer.Lex();
981 Pow2AlignmentLoc = Lexer.getLoc();
982 if (ParseAbsoluteExpression(Pow2Alignment))
983 return true;
984 }
985
Daniel Dunbar3f872332009-07-28 16:08:33 +0000986 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +0000987 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000988
989 Lexer.Lex();
990
Chris Lattner1fc3d752009-07-09 17:25:12 +0000991 // NOTE: a size of zero for a .comm should create a undefined symbol
992 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000993 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +0000994 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
995 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000996
997 // NOTE: The alignment in the directive is a power of 2 value, the assember
998 // may internally end up wanting an alignment in bytes.
999 // FIXME: Diagnose overflow.
1000 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001001 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1002 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001003
1004 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1005 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1006 return Error(IDLoc, "invalid symbol redefinition");
1007
Chris Lattner1fc3d752009-07-09 17:25:12 +00001008 // Create the Symbol as a common or local common with Size and Pow2Alignment
1009 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001010
1011 return false;
1012}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001013
1014/// ParseDirectiveDarwinZerofill
1015/// ::= .zerofill segname , sectname [, identifier , size_expression [
1016/// , align_expression ]]
1017bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001018 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001019 return TokError("expected segment name after '.zerofill' directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001020 std::string Section = Lexer.getTok().getString();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001021 Lexer.Lex();
1022
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001024 return TokError("unexpected token in directive");
1025 Section += ',';
1026 Lexer.Lex();
1027
Daniel Dunbar3f872332009-07-28 16:08:33 +00001028 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001029 return TokError("expected section name after comma in '.zerofill' "
1030 "directive");
Daniel Dunbar419aded2009-07-28 16:38:40 +00001031 Section += Lexer.getTok().getString().str();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001032 Lexer.Lex();
1033
1034 // FIXME: we will need to tell GetSection() that this is to be created with or
1035 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1036 // below could be done but for now it is not as EmitZerofill() does not know
1037 // how to deal with a section type in the section name like
1038 // ParseDirectiveDarwinSection() allows.
1039 // Section += ',';
1040 // Section += "zerofill";
1041
1042 // If this is the end of the line all that was wanted was to create the
1043 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001044 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001045 // Create the zerofill section but no symbol
1046 Out.EmitZerofill(Ctx.GetSection(Section.c_str()));
1047 return false;
1048 }
1049
Daniel Dunbar3f872332009-07-28 16:08:33 +00001050 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001051 return TokError("unexpected token in directive");
1052 Lexer.Lex();
1053
Daniel Dunbar3f872332009-07-28 16:08:33 +00001054 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001055 return TokError("expected identifier in directive");
1056
1057 // handle the identifier as the key symbol.
1058 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001059 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Chris Lattner9be3fee2009-07-10 22:20:30 +00001060 Lexer.Lex();
1061
Daniel Dunbar3f872332009-07-28 16:08:33 +00001062 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001063 return TokError("unexpected token in directive");
1064 Lexer.Lex();
1065
1066 int64_t Size;
1067 SMLoc SizeLoc = Lexer.getLoc();
1068 if (ParseAbsoluteExpression(Size))
1069 return true;
1070
1071 int64_t Pow2Alignment = 0;
1072 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001073 if (Lexer.is(AsmToken::Comma)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001074 Lexer.Lex();
1075 Pow2AlignmentLoc = Lexer.getLoc();
1076 if (ParseAbsoluteExpression(Pow2Alignment))
1077 return true;
1078 }
1079
Daniel Dunbar3f872332009-07-28 16:08:33 +00001080 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001081 return TokError("unexpected token in '.zerofill' directive");
1082
1083 Lexer.Lex();
1084
1085 if (Size < 0)
1086 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1087 "than zero");
1088
1089 // NOTE: The alignment in the directive is a power of 2 value, the assember
1090 // may internally end up wanting an alignment in bytes.
1091 // FIXME: Diagnose overflow.
1092 if (Pow2Alignment < 0)
1093 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1094 "can't be less than zero");
1095
1096 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1097 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1098 return Error(IDLoc, "invalid symbol redefinition");
1099
1100 // Create the zerofill Symbol with Size and Pow2Alignment
1101 Out.EmitZerofill(Ctx.GetSection(Section.c_str()), Sym, Size, Pow2Alignment);
1102
1103 return false;
1104}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001105
1106/// ParseDirectiveDarwinSubsectionsViaSymbols
1107/// ::= .subsections_via_symbols
1108bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001109 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001110 return TokError("unexpected token in '.subsections_via_symbols' directive");
1111
1112 Lexer.Lex();
1113
Kevin Enderbyf96db462009-07-16 17:56:39 +00001114 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001115
1116 return false;
1117}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001118
1119/// ParseDirectiveAbort
1120/// ::= .abort [ "abort_string" ]
1121bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001122 // FIXME: Use loc from directive.
1123 SMLoc Loc = Lexer.getLoc();
1124
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001125 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001126 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1127 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001128 return TokError("expected string in '.abort' directive");
1129
Daniel Dunbar419aded2009-07-28 16:38:40 +00001130 Str = Lexer.getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001131
1132 Lexer.Lex();
1133 }
1134
Daniel Dunbar3f872332009-07-28 16:08:33 +00001135 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001136 return TokError("unexpected token in '.abort' directive");
1137
1138 Lexer.Lex();
1139
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001140 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001141 if (Str.empty())
1142 Error(Loc, ".abort detected. Assembly stopping.");
1143 else
1144 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001145
1146 return false;
1147}
Kevin Enderby71148242009-07-14 21:35:03 +00001148
1149/// ParseDirectiveLsym
1150/// ::= .lsym identifier , expression
1151bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001152 if (Lexer.isNot(AsmToken::Identifier))
Kevin Enderby71148242009-07-14 21:35:03 +00001153 return TokError("expected identifier in directive");
1154
1155 // handle the identifier as the key symbol.
1156 SMLoc IDLoc = Lexer.getLoc();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001157 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
Kevin Enderby71148242009-07-14 21:35:03 +00001158 Lexer.Lex();
1159
Daniel Dunbar3f872332009-07-28 16:08:33 +00001160 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001161 return TokError("unexpected token in '.lsym' directive");
1162 Lexer.Lex();
1163
1164 MCValue Expr;
1165 if (ParseRelocatableExpression(Expr))
1166 return true;
1167
Daniel Dunbar3f872332009-07-28 16:08:33 +00001168 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001169 return TokError("unexpected token in '.lsym' directive");
1170
1171 Lexer.Lex();
1172
1173 // Create the Sym with the value of the Expr
1174 Out.EmitLocalSymbol(Sym, Expr);
1175
1176 return false;
1177}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001178
1179/// ParseDirectiveInclude
1180/// ::= .include "filename"
1181bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001182 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001183 return TokError("expected string in '.include' directive");
1184
Daniel Dunbar419aded2009-07-28 16:38:40 +00001185 std::string Filename = Lexer.getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001186 SMLoc IncludeLoc = Lexer.getLoc();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001187 Lexer.Lex();
1188
Daniel Dunbar3f872332009-07-28 16:08:33 +00001189 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001190 return TokError("unexpected token in '.include' directive");
1191
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001192 // Strip the quotes.
1193 Filename = Filename.substr(1, Filename.size()-2);
1194
1195 // Attempt to switch the lexer to the included file before consuming the end
1196 // of statement to avoid losing it when we switch.
1197 if (Lexer.EnterIncludeFile(Filename)) {
1198 Lexer.PrintMessage(IncludeLoc,
1199 "Could not find include file '" + Filename + "'",
1200 "error");
1201 return true;
1202 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001203
1204 return false;
1205}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001206
1207/// ParseDirectiveDarwinDumpOrLoad
1208/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001209bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001210 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001211 return TokError("expected string in '.dump' or '.load' directive");
1212
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001213 Lexer.Lex();
1214
Daniel Dunbar3f872332009-07-28 16:08:33 +00001215 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001216 return TokError("unexpected token in '.dump' or '.load' directive");
1217
1218 Lexer.Lex();
1219
Kevin Enderby5026ae42009-07-20 20:25:37 +00001220 // FIXME: If/when .dump and .load are implemented they will be done in the
1221 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001222 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001223 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001224 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001225 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001226
1227 return false;
1228}